简体   繁体   English

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

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

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 用于测试目的,但是当我的 Terratest 可以从 tfvars 文件中获取变量值时,它的目的是什么? How can I utilize it to it's max ability?我怎样才能最大限度地利用它?

Also, most of my variables in tfvars are nested.此外,我在 tfvars 中的大部分变量都是嵌套的。 How would I map that out in terraform.Options?我怎么会在terraform.Options中把map那个出来?

#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 用于测试目的,但是当我的 Terratest 可以从 tfvars 文件中获取变量值时,它的目的是什么?

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.您可以使用terraform.Options为模块提供各种输入,然后确定模块的行为和输出是否符合预期。 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?我怎么会在terraform.Options中把map那个出来?

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

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

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