Terraform - 在 variables.tf 中为 type = map(object()) 添加验证
Terraform - Adding Validation for type = map(object()) in variables.tf
首先感谢这个 post ,它回答了我在获取默认值以使用类型 map(object()) 时遇到的困难的第一部分。我试图开始工作的最后一部分是如何验证输入值。
terraform {
experiments = [module_variable_optional_attrs]
}
variable "dns_server" {
description = "Add DNS Servers for domain resolution. You can configure a maximum of two servers. Only one can be preferred 'true'."
type = map(object({
preferred = optional(bool)
server = optional(string)
}))
default = {
default = {
preferred = false
server = "198.18.1.1"
}
}
validation {
condition = (
can(regexall("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", var.dns_server["server"]))
)
error_message = "The DNS Server is not a valid IPv4 Address."
}
}
locals {
dns_server = {
for k, v in var.dns_server : k => {
preferred = coalesce(v.preferred, false)
server = coalesce(v.server, "198.18.1.1")
}
}
}
我知道变量字段中的默认值未被使用,但我将其用作 terraform 文档输出的占位符。
我也知道我上面的验证是不正确的,因为如果用户使用默认服务器 IPv4,则在本地定义之前不会设置。我只是不知道进行验证的方法,因为我可信赖的 google 搜索没有找到任何类似的例子。
如果您需要有关代码使用方式的更多详细信息,代码位于此处:
https://github.com/scotttyso/terraform-aci-fabric/tree/main/test
如果我注释掉验证,其他一切都正常。提前致谢。
这就是你想要的吗?
variable "mapobject" {
type = map(object({
cidr_block = string
destination_type = string
}
))
validation {
condition = alltrue([
for o in var.mapobject : contains(["CIDR_BLOCK","NETWORK_SECURITY_GROUP","SERVICE_CIDR_BLOCK"],o.destination_type)]) error_message = "All destination_types must be one of CIDR_BLOCK,NETWORK_SECURITY_GROUP or SERVICE_CIDR_BLOCK!"
}
}
变量赋值
mapobject = {
"r0" = {cidr_block = "10.1.1.0/24",destination_type = "CIDR_BLOCK" }
}
验证成功,如下所示失败(按要求)
mapobject = {
"r0" = {cidr_block = "10.1.1.0/24",destination_type = "CIRD_BLOCK" }
}
Error: Invalid value for variable
on main.tf line 86:
86: variable "mapobject" {
All destination_types must be one of CIDR_BLOCK,NETWORK_SECURITY_GROUP or
SERVICE_CIDR_BLOCK!
This was checked by the validation rule at main.tf:93,2-12.
如果是,那么荣誉就在这里:https://discuss.hashicorp.com/t/validate-list-object-variables/18291/2
首先感谢这个 post
terraform {
experiments = [module_variable_optional_attrs]
}
variable "dns_server" {
description = "Add DNS Servers for domain resolution. You can configure a maximum of two servers. Only one can be preferred 'true'."
type = map(object({
preferred = optional(bool)
server = optional(string)
}))
default = {
default = {
preferred = false
server = "198.18.1.1"
}
}
validation {
condition = (
can(regexall("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", var.dns_server["server"]))
)
error_message = "The DNS Server is not a valid IPv4 Address."
}
}
locals {
dns_server = {
for k, v in var.dns_server : k => {
preferred = coalesce(v.preferred, false)
server = coalesce(v.server, "198.18.1.1")
}
}
}
我知道变量字段中的默认值未被使用,但我将其用作 terraform 文档输出的占位符。
我也知道我上面的验证是不正确的,因为如果用户使用默认服务器 IPv4,则在本地定义之前不会设置。我只是不知道进行验证的方法,因为我可信赖的 google 搜索没有找到任何类似的例子。
如果您需要有关代码使用方式的更多详细信息,代码位于此处:
https://github.com/scotttyso/terraform-aci-fabric/tree/main/test
如果我注释掉验证,其他一切都正常。提前致谢。
这就是你想要的吗?
variable "mapobject" {
type = map(object({
cidr_block = string
destination_type = string
}
))
validation {
condition = alltrue([
for o in var.mapobject : contains(["CIDR_BLOCK","NETWORK_SECURITY_GROUP","SERVICE_CIDR_BLOCK"],o.destination_type)]) error_message = "All destination_types must be one of CIDR_BLOCK,NETWORK_SECURITY_GROUP or SERVICE_CIDR_BLOCK!"
}
}
变量赋值
mapobject = {
"r0" = {cidr_block = "10.1.1.0/24",destination_type = "CIDR_BLOCK" }
}
验证成功,如下所示失败(按要求)
mapobject = {
"r0" = {cidr_block = "10.1.1.0/24",destination_type = "CIRD_BLOCK" } }
Error: Invalid value for variable
on main.tf line 86:
86: variable "mapobject" {
All destination_types must be one of CIDR_BLOCK,NETWORK_SECURITY_GROUP or
SERVICE_CIDR_BLOCK!
This was checked by the validation rule at main.tf:93,2-12.
如果是,那么荣誉就在这里:https://discuss.hashicorp.com/t/validate-list-object-variables/18291/2