Support user passing an ARN for x-aws-vpc

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-12-16 10:37:16 +01:00
parent 42acaea3c9
commit 06742e3c35
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
2 changed files with 27 additions and 1 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/errdefs" "github.com/docker/compose-cli/errdefs"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/elbv2" "github.com/aws/aws-sdk-go/service/elbv2"
"github.com/awslabs/goformation/v4/cloudformation" "github.com/awslabs/goformation/v4/cloudformation"
"github.com/awslabs/goformation/v4/cloudformation/ec2" "github.com/awslabs/goformation/v4/cloudformation/ec2"
@ -168,7 +169,15 @@ func (b *ecsAPIService) parseVPCExtension(ctx context.Context, project *types.Pr
var vpc string var vpc string
if x, ok := project.Extensions[extensionVPC]; ok { if x, ok := project.Extensions[extensionVPC]; ok {
vpc = x.(string) vpc = x.(string)
err := b.aws.CheckVPC(ctx, vpc) ARN, err := arn.Parse(vpc)
if err == nil {
// User has set an ARN, like the one Terraform shows as output, while we expect an ID
id := ARN.Resource
i := strings.LastIndex(id, "/")
vpc = id[i+1:]
}
err = b.aws.CheckVPC(ctx, vpc)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }

View File

@ -543,6 +543,23 @@ services:
assert.Equal(t, template.Metadata["Cluster"], "arn:aws:ecs:region:account:cluster/name") assert.Equal(t, template.Metadata["Cluster"], "arn:aws:ecs:region:account:cluster/name")
} }
func TestARNUsedAsVpcID(t *testing.T) {
convertYaml(t, `
x-aws-vpc: "arn:aws:ec2:us-west-1:EXAMPLE:vpc/vpc-1234acbd"
services:
test:
image: nginx
`, func(m *MockAPIMockRecorder) {
m.CheckVPC(gomock.Any(), "vpc-1234acbd").Return(nil)
m.GetSubNets(gomock.Any(), "vpc-1234acbd").Return([]awsResource{
existingAWSResource{id: "subnet1"},
existingAWSResource{id: "subnet2"},
}, nil)
m.IsPublicSubnet(gomock.Any(), "subnet1").Return(true, nil)
m.IsPublicSubnet(gomock.Any(), "subnet2").Return(true, nil)
})
}
func convertYaml(t *testing.T, yaml string, fn ...func(m *MockAPIMockRecorder)) *cloudformation.Template { func convertYaml(t *testing.T, yaml string, fn ...func(m *MockAPIMockRecorder)) *cloudformation.Template {
project := loadConfig(t, yaml) project := loadConfig(t, yaml)
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)