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

feat: update app show to display if workload is deployed #3379

Merged
merged 16 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
42 changes: 34 additions & 8 deletions internal/pkg/cli/app_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,25 @@ type showAppOpts struct {
w io.Writer
sel appSelector
pipelineSvc pipelineGetter
deployStore deployedEnvironmentLister
newVersionGetter func(string) (versionGetter, error)
}

func newShowAppOpts(vars showAppVars) (*showAppOpts, error) {
defaultSession, err := sessions.ImmutableProvider(sessions.UserAgentExtras("app show")).Default()
sessProvider := sessions.ImmutableProvider(sessions.UserAgentExtras("app show"))
defaultSession, err := sessProvider.Default()
if err != nil {
return nil, fmt.Errorf("default session: %w", err)
}
store := config.NewSSMStore(identity.New(defaultSession), ssm.New(defaultSession), aws.StringValue(defaultSession.Config.Region))
deployStore, err := deploy.NewStore(sessProvider, store)
if err != nil {
return nil, fmt.Errorf("connect to deploy store: %w", err)
}
return &showAppOpts{
showAppVars: vars,
store: store,
deployStore: deployStore,
w: log.OutputWriter,
sel: selector.NewSelect(prompt.New(), store),
pipelineSvc: codepipeline.New(defaultSession),
Expand Down Expand Up @@ -105,6 +112,7 @@ func (o *showAppOpts) Execute() error {
}

func (o *showAppOpts) description() (*describe.App, error) {
workloadEnvs := make(map[string][]string)
app, err := o.store.GetApplication(o.name)
if err != nil {
return nil, fmt.Errorf("get application %s: %w", o.name, err)
Expand All @@ -121,6 +129,23 @@ func (o *showAppOpts) description() (*describe.App, error) {
if err != nil {
return nil, fmt.Errorf("list jobs in application %s: %w", o.name, err)
}
//1st approach takes 2 seconds to display the table
for _, env := range envs {
deployedJobs, err := o.deployStore.ListDeployedJobs(o.name, env.Name)
if err != nil {
return nil, fmt.Errorf("list deployed jobs %s: %w", o.name, err)
}
for _, job := range deployedJobs {
workloadEnvs[job] = append(workloadEnvs[job], env.Name)
}
deployedSvcs, err := o.deployStore.ListDeployedServices(o.name, env.Name)
if err != nil {
return nil, fmt.Errorf("list deployed services %s: %w", o.name, err)
}
for _, svc := range deployedSvcs {
workloadEnvs[svc] = append(workloadEnvs[svc], env.Name)
}
}

pipelines, err := o.pipelineSvc.GetPipelinesByTags(map[string]string{
deploy.AppTagKey: o.name,
Expand Down Expand Up @@ -162,13 +187,14 @@ func (o *showAppOpts) description() (*describe.App, error) {
return nil, fmt.Errorf("get version for application %s: %w", o.name, err)
}
return &describe.App{
Name: app.Name,
Version: version,
URI: app.Domain,
Envs: trimmedEnvs,
Services: trimmedSvcs,
Jobs: trimmedJobs,
Pipelines: pipelines,
Name: app.Name,
Version: version,
URI: app.Domain,
Envs: trimmedEnvs,
Services: trimmedSvcs,
Jobs: trimmedJobs,
Pipelines: pipelines,
WorkloadEnvs: workloadEnvs,
}, nil
}

Expand Down
112 changes: 104 additions & 8 deletions internal/pkg/cli/app_show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type showAppMocks struct {
sel *mocks.MockappSelector
pipelineSvc *mocks.MockpipelineGetter
versionGetter *mocks.MockversionGetter
deployStore *mocks.MockdeployedEnvironmentLister
}

func TestShowAppOpts_Validate(t *testing.T) {
Expand Down Expand Up @@ -200,6 +201,10 @@ func TestShowAppOpts_Execute(t *testing.T) {
Prod: true,
},
}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "test").Return([]string{"my-job"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedJobs("my-app", "prod").Return([]string{"my-job"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedServices("my-app", "test").Return([]string{"my-svc"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedServices("my-app", "prod").Return([]string{"my-svc"}, nil).AnyTimes()
m.pipelineSvc.EXPECT().
GetPipelinesByTags(gomock.Eq(map[string]string{"copilot-application": "my-app"})).
Return([]*codepipeline.Pipeline{
Expand Down Expand Up @@ -241,6 +246,10 @@ func TestShowAppOpts_Execute(t *testing.T) {
Region: "us-west-1",
},
}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "test").Return([]string{"my-job"}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "prod").Return([]string{"my-job"}, nil)
m.deployStore.EXPECT().ListDeployedServices("my-app", "test").Return([]string{"my-svc"}, nil)
m.deployStore.EXPECT().ListDeployedServices("my-app", "prod").Return([]string{"my-svc"}, nil)
m.pipelineSvc.EXPECT().
GetPipelinesByTags(gomock.Eq(map[string]string{"copilot-application": "my-app"})).
Return([]*codepipeline.Pipeline{
Expand All @@ -265,10 +274,10 @@ Environments

Workloads

Name Type
---- ----
my-svc lb-web-svc
my-job Scheduled Job
Name Type Environments
---- ---- ------------
my-svc lb-web-svc test, prod
my-job Scheduled Job test, prod

Pipelines

Expand Down Expand Up @@ -308,6 +317,10 @@ Pipelines
Region: "us-west-1",
},
}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "test").Return([]string{"my-job"}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "prod").Return([]string{"my-job"}, nil)
m.deployStore.EXPECT().ListDeployedServices("my-app", "test").Return([]string{"my-svc"}, nil)
m.deployStore.EXPECT().ListDeployedServices("my-app", "prod").Return([]string{"my-svc"}, nil)
m.pipelineSvc.EXPECT().
GetPipelinesByTags(gomock.Eq(map[string]string{"copilot-application": "my-app"})).
Return([]*codepipeline.Pipeline{
Expand All @@ -332,10 +345,81 @@ Environments

Workloads

Name Type
---- ----
my-svc lb-web-svc
my-job Scheduled Job
Name Type Environments
---- ---- ------------
my-svc lb-web-svc test, prod
my-job Scheduled Job test, prod

Pipelines

Name
----
pipeline1
pipeline2
`,
},
"when service/job is not deployed": {
setupMocks: func(m showAppMocks) {
m.storeSvc.EXPECT().GetApplication("my-app").Return(&config.Application{
Name: "my-app",
Domain: "example.com",
}, nil)
m.storeSvc.EXPECT().ListServices("my-app").Return([]*config.Workload{
{
Name: "my-svc",
Type: "lb-web-svc",
},
}, nil)
m.storeSvc.EXPECT().ListJobs("my-app").Return([]*config.Workload{
{
Name: "my-job",
Type: "Scheduled Job",
},
}, nil)
m.storeSvc.EXPECT().ListEnvironments("my-app").Return([]*config.Environment{
{
Name: "test",
Region: "us-west-2",
AccountID: "123456789",
},
{
Name: "prod",
AccountID: "123456789",
Region: "us-west-1",
},
}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "test").Return([]string{}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "prod").Return([]string{}, nil)
m.deployStore.EXPECT().ListDeployedServices("my-app", "test").Return([]string{}, nil)
m.deployStore.EXPECT().ListDeployedServices("my-app", "prod").Return([]string{}, nil)
m.pipelineSvc.EXPECT().
GetPipelinesByTags(gomock.Eq(map[string]string{"copilot-application": "my-app"})).
Return([]*codepipeline.Pipeline{
{Name: "pipeline1"},
{Name: "pipeline2"},
}, nil)
m.versionGetter.EXPECT().Version().Return(deploy.LatestAppTemplateVersion, nil)
},

wantedContent: `About

Name my-app
Version v1.0.2
URI example.com

Environments

Name AccountID Region
---- --------- ------
test 123456789 us-west-2
prod 123456789 us-west-1

Workloads

Name Type Environments
---- ---- ------------
my-svc lb-web-svc
my-job Scheduled Job

Pipelines

Expand Down Expand Up @@ -453,6 +537,10 @@ Pipelines
Type: "Scheduled Job",
},
}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "test").Return([]string{"my-job"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedJobs("my-app", "prod").Return([]string{"my-job"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedServices("my-app", "test").Return([]string{"my-svc"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedServices("my-app", "prod").Return([]string{"my-svc"}, nil).AnyTimes()
m.pipelineSvc.EXPECT().
GetPipelinesByTags(gomock.Eq(map[string]string{"copilot-application": "my-app"})).
Return(nil, testError)
Expand Down Expand Up @@ -491,6 +579,11 @@ Pipelines
Type: "Scheduled Job",
},
}, nil)
m.deployStore.EXPECT().ListDeployedJobs("my-app", "test").Return([]string{"my-job"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedJobs("my-app", "prod").Return([]string{"my-job"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedServices("my-app", "test").Return([]string{"my-svc"}, nil).AnyTimes()
m.deployStore.EXPECT().ListDeployedServices("my-app", "prod").Return([]string{"my-svc"}, nil).AnyTimes()

m.pipelineSvc.EXPECT().
GetPipelinesByTags(gomock.Eq(map[string]string{"copilot-application": "my-app"})).
Return([]*codepipeline.Pipeline{
Expand All @@ -512,11 +605,13 @@ Pipelines
mockStoreReader := mocks.NewMockstore(ctrl)
mockPLSvc := mocks.NewMockpipelineGetter(ctrl)
mockVersionGetter := mocks.NewMockversionGetter(ctrl)
mockDeployStore := mocks.NewMockdeployedEnvironmentLister(ctrl)

mocks := showAppMocks{
storeSvc: mockStoreReader,
pipelineSvc: mockPLSvc,
versionGetter: mockVersionGetter,
deployStore: mockDeployStore,
}
tc.setupMocks(mocks)

Expand All @@ -528,6 +623,7 @@ Pipelines
store: mockStoreReader,
w: b,
pipelineSvc: mockPLSvc,
deployStore: mockDeployStore,
newVersionGetter: func(s string) (versionGetter, error) {
return mockVersionGetter, nil
},
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/cli/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ type store interface {
type deployedEnvironmentLister interface {
ListEnvironmentsDeployedTo(appName, svcName string) ([]string, error)
ListDeployedServices(appName, envName string) ([]string, error)
ListDeployedJobs(appName string, envName string) ([]string, error)
IsServiceDeployed(appName, envName string, svcName string) (bool, error)
ListSNSTopics(appName string, envName string) ([]deploy.Topic, error)
}
Expand Down
15 changes: 15 additions & 0 deletions internal/pkg/cli/mocks/mock_interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions internal/pkg/describe/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import (

// App contains serialized parameters for an application.
type App struct {
Name string `json:"name"`
Version string `json:"version"`
URI string `json:"uri"`
Envs []*config.Environment `json:"environments"`
Services []*config.Workload `json:"services"`
Jobs []*config.Workload `json:"jobs"`
Pipelines []*codepipeline.Pipeline `json:"pipelines"`
Name string `json:"name"`
Version string `json:"version"`
URI string `json:"uri"`
Envs []*config.Environment `json:"environments"`
Services []*config.Workload `json:"services"`
Jobs []*config.Workload `json:"jobs"`
Pipelines []*codepipeline.Pipeline `json:"pipelines"`
WorkloadEnvs map[string][]string `json:"-"`
}

// JSONString returns the stringified App struct with json format.
Expand Down Expand Up @@ -64,14 +65,14 @@ func (a *App) HumanString() string {
}
fmt.Fprint(writer, color.Bold.Sprint("\nWorkloads\n\n"))
writer.Flush()
headers = []string{"Name", "Type"}
headers = []string{"Name", "Type", "Environments"}
fmt.Fprintf(writer, " %s\n", strings.Join(headers, "\t"))
fmt.Fprintf(writer, " %s\n", strings.Join(underline(headers), "\t"))
for _, svc := range a.Services {
fmt.Fprintf(writer, " %s\t%s\n", svc.Name, svc.Type)
fmt.Fprintf(writer, " %s\t%s\t%s\n", svc.Name, svc.Type, strings.Join(a.WorkloadEnvs[svc.Name], ", "))
}
for _, job := range a.Jobs {
fmt.Fprintf(writer, " %s\t%s\n", job.Name, job.Type)
fmt.Fprintf(writer, " %s\t%s\t%s\n", job.Name, job.Type, strings.Join(a.WorkloadEnvs[job.Name], ", "))
}
writer.Flush()
fmt.Fprint(writer, color.Bold.Sprint("\nPipelines\n\n"))
Expand Down