diff --git a/ecs/cmd/commands/setup.go b/ecs/cmd/commands/setup.go index be876d666..33cd31291 100644 --- a/ecs/cmd/commands/setup.go +++ b/ecs/cmd/commands/setup.go @@ -1,6 +1,7 @@ package commands import ( + "context" "fmt" "os" "path/filepath" @@ -10,6 +11,7 @@ import ( "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/defaults" + amazon "github.com/docker/ecs-plugin/pkg/amazon/backend" contextStore "github.com/docker/ecs-plugin/pkg/docker" "github.com/manifoldco/promptui" "github.com/spf13/cobra" @@ -53,6 +55,14 @@ func SetupCommand() *cobra.Command { return err } } + backend, err := amazon.NewBackend(opts.context.Profile, opts.context.Cluster, opts.context.Region) + if err != nil { + return err + } + _, _, err = backend.CreateContextData(context.Background(), nil) + if err != nil { + return err + } return contextStore.NewContext(opts.name, &opts.context) }, } @@ -206,6 +216,7 @@ func setCluster(opts *setupOptions, err error) error { if err != nil { return err } + opts.context.Cluster = result return nil } diff --git a/ecs/cmd/commands/setup_test.go b/ecs/cmd/commands/setup_test.go deleted file mode 100644 index 97b7add38..000000000 --- a/ecs/cmd/commands/setup_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package commands - -import ( - "io/ioutil" - "path/filepath" - "testing" - - "github.com/docker/cli/cli/config" - "gotest.tools/v3/assert" - "gotest.tools/v3/fs" - "gotest.tools/v3/golden" -) - -func TestDefaultAwsContextName(t *testing.T) { - dir := fs.NewDir(t, "setup") - defer dir.Remove() - cmd := NewRootCmd(nil) - dockerConfig := config.Dir() - config.SetDir(dir.Path()) - defer config.SetDir(dockerConfig) - - cmd.SetArgs([]string{"setup", "--cluster", "clusterName", "--profile", "profileName", "--region", "regionName"}) - err := cmd.Execute() - assert.NilError(t, err) - - files, err := filepath.Glob(dir.Join("contexts", "meta", "*", "meta.json")) - assert.NilError(t, err) - assert.Equal(t, len(files), 1) - b, err := ioutil.ReadFile(files[0]) - assert.NilError(t, err) - golden.Assert(t, string(b), "context.golden") -} diff --git a/ecs/cmd/commands/testdata/context.golden b/ecs/cmd/commands/testdata/context.golden deleted file mode 100644 index 891cb9cf8..000000000 --- a/ecs/cmd/commands/testdata/context.golden +++ /dev/null @@ -1 +0,0 @@ -{"Name":"aws","Metadata":{"Type":"aws"},"Endpoints":{"aws":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"},"docker":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"}}} \ No newline at end of file diff --git a/ecs/pkg/amazon/backend/context.go b/ecs/pkg/amazon/backend/context.go new file mode 100644 index 000000000..bc785d91a --- /dev/null +++ b/ecs/pkg/amazon/backend/context.go @@ -0,0 +1,24 @@ +package backend + +import ( + "context" + "fmt" +) + +func (b *Backend) CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error) { + err = b.api.CheckRequirements(ctx) + if err != nil { + return "", "", err + } + + if b.Cluster != "" { + exists, err := b.api.ClusterExists(ctx, b.Cluster) + if err != nil { + return "", "", err + } + if !exists { + return "", "", fmt.Errorf("cluster %s does not exists", b.Cluster) + } + } + return "", "", nil +} diff --git a/ecs/pkg/amazon/sdk/api.go b/ecs/pkg/amazon/sdk/api.go index 057823c53..5de5d5af2 100644 --- a/ecs/pkg/amazon/sdk/api.go +++ b/ecs/pkg/amazon/sdk/api.go @@ -9,6 +9,8 @@ import ( ) type API interface { + CheckRequirements(ctx context.Context) error + GetDefaultVPC(ctx context.Context) (string, error) VpcExists(ctx context.Context, vpcID string) (bool, error) GetSubNets(ctx context.Context, vpcID string) ([]string, error) diff --git a/ecs/pkg/amazon/sdk/sdk.go b/ecs/pkg/amazon/sdk/sdk.go index 3365b07f8..fd0e649fa 100644 --- a/ecs/pkg/amazon/sdk/sdk.go +++ b/ecs/pkg/amazon/sdk/sdk.go @@ -2,15 +2,13 @@ package sdk import ( "context" + "errors" "fmt" "strings" "time" - "github.com/docker/ecs-plugin/internal" - - "github.com/aws/aws-sdk-go/aws/request" - "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudformation" "github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface" @@ -27,6 +25,7 @@ import ( "github.com/aws/aws-sdk-go/service/secretsmanager" "github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface" cf "github.com/awslabs/goformation/v4/cloudformation" + "github.com/docker/ecs-plugin/internal" "github.com/docker/ecs-plugin/pkg/compose" "github.com/sirupsen/logrus" ) @@ -57,8 +56,23 @@ func NewAPI(sess *session.Session) API { } } +func (s sdk) CheckRequirements(ctx context.Context) error { + settings, err := s.ECS.ListAccountSettingsWithContext(ctx, &ecs.ListAccountSettingsInput{ + EffectiveSettings: aws.Bool(true), + Name: aws.String("serviceLongArnFormat"), + }) + if err != nil { + return err + } + serviceLongArnFormat := settings.Settings[0].Value + if *serviceLongArnFormat != "enabled" { + return errors.New("this tool requires the \"new ARN resource ID format\"") + } + return nil +} + func (s sdk) ClusterExists(ctx context.Context, name string) (bool, error) { - logrus.Debug("Check if cluster was already created: ", name) + logrus.Debug("CheckRequirements if cluster was already created: ", name) clusters, err := s.ECS.DescribeClustersWithContext(ctx, &ecs.DescribeClustersInput{ Clusters: []*string{aws.String(name)}, }) @@ -78,7 +92,7 @@ func (s sdk) CreateCluster(ctx context.Context, name string) (string, error) { } func (s sdk) VpcExists(ctx context.Context, vpcID string) (bool, error) { - logrus.Debug("Check if VPC exists: ", vpcID) + logrus.Debug("CheckRequirements if VPC exists: ", vpcID) _, err := s.EC2.DescribeVpcsWithContext(ctx, &ec2.DescribeVpcsInput{VpcIds: []*string{&vpcID}}) return err == nil, err } @@ -421,7 +435,7 @@ func (s sdk) GetPublicIPs(ctx context.Context, interfaces ...string) (map[string } func (s sdk) LoadBalancerExists(ctx context.Context, arn string) (bool, error) { - logrus.Debug("Check if LoadBalancer exists: ", arn) + logrus.Debug("CheckRequirements if LoadBalancer exists: ", arn) lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{ LoadBalancerArns: []*string{aws.String(arn)}, }) diff --git a/ecs/pkg/compose/api.go b/ecs/pkg/compose/api.go index 5f8278c65..66364703e 100644 --- a/ecs/pkg/compose/api.go +++ b/ecs/pkg/compose/api.go @@ -12,6 +12,8 @@ type API interface { Up(ctx context.Context, options cli.ProjectOptions) error Down(ctx context.Context, options cli.ProjectOptions) error + CreateContextData(ctx context.Context, params map[string]string) (contextData interface{}, description string, err error) + Convert(project *types.Project) (*cloudformation.Template, error) Logs(ctx context.Context, projectName cli.ProjectOptions) error Ps(background context.Context, options cli.ProjectOptions) ([]ServiceStatus, error)