定义复杂输入变量时:
variable "s3_shares" {
type = map(object({
s3_bucket_arn = string
client_list = list(string)
read_only = bool
default_storage_class = string
}))
}
如何处理read_only
和default_storage_class
是互斥的?换句话说,在使用该模块并使用read_only = true
定义s3_share
时,可以省略default_storage_class
。
使用验证{}块和alltrue()函数:
variable "s3_shares" {
type = map(object({
s3_bucket_arn = string
client_list = list(string)
read_only = bool
default_storage_class = string
}))
default = {
"one" = {
s3_bucket_arn = "foo"
client_list = ["foo","bar"]
read_only = false
default_storage_class = "bar" # IS OK
}
"two" = {
s3_bucket_arn = "foo"
client_list = ["foo","bar"]
read_only = false
default_storage_class = "" # IS OK
}}
"three" = {
s3_bucket_arn = "foo"
client_list = ["foo","bar"]
read_only = true
default_storage_class = "" # IS OK
}}
"four" = {
s3_bucket_arn = "foo"
client_list = ["foo","bar"]
read_only = true
default_storage_class = "bar" # IS KO
}}
validation {
condition = alltrue([
for o in var.s3_shares : !(o.read_only && length(o.default_storage_class) > 0)])
error_message = "Read_only and default_storage_class are exclusive."
}
}
注意用例"two">其中read_only
设置为false
,default_storage_class
为空:这将返回true
。这可能不是您想要的行为。