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(cli): add app show support for backend app #876

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 32 additions & 15 deletions internal/pkg/cli/app_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"

"github.com/aws/amazon-ecs-cli-v2/internal/pkg/describe"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/manifest"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/store"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/term/color"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/term/log"
Expand All @@ -35,9 +36,9 @@ type showAppOpts struct {

w io.Writer
storeSvc storeReader
describer webAppDescriber
describer appDescriber
ws wsAppReader
initDescriber func(*showAppOpts, bool) error // Overriden in tests.
initDescriber func(bool) error // Overriden in tests.
}

func newShowAppOpts(vars showAppVars) (*showAppOpts, error) {
Expand All @@ -50,26 +51,42 @@ func newShowAppOpts(vars showAppVars) (*showAppOpts, error) {
return nil, err
}

return &showAppOpts{
opts := &showAppOpts{
showAppVars: vars,
storeSvc: ssmStore,
ws: ws,
w: log.OutputWriter,
initDescriber: func(o *showAppOpts, enableResources bool) error {
var d *describe.WebAppDescriber
var err error
}
opts.initDescriber = func(enableResources bool) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we add more patterns we might want this to be a describer provider or something that can look up the right functions in a map or something like that - so we don't end up with one giant function. We needn't do it now, but just a note.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. That's a good idea!

var d appDescriber
app, err := opts.storeSvc.GetApplication(opts.ProjectName(), opts.appName)
if err != nil {
return err
}
switch app.Type {
case manifest.LoadBalancedWebApplication:
if enableResources {
d, err = describe.NewWebAppDescriberWithResources(o.ProjectName(), o.appName)
d, err = describe.NewWebAppDescriberWithResources(opts.ProjectName(), opts.appName)
} else {
d, err = describe.NewWebAppDescriber(o.ProjectName(), o.appName)
d, err = describe.NewWebAppDescriber(opts.ProjectName(), opts.appName)
}
if err != nil {
return fmt.Errorf("creating describer for application %s in project %s: %w", o.appName, o.ProjectName(), err)
case manifest.BackendApplication:
if enableResources {
d, err = describe.NewBackendAppDescriberWithResources(opts.ProjectName(), opts.appName)
} else {
d, err = describe.NewBackendAppDescriber(opts.ProjectName(), opts.appName)
}
o.describer = d
return nil
},
}, nil
default:
return fmt.Errorf("invalid application type %s", app.Type)
}

if err != nil {
return fmt.Errorf("creating describer for application %s in project %s: %w", opts.appName, opts.ProjectName(), err)
}
opts.describer = d
return nil
}
return opts, nil
}

// Validate returns an error if the values provided by the user are invalid.
Expand Down Expand Up @@ -102,7 +119,7 @@ func (o *showAppOpts) Execute() error {
// If there are no local applications in the workspace, we exit without error.
return nil
}
err := o.initDescriber(o, o.shouldOutputResources)
err := o.initDescriber(o.shouldOutputResources)
if err != nil {
return err
}
Expand Down
25 changes: 18 additions & 7 deletions internal/pkg/cli/app_show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
type showAppMocks struct {
storeSvc *climocks.MockstoreReader
prompt *climocks.Mockprompter
describer *climocks.MockwebAppDescriber
describer *climocks.MockappDescriber
ws *climocks.MockwsAppReader
}

Expand Down Expand Up @@ -372,7 +372,7 @@ func TestAppShow_Execute(t *testing.T) {
projectName := "my-project"
webApp := describe.WebAppDesc{
AppName: "my-app",
Configurations: []*describe.WebAppConfig{
Configurations: []*describe.AppConfig{
{
CPU: "256",
Environment: "test",
Expand Down Expand Up @@ -411,6 +411,12 @@ func TestAppShow_Execute(t *testing.T) {
URL: "http://my-pr-Publi.us-west-2.elb.amazonaws.com/backend",
},
},
ServiceDiscovery: []*describe.ServiceDiscovery{
{
Environment: []string{"test", "prod"},
Namespace: "http://my-app.my-project.local:5000",
},
},
Resources: map[string][]*describe.CfnResource{
"test": []*describe.CfnResource{
{
Expand Down Expand Up @@ -452,7 +458,7 @@ func TestAppShow_Execute(t *testing.T) {
)
},

wantedContent: "{\"appName\":\"my-app\",\"type\":\"\",\"project\":\"my-project\",\"configurations\":[{\"environment\":\"test\",\"port\":\"80\",\"tasks\":\"1\",\"cpu\":\"256\",\"memory\":\"512\"},{\"environment\":\"prod\",\"port\":\"5000\",\"tasks\":\"3\",\"cpu\":\"512\",\"memory\":\"1024\"}],\"routes\":[{\"environment\":\"test\",\"url\":\"http://my-pr-Publi.us-west-2.elb.amazonaws.com/frontend\"},{\"environment\":\"prod\",\"url\":\"http://my-pr-Publi.us-west-2.elb.amazonaws.com/backend\"}],\"variables\":[{\"environment\":\"prod\",\"name\":\"ECS_CLI_ENVIRONMENT_NAME\",\"value\":\"prod\"},{\"environment\":\"test\",\"name\":\"ECS_CLI_ENVIRONMENT_NAME\",\"value\":\"test\"}],\"resources\":{\"prod\":[{\"type\":\"AWS::EC2::SecurityGroupIngress\",\"physicalID\":\"ContainerSecurityGroupIngressFromPublicALB\"}],\"test\":[{\"type\":\"AWS::EC2::SecurityGroup\",\"physicalID\":\"sg-0758ed6b233743530\"}]}}\n",
wantedContent: "{\"appName\":\"my-app\",\"type\":\"\",\"project\":\"my-project\",\"configurations\":[{\"environment\":\"test\",\"port\":\"80\",\"tasks\":\"1\",\"cpu\":\"256\",\"memory\":\"512\"},{\"environment\":\"prod\",\"port\":\"5000\",\"tasks\":\"3\",\"cpu\":\"512\",\"memory\":\"1024\"}],\"routes\":[{\"environment\":\"test\",\"url\":\"http://my-pr-Publi.us-west-2.elb.amazonaws.com/frontend\"},{\"environment\":\"prod\",\"url\":\"http://my-pr-Publi.us-west-2.elb.amazonaws.com/backend\"}],\"serviceDiscovery\":[{\"environment\":[\"test\",\"prod\"],\"namespace\":\"http://my-app.my-project.local:5000\"}],\"variables\":[{\"environment\":\"prod\",\"name\":\"ECS_CLI_ENVIRONMENT_NAME\",\"value\":\"prod\"},{\"environment\":\"test\",\"name\":\"ECS_CLI_ENVIRONMENT_NAME\",\"value\":\"test\"}],\"resources\":{\"prod\":[{\"type\":\"AWS::EC2::SecurityGroupIngress\",\"physicalID\":\"ContainerSecurityGroupIngressFromPublicALB\"}],\"test\":[{\"type\":\"AWS::EC2::SecurityGroup\",\"physicalID\":\"sg-0758ed6b233743530\"}]}}\n",
},
"prompt for all input for human output": {
inputApp: "my-app",
Expand Down Expand Up @@ -483,6 +489,11 @@ Routes
test http://my-pr-Publi.us-west-2.elb.amazonaws.com/frontend
prod http://my-pr-Publi.us-west-2.elb.amazonaws.com/backend

Service Discovery

Environment Namespace
test, prod http://my-app.my-project.local:5000

Variables

Name Environment Value
Expand Down Expand Up @@ -517,10 +528,10 @@ Resources
defer ctrl.Finish()

b := &bytes.Buffer{}
mockWebAppDescriber := climocks.NewMockwebAppDescriber(ctrl)
mockAppDescriber := climocks.NewMockappDescriber(ctrl)

mocks := showAppMocks{
describer: mockWebAppDescriber,
describer: mockAppDescriber,
}

tc.setupMocks(mocks)
Expand All @@ -534,8 +545,8 @@ Resources
projectName: projectName,
},
},
describer: mockWebAppDescriber,
initDescriber: func(*showAppOpts, bool) error { return nil },
describer: mockAppDescriber,
initDescriber: func(bool) error { return nil },
w: b,
}

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/cli/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ type sessionProvider interface {
sessionFromRoleProvider
}

type webAppDescriber interface {
Describe() (*describe.WebAppDesc, error)
type appDescriber interface {
Describe() (describe.HumanJSONStringer, error)
}

type storeReader interface {
Expand Down
30 changes: 15 additions & 15 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.

Loading