英文:
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, "")
}))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论