github.com/GoogleCloudPlatform/terraformer@v0.8.18/tests/gcp/main.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 main
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  	"os/exec"
    21  	"sort"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/cmd"
    24  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    25  
    26  	gcp_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/gcp"
    27  )
    28  
    29  const command = "terraform init && terraform plan"
    30  
    31  func main() {
    32  	zone := "europe-west1-c"
    33  	services := []string{}
    34  	provider := &gcp_terraforming.GCPProvider{}
    35  	for service := range provider.GetSupportedService() {
    36  		if service == "images" {
    37  			continue
    38  		}
    39  		if service == "iam" {
    40  			continue
    41  		}
    42  		if service == "instanceTemplates" {
    43  			continue
    44  		}
    45  		if service == "targetHttpProxies" {
    46  			continue
    47  		}
    48  		if service == "monitoring" {
    49  			continue
    50  		}
    51  		if service == "cloudsql" {
    52  			continue
    53  		}
    54  		if service == "bigQuery" {
    55  			continue
    56  		}
    57  		services = append(services, service)
    58  	}
    59  	sort.Strings(services)
    60  	provider = &gcp_terraforming.GCPProvider{
    61  		Provider: terraformutils.Provider{},
    62  	}
    63  	err := cmd.Import(provider, cmd.ImportOptions{
    64  		Resources:   services,
    65  		PathPattern: cmd.DefaultPathPattern,
    66  		PathOutput:  cmd.DefaultPathOutput,
    67  		State:       "local",
    68  		Zone:        "europe-west1-a",
    69  		Connect:     true,
    70  	}, []string{zone})
    71  	if err != nil {
    72  		log.Println(err)
    73  		os.Exit(1)
    74  	}
    75  	rootPath, _ := os.Getwd()
    76  	for _, serviceName := range services {
    77  		currentPath := cmd.Path(cmd.DefaultPathPattern, provider.GetName(), serviceName, cmd.DefaultPathOutput)
    78  		if err := os.Chdir(currentPath); err != nil {
    79  			log.Println(err)
    80  			os.Exit(1)
    81  		}
    82  		cmd := exec.Command("sh", "-c", command)
    83  		cmd.Stdout = os.Stdout
    84  		cmd.Stderr = os.Stderr
    85  		err = cmd.Run()
    86  		if err != nil {
    87  			log.Println(err)
    88  			os.Exit(1)
    89  		}
    90  		err := os.Chdir(rootPath)
    91  		if err != nil {
    92  			log.Println(err)
    93  		}
    94  	}
    95  }