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

     1  // Copyright 2018 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  	"log"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  
    23  	"github.com/aws/aws-sdk-go-v2/service/ec2"
    24  )
    25  
    26  var rtbAllowEmptyValues = []string{"tags."}
    27  
    28  type RouteTableGenerator struct {
    29  	AWSService
    30  }
    31  
    32  func (g *RouteTableGenerator) createRouteTablesResources(svc *ec2.Client) []terraformutils.Resource {
    33  	var resources []terraformutils.Resource
    34  	p := ec2.NewDescribeRouteTablesPaginator(svc, &ec2.DescribeRouteTablesInput{})
    35  	for p.HasMorePages() {
    36  		page, err := p.NextPage(context.TODO())
    37  		if err != nil {
    38  			log.Println(err)
    39  			return resources
    40  		}
    41  		for _, table := range page.RouteTables {
    42  			// route table
    43  			resources = append(resources, terraformutils.NewSimpleResource(
    44  				StringValue(table.RouteTableId),
    45  				StringValue(table.RouteTableId),
    46  				"aws_route_table",
    47  				"aws",
    48  				rtbAllowEmptyValues,
    49  			))
    50  
    51  			for _, assoc := range table.Associations {
    52  				if assoc.Main {
    53  					// main route table association
    54  					resources = append(resources, terraformutils.NewResource(
    55  						StringValue(assoc.RouteTableAssociationId),
    56  						StringValue(table.VpcId),
    57  						"aws_main_route_table_association",
    58  						"aws",
    59  						map[string]string{
    60  							"vpc_id":         StringValue(table.VpcId),
    61  							"route_table_id": StringValue(table.RouteTableId),
    62  						},
    63  						rtbAllowEmptyValues,
    64  						map[string]interface{}{},
    65  					))
    66  				} else {
    67  					// subnet-specific route table association
    68  					resources = append(resources, terraformutils.NewResource(
    69  						StringValue(assoc.RouteTableAssociationId),
    70  						StringValue(assoc.SubnetId),
    71  						"aws_route_table_association",
    72  						"aws",
    73  						map[string]string{
    74  							"subnet_id":      StringValue(assoc.SubnetId),
    75  							"route_table_id": StringValue(table.RouteTableId),
    76  						},
    77  						rtbAllowEmptyValues,
    78  						map[string]interface{}{},
    79  					))
    80  				}
    81  			}
    82  		}
    83  	}
    84  	return resources
    85  }
    86  
    87  // Generate TerraformResources from AWS API,
    88  // create terraform resource for each route tables
    89  func (g *RouteTableGenerator) InitResources() error {
    90  	config, e := g.generateConfig()
    91  	if e != nil {
    92  		return e
    93  	}
    94  	svc := ec2.NewFromConfig(config)
    95  
    96  	g.Resources = g.createRouteTablesResources(svc)
    97  	return nil
    98  }