gpt4 book ai didi

azure - Terraform - 根据本地字符串列表验证传递的字符串列表

转载 作者:行者123 更新时间:2023-12-03 06:16:12 25 4
gpt4 key购买 nike

我正在创建一个 Azure 事件网格 Terraform 模块,并且只有批准使用的指定事件类型列表。还将部署 Azure 策略,以从 ClickOps 角度执行相同的功能(作为辅助过滤器)。

以下是相关代码片段:

本地人.tf

local {
event_types = [
"Microsoft.Storage.BlobCreated",
"Microsoft.Storage.BlobDeleted",
"Microsoft.Storage.BlobRenamed",
"Microsoft.Web.AppUpdated"
]
}

variables.tf(默认是客户输入的示例)

variable "included_event_types" {
type = list(string)
default = ["Microsoft.Storage.BlobDeleted",
"Microsoft.Storage.BlobRenamed",
"Microsoft.Storage.DirectoryRenamed"
]
}

我的问题是,如何构建验证规则,以便它将根据本地列表检查传递的变量列表中的每个项目,以查看是否所有项目都存在?我最初有一个解决方案,在 for 关键字处出现缺少项目分隔符错误:

variable "included_event_types" {
type = list(string)
default = []

validation {
condition = all([contains(lower(local.event_types), lower(s)) for s in var.included_event_types])
error_message = "Error"
}
}

最佳答案

变量验证规则必须是独立的,并且不引用模块中的任何其他内容,因此您不能在此处引用 local.event_types,但您可以将其值内联到表达式中,像这样:

variable "included_event_types" {
type = list(string)
default = []

validation {
condition = alltrue([
for s in var.included_event_types :
contains(
[
"microsoft.storage.blobcreated",
"microsoft.storage.blobdeleted",
"microsoft.storage.blobrenamed",
"microsoft.web.appupdated",
],
lower(s),
)
])
error_message = "<helpful error message>"
}
}

关于azure - Terraform - 根据本地字符串列表验证传递的字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76232393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com