Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不中断现有客户端的情况下扩展terraform模块输入变量模式?_Terraform - Fatal编程技术网

如何在不中断现有客户端的情况下扩展terraform模块输入变量模式?

如何在不中断现有客户端的情况下扩展terraform模块输入变量模式?,terraform,Terraform,我有一个具有以下输入变量的模块: variable "apsvc_map" { description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties." type = map(object({ identity_ids = list(string), disabled = bool })) } 现在我想

我有一个具有以下输入变量的模块:

variable "apsvc_map" {
  description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
  type        = map(object({ 
    identity_ids = list(string),
    disabled = bool
  }))
}
现在我想向模式添加一个新属性-
无自定义主机名绑定
。新版本将是:

variable "apsvc_map" {
  description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
  type        = map(object({ 
    identity_ids = list(string),
    disabled = bool
    no_custom_hostname_binding = bool
  }))
}
通过使用
try
函数,可以使此更改在模块代码中向后兼容,因为省略新属性相当于为其提供
false

但是,terraform严格对待此模式,不允许在没有新字段的情况下传递输入:

2020-05-30T15:34:20.8061749Z Error: Invalid value for module argument
2020-05-30T15:34:20.8062005Z 
2020-05-30T15:34:20.8062205Z   on ..\..\modules\web\main.tf line 47, in module "web":
2020-05-30T15:34:20.8062336Z   47:   apsvc_map = {
2020-05-30T15:34:20.8062484Z   48:     dfhub = {
2020-05-30T15:34:20.8062727Z   49:       disabled     = false
2020-05-30T15:34:20.8065156Z   50:       identity_ids = [local.identity_id]
2020-05-30T15:34:20.8065370Z   51:     }
2020-05-30T15:34:20.8065459Z   52:   }
2020-05-30T15:34:20.8065538Z 
我从terraform抱怨的错误中了解到,因为我没有在输入中指定新属性的值

因此,有三种解决方案:

  • 更新所有现有代码以添加新属性-不可能
  • 以不同的方式标记模块的新版本,并让新代码引用新标记,而旧代码继续引用旧标记-从长远来看,将导致标记激增,在标记名称中创建各种奇怪的笛卡尔特征乘法。最终,这是不可能的
  • 通过注释掉可选属性来放松输入变量模式,并在代码中使用
    try
  • 最后一个选项并不理想,因为模块的文档不会列出可选属性。但是从代码管理的角度来看,它是最好的

    所以问题是-输入对象属性可以定义为可选的吗?理想情况下,它应该包括默认值,但我现在同意
    try
    方法

    编辑1


    实际上,我以为我可以在对象中传递未知属性,但是不行。一旦给出了模式,它就不存在了。因此,在我的例子中,唯一向后兼容的解决方案是使用
    map(any)

    建议Terraform使用对象变量中的可选参数:

    不幸的是,截至2020年5月30日,在这方面没有任何进展。
    这是他们回购协议中投票最多的问题,我们所能做的就是继续投票,希望很快就能实施


    你是对的。考虑到你的选择,你的偏好,以及我认为你有第四个选择的事实,这些选择是不可能的,或者是纯粹的黑客行为

    variable "apsvc_map" {
      description = "The App Services sharing the same App Service Plan. Maps an App Service name to its properties."
      default = {}
      type        = map(object({ 
        identity_ids = list(string),
        disabled = bool
      }))
    }
    
    variable "no_custom_hostname_binding" {
      description = "Whether or not an App Service should disable hostname binding. Maps an App Service name to an override of the no_custom_hostname_binding property."
      type        = map(bool)
    }
    
    从那里,您可以像这样使用它:

    lookup(var.no_custom_hostname_binding[local.awsvpc_map_key], null)
    
    no_custom_hostname_binding = {
      "vpc_key" = true
    }
    
    并声明如下所示的覆盖:

    lookup(var.no_custom_hostname_binding[local.awsvpc_map_key], null)
    
    no_custom_hostname_binding = {
      "vpc_key" = true
    }
    
    在需要知道该参数的表达式中。这不是非常优雅,但是如果没有可选参数,您就没有很多好的选择


    您可以按照此模式添加所需的任意多个可选覆盖,以后也可以添加更多,而不会中断客户端。

    加入俱乐部:+1,并给他们留下一条评论,说明您为什么需要它,希望它很快就会实现。。。这是他们报告中投票率最高的问题,真是糟糕透顶。请把你的评论推广到回答上来,这样我就可以相信你了。真的不是很优雅。。。正如一位用户所说:
    。。。只要你能忍受它给你带来的可怕的负罪感。