Skip to content

Commit

Permalink
GoogleCloudPlatform#511 ECR adjustments, added ECR public
Browse files Browse the repository at this point in the history
  • Loading branch information
meshuga committed Apr 4, 2021
1 parent dfd8188 commit 90b5f3c
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ In that case terraformer will not know with which region resources are associate
* `aws_ecr_lifecycle_policy`
* `aws_ecr_repository`
* `aws_ecr_repository_policy`
* `ecrpublic`
* `aws_ecrpublic_repository`
* `efs`
* `aws_efs_access_point`
* `aws_efs_file_system`
Expand Down Expand Up @@ -735,6 +737,7 @@ AWS services that are global will be imported without specified region even if s
List of global AWS services:
* `budgets`
* `cloudfront`
* `ecrpublic`
* `iam`
* `organization`
* `route53`
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.2.1
github.com/aws/aws-sdk-go-v2/service/ec2 v1.3.0
github.com/aws/aws-sdk-go-v2/service/ecr v1.2.1
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.2.0
github.com/aws/aws-sdk-go-v2/service/ecs v1.2.1
github.com/aws/aws-sdk-go-v2/service/efs v1.2.1
github.com/aws/aws-sdk-go-v2/service/eks v1.2.1
Expand Down Expand Up @@ -91,8 +92,8 @@ require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cloudflare/cloudflare-go v0.13.6
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 // indirect
github.com/ddelnano/terraform-provider-xenorchestra/client v0.0.0-20210401070256-0d721c6762ef // indirect
github.com/ddelnano/terraform-provider-mikrotik/client v0.0.0-20210401060029-7f652169b2c4
github.com/ddelnano/terraform-provider-xenorchestra/client v0.0.0-20210401070256-0d721c6762ef
github.com/denverdino/aliyungo v0.0.0-20200327235253-d59c209c7e93
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/digitalocean/godo v1.57.0
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ github.com/aws/aws-sdk-go-v2/service/ec2 v1.3.0 h1:y82WbYudKuiWx0KuKQheqTQ4RIF8Z
github.com/aws/aws-sdk-go-v2/service/ec2 v1.3.0/go.mod h1:KW2/Fgs+L1m1X53O9hTFpJqPtLyYGbf9j1Ay5xPSy74=
github.com/aws/aws-sdk-go-v2/service/ecr v1.2.1 h1:rdvoJaE51N1tuNc2B75vNsZu2P0xV6y7ibOAwNeb1mI=
github.com/aws/aws-sdk-go-v2/service/ecr v1.2.1/go.mod h1:iuqVazSEO7GWaX6WYrVWCVECL3ixXwhL6Q6RF4V2Db0=
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.2.0 h1:Qb0q2R4dff4/e/qvSVPyTs3ZME+zlzN8HDCm0+F1Ko0=
github.com/aws/aws-sdk-go-v2/service/ecrpublic v1.2.0/go.mod h1:njYzwMTk1bscTUcF56dR9DO5ORsVaV6FMQC+/wZNGjU=
github.com/aws/aws-sdk-go-v2/service/ecs v1.2.1 h1:T8CKZFKySyOivXUc/80qlvNqvX9MQvvRCxFQCL6xxQU=
github.com/aws/aws-sdk-go-v2/service/ecs v1.2.1/go.mod h1:7gzy9DNs3NWZnxJcV61X0EjhLzDuZm8uUA+moRIoiKs=
github.com/aws/aws-sdk-go-v2/service/efs v1.2.1 h1:Cm/7OSlvQVPQpksrq7FYXw75Uv1D8++CA6tcWMb6y38=
Expand Down Expand Up @@ -311,7 +313,6 @@ github.com/aws/aws-sdk-go-v2/service/workspaces v1.2.1 h1:hSzpp50D6D37eIELSSpYwJ
github.com/aws/aws-sdk-go-v2/service/workspaces v1.2.1/go.mod h1:8k9EEz8LMNPUDENPlW0laaQkAZC2TbEYF+XNUu1lFLk=
github.com/aws/aws-sdk-go-v2/service/xray v1.2.1 h1:RiTWbH90tIuJNnZZys9HeqBfpT3oSPulh7fM7anPQiY=
github.com/aws/aws-sdk-go-v2/service/xray v1.2.1/go.mod h1:MZS769M8/1Xm4XYQydDJbvugo7dG8fXzsVU3J84QXzY=
github.com/aws/smithy-go v1.2.0 h1:0PoGBWXkXDIyVdPaZW9gMhaGzj3UOAgTdiVoHuuZAFA=
github.com/aws/smithy-go v1.2.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
github.com/aws/smithy-go v1.3.0 h1:awbB2OJBZ/Txj+c4q+qhDQs3Ob0sRhBuIIkOD4Aq8yc=
github.com/aws/smithy-go v1.3.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
Expand Down Expand Up @@ -367,13 +368,10 @@ github.com/cucumber/messages-go/v12 v12.0.0/go.mod h1:5zuJu21U6rB+BBqyGoQr839a4h
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidrjenni/reftools v0.0.0-20191222082827-65925cf01315/go.mod h1:QXiCu8WKyFL/X+NMCmtxoCq/jSOb54R4iDXdjsaYEWA=
github.com/ddelnano/terraform-provider-xenorchestra/client v0.0.0-20210401070256-0d721c6762ef h1:q/XC0MjIz8cl/NtyKKewDns6mNd2cUzLQ5Xnh+jGoRg=
github.com/ddelnano/terraform-provider-xenorchestra/client v0.0.0-20210401070256-0d721c6762ef/go.mod h1:1T10mQuoIeT4CXsvyc1G1m/+L7mWv6p7cBwmAut/uB0=
github.com/ddelnano/terraform-provider-xenorchestra/client v0.18.0-alpha1 h1:P0a6ZVjbOqzOYqsSSWWF19dHaxX2AuHcv9kQafNVC+Y=
github.com/ddelnano/terraform-provider-xenorchestra/client v0.18.0-alpha1/go.mod h1:1T10mQuoIeT4CXsvyc1G1m/+L7mWv6p7cBwmAut/uB0=
github.com/ddelnano/terraform-provider-mikrotik/client v0.0.0-20210401060029-7f652169b2c4 h1:AMuZg5VR0/QGCqFpO4CUwAhZc4tbgDHpM8c4SvMt2yM=
github.com/ddelnano/terraform-provider-mikrotik/client v0.0.0-20210401060029-7f652169b2c4/go.mod h1:JrRtRlTHCkdIr7OTHCAonKS2yWwmwdqnvf+JEy+OYAA=
github.com/ddelnano/terraform-provider-xenorchestra/client v0.0.0-20210401070256-0d721c6762ef h1:q/XC0MjIz8cl/NtyKKewDns6mNd2cUzLQ5Xnh+jGoRg=
github.com/ddelnano/terraform-provider-xenorchestra/client v0.0.0-20210401070256-0d721c6762ef/go.mod h1:1T10mQuoIeT4CXsvyc1G1m/+L7mWv6p7cBwmAut/uB0=
github.com/denverdino/aliyungo v0.0.0-20200327235253-d59c209c7e93 h1:ujQ4DKs+MqFWy/hoLAU4Ey/nQhqJ6pXyocMDbVJ4qSw=
github.com/denverdino/aliyungo v0.0.0-20200327235253-d59c209c7e93/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
github.com/dghubble/sling v1.1.0 h1:DLu20Bq2qsB9cI5Hldaxj+TMPEaPpPE8IR2kvD22Atg=
Expand Down
5 changes: 4 additions & 1 deletion providers/aws/aws_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type AWSProvider struct { //nolint
}

const GlobalRegion = "aws-global"
const MainRegionPublicPartition = "us-east-1"
const NoRegion = ""

// SupportedGlobalResources should be bound to a default region. AWS doesn't specify in which region default services are
Expand All @@ -39,6 +40,7 @@ const NoRegion = ""
var SupportedGlobalResources = []string{
"budgets",
"cloudfront",
"ecrpublic",
"iam",
"organization",
"route53",
Expand Down Expand Up @@ -142,7 +144,7 @@ func (p AWSProvider) GetProviderData(arg ...string) map[string]interface{} {
awsConfig := map[string]interface{}{}

if p.region == GlobalRegion {
awsConfig["region"] = "us-east-1" // For TF to workaround terraform-providers/terraform-provider-aws#1043
awsConfig["region"] = MainRegionPublicPartition // For TF to workaround terraform-providers/terraform-provider-aws#1043
} else if p.region != NoRegion {
awsConfig["region"] = p.region
}
Expand Down Expand Up @@ -253,6 +255,7 @@ func (p *AWSProvider) GetSupportedService() map[string]terraformutils.ServiceGen
"ebs": &AwsFacade{service: &EbsGenerator{}},
"ec2_instance": &AwsFacade{service: &Ec2Generator{}},
"ecr": &AwsFacade{service: &EcrGenerator{}},
"ecrpublic": &AwsFacade{service: &EcrPublicGenerator{}},
"ecs": &AwsFacade{service: &EcsGenerator{}},
"efs": &AwsFacade{service: &EfsGenerator{}},
"eks": &AwsFacade{service: &EksGenerator{}},
Expand Down
64 changes: 51 additions & 13 deletions providers/aws/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package aws

import (
"context"
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/aws/aws-sdk-go-v2/service/ecr"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

var ecrAllowEmptyValues = []string{"tags."}
Expand All @@ -32,6 +34,7 @@ func (g *EcrGenerator) InitResources() error {
if e != nil {
return e
}

svc := ecr.NewFromConfig(config)

p := ecr.NewDescribeRepositoriesPaginator(svc, &ecr.DescribeRepositoriesInput{})
Expand All @@ -47,18 +50,53 @@ func (g *EcrGenerator) InitResources() error {
"aws_ecr_repository",
"aws",
ecrAllowEmptyValues))
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
*repository.RepositoryName,
*repository.RepositoryName,
"aws_ecr_repository_policy",
"aws",
ecrAllowEmptyValues))
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
*repository.RepositoryName,
*repository.RepositoryName,
"aws_ecr_lifecycle_policy",
"aws",
ecrAllowEmptyValues))

_, err := svc.GetRepositoryPolicy(context.TODO(), &ecr.GetRepositoryPolicyInput{
RepositoryName: repository.RepositoryName,
RegistryId: repository.RegistryId,
})
if err == nil {
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
*repository.RepositoryName,
*repository.RepositoryName,
"aws_ecr_repository_policy",
"aws",
ecrAllowEmptyValues))
}

_, err = svc.GetLifecyclePolicy(context.TODO(), &ecr.GetLifecyclePolicyInput{
RepositoryName: repository.RepositoryName,
RegistryId: repository.RegistryId,
})
if err == nil {
g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
*repository.RepositoryName,
*repository.RepositoryName,
"aws_ecr_lifecycle_policy",
"aws",
ecrAllowEmptyValues))
}
}
}
return nil
}

func (g *EcrGenerator) PostConvertHook() error {
for i, resource := range g.Resources {
if resource.InstanceInfo.Type == "aws_ecr_repository_policy" {
if val, ok := g.Resources[i].Item["policy"]; ok {
policy := g.escapeAwsInterpolation(val.(string))
g.Resources[i].Item["policy"] = fmt.Sprintf(`<<POLICY
%s
POLICY`, policy)
}
} else if resource.InstanceInfo.Type == "aws_ecr_lifecycle_policy" {
if val, ok := g.Resources[i].Item["policy"]; ok {
policy := g.escapeAwsInterpolation(val.(string))
g.Resources[i].Item["policy"] = fmt.Sprintf(`<<POLICY
%s
POLICY`, policy)
}
}
}
return nil
Expand Down
57 changes: 57 additions & 0 deletions providers/aws/ecrpublic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2021 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aws

import (
"context"
"github.com/aws/aws-sdk-go-v2/service/ecrpublic"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

var ecrPublicAllowEmptyValues = []string{"tags."}

type EcrPublicGenerator struct {
AWSService
}

func (g *EcrPublicGenerator) InitResources() error {
config, e := g.generateConfig()
if e != nil {
return e
}

ecrPublicConfig := config.Copy()
ecrPublicConfig.Region = MainRegionPublicPartition
svc := ecrpublic.NewFromConfig(ecrPublicConfig)

p := ecrpublic.NewDescribeRepositoriesPaginator(svc, &ecrpublic.DescribeRepositoriesInput{})
for p.HasMorePages() {
page, e := p.NextPage(context.TODO())
if e != nil {
return e
}
for _, repository := range page.Repositories {
resource := terraformutils.NewSimpleResource(
*repository.RepositoryName,
*repository.RepositoryName,
"aws_ecrpublic_repository",
"aws",
ecrPublicAllowEmptyValues)
g.Resources = append(g.Resources, resource)
}
}
return nil
}
4 changes: 2 additions & 2 deletions tests/aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func main() {
tCommand.SetArgs([]string{
"import",
"aws",
"--regions=ap-southeast-1",
"--resources=*",
"--regions=us-east-1",
"--resources=ecrpublic",
"--profile=personal",
"--verbose",
"--compact",
Expand Down

0 comments on commit 90b5f3c

Please sign in to comment.