From cec44fbb7b07db4ba9fda046e7193239673996e9 Mon Sep 17 00:00:00 2001 From: aiordache Date: Tue, 28 Apr 2020 14:33:40 +0200 Subject: [PATCH] move to sdk Signed-off-by: aiordache Signed-off-by: Nicolas De Loof --- ecs/pkg/amazon/cloudformation.go | 29 ++++++++++++++++++++++++++++- ecs/pkg/amazon/sdk.go | 6 ++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ecs/pkg/amazon/cloudformation.go b/ecs/pkg/amazon/cloudformation.go index 44a952fec..32afb3466 100644 --- a/ecs/pkg/amazon/cloudformation.go +++ b/ecs/pkg/amazon/cloudformation.go @@ -1,7 +1,11 @@ package amazon import ( +<<<<<<< HEAD "context" +======= + "errors" +>>>>>>> a0701b8... move to sdk "fmt" "strings" @@ -18,7 +22,7 @@ import ( func (c client) Convert(ctx context.Context, project *compose.Project) (*cloudformation.Template, error) { template := cloudformation.NewTemplate() - vpc, err := c.api.GetDefaultVPC(ctx) + vpc, err := c.GetVPC(ctx, project) if err != nil { return nil, err } @@ -83,6 +87,28 @@ func (c client) Convert(ctx context.Context, project *compose.Project) (*cloudfo return template, nil } +func (c client) GetVPC(project *compose.Project) (string, error) { + //check compose file for the default external network + if net, ok := project.Networks["default"]; ok { + if net.External.External { + vpc := net.Name + ok, err := c.api.VpcExists(vpc) + if err != nil { + return "", err + } + if !ok { + return "", errors.New("Vpc does not exist: " + vpc) + } + return vpc, nil + } + } + defaultVPC, err := c.api.GetDefaultVPC() + if err != nil { + return "", err + } + return defaultVPC, nil +} + const ECSTaskExecutionPolicy = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy" var defaultTaskExecutionRole string @@ -118,6 +144,7 @@ func (c client) GetEcsTaskExecutionRole(ctx context.Context, spec types.ServiceC type convertAPI interface { GetDefaultVPC(ctx context.Context) (string, error) + VpcExists(ctx context.Context, vpcID string) (bool, error) GetSubNets(ctx context.Context, vpcID string) ([]string, error) ListRolesForPolicy(ctx context.Context, policy string) ([]string, error) GetRoleArn(ctx context.Context, name string) (string, error) diff --git a/ecs/pkg/amazon/sdk.go b/ecs/pkg/amazon/sdk.go index e3460c0db..b02e0df8c 100644 --- a/ecs/pkg/amazon/sdk.go +++ b/ecs/pkg/amazon/sdk.go @@ -75,6 +75,12 @@ func (s sdk) DeleteCluster(ctx context.Context, name string) error { return fmt.Errorf("Failed to delete cluster, status: %s" + *response.Cluster.Status) } +func (s sdk) VpcExists(ctx context.Context, vpcID string) (bool, error) { + logrus.Debug("Check if VPC exists: ", vpcID) + _, err := s.EC2.DescribeVpcsWithContext(aws.Context(ctx), &ec2.DescribeVpcsInput{VpcIds: []*string{&vpcID}}) + return err == nil, err +} + func (s sdk) GetDefaultVPC(ctx context.Context) (string, error) { logrus.Debug("Retrieve default VPC") vpcs, err := s.EC2.DescribeVpcsWithContext(aws.Context(ctx), &ec2.DescribeVpcsInput{