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