简体   繁体   中英

Concatenate declared variable with random string

It is possible, using Terratest, to declare a tfvars file with the following variable:

bar = {
  name   = "test"
  domain = "test.com"
  regions = [
    { location = "France Central", alias = "france" }
  ]
}

But include a random prefix to the bar.domain string inside the go code?

I'm using terraformOptions as follows:

terraformOptions := &terraform.Options{
        TerraformDir: sourcePath,
        VarFiles:     []string{variablesPath + "/integration.tfvars"},
}

It is not ideal for one to make use of the tfvars file directly to take the input in case of tests. More on this here

To answer your question :

You can use something similar to this :

options := terraform.Options{
        TerraformDir: "sourcePath",
        Vars: map[string]interface{}{
                "name":  "test",
                "domain": addRandomprefix()+"test.com",
                "region ":    map[string]interface{}{
                    "location" : "France Central",
                    "alias" : "france",
                },
        },
    }

Just create your own custom addRandomprefix() method. I hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM