github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/eip.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 eipAllowEmptyValues = []string{"tags."} 27 28 type ElasticIPGenerator struct { 29 AWSService 30 } 31 32 func (g *ElasticIPGenerator) createElasticIpsResources(svc *ec2.Client) []terraformutils.Resource { 33 resources := []terraformutils.Resource{} 34 addresses, err := svc.DescribeAddresses(context.TODO(), &ec2.DescribeAddressesInput{}) 35 36 if err != nil { 37 log.Println(err) 38 return resources 39 } 40 41 for _, eip := range addresses.Addresses { 42 resources = append(resources, terraformutils.NewSimpleResource( 43 StringValue(eip.AllocationId), 44 StringValue(eip.AllocationId), 45 "aws_eip", 46 "aws", 47 eipAllowEmptyValues, 48 )) 49 } 50 51 return resources 52 } 53 54 // Generate TerraformResources from AWS API, 55 // create terraform resource for each elastic IPs 56 func (g *ElasticIPGenerator) InitResources() error { 57 config, e := g.generateConfig() 58 if e != nil { 59 return e 60 } 61 svc := ec2.NewFromConfig(config) 62 63 g.Resources = g.createElasticIpsResources(svc) 64 return nil 65 }