Pārlūkot izejas kodu

move to sdk

Signed-off-by: aiordache <[email protected]>
Signed-off-by: Nicolas De Loof <[email protected]>
aiordache 5 gadi atpakaļ
vecāks
revīzija
cec44fbb7b
2 mainītis faili ar 34 papildinājumiem un 1 dzēšanām
  1. 28 1
      ecs/pkg/amazon/cloudformation.go
  2. 6 0
      ecs/pkg/amazon/sdk.go

+ 28 - 1
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)

+ 6 - 0
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{