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

     1  // Copyright 2019 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  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/aws/aws-sdk-go-v2/service/cloudformation"
    22  	"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
    23  )
    24  
    25  var cloudFormationAllowEmptyValues = []string{"tags."}
    26  
    27  type CloudFormationGenerator struct {
    28  	AWSService
    29  }
    30  
    31  func (g *CloudFormationGenerator) InitResources() error {
    32  	config, e := g.generateConfig()
    33  	if e != nil {
    34  		return e
    35  	}
    36  	svc := cloudformation.NewFromConfig(config)
    37  	p := cloudformation.NewListStacksPaginator(svc, &cloudformation.ListStacksInput{})
    38  	for p.HasMorePages() {
    39  		page, e := p.NextPage(context.TODO())
    40  		if e != nil {
    41  			return e
    42  		}
    43  		for _, stackSummary := range page.StackSummaries {
    44  			if stackSummary.StackStatus == types.StackStatusDeleteComplete {
    45  				continue
    46  			}
    47  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    48  				*stackSummary.StackId,
    49  				*stackSummary.StackName,
    50  				"aws_cloudformation_stack",
    51  				"aws",
    52  				cloudFormationAllowEmptyValues,
    53  			))
    54  		}
    55  	}
    56  	stackSets, err := svc.ListStackSets(context.TODO(), &cloudformation.ListStackSetsInput{})
    57  	if err != nil {
    58  		return err
    59  	}
    60  	for _, stackSetSummary := range stackSets.Summaries {
    61  		if stackSetSummary.Status == types.StackSetStatusDeleted {
    62  			continue
    63  		}
    64  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    65  			*stackSetSummary.StackSetId,
    66  			*stackSetSummary.StackSetName,
    67  			"aws_cloudformation_stack_set",
    68  			"aws",
    69  			cloudFormationAllowEmptyValues,
    70  		))
    71  
    72  		stackSetInstances, err := svc.ListStackInstances(context.TODO(), &cloudformation.ListStackInstancesInput{
    73  			StackSetName: stackSetSummary.StackSetName,
    74  		})
    75  		if err != nil {
    76  			return err
    77  		}
    78  		for _, stackSetI := range stackSetInstances.Summaries {
    79  			id := StringValue(stackSetI.StackSetId) + "," + StringValue(stackSetI.Account) + "," + StringValue(stackSetI.Region)
    80  
    81  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    82  				id,
    83  				id,
    84  				"aws_cloudformation_stack_set_instance",
    85  				"aws",
    86  				cloudFormationAllowEmptyValues,
    87  			))
    88  		}
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  func (g *CloudFormationGenerator) PostConvertHook() error {
    95  	for _, resource := range g.Resources {
    96  		if resource.InstanceInfo.Type == "aws_cloudformation_stack" {
    97  			delete(resource.Item, "outputs")
    98  			if templateBody, ok := resource.InstanceState.Attributes["template_body"]; ok {
    99  				resource.Item["template_body"] = g.escapeAwsInterpolation(templateBody)
   100  			}
   101  		}
   102  	}
   103  	return nil
   104  }