github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/ec2.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  	"strings"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  
    23  	"github.com/aws/aws-sdk-go-v2/aws"
    24  	"github.com/aws/aws-sdk-go-v2/service/ec2"
    25  	"github.com/aws/aws-sdk-go-v2/service/ec2/types"
    26  )
    27  
    28  var ec2AllowEmptyValues = []string{"tags."}
    29  
    30  type Ec2Generator struct {
    31  	AWSService
    32  }
    33  
    34  func (g *Ec2Generator) InitResources() error {
    35  	config, e := g.generateConfig()
    36  	if e != nil {
    37  		return e
    38  	}
    39  	svc := ec2.NewFromConfig(config)
    40  	var filters []types.Filter
    41  	for _, filter := range g.Filter {
    42  		if strings.HasPrefix(filter.FieldPath, "tags.") && filter.IsApplicable("instance") {
    43  			filters = append(filters, types.Filter{
    44  				Name:   aws.String("tag:" + strings.TrimPrefix(filter.FieldPath, "tags.")),
    45  				Values: filter.AcceptableValues,
    46  			})
    47  		}
    48  	}
    49  	p := ec2.NewDescribeInstancesPaginator(svc, &ec2.DescribeInstancesInput{
    50  		Filters: filters,
    51  	})
    52  	for p.HasMorePages() {
    53  		page, e := p.NextPage(context.TODO())
    54  		if e != nil {
    55  			return e
    56  		}
    57  		for _, reservation := range page.Reservations {
    58  			for _, instance := range reservation.Instances {
    59  				name := ""
    60  				for _, tag := range instance.Tags {
    61  					if strings.ToLower(*tag.Key) == "name" {
    62  						name = *tag.Value
    63  					}
    64  				}
    65  				attr, err := svc.DescribeInstanceAttribute(context.TODO(), &ec2.DescribeInstanceAttributeInput{
    66  					Attribute:  types.InstanceAttributeNameUserData,
    67  					InstanceId: instance.InstanceId,
    68  				})
    69  				userDataBase64 := ""
    70  				if err == nil && attr.UserData != nil && attr.UserData.Value != nil {
    71  					userDataBase64 = *attr.UserData.Value
    72  				}
    73  				r := terraformutils.NewResource(
    74  					*instance.InstanceId,
    75  					*instance.InstanceId+"_"+name,
    76  					"aws_instance",
    77  					"aws",
    78  					map[string]string{
    79  						"user_data_base64":  userDataBase64,
    80  						"source_dest_check": "true",
    81  					},
    82  					ec2AllowEmptyValues,
    83  					map[string]interface{}{},
    84  				)
    85  				g.Resources = append(g.Resources, r)
    86  			}
    87  		}
    88  	}
    89  	return nil
    90  }
    91  
    92  func (g *Ec2Generator) PostConvertHook() error {
    93  	for _, r := range g.Resources {
    94  		if r.InstanceInfo.Type != "aws_instance" {
    95  			continue
    96  		}
    97  		rootDeviceVolumeType := r.InstanceState.Attributes["root_block_device.0.volume_type"]
    98  		if !(rootDeviceVolumeType == "io1" || rootDeviceVolumeType == "io2" || rootDeviceVolumeType == "gp3") {
    99  			delete(r.Item["root_block_device"].([]interface{})[0].(map[string]interface{}), "iops")
   100  		}
   101  		if rootDeviceVolumeType != "gp3" {
   102  			delete(r.Item["root_block_device"].([]interface{})[0].(map[string]interface{}), "throughput")
   103  		}
   104  	}
   105  
   106  	return nil
   107  }