Difference between variables passed in terraform.Options for Terratest and terraform.tfvars?

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

Difference between variables passed in terraform.Options for Terratest and terraform.tfvars?

问题

我理解你的问题是关于使用terraform.Options的目的以及如何在其中映射嵌套变量值的问题。

terraform.Options是用于在Terratest中配置Terraform的选项。它允许你指定Terraform代码的路径以及变量的值。尽管你可以从tfvars文件中获取变量的值,但使用terraform.Options可以更灵活地控制和定制测试环境。

对于嵌套变量,你可以使用类似于tfvars文件中的嵌套结构来映射它们。在你的示例中,resource_group是一个嵌套的变量,它包含两个子变量resource_group1resource_group2。你可以在terraform.OptionsVars字段中使用相同的嵌套结构来映射这些变量。例如:

terraformOptions := &terraform.Options{
    TerraformDir: "../../examples/azure/terraform-azure-resourcegroup-example",
    Vars: map[string]interface{}{
        "resource_group": map[string]interface{}{
            "resource_group1": map[string]interface{}{
                "rg_name":  "Terratest1",
                "location": "us",
                "tags": map[string]interface{}{
                    "foo": "foo",
                    "bar": "bar",
                },
            },
            "resource_group2": map[string]interface{}{
                "rg_name":  "Terratest2",
                "location": "us",
                "tags": map[string]interface{}{
                    "foo": "foo",
                    "bar": "bar",
                },
            },
        },
        "postfix": uniquePostfix,
    },
}

通过这种方式,你可以在terraform.Options中正确地映射嵌套变量的值,并将其传递给Terratest进行测试。

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

I get terraform.Options is for testing purposes but what is the purpose of it when my Terratest can get the variable values from the tfvars file? How can I utilize it to it's max ability?

Also, most of my variables in tfvars are nested. How would I map that out in terraform.Options?

#variable in tfvars
resource_group = {
  resource_group1 = { 
    rg_name = "Terratest1"
    #resource group location
    location = "us"
    #tags
    tags = {
      foo = "foo"
      bar = "bar"
    }
  }
  resource_group2= {
    rg_name  = "Terratest2"
    location = "us"
    tags = {
      foo = "foo"
      bar = "bar"
    }
  }
}
#how would I map in terraform.Options?
	terraformOptions := &terraform.Options{
		// The path to where our Terraform code is located
		TerraformDir: "../../examples/azure/terraform-azure-resourcegroup-example",
		Vars: map[string]interface{}{
			"postfix": uniquePostfix,
		},
	}

答案1

得分: 1

terraform.Options是用于测试目的的,但当我的Terratest可以从tfvars文件中获取变量值时,它的目的是什么?

测试的一个重要方面是变化输入,并根据这些输入验证行为和输出。您可以使用terraform.Options为模块提供各种输入,然后确定模块的行为和输出是否符合预期。如果这是一个根模块配置,那么该值的重要性就大大降低了。如果这是一个可重用且经常声明的模块,那么它就有很大的价值。

我该如何在terraform.Options中映射这个?

最简单的方法是使用嵌套类型构造函数:

terraformOptions := &terraform.Options{
    // terraform模块的路径
    TerraformDir: "../../examples/azure/terraform-azure-resourcegroup-example",
    // 模块的输入变量映射
    Vars: map[string]interface{}{
        "resource_group1": map[string]interface{}{
            "rg_name":  "Terratest1",
            "location": "us",
            "tags": map[string]string{
                "foo": "foo",
                "bar": "bar",
            },
        },
        "resource_group2": map[string]interface{}{
            "rg_name":  "Terratest2",
            "location": "us",
            "tags": map[string]string{
                "foo": "foo",
                "bar": "bar",
            },
        },
    },
}
英文:

> I get terraform.Options is for testing purposes but what is the purpose of it when my Terratest can get the variable values from the tfvars file?

One of the important aspects of testing is to vary the inputs, and validate the behavior and outputs based on those inputs. You can use terraform.Options to provide a variety of inputs to a module, and then determine that the behavior and outputs of the module are as expected. If this is a root module config, then that value is significantly diminished. If this is a reusable and often declared module, then there is substantial value.

> How would I map that out in terraform.Options?

With nested type constructors most easily:

terraformOptions := &terraform.Options{
	// path to terraform module
	TerraformDir: "../../examples/azure/terraform-azure-resourcegroup-example",
	// map of input variables to module
	Vars: map[string]interface{}{
		"resource_group1":  map[string]interface{}{
			"rg_name":  "Terratest1",
			"location": "us",
			"tags ":    map[string]string{
                "foo": "foo",
			    "bar": "bar",
            },
		},
        "resource_group2":  map[string]interface{}{
			"rg_name":  "Terratest2",
			"location": "us",
			"tags ":    map[string]string{
                "foo": "foo",
			    "bar": "bar",
            },
		},
	},
}

huangapple
  • 本文由 发表于 2022年4月15日 10:30:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/71879471.html
匿名

发表评论

匿名网友

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

确定