从 Bicep 中的符号名称数组获取资源

Get resource from symbolic name array in Bicep

在 Bicep 中,我正在创建一个带有 for 循环的原始组数组。我希望能够引用此数组中的特定值作为另一个资源的 parent。

我正在这样创建数组:

var originGroups = [
  {
    name: 'firstOriginGroup'
  }
  {
    name: 'secondOriginGroup'
  }

]   

resource origin_groups 'Microsoft.Cdn/profiles/originGroups@2021-06-01' = [for group in originGroups :{ 
  name: group.name
  other properties...
}

然后我有一组原始组,“Microsoft.Cdn/profiles/originGroups@2021-06-01[]”。然后我想用 parent 做一个原点。 secondOriginGroup.

resource my_origin 'Microsoft.Cdn/profiles/originGroups/origins@2021-06-01' = {
  name: 'myOrigin' 
  parent: origin_groups[ //select specific name here ]
  other parameters...
}

在 bicep 中是否可以 select 一个特定的索引器,或者我只能对序数进行索引?我能做吗,where name == 'secondOriginGroup'?

您需要在循环中使用索引,如下所示:

var originGroups = [
   {
      name: 'firstOriginGroup'
   }
   {
      name: 'secondOriginGroup'
   }
]   
        
resource origin_groups 'Microsoft.Cdn/profiles/originGroups@2021-06-01' = [for (group, index) in originGroups :{ 
   name: group.name
   other properties...
}
    
resource my_origin 'Microsoft.Cdn/profiles/originGroups/origins@2021-06-01' = {
   name: 'myOrigin' 
   parent: origin_groups[index]
   other parameters...
}

您可以再次遍历 originGroups 并过滤 'secondOriginGroup':

resource my_origin 'Microsoft.Cdn/profiles/originGroups/origins@2021-06-01' = [for (group, i) in originGroups: if (group.name == 'secondOriginGroup') {
  name: 'myOrigin'
  parent: origin_groups[i]
  other parameters...
}]