Home > Software design >  Get resource from symbolic name array in Bicep
Get resource from symbolic name array in Bicep

Time:04-07

In Bicep I am creating an array of origin groups with a for loop. I want to be able to reference specific values in this array as a parent for another resource.

I'm creating the array like this:

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...
}

Then I have an array of origin groups, "Microsoft.Cdn/profiles/originGroups@2021-06-01[]". I then want to make a origin with a parent. secondOriginGroup.

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

Is it posible in bicep to select a specific indexer here or am i only able to index on ordinal numbers? Am I able to do, where name == 'secondOriginGroup'?

CodePudding user response:

You'll need to use indexes in your loop, like this:

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...
}

CodePudding user response:

You could loop through the originGroups again and filter on '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...
}]
  • Related