github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/transit_gateway.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 "log" 20 21 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 22 23 "github.com/aws/aws-sdk-go-v2/service/ec2" 24 ) 25 26 var tgwAllowEmptyValues = []string{"tags."} 27 28 type TransitGatewayGenerator struct { 29 AWSService 30 } 31 32 func (g *TransitGatewayGenerator) getTransitGateways(svc *ec2.Client) error { 33 p := ec2.NewDescribeTransitGatewaysPaginator(svc, &ec2.DescribeTransitGatewaysInput{}) 34 for p.HasMorePages() { 35 page, err := p.NextPage(context.TODO()) 36 if err != nil { 37 return err 38 } 39 for _, tgw := range page.TransitGateways { 40 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 41 StringValue(tgw.TransitGatewayId), 42 StringValue(tgw.TransitGatewayId), 43 "aws_ec2_transit_gateway", 44 "aws", 45 tgwAllowEmptyValues, 46 )) 47 } 48 } 49 return nil 50 } 51 52 func (g *TransitGatewayGenerator) getTransitGatewayRouteTables(svc *ec2.Client) error { 53 p := ec2.NewDescribeTransitGatewayRouteTablesPaginator(svc, &ec2.DescribeTransitGatewayRouteTablesInput{}) 54 for p.HasMorePages() { 55 page, err := p.NextPage(context.TODO()) 56 if err != nil { 57 return err 58 } 59 for _, tgwrt := range page.TransitGatewayRouteTables { 60 // Default route table are automatically created on the tgw creation 61 if tgwrt.DefaultAssociationRouteTable { 62 continue 63 } else { 64 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 65 StringValue(tgwrt.TransitGatewayRouteTableId), 66 StringValue(tgwrt.TransitGatewayRouteTableId), 67 "aws_ec2_transit_gateway_route_table", 68 "aws", 69 tgwAllowEmptyValues, 70 )) 71 } 72 } 73 } 74 return nil 75 } 76 77 func (g *TransitGatewayGenerator) getTransitGatewayVpcAttachments(svc *ec2.Client) error { 78 p := ec2.NewDescribeTransitGatewayVpcAttachmentsPaginator(svc, &ec2.DescribeTransitGatewayVpcAttachmentsInput{}) 79 for p.HasMorePages() { 80 page, err := p.NextPage(context.TODO()) 81 if err != nil { 82 return err 83 } 84 for _, tgwa := range page.TransitGatewayVpcAttachments { 85 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 86 StringValue(tgwa.TransitGatewayAttachmentId), 87 StringValue(tgwa.TransitGatewayAttachmentId), 88 "aws_ec2_transit_gateway_vpc_attachment", 89 "aws", 90 tgwAllowEmptyValues, 91 )) 92 } 93 } 94 return nil 95 } 96 97 // Generate TerraformResources from AWS API, 98 // from each customer gateway create 1 TerraformResource. 99 // Need CustomerGatewayId as ID for terraform resource 100 func (g *TransitGatewayGenerator) InitResources() error { 101 config, e := g.generateConfig() 102 if e != nil { 103 return e 104 } 105 svc := ec2.NewFromConfig(config) 106 g.Resources = []terraformutils.Resource{} 107 err := g.getTransitGateways(svc) 108 if err != nil { 109 log.Println(err) 110 } 111 112 err = g.getTransitGatewayRouteTables(svc) 113 if err != nil { 114 log.Println(err) 115 } 116 117 err = g.getTransitGatewayVpcAttachments(svc) 118 if err != nil { 119 log.Println(err) 120 } 121 122 return nil 123 }