github.com/GoogleCloudPlatform/terraformer@v0.8.18/cmd/provider_cmd_aws.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  package cmd
    15  
    16  import (
    17  	"log"
    18  
    19  	awsterraformer "github.com/GoogleCloudPlatform/terraformer/providers/aws"
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  func newCmdAwsImporter(options ImportOptions) *cobra.Command {
    25  	cmd := &cobra.Command{
    26  		Use:   "aws",
    27  		Short: "Import current state to Terraform configuration from AWS",
    28  		Long:  "Import current state to Terraform configuration from AWS",
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			originalResources := options.Resources
    31  			originalRegions := options.Regions
    32  			originalPathPattern := options.PathPattern
    33  
    34  			if len(options.Regions) > 0 {
    35  				shouldSpecifyPathRegion := len(options.Regions) > 1
    36  				globalResources := parseGlobalResources(originalResources)
    37  				options.Resources = globalResources
    38  				options.Regions = []string{awsterraformer.GlobalRegion}
    39  				e := importGlobalResources(options)
    40  				if e != nil {
    41  					return e
    42  				}
    43  
    44  				options.Resources = parseRegionalResources(originalResources)
    45  				options.Regions = originalRegions
    46  				if len(options.Resources) > 0 { // don't import anything and potentially override global resources
    47  					if len(globalResources) > 0 {
    48  						shouldSpecifyPathRegion = true // we should keep global resources away from regional
    49  					}
    50  					for _, region := range originalRegions {
    51  						e := importRegionResources(options, originalPathPattern, region, shouldSpecifyPathRegion)
    52  						if e != nil {
    53  							return e
    54  						}
    55  					}
    56  				}
    57  				return nil
    58  			}
    59  			err := importRegionResources(options, options.PathPattern, awsterraformer.NoRegion, false)
    60  			if err != nil {
    61  				return err
    62  			}
    63  			return nil
    64  		},
    65  	}
    66  	cmd.AddCommand(listCmd(newAWSProvider()))
    67  	baseProviderFlags(cmd.PersistentFlags(), &options, "vpc,subnet,nacl", "elb=id1:id2:id4")
    68  
    69  	cmd.PersistentFlags().StringVarP(&options.Profile, "profile", "", "default", "prod")
    70  	cmd.PersistentFlags().StringSliceVarP(&options.Regions, "regions", "", []string{}, "eu-west-1,eu-west-2,us-east-1")
    71  	return cmd
    72  }
    73  
    74  func parseGlobalResources(allResources []string) []string {
    75  	var globalResources []string
    76  	for _, resourceName := range allResources {
    77  		if contains(awsterraformer.SupportedGlobalResources, resourceName) {
    78  			globalResources = append(globalResources, resourceName)
    79  		}
    80  	}
    81  	return globalResources
    82  }
    83  
    84  func importGlobalResources(options ImportOptions) error {
    85  	if len(options.Resources) > 0 {
    86  		return importRegionResources(options, options.PathPattern, awsterraformer.GlobalRegion, false)
    87  	}
    88  	return nil
    89  }
    90  
    91  func parseRegionalResources(allResources []string) []string {
    92  	var localResources []string
    93  	for _, resourceName := range allResources {
    94  		if !contains(awsterraformer.SupportedGlobalResources, resourceName) {
    95  			localResources = append(localResources, resourceName)
    96  		}
    97  	}
    98  	return localResources
    99  }
   100  
   101  func importRegionResources(options ImportOptions, originalPathPattern string, region string, shouldSpecifyPathRegion bool) error {
   102  	provider := newAWSProvider()
   103  	options.PathPattern = originalPathPattern
   104  	if region != awsterraformer.GlobalRegion && region != awsterraformer.NoRegion {
   105  		if shouldSpecifyPathRegion {
   106  			options.PathPattern += region + "/"
   107  		}
   108  		log.Println(provider.GetName() + " importing region " + region)
   109  	} else {
   110  		log.Println(provider.GetName() + " importing default region")
   111  	}
   112  	err := Import(provider, options, []string{region, options.Profile})
   113  	if err != nil {
   114  		return err
   115  	}
   116  	return nil
   117  }
   118  
   119  func newAWSProvider() terraformutils.ProviderGenerator {
   120  	return &awsterraformer.AWSProvider{}
   121  }
   122  
   123  func contains(s []string, e string) bool {
   124  	for _, a := range s {
   125  		if a == e {
   126  			return true
   127  		}
   128  	}
   129  	return false
   130  }