github.com/GoogleCloudPlatform/terraformer@v0.8.18/tests/datadog/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 "errors" 19 "fmt" 20 "log" 21 "os" 22 "reflect" 23 "sort" 24 "strings" 25 26 "github.com/GoogleCloudPlatform/terraformer/cmd" 27 datadog_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/datadog" 28 ) 29 30 func main() { 31 var terraformerServices []string 32 var terraformerFilters []string 33 34 provider := &datadog_terraforming.DatadogProvider{} 35 cfg, _ := getConfig() 36 37 // CD into 'tests/datadog/resources' 38 err := os.Chdir(datadogResourcesPath) 39 if err != nil { 40 handleFatalErr(cfg, err, "Error changing directory: ") 41 } 42 // Run the terraform v13 upgrade command if applicable 43 if strings.Contains(cfg.tfVersion, "0.13.") { 44 if err := cmdRun(cfg, []string{commandTerraformV13Upgrade}); err != nil { 45 handleFatalErr(cfg, err, "Error running command 'terraform 0.13upgrade'") 46 } 47 } 48 // Initialize the Datadog provider in dir 'tests/datadog/resources' 49 err = initializeDatadogProvider(cfg) 50 if err != nil { 51 handleFatalErr(cfg, err, "Error initializing the Datadog provider: ") 52 } 53 54 // Create datadog resources 55 resourcesMap, err := createDatadogResource(cfg) 56 if err != nil { 57 handleFatalErr(cfg, err, "Error creating resources") 58 } 59 60 // Get list of terraformerServices and terraformerFilters from created resources 61 for resource, resourceID := range *resourcesMap { 62 terraformerServices = append(terraformerServices, resource) 63 terraformerFilters = append(terraformerFilters, fmt.Sprintf("%s=%s", resource, strings.Join(resourceID, ":"))) 64 } 65 if len(terraformerServices) == 0 { 66 terraformerServices = getAllServices(provider) 67 } 68 69 // Delete the 'generated/' directory if it already exists 70 _ = os.RemoveAll("generated/") 71 72 // Import created resources with Terraformer 73 err = cmd.Import(provider, cmd.ImportOptions{ 74 Resources: terraformerServices, 75 PathPattern: "{output}/", 76 PathOutput: cmd.DefaultPathOutput, 77 State: "local", 78 Connect: true, 79 Output: "hcl", 80 Filter: terraformerFilters, 81 Verbose: true, 82 }, []string{cfg.Datadog.apiKey, cfg.Datadog.appKey, cfg.Datadog.apiURL}) 83 if err != nil { 84 handleFatalErr(cfg, err, "Error while importing resources") 85 } 86 87 // Run tests on created and imported resources 88 err = terraformerResourcesTest(cfg, resourcesMap) 89 if err != nil { 90 handleFatalErr(cfg, err, "Terraform resource test step failed") 91 } 92 93 // Destroy created resources 94 err = destroyDatadogResources(cfg) 95 if err != nil { 96 log.Fatal("Error while destroying resources ", err) 97 } 98 99 log.Print("Successfully created and imported resources with Terraformer") 100 } 101 102 func terraformerResourcesTest(cfg *Config, resourcesMap *map[string][]string) error { 103 if err := os.Chdir("generated/"); err != nil { 104 return err 105 } 106 107 // Run TF 0.13 upgrade command if applicable 108 if strings.Contains(cfg.tfVersion, "0.13.") { 109 if err := cmdRun(cfg, []string{commandTerraformV13Upgrade}); err != nil { 110 return err 111 } 112 } 113 114 // Initialize Datadog provider in the 'generated/' directory 115 err := initializeDatadogProvider(cfg) 116 if err != nil { 117 handleFatalErr(cfg, err, "Error initializing the Datadog provider: ") 118 } 119 120 // Collect tf outputs from generated resources 121 terraformerResourcesOutput, err := terraformOutput() 122 if err != nil { 123 log.Println(err) 124 return err 125 } 126 terraformResourcesMap := parseTerraformOutput(string(terraformerResourcesOutput)) 127 128 // Sort the map values 129 for _, v := range *terraformResourcesMap { 130 sort.Strings(v) 131 } 132 for _, v := range *resourcesMap { 133 sort.Strings(v) 134 } 135 136 log.Println("Comparing resource names and resources ids. \n Created resources:", resourcesMap, "\n Imported Resources:", terraformResourcesMap) 137 match := reflect.DeepEqual(resourcesMap, terraformResourcesMap) 138 if match { 139 // Run 'terraform plan' against the generated resources 140 // Command will exit with exit code 2 if diff is produced 141 log.Println("Running terraform plan on generated resources. This should produce no diff") 142 err := terraformPlan(cfg) 143 if err != nil { 144 return err 145 } 146 147 if err := os.Chdir(cfg.rootPath); err != nil { 148 return err 149 } 150 } else { 151 return errors.New("imported resource names and/or ids did not match the created") 152 } 153 154 if err := os.Chdir(cfg.rootPath); err != nil { 155 return err 156 } 157 158 return nil 159 } 160 161 func handleFatalErr(cfg *Config, err error, msg string) { 162 // Destroy any lingering resources before exiting 163 log.Print("Destroying resources before exiting") 164 if err := destroyDatadogResources(cfg); err != nil { 165 log.Printf("Error while destroying resources: %s", err) 166 } 167 168 log.Fatalf("Message: %s. Error: %s", msg, err) 169 }