github.com/GoogleCloudPlatform/terraformer@v0.8.18/cmd/provider_cmd_google.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  	"strings"
    19  
    20  	gcp_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/gcp"
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  func newCmdGoogleImporter(options ImportOptions) *cobra.Command {
    26  	providerType := ""
    27  	cmd := &cobra.Command{
    28  		Use:   "google",
    29  		Short: "Import current state to Terraform configuration from Google Cloud",
    30  		Long:  "Import current state to Terraform configuration from Google Cloud",
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			originalPathPattern := options.PathPattern
    33  			for _, project := range options.Projects {
    34  				for _, region := range options.Regions {
    35  					provider := newGoogleProvider()
    36  					options.PathPattern = originalPathPattern
    37  					options.PathPattern = strings.ReplaceAll(options.PathPattern, "{provider}/{service}", "{provider}/"+project+"/{service}/"+region)
    38  					log.Println(provider.GetName() + " importing project " + project + " region " + region)
    39  					err := Import(provider, options, []string{region, project, providerType})
    40  					if err != nil {
    41  						return err
    42  					}
    43  				}
    44  			}
    45  			return nil
    46  		},
    47  	}
    48  	cmd.AddCommand(listCmd(newGoogleProvider()))
    49  	baseProviderFlags(cmd.PersistentFlags(), &options, "firewalls,networks", "compute_firewall=id1:id2:id4")
    50  	cmd.PersistentFlags().StringSliceVarP(&options.Regions, "regions", "z", []string{"global"}, "europe-west1,")
    51  	cmd.PersistentFlags().StringSliceVarP(&options.Projects, "projects", "", []string{}, "")
    52  	cmd.PersistentFlags().StringVarP(&providerType, "provider-type", "", "", "beta")
    53  	_ = cmd.MarkPersistentFlagRequired("projects")
    54  	return cmd
    55  }
    56  
    57  func newGoogleProvider() terraformutils.ProviderGenerator {
    58  	return &gcp_terraforming.GCPProvider{}
    59  }