github.com/GoogleCloudPlatform/terraformer@v0.8.18/tests/aws/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  	"time"
    22  
    23  	"github.com/GoogleCloudPlatform/terraformer/cmd"
    24  	aws_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/aws"
    25  )
    26  
    27  func main() {
    28  	tCommand := cmd.NewCmdRoot()
    29  	pathPattern := "{output}/{provider}/"
    30  	tCommand.SetArgs([]string{
    31  		"import",
    32  		"aws",
    33  		"--regions=ap-southeast-1",
    34  		"--resources=ssm",
    35  		"--profile=personal",
    36  		"--verbose",
    37  		"--compact",
    38  		"--path-pattern=" + pathPattern,
    39  	})
    40  	start := time.Now()
    41  	if err := tCommand.Execute(); err != nil {
    42  		log.Println(err)
    43  		os.Exit(1)
    44  	}
    45  	log.Printf("Importing took %s", time.Since(start))
    46  	start = time.Now()
    47  	runTerraform(pathPattern)
    48  	log.Printf("Terraform init + plan took %s", time.Since(start))
    49  }
    50  
    51  func runTerraform(pathPattern string) {
    52  	rootPath, _ := os.Getwd()
    53  	provider := &aws_terraforming.AWSProvider{}
    54  
    55  	currentPath := cmd.Path(pathPattern, provider.GetName(), "", cmd.DefaultPathOutput)
    56  	if err := os.Chdir(currentPath); err != nil {
    57  		log.Println(err)
    58  		os.Exit(1)
    59  	}
    60  	tfCmd := exec.Command("sh", "-c", "terraform init && terraform plan")
    61  	tfCmd.Stdout = os.Stdout
    62  	tfCmd.Stderr = os.Stderr
    63  	err := tfCmd.Run()
    64  	if err != nil {
    65  		log.Println(err)
    66  		os.Exit(1)
    67  	}
    68  	err = os.Chdir(rootPath)
    69  	if err != nil {
    70  		log.Println(err)
    71  	}
    72  }