Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): add S3 bucket object tree to svc show #4966

Merged
merged 16 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions internal/pkg/describe/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aws/copilot-cli/internal/pkg/aws/apprunner"
"github.com/aws/copilot-cli/internal/pkg/aws/cloudwatch"
awsecs "github.com/aws/copilot-cli/internal/pkg/aws/ecs"
"github.com/aws/copilot-cli/internal/pkg/aws/sessions"
"github.com/aws/copilot-cli/internal/pkg/config"
"github.com/aws/copilot-cli/internal/pkg/describe/stack"
"github.com/aws/copilot-cli/internal/pkg/ecs"
Expand Down Expand Up @@ -127,9 +128,10 @@ type appRunnerServiceDescriber struct {

// NewServiceConfig contains fields that initiates service describer struct.
type NewServiceConfig struct {
App string
Svc string
ConfigStore ConfigStoreSvc
App string
Svc string
ConfigStore ConfigStoreSvc
SessionProvider *sessions.Provider
huanjani marked this conversation as resolved.
Show resolved Hide resolved

EnableResources bool
DeployStore DeployedEnvServicesLister
Expand Down
30 changes: 19 additions & 11 deletions internal/pkg/describe/static_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,17 @@ type StaticSiteDescriber struct {
store DeployedEnvServicesLister
initWkldStackDescriber func(string) (workloadDescriber, error)
wkldDescribers map[string]workloadDescriber
awsS3Client bucketDescriber
s3Client bucketNameGetter
initS3Client func(string) (bucketDescriber, bucketNameGetter, error)
}

// NewStaticSiteDescriber instantiates a static site service describer.
func NewStaticSiteDescriber(opt NewServiceConfig) (*StaticSiteDescriber, error) {
defaultSess, err := sessions.ImmutableProvider().Default()
if err != nil {
return nil, fmt.Errorf("default session: %v", err)
}
describer := &StaticSiteDescriber{
app: opt.App,
svc: opt.Svc,
enableResources: opt.EnableResources,
store: opt.DeployStore,
wkldDescribers: make(map[string]workloadDescriber),
awsS3Client: awsS3.New(defaultSess),
s3Client: s3.New(defaultSess),
}
describer.initWkldStackDescriber = func(env string) (workloadDescriber, error) {
if describer, ok := describer.wkldDescribers[env]; ok {
Expand All @@ -67,6 +60,17 @@ func NewStaticSiteDescriber(opt NewServiceConfig) (*StaticSiteDescriber, error)
describer.wkldDescribers[env] = svcDescr
return svcDescr, nil
}
describer.initS3Client = func(env string) (bucketDescriber, bucketNameGetter, error) {
environment, err := opt.ConfigStore.GetEnvironment(opt.App, env)
if err != nil {
return nil, nil, fmt.Errorf("get environment %s: %w", env, err)
}
sess, err := sessions.ImmutableProvider().FromRole(environment.ManagerRoleARN, environment.Region)
if err != nil {
return nil, nil, err
}
return awsS3.New(sess), s3.New(sess), nil
}
return describer, nil
}

Expand Down Expand Up @@ -99,6 +103,10 @@ func (d *StaticSiteDescriber) Describe() (HumanJSONStringer, error) {
var routes []*WebServiceRoute
var objects []*S3ObjectTree
for _, env := range environments {
bucketDescriber, bucketNameDescriber, err := d.initS3Client(env)
if err != nil {
return nil, err
}
uri, err := d.URI(env)
if err != nil {
return nil, fmt.Errorf("retrieve service URI: %w", err)
Expand All @@ -109,11 +117,11 @@ func (d *StaticSiteDescriber) Describe() (HumanJSONStringer, error) {
URL: uri.URI,
})
}
bucketName, err := d.s3Client.BucketName(d.app, env, d.svc)
bucketName, err := bucketNameDescriber.BucketName(d.app, env, d.svc)
if err != nil {
return nil, fmt.Errorf("get bucket name for %q env: %w", env, err)
}
tree, err := d.awsS3Client.GetBucketTree(bucketName)
tree, err := bucketDescriber.GetBucketTree(bucketName)
if err != nil {
return nil, fmt.Errorf("get tree representation of bucket contents: %w", err)
}
Expand Down Expand Up @@ -213,8 +221,8 @@ func (w *staticSiteDesc) HumanString() string {
for _, object := range w.Objects {
fmt.Fprintf(writer, "\n %s\t%s\n", "Environment", object.Environment)
fmt.Fprintf(writer, object.Tree)
writer.Flush()
}
writer.Flush()
}
if len(w.Resources) != 0 {
fmt.Fprint(writer, color.Bold.Sprint("\nResources\n"))
Expand Down
3 changes: 1 addition & 2 deletions internal/pkg/describe/static_site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ Resources
store: mocks.store,
initWkldStackDescriber: func(string) (workloadDescriber, error) { return mocks.wkldDescriber, nil },
wkldDescribers: make(map[string]workloadDescriber),
awsS3Client: mocks.awsS3Client,
s3Client: mocks.s3Client,
initS3Client: func(string) (bucketDescriber, bucketNameGetter, error) { return mocks.awsS3Client, mocks.s3Client, nil },
}

// WHEN
Expand Down