gpt4 book ai didi

arrays - 如何循环访问资源的多个数组

转载 作者:行者123 更新时间:2023-12-02 07:42:35 25 4
gpt4 key购买 nike

我想一次为多个区域、环境、应用程序等创建资源。我想做这样的事情:

param apps array = [
'app1'
'app2'
'app3'
]

param environments array = [
'alpha'
'beta'
]

param regions array = [
'ne'
'we'
'uks'
]


resource origin_group 'Microsoft.Cdn/profiles/origingroups@2021-06-01' = [ for region in regions: {
[ for env in environments: {
[ for app in apps: {
parent: profiles_global_fd_name_resource
name: '${env}-${region}-${app}-origin-group'
properties: {
loadBalancingSettings: {
sampleSize: 4
successfulSamplesRequired: 3
additionalLatencyInMilliseconds: 50
}
healthProbeSettings: {
probePath: '/'
probeRequestType: 'HEAD'
probeProtocol: 'Http'
probeIntervalInSeconds: 100
}
sessionAffinityState: 'Disabled'
}
}]
}]
}]

所有提到嵌套循环的文档都讨论在资源内部循环以创建许多子资源。不是我所追求的。也许另一种方法是以某种方式将所有这些数组合并到每个可能迭代的单个对象数组中。也不知道从哪里开始。

非常感谢任何帮助。

最佳答案

目前尚不支持此功能,但将来会支持(请参阅 Is there plans to support nested loop on resources? )。

编辑 2023 年 4 月

二头肌现在支持lambda functionsreducemap 函数可用于生成所有可能的组合:

param environments array = [ 'alpha', 'beta' ]
param regions array = [ 'ne', 'we', 'uks' ]
param apps array = [ 'app1', 'app2', 'app3' ]

// Create an array of env-region
var envRegions = reduce(environments, [], (cur, next) => concat(cur, map(regions, region => '${next}-${region}')))

// Add the app to the previously created array env-region-app
output envRegionApps array = reduce(envRegions, [], (cur, next) => concat(cur, map(apps, app => '${next}-${app}')))

原始答案

使用一点数学知识,您就可以实现您想要的目标(不确定是否应该):

param environments array = [ 'alpha', 'beta' ]
param regions array = [ 'ne', 'we', 'uks' ]
param apps array = [ 'app1', 'app2', 'app3' ]

// Setting some variables for clarity
var envCount = length(environments)
var regionCount = length(regions)
var appCount = length(apps)

// Setting the total number of combination
var originGroupCount = envCount * regionCount * appCount

// Iterate all possible combinations
output originGroupNames array = [for i in range(0, originGroupCount): {
name: '${environments[i / (regionCount * appCount) % envCount]}-${regions[i / appCount % regionCount]}-${apps[i % appCount]}-origin-group'
}]

这将输出原始组名称的所有可能组合(我认为)。

关于arrays - 如何循环访问资源的多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72731171/

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