Skip to content

Commit

Permalink
fix: location rotation solution for ptn/utl (#3212)
Browse files Browse the repository at this point in the history
## Description

<!--
>Thank you for your contribution !
> Please include a summary of the change and which issue is fixed.
> Please also include the context.
> List any dependencies that are required for this change.

Fixes #123
Fixes #456
Closes #123
Closes #456
-->

## Pipeline Reference

<!-- Insert your Pipeline Status Badge below -->

| Pipeline |
| -------- |
|
[![avm.ptn.security.security-center](https://github.com/ChrisSidebotham/bicep-registry-modules/actions/workflows/avm.ptn.security.security-center.yml/badge.svg?branch=anchor)](https://github.com/ChrisSidebotham/bicep-registry-modules/actions/workflows/avm.ptn.security.security-center.yml)
|
|
[![avm.res.key-vault.vault](https://github.com/ChrisSidebotham/bicep-registry-modules/actions/workflows/avm.res.key-vault.vault.yml/badge.svg?branch=anchor)](https://github.com/ChrisSidebotham/bicep-registry-modules/actions/workflows/avm.res.key-vault.vault.yml)
|

## Type of Change

<!-- Use the checkboxes [x] on the options that are relevant. -->

- [x] Update to CI Environment or utilities (Non-module affecting
changes)
- [ ] Azure Verified Module updates:
- [ ] Bugfix containing backwards-compatible bug fixes, and I have NOT
bumped the MAJOR or MINOR version in `version.json`:
- [ ] Someone has opened a bug report issue, and I have included "Closes
#{bug_report_issue_number}" in the PR description.
- [ ] The bug was found by the module author, and no one has opened an
issue to report it yet.
- [ ] Feature update backwards compatible feature updates, and I have
bumped the MINOR version in `version.json`.
- [ ] Breaking changes and I have bumped the MAJOR version in
`version.json`.
  - [ ] Update to documentation

## Checklist

- [x] I'm sure there are no other open Pull Requests for the same
update/change
- [ ] I have run `Set-AVMModule` locally to generate the supporting
module files.
- [x] My corresponding pipelines / checks run clean and green without
any errors or warnings

<!-- Please keep up to date with the contribution guide at
https://aka.ms/avm/contribute/bicep -->
  • Loading branch information
ChrisSidebotham authored Sep 6, 2024
1 parent c92a8c2 commit ae50ebb
Showing 1 changed file with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,47 @@ function Get-AvailableResourceLocation {

# Configure Resource Type
$fullModuleIdentifier = ($ModuleRoot -split '[\/|\\]{0,1}avm[\/|\\]{1}(res|ptn)[\/|\\]{1}')[2] -replace '\\', '/'
Write-Verbose "Full module identifier: $fullModuleIdentifier"
$formattedResourceType = Get-SpecsAlignedResourceName -ResourceIdentifier $fullModuleIdentifier -Verbose
Write-Verbose "Formatted resource type: $formattedResourceType"

# Get the resource provider and resource name
$formattedResourceProvider = ($formattedResourceType -split '[\/|\\]{1}')[0]
Write-Verbose "Resource type: $formattedResourceProvider"
$formattedServiceName = ($formattedResourceType -split '[\/|\\]{1}')[1]
Write-Verbose "Resource: $formattedServiceName"

$resourceRegionList = @()
$ResourceRegionList = (Get-AzResourceProvider | Where-Object { $_.ProviderNamespace -eq $formattedResourceProvider }).ResourceTypes | Where-Object { $_.ResourceTypeName -eq $formattedServiceName } | Select-Object -ExpandProperty Locations
Write-Verbose "Region list: $($resourceRegionList | ConvertTo-Json)"

if ($resourceRegionList -eq 'global' -or $null -eq $resourceRegionList) {
Write-Verbose "Resource is global or does not have a location in the Resource Providers API, default region [$GlobalResourceGroupLocation] will be used for resource group creation"
$location = $GlobalResourceGroupLocation # Set Location to resource group location. Globabl resources should have hardocded location in `main.bicep`
} else {

$locations = Get-AzLocation | Where-Object {
$_.DisplayName -in $ResourceRegionList -and
$_.Location -notin $ExcludedRegions -and
$_.PairedRegion -ne '{}' -and
$_.RegionCategory -eq 'Recommended'
} | Select-Object -ExpandProperty Location
Write-Verbose "Available Locations: $($locations | ConvertTo-Json)"

$filteredAllowedLocations = @($locations | Where-Object { $_ -in $AllowedRegionsList })
Write-Verbose "Filtered allowed locations: $($filteredAllowedLocations | ConvertTo-Json)"
$index = Get-Random -Maximum ($filteredAllowedLocations.Count)
if ($ModuleRoot -like 'avm/res*') {

Write-Verbose "Full module identifier: $fullModuleIdentifier"
$formattedResourceType = Get-SpecsAlignedResourceName -ResourceIdentifier $fullModuleIdentifier -Verbose
Write-Verbose "Formatted resource type: $formattedResourceType"

# Get the resource provider and resource name
$formattedResourceProvider = ($formattedResourceType -split '[\/|\\]{1}')[0]
Write-Verbose "Resource type: $formattedResourceProvider"
$formattedServiceName = ($formattedResourceType -split '[\/|\\]{1}')[1]
Write-Verbose "Resource: $formattedServiceName"

$resourceRegionList = @()
$ResourceRegionList = (Get-AzResourceProvider | Where-Object { $_.ProviderNamespace -eq $formattedResourceProvider }).ResourceTypes | Where-Object { $_.ResourceTypeName -eq $formattedServiceName } | Select-Object -ExpandProperty Locations
Write-Verbose "Region list: $($resourceRegionList | ConvertTo-Json)"

if ($resourceRegionList -eq 'global' -or $null -eq $resourceRegionList) {
Write-Verbose "Resource is global or does not have a location in the Resource Providers API, default region [$GlobalResourceGroupLocation] will be used for resource group creation"
$location = $GlobalResourceGroupLocation # Set Location to resource group location. Globabl resources should have hardocded location in `main.bicep`
} else {

$locations = Get-AzLocation | Where-Object {
$_.DisplayName -in $ResourceRegionList -and
$_.Location -notin $ExcludedRegions -and
$_.PairedRegion -ne '{}' -and
$_.RegionCategory -eq 'Recommended'
} | Select-Object -ExpandProperty Location
Write-Verbose "Available Locations: $($locations | ConvertTo-Json)"

$filteredAllowedLocations = @($locations | Where-Object { $_ -in $AllowedRegionsList })
Write-Verbose "Filtered allowed locations: $($filteredAllowedLocations | ConvertTo-Json)"
$index = Get-Random -Maximum ($filteredAllowedLocations.Count)
Write-Verbose "Generated random index [$index]"
$location = $filteredAllowedLocations[$index]
}
} else {
Write-Verbose 'Module is not resource so defaulting to the allowed region list'
$index = Get-Random -Maximum ($AllowedRegionsList.Count)
Write-Verbose "Generated random index [$index]"
$location = $filteredAllowedLocations[$index]
$location = $AllowedRegionsList[$index]
}

Write-Verbose "Selected location [$location]" -Verbose
Expand Down

0 comments on commit ae50ebb

Please sign in to comment.