github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/ecr.go (about)

     1  // Copyright 2020 The Terraformer Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aws
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/aws/aws-sdk-go-v2/service/ecr"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    24  )
    25  
    26  var ecrAllowEmptyValues = []string{"tags."}
    27  
    28  type EcrGenerator struct {
    29  	AWSService
    30  }
    31  
    32  func (g *EcrGenerator) InitResources() error {
    33  	config, e := g.generateConfig()
    34  	if e != nil {
    35  		return e
    36  	}
    37  
    38  	svc := ecr.NewFromConfig(config)
    39  
    40  	p := ecr.NewDescribeRepositoriesPaginator(svc, &ecr.DescribeRepositoriesInput{})
    41  	for p.HasMorePages() {
    42  		page, e := p.NextPage(context.TODO())
    43  		if e != nil {
    44  			return e
    45  		}
    46  		for _, repository := range page.Repositories {
    47  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    48  				*repository.RepositoryName,
    49  				*repository.RepositoryName,
    50  				"aws_ecr_repository",
    51  				"aws",
    52  				ecrAllowEmptyValues))
    53  
    54  			_, err := svc.GetRepositoryPolicy(context.TODO(), &ecr.GetRepositoryPolicyInput{
    55  				RepositoryName: repository.RepositoryName,
    56  				RegistryId:     repository.RegistryId,
    57  			})
    58  			if err == nil {
    59  				g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    60  					*repository.RepositoryName,
    61  					*repository.RepositoryName,
    62  					"aws_ecr_repository_policy",
    63  					"aws",
    64  					ecrAllowEmptyValues))
    65  			}
    66  
    67  			_, err = svc.GetLifecyclePolicy(context.TODO(), &ecr.GetLifecyclePolicyInput{
    68  				RepositoryName: repository.RepositoryName,
    69  				RegistryId:     repository.RegistryId,
    70  			})
    71  			if err == nil {
    72  				g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    73  					*repository.RepositoryName,
    74  					*repository.RepositoryName,
    75  					"aws_ecr_lifecycle_policy",
    76  					"aws",
    77  					ecrAllowEmptyValues))
    78  			}
    79  		}
    80  	}
    81  	return nil
    82  }
    83  
    84  func (g *EcrGenerator) PostConvertHook() error {
    85  	for i, resource := range g.Resources {
    86  		if resource.InstanceInfo.Type == "aws_ecr_repository_policy" {
    87  			if val, ok := g.Resources[i].Item["policy"]; ok {
    88  				policy := g.escapeAwsInterpolation(val.(string))
    89  				g.Resources[i].Item["policy"] = fmt.Sprintf(`<<POLICY
    90  %s
    91  POLICY`, policy)
    92  			}
    93  		} else if resource.InstanceInfo.Type == "aws_ecr_lifecycle_policy" {
    94  			if val, ok := g.Resources[i].Item["policy"]; ok {
    95  				policy := g.escapeAwsInterpolation(val.(string))
    96  				g.Resources[i].Item["policy"] = fmt.Sprintf(`<<POLICY
    97  %s
    98  POLICY`, policy)
    99  			}
   100  		}
   101  	}
   102  	return nil
   103  }