github.com/GoogleCloudPlatform/terraformer@v0.8.18/tests/openstack/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  	openstack_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/openstack"
    25  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    26  )
    27  
    28  const command = "terraform init && terraform plan"
    29  
    30  func main() {
    31  	region := "RegionOne"
    32  	services := []string{}
    33  	provider := &openstack_terraforming.OpenStackProvider{}
    34  	for service := range provider.GetSupportedService() {
    35  		services = append(services, service)
    36  	}
    37  	sort.Strings(services)
    38  	provider = &openstack_terraforming.OpenStackProvider{
    39  		Provider: terraformutils.Provider{},
    40  	}
    41  	err := cmd.Import(provider, cmd.ImportOptions{
    42  		Resources:   services,
    43  		PathPattern: cmd.DefaultPathPattern,
    44  		PathOutput:  cmd.DefaultPathOutput,
    45  		State:       "local",
    46  		Connect:     true,
    47  	}, []string{region})
    48  	if err != nil {
    49  		log.Println(err)
    50  		os.Exit(1)
    51  	}
    52  	rootPath, _ := os.Getwd()
    53  	for _, serviceName := range services {
    54  		currentPath := cmd.Path(cmd.DefaultPathPattern, provider.GetName(), serviceName, cmd.DefaultPathOutput)
    55  		if err := os.Chdir(currentPath); err != nil {
    56  			log.Println(err)
    57  			os.Exit(1)
    58  		}
    59  		cmd := exec.Command("sh", "-c", command)
    60  		cmd.Stdout = os.Stdout
    61  		cmd.Stderr = os.Stderr
    62  		err = cmd.Run()
    63  		if err != nil {
    64  			log.Println(err)
    65  			os.Exit(1)
    66  		}
    67  		err := os.Chdir(rootPath)
    68  		if err != nil {
    69  			log.Println(err)
    70  		}
    71  	}
    72  }