azure 如何为资源循环多个数组

qncylg1j  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(112)

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

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'
  }
}]
}]
}]

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

axr492tv

axr492tv1#

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

编辑2023年4月

Bicep现在支持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'
}]

这将输出所有可能的组合(我认为)的起源组名称.

相关问题