Azure / bicep

Bicep is a declarative language for describing and deploying Azure resources
MIT License
3.22k stars 746 forks source link

A way to create custom dictionary objects from arrays #8601

Closed SvenAelterman closed 1 year ago

SvenAelterman commented 2 years ago

I would like to be able to create custom dictionary objects from arrays, such as when outputting the result of a loop. Currently, we can only use arrays for that, but that makes using that output dependent on the position of a particular resource in an array not changing. This isn't always guaranteed.

I would like to do something like this:

// This is possible today
resource subnetsCreated '...subnet' existing = [for ...]

// This is not possible today for two reasons:
// * you can't iterate over a resource collection
// * you can only create an array, not a dictionary object
var things = { [for (subnet, i) in subnetsCreated:
  '${subnet.name}': {
    id: subnet.id
    name: subnet.name
    // You also can't do this today because 'properties' isn't known until runtime
    addressPrefixes: subnet.properties.addressPrefixes
  }
}]}

output theseThings object = things
alex-frankel commented 2 years ago

Are all of the subnets part of the same vnet? If so, can you do something like this w/ lambda functions?

resource vnet 'Microsoft.Network/virtualNetworks@2022-01-01' existing = {
  name: 'myVnet'
}

var reduceThings = reduce(vnet.properties.subnets, {}, (prev, cur) => union(prev, { '${cur.name}': {
  id: cur.id
  name: cur.name
  addressPrefixes: cur.properties.addressPrefixes
}}))
alex-frankel commented 2 years ago

Related to #8519. Definitely feels like we need a more straightforward way to go from array to dictionary.

SvenAelterman commented 2 years ago

@alex-frankel Yes, they are all part of the same VNet. I'll try out those lambda functions.

ghost commented 1 year ago

Hi SvenAelterman, this issue has been marked as stale because it was labeled as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thanks for contributing to bicep! :smile: :mechanical_arm: