Terraform验证可选变量的列表对象

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

Terraform validation on list object using optional variable

问题

这里的验证在我将accelerator_type设置在两个列表对象中时有效,但我想要能够在单个对象上使用验证,如果你从.tfvars文件中的其中一个对象中删除accelerator_type,验证将失败。

variable "notebook_configurations" {
  type = list(object({
    notebook_name       = string
    accelerator_type    = optional(string, "")
  }))
  default = [{
    "notebook_name"       = "my-first-notebook"
  }]
  validation {
    condition = length([
      for o in var.notebook_configurations : true
      if contains(["ACCELERATOR_TYPE_UNSPECIFIED", "NVIDIA_TESLA_K80"], o.accelerator_type)
    ]) == length(var.notebook_configurations)
    error_message = "Possible types of this accelerator. - ('ACCELERATOR_TYPE_UNSPECIFIED', 'NVIDIA_TESLA_K80')"
  }
}

terraform.tfvars

notebook_configurations = [{
  notebook_name       = "test1-standard-notebook",
  accelerator_type =  "ACCELERATOR_TYPE_UNSPECIFIED"
  },
  {
    notebook_name       = "test2-standard-notebook",
    accelerator_type =  "NVIDIA_TESLA_K80"
  }
]

错误信息:

Error: Invalid value for variable
  │   
  │   on validate_variable.tf line 2:
  │   2: variable "notebook_configurations" {
  │   
  ├────────────────
  │     │ var.notebook_configurations is list of object with 2 elements
  │   Possible types of this accelerator. - ('ACCELERATOR_TYPE_UNSPECIFIED', 'NVIDIA_TESLA_K80')
  │   This was checked by the validation rule at validate_variable.tf:11,3-13.
英文:

Think I am pretty close to getting this but...

The validation here works when I have the accelerator_type set in both list objects but I want to be able to use the validation on individual objects not all objects, if you remove accelerator_type ​from one of the objects in the list in the .tfvars it will fail validation.

variable "notebook_configurations" {
  type = list(object({
    notebook_name       = string
    accelerator_type    = optional(string, "")
  }))
  default = [{
    "notebook_name"       = "my-first-notebook"
  }]
  validation {
    condition = length([
      for o in var.notebook_configurations : true
      if contains(["ACCELERATOR_TYPE_UNSPECIFIED", "NVIDIA_TESLA_K80"], o.accelerator_type)
    ]) == length(var.notebook_configurations)
    error_message = "Possible types of this accelerator. - ('ACCELERATOR_TYPE_UNSPECIFIED', 'NVIDIA_TESLA_K80')"
  }
#  validation {
#    condition = alltrue([
#      for obj in var.notebook_configurations : contains(["ACCELERATOR_TYPE_UNSPECIFIED", "NVIDIA_TESLA_K80"], obj.accelerator_type)
#    ])
#    error_message = "Possible types of this accelerator. - ('ACCELERATOR_TYPE_UNSPECIFIED', 'NVIDIA_TESLA_K80')."
#  }
}

terraform.tfvars

notebook_configurations = [{
  notebook_name       = "test1-standard-notebook",
  accelerator_type =  "ACCELERATOR_TYPE_UNSPECIFIED"
  },
  {
    notebook_name       = "test2-standard-notebook",
    accelerator_type =  "NVIDIA_TESLA_K80"
  }
]

error:

> Error: Invalid value for variable │ │ on validate_variable.tf line
> 2: │ 2: variable "notebook_configurations" { │
> ├──────────────── │ │ var.notebook_configurations is list of
> object with 2 elements │ │ Possible types of this accelerator. -
> ('ACCELERATOR_TYPE_UNSPECIFIED', 'NVIDIA_TESLA_K80') │ │ This was
> checked by the validation rule at validate_variable.tf:11,3-13.

答案1

得分: 1

只需在条件中添加一个OR运算符,以允许通过变量定义中设置的默认值,例如:

validation {
  condition = length([
    for o in var.notebook_configurations : true
    if contains(["ACCELERATOR_TYPE_UNSPECIFIED", "NVIDIA_TESLA_K80"], o.accelerator_type) || o.accelerator_type == ""
  ]) == length(var.notebook_configurations)
  error_message = "可能的加速器类型。 - ('ACCELERATOR_TYPE_UNSPECIFIED', 'NVIDIA_TESLA_K80')"
}

在这种情况下,条件检查accelerator_type是否设置为"",这是在这里设置的默认值:

type = list(object({
  notebook_name    = string
  accelerator_type = optional(string, "")
}))
英文:

Just add an OR operator to the conditional to allow the default value set in the variable definition to pass, e.g.:

  validation {
    condition = length([
      for o in var.notebook_configurations : true
      if contains(["ACCELERATOR_TYPE_UNSPECIFIED", "NVIDIA_TESLA_K80"], o.accelerator_type) || o.accelerator_type == ""
    ]) == length(var.notebook_configurations)
    error_message = "Possible types of this accelerator. - ('ACCELERATOR_TYPE_UNSPECIFIED', 'NVIDIA_TESLA_K80')"
  }

In this case, the condition checks if the accelerator_type is set to "", which it's the default value set in here:

  type = list(object({
    notebook_name    = string
    accelerator_type = optional(string, "")
  }))

huangapple
  • 本文由 发表于 2023年2月24日 00:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547582.html
匿名

发表评论

匿名网友

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

确定