I’m trying to filter a list of Azure locations to pick out the primary ones; primary location in this sense is the first location of a region pair that is in the list e.g. given a list [ 'westeurope', 'northeurope' ]
the resulting list should be [ 'westeurope' ]
.
So to solve this in the general case, I need to look at the partially generated new list to see if the current value’s pair is already present and I can’t come up with a syntax/method that works.
The only hack I have is something like this…
locals {
pairs = {
# Europe
northeurope = "westeurope"
westeurope = "northeurope"
# UK
uksouth = "ukwest"
ukwest = "uksouth"
# Plus lots more ignored for brevity
}
# Just filter even indicies
primaries = [ for x, index in var.locations : x if index / 2 = 0 ]
}
But it requires that the items in the correct order i.e. would work for [ 'westeurope', 'northeurope', 'uksouth', 'ukwest' ]
but would provide an incorrect response for [ 'westeurope', 'uksouth', 'northeurope', 'ukwest' ]
Here’s a table showing var.locations and the expected results for the different scenarios…
var.locations | primaries |
---|---|
[ ‘westeurope’ ] | [ ‘westeurope’ ] |
[ ‘northeurope’ ] | [ ‘northeurope’ ] |
[ ‘westeurope’, ‘northeurope’ ] | [ ‘westeurope’ ] |
[ ‘northeurope’, ‘westeurope’ ] | [ ‘northeurope’ ] |
[ ‘westeurope’, ‘uksouth’, ‘northeurope’ ] | [ ‘westeurope’, ‘uksouth’ ] |
[ ‘westeurope’, ‘northeurope’, ‘uksouth’ ] | [ ‘westeurope’, ‘uksouth’ ] |