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