diff --git a/README.md b/README.md index e2819e58..d2a31ec2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # ALZ [![ActionsTest-Windows-pwsh-Build](https://github.com/Azure/ALZ-PowerShell-Module/actions/workflows/wf_Windows_Core.yml/badge.svg?branch=main)](https://github.com/Azure/ALZ-PowerShell-Module/actions/workflows/wf_Windows_Core.yml) +[![license](https://img.shields.io/badge/License-MIT-purple.svg)](LICENSE) ![Logo](./docs/ALZLogo-Small.png) diff --git a/src/ALZ/Assets/alz-bicep-config/v0.13.0.config.json b/src/ALZ/Assets/alz-bicep-config/v0.13.0.config.json index c9e64751..be475e2a 100644 --- a/src/ALZ/Assets/alz-bicep-config/v0.13.0.config.json +++ b/src/ALZ/Assets/alz-bicep-config/v0.13.0.config.json @@ -92,7 +92,7 @@ }, "Location": { "Type": "UserInput", - "Description": "Deployment location.", + "Description": "Deployment location. (e.g. 'uksouth')", "Value": "", "Targets": [ { @@ -108,81 +108,13 @@ "Destination": "Parameters" } ], - "AllowedValues": [ - "asia", - "asiapacific", - "australia", - "australiacentral", - "australiacentral2", - "australiaeast", - "australiasoutheast", - "brazil", - "brazilsouth", - "brazilsoutheast", - "canada", - "canadacentral", - "canadaeast", - "centralindia", - "centralus", - "centraluseuap", - "centralusstage", - "eastasia", - "eastasiastage", - "eastus", - "eastus2", - "eastus2euap", - "eastus2stage", - "eastusstg", - "europe", - "france", - "francecentral", - "francesouth", - "germany", - "germanynorth", - "germanywestcentral", - "global", - "india", - "japan", - "japaneast", - "japanwest", - "jioindiacentral", - "jioindiawest", - "korea", - "koreacentral", - "koreasouth", - "northcentralus", - "northcentralusstage", - "northeurope", - "norway", - "norwayeast", - "norwaywest", - "qatarcentral", - "singapore", - "southafrica", - "southafricanorth", - "southafricawest", - "southcentralus", - "southcentralusstage", - "southeastasia", - "southindia", - "swedencentral", - "switzerland", - "switzerlandnorth", - "switzerlandwest", - "uaecentral", - "uaenorth", - "uksouth", - "ukwest", - "unitedstates", - "westcentralus", - "westeurope", - "westindia", - "westus", - "westus2", - "westus2stage", - "westus3", - "westusstage" - ] + "AllowedValues": { + "Display": false, + "Description": "Getting Azure deployment locations.", + "Type": "PSScript", + "Script": "Get-AzLocation | Where-Object {$_.RegionType -eq 'Physical'} | Sort-Object Location | Select-Object -ExpandProperty Location", + "Values": [] + } }, "Environment": { "Type": "UserInput", diff --git a/src/ALZ/Private/Request-ConfigurationValue.ps1 b/src/ALZ/Private/Request-ConfigurationValue.ps1 index 5ab6cd6d..99fdfa1a 100644 --- a/src/ALZ/Private/Request-ConfigurationValue.ps1 +++ b/src/ALZ/Private/Request-ConfigurationValue.ps1 @@ -10,8 +10,15 @@ function Request-ConfigurationValue { [System.Boolean] $withRetries = $true ) - $allowedValues = $configValue.AllowedValues - $hasAllowedValues = $null -ne $configValue.AllowedValues + #if the file has a script - execute it: + if ($null -ne $configValue.AllowedValues -and $configValue.AllowedValues.Type -eq "PSScript") { + Write-InformationColored $configValue.AllowedValues.Description -ForegroundColor Yellow -InformationAction Continue + $script = [System.Management.Automation.ScriptBlock]::Create($configValue.AllowedValues.Script) + $configValue.AllowedValues.Values = Invoke-Command -ScriptBlock $script + } + + $allowedValues = $configValue.AllowedValues.Values + $hasAllowedValues = $null -ne $configValue.AllowedValues -and $null -ne $configValue.AllowedValues.Values -and $configValue.AllowedValues.Values.Length -gt 0 $defaultValue = $configValue.DefaultValue $hasDefaultValue = $null -ne $configValue.DefaultValue @@ -19,7 +26,7 @@ function Request-ConfigurationValue { $hasValidator = $null -ne $configValue.Valid Write-InformationColored $configValue.Description -ForegroundColor White -InformationAction Continue - if ($hasAllowedValues) { + if ($hasAllowedValues -and $configValue.AllowedValues.Display -eq $true) { Write-InformationColored "[allowed: $allowedValues] " -ForegroundColor Yellow -InformationAction Continue } @@ -54,7 +61,9 @@ function Request-ConfigurationValue { $shouldRetry = $validationError -and $withRetries } - while (($hasNotSpecifiedValue -or $isDisallowedValue -or $isNotValid) -and $shouldRetry) + while ( + + ($hasNotSpecifiedValue -or $isDisallowedValue -or $isNotValid) -and $shouldRetry) Write-InformationColored "" -InformationAction Continue } diff --git a/src/Tests/Unit/Private/Request-ConfigurationValue.Tests.ps1 b/src/Tests/Unit/Private/Request-ConfigurationValue.Tests.ps1 index cf34c51a..44ec0221 100644 --- a/src/Tests/Unit/Private/Request-ConfigurationValue.Tests.ps1 +++ b/src/Tests/Unit/Private/Request-ConfigurationValue.Tests.ps1 @@ -37,7 +37,7 @@ InModuleScope 'ALZ' { Request-ConfigurationValue -configName "prefix" -configValue $configValue - Assert-MockCalled -CommandName Write-InformationColored -Times 3 + Should -Invoke -CommandName Write-InformationColored -Times 4 -Exactly $configValue.Value | Should -BeExactly "user input value" } @@ -56,7 +56,7 @@ InModuleScope 'ALZ' { Request-ConfigurationValue -configName "prefix" -configValue $configValue - Assert-MockCalled -CommandName Write-InformationColored -Times 3 + Should -Invoke -CommandName Write-InformationColored -Times 4 -Exactly $configValue.Value | Should -BeExactly "alz" } @@ -103,13 +103,61 @@ InModuleScope 'ALZ' { Description = "The prefix that will be added to all resources created by this deployment." Names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix") Value = "" - AllowedValues = @("alz", "slz") + AllowedValues = @{ + Values = @("alz", "slz") + } } Request-ConfigurationValue -configName "prefix" -configValue $configValue -withRetries $false Should -Invoke -CommandName Write-InformationColored -ParameterFilter { $ForegroundColor -eq "Red" } -Scope It $configValue.Value | Should -BeExactly "" } + + It 'Prompt user with a calculated list of AllowedValues' { + Mock -CommandName Read-Host -MockWith { + "l" + } + + $configValue = @{ + Description = "The prefix that will be added to all resources created by this deployment." + Names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix") + Value = "" + AllowedValues = @{ + Type = "PSScript" + Values = @() + Script = '"h e l l o" -split " "' + Display = $true + Description = "A collection of values returned by PS Script" + } + } + Request-ConfigurationValue -configName "calculated" -configValue $configValue -withRetries $false + + Should -Invoke -CommandName Write-InformationColored -Times 6 -Exactly + $configValue.Value | Should -BeExactly "l" + } + + It 'Do not display the calculated list of AllowedValues if Display is false' { + Mock -CommandName Read-Host -MockWith { + "l" + } + + $configValue = @{ + Description = "The prefix that will be added to all resources created by this deployment." + Names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix") + Value = "" + AllowedValues = @{ + Type = "PSScript" + Values = @() + Script = '"h e l l o" -split " "' + Display = $false + Description = "A collection of values returned by PS Script" + } + } + Request-ConfigurationValue -configName "calculated" -configValue $configValue -withRetries $false + + Should -Invoke -CommandName Write-InformationColored -Times 5 -Exactly + $configValue.Value | Should -BeExactly "l" + } } } }