github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/config.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  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/aws/aws-sdk-go-v2/service/configservice"
    22  )
    23  
    24  var configAllowEmptyValues = []string{"tags."}
    25  
    26  type ConfigGenerator struct {
    27  	AWSService
    28  }
    29  
    30  func (g *ConfigGenerator) InitResources() error {
    31  	config, e := g.generateConfig()
    32  	if e != nil {
    33  		return e
    34  	}
    35  	client := configservice.NewFromConfig(config)
    36  
    37  	configurationRecorderRefs, err := g.addConfigurationRecorders(client)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	err = g.addConfigRules(client, configurationRecorderRefs)
    42  	if err != nil {
    43  		return err
    44  	}
    45  	err = g.addDeliveryChannels(client, configurationRecorderRefs)
    46  	return err
    47  }
    48  
    49  func (g *ConfigGenerator) addConfigurationRecorders(svc *configservice.Client) ([]string, error) {
    50  	configurationRecorders, err := svc.DescribeConfigurationRecorders(context.TODO(),
    51  		&configservice.DescribeConfigurationRecordersInput{})
    52  
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	var configurationRecorderRefs []string
    57  	for _, configurationRecorder := range configurationRecorders.ConfigurationRecorders {
    58  		name := *configurationRecorder.Name
    59  		g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    60  			name,
    61  			name,
    62  			"aws_config_configuration_recorder",
    63  			"aws",
    64  			configAllowEmptyValues,
    65  		))
    66  		configurationRecorderRefs = append(configurationRecorderRefs,
    67  			"aws_config_configuration_recorder.tfer--"+name)
    68  	}
    69  	return configurationRecorderRefs, nil
    70  }
    71  
    72  func (g *ConfigGenerator) addConfigRules(svc *configservice.Client, configurationRecorderRefs []string) error {
    73  	var nextToken *string
    74  
    75  	for {
    76  		configRules, err := svc.DescribeConfigRules(
    77  			context.TODO(),
    78  			&configservice.DescribeConfigRulesInput{
    79  				NextToken: nextToken,
    80  			})
    81  
    82  		if err != nil {
    83  			return err
    84  		}
    85  		for _, configRule := range configRules.ConfigRules {
    86  			name := *configRule.ConfigRuleName
    87  			g.Resources = append(g.Resources, terraformutils.NewResource(
    88  				name,
    89  				name,
    90  				"aws_config_config_rule",
    91  				"aws",
    92  				map[string]string{},
    93  				configAllowEmptyValues,
    94  				map[string]interface{}{
    95  					"depends_on": configurationRecorderRefs,
    96  				},
    97  			))
    98  		}
    99  		nextToken = configRules.NextToken
   100  		if nextToken == nil {
   101  			break
   102  		}
   103  	}
   104  	return nil
   105  }
   106  
   107  func (g *ConfigGenerator) addDeliveryChannels(svc *configservice.Client, configurationRecorderRefs []string) error {
   108  	deliveryChannels, err := svc.DescribeDeliveryChannels(context.TODO(),
   109  		&configservice.DescribeDeliveryChannelsInput{})
   110  
   111  	if err != nil {
   112  		return err
   113  	}
   114  	for _, deliveryChannel := range deliveryChannels.DeliveryChannels {
   115  		name := *deliveryChannel.Name
   116  		g.Resources = append(g.Resources, terraformutils.NewResource(
   117  			name,
   118  			name,
   119  			"aws_config_delivery_channel",
   120  			"aws",
   121  			map[string]string{},
   122  			configAllowEmptyValues,
   123  			map[string]interface{}{
   124  				"depends_on": configurationRecorderRefs,
   125  			},
   126  		))
   127  	}
   128  	return nil
   129  }