Terratest terraform.Options 和 terraform.tfvars 中传递的变量之间的差异?

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

我得到 terraform.Options 是为了测试目的,但是当我的 Terratest 可以从 tfvars 文件中获取变量值时,它的目的是什么?我怎样才能最大限度地利用它?

此外,我在 tfvars 中的大部分变量都是嵌套的。我如何在 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,
        },
    }

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?

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

How would I map that out in terraform.Options?

最容易使用嵌套类型构造函数:

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