简体   繁体   English

用随机字符串连接声明的变量

[英]Concatenate declared variable with random string

It is possible, using Terratest, to declare a tfvars file with the following variable:可以使用 Terratest 声明具有以下变量的 tfvars 文件:

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?但是在 go 代码中包含 bar.domain 字符串的随机前缀?

I'm using terraformOptions as follows:我正在使用 terraformOptions 如下:

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.在测试的情况下,直接使用 tfvars 文件来获取输入并不理想。 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.只需创建您自己的自定义 addRandomprefix() 方法。 I hope this helps :)我希望这有帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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