我正在使用 Open Policy Agent 针对我的 terraform 状态的 JSON 输出编写策略。
这是状态文件的结构:
{
"format_version": "0.1",
"terraform_version": "0.12.28",
"values": {
"root_module": {
"resources": [],
"child_modules": [
{
"resources": [],
"address": "",
"child_modules": [
{
"resources": [],
"address": "",
"child_modules": [
{}
]
}
]
}
]
}
}
}
我定义了这个讨厌的规则来实现我想要的,但这显然不是聚合这些资源的理想方式。
resources[resource_type] = all {
some resource_type
resource_types[resource_type]
rm := tfstate.values.root_module
# I think the below can be simplified with the built in "walk" function TODO: do that.
root_resources := [name |
name := rm.resources[_]
name.type == resource_type
]
cmone_resources = [name |
name := rm.child_modules[_].resources[_]
name.type == resource_type
]
cmtwo_resources = [name |
name := rm.child_modules[_].child_modules[_].resources[_]
name.type == resource_type
]
cm := array.concat(cmone_resources, cmtwo_resources)
all := array.concat(cm, root_resources)
}
我已阅读内置函数的文档walk(x, [path, value])
。文档在这里。我相信这个函数可以做我想做的事情,但是根据给出的文档和我在其他地方找到的公认的稀疏示例,我无法弄清楚如何让它按我的预期工作。
我已经包含了一个带有非常基本设置和我定义的当前规则的游乐场。任何帮助都将不胜感激。