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

huangapple go评论67阅读模式
英文:

Concatenate declared variable with random string

问题

使用Terratest,可以声明一个包含以下变量的tfvars文件:

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

但是在go代码中,如何在bar.domain字符串中包含一个随机前缀?

我正在使用以下terraformOptions:

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

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"},
}

答案1

得分: 1

在测试中,直接使用tfvars文件来接收输入并不理想。更多信息请参考这里

为了回答你的问题:

你可以使用类似以下的方法:

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

你可以创建自己的addRandomprefix()方法。希望这能帮到你 连接已声明的变量与随机字符串

英文:

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 连接已声明的变量与随机字符串

huangapple
  • 本文由 发表于 2022年5月25日 02:24:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/72367753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定