A nested loop variation #13280
-
Chatgpt gave me the following suggestion for looping which I was doubtful about but in just this case it would be very tidy. When I look at all the questions about looping, none of them are asking about the method at the end of the bicep below. I am currently getting an error running this but I wonder if it is just that I have the syntax a little wrong or that nested loops in this way won't work. Is it possible? The rest of the syntax is fine; the module works without the nesting when groups are iterated outside of the module but inside the module would be cleaner... @description('Name of existing APIM')
param apiManagementName string
@description('A list of groups to create with a corresponding products')
param groups array
resource apimService 'Microsoft.ApiManagement/service@2021-08-01' existing = {
name: apiManagementName
}
resource serviceGroup 'Microsoft.ApiManagement/service/groups@2021-08-01' = [for group in groups: {
name: group.Name
parent: apimService
properties: {
displayName: group.DisplayName
description: group.Description
externalId: group.?AADGroupId
type: ( empty(group.AADGroupId) ? 'custom' : 'external')
}
} ]
// Loop to associate groups with products
resource groupProductAssociations 'Microsoft.ApiManagement/service/products/groups@2021-08-01' = [for group in groups: [ for product in group.Products: {
name: '${apiManagementName}/${product}/${group.Name}'
dependsOn: [ serviceGroup ]
}]
For completeness' sake, here is the corresponding parameter file that is parsed and sent to the module by a calling bicep file.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Nested looping is not possible but using var test = [
{
name: 'name'
products: [
1
2
]
}
]
var productByName = [for item in test: map(item.products, product => {
name: item.name
product: product
})]
output testFlatten array = flatten(productByName)
Which you can then loop over with the Also I would not rely on ChatGPT for anything bicep related. If Github copilot which uses real repositories as a data source can't get it right most of the time, a LLM that confidently spits false information is never going to give you anything useable. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer @GABRIELNGBTUC, that got me on the right track! I didn't quite understand what you meant with the items() bit but got it working using for looping. :) I know ChatGPT is likely to lead me astray, that was why I mentioned it - trying to indicate I recognized it as a dubious source but worth double checking JIC. For some reason I just get an error from VS Code when I try to ask bicep related questions. I tried to do the mapping and flattening in the one variable assignment but the validation steps didn't like it. Could be I had some other error in the assignment... This is my updated code, in case anyone would like to see it.
Thank you again for taking the time to answer! |
Beta Was this translation helpful? Give feedback.
Nested looping is not possible but using
map()
you can create the desired outcome:testFlatten
resolves to the following array:Which you can then loop over with the
items()
function.Also I would not rely on ChatGPT for anything bicep related. If Github copilot which uses real repositories as a data source can't get it right most of the time, a LLM that confidently spits false information is …