Skip to content

Commit 7245201

Browse files
Adding dynamic allowed value creation, hiding allowed values and filtering invalid dynamically created allowed values. (#8)
# Pull Request An mechanism which allows for dynamically calculated AllowedValues to be specified. Also configurable whether the allowed values are displayed or not. ## Issue #5 ## Description * Locations can be dynamically calculated again. * Allowed Values can be displayed or not based on configuration. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent c19fc5f commit 7245201

File tree

4 files changed

+73
-83
lines changed

4 files changed

+73
-83
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# ALZ
22

33
[![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)
4+
[![license](https://img.shields.io/badge/License-MIT-purple.svg)](LICENSE)
45

56
![Logo](./docs/ALZLogo-Small.png)
67

src/ALZ/Assets/alz-bicep-config/v0.13.0.config.json

Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
},
9393
"Location": {
9494
"Type": "UserInput",
95-
"Description": "Deployment location.",
95+
"Description": "Deployment location. (e.g. 'uksouth')",
9696
"Value": "",
9797
"Targets": [
9898
{
@@ -108,81 +108,13 @@
108108
"Destination": "Parameters"
109109
}
110110
],
111-
"AllowedValues": [
112-
"asia",
113-
"asiapacific",
114-
"australia",
115-
"australiacentral",
116-
"australiacentral2",
117-
"australiaeast",
118-
"australiasoutheast",
119-
"brazil",
120-
"brazilsouth",
121-
"brazilsoutheast",
122-
"canada",
123-
"canadacentral",
124-
"canadaeast",
125-
"centralindia",
126-
"centralus",
127-
"centraluseuap",
128-
"centralusstage",
129-
"eastasia",
130-
"eastasiastage",
131-
"eastus",
132-
"eastus2",
133-
"eastus2euap",
134-
"eastus2stage",
135-
"eastusstg",
136-
"europe",
137-
"france",
138-
"francecentral",
139-
"francesouth",
140-
"germany",
141-
"germanynorth",
142-
"germanywestcentral",
143-
"global",
144-
"india",
145-
"japan",
146-
"japaneast",
147-
"japanwest",
148-
"jioindiacentral",
149-
"jioindiawest",
150-
"korea",
151-
"koreacentral",
152-
"koreasouth",
153-
"northcentralus",
154-
"northcentralusstage",
155-
"northeurope",
156-
"norway",
157-
"norwayeast",
158-
"norwaywest",
159-
"qatarcentral",
160-
"singapore",
161-
"southafrica",
162-
"southafricanorth",
163-
"southafricawest",
164-
"southcentralus",
165-
"southcentralusstage",
166-
"southeastasia",
167-
"southindia",
168-
"swedencentral",
169-
"switzerland",
170-
"switzerlandnorth",
171-
"switzerlandwest",
172-
"uaecentral",
173-
"uaenorth",
174-
"uksouth",
175-
"ukwest",
176-
"unitedstates",
177-
"westcentralus",
178-
"westeurope",
179-
"westindia",
180-
"westus",
181-
"westus2",
182-
"westus2stage",
183-
"westus3",
184-
"westusstage"
185-
]
111+
"AllowedValues": {
112+
"Display": false,
113+
"Description": "Getting Azure deployment locations.",
114+
"Type": "PSScript",
115+
"Script": "Get-AzLocation | Where-Object {$_.RegionType -eq 'Physical'} | Sort-Object Location | Select-Object -ExpandProperty Location",
116+
"Values": []
117+
}
186118
},
187119
"Environment": {
188120
"Type": "UserInput",

src/ALZ/Private/Request-ConfigurationValue.ps1

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@ function Request-ConfigurationValue {
1010
[System.Boolean] $withRetries = $true
1111
)
1212

13-
$allowedValues = $configValue.AllowedValues
14-
$hasAllowedValues = $null -ne $configValue.AllowedValues
13+
#if the file has a script - execute it:
14+
if ($null -ne $configValue.AllowedValues -and $configValue.AllowedValues.Type -eq "PSScript") {
15+
Write-InformationColored $configValue.AllowedValues.Description -ForegroundColor Yellow -InformationAction Continue
16+
$script = [System.Management.Automation.ScriptBlock]::Create($configValue.AllowedValues.Script)
17+
$configValue.AllowedValues.Values = Invoke-Command -ScriptBlock $script
18+
}
19+
20+
$allowedValues = $configValue.AllowedValues.Values
21+
$hasAllowedValues = $null -ne $configValue.AllowedValues -and $null -ne $configValue.AllowedValues.Values -and $configValue.AllowedValues.Values.Length -gt 0
1522

1623
$defaultValue = $configValue.DefaultValue
1724
$hasDefaultValue = $null -ne $configValue.DefaultValue
1825

1926
$hasValidator = $null -ne $configValue.Valid
2027

2128
Write-InformationColored $configValue.Description -ForegroundColor White -InformationAction Continue
22-
if ($hasAllowedValues) {
29+
if ($hasAllowedValues -and $configValue.AllowedValues.Display -eq $true) {
2330
Write-InformationColored "[allowed: $allowedValues] " -ForegroundColor Yellow -InformationAction Continue
2431
}
2532

@@ -54,7 +61,9 @@ function Request-ConfigurationValue {
5461

5562
$shouldRetry = $validationError -and $withRetries
5663
}
57-
while (($hasNotSpecifiedValue -or $isDisallowedValue -or $isNotValid) -and $shouldRetry)
64+
while (
65+
66+
($hasNotSpecifiedValue -or $isDisallowedValue -or $isNotValid) -and $shouldRetry)
5867

5968
Write-InformationColored "" -InformationAction Continue
6069
}

src/Tests/Unit/Private/Request-ConfigurationValue.Tests.ps1

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ InModuleScope 'ALZ' {
3737

3838
Request-ConfigurationValue -configName "prefix" -configValue $configValue
3939

40-
Assert-MockCalled -CommandName Write-InformationColored -Times 3
40+
Should -Invoke -CommandName Write-InformationColored -Times 4 -Exactly
4141

4242
$configValue.Value | Should -BeExactly "user input value"
4343
}
@@ -56,7 +56,7 @@ InModuleScope 'ALZ' {
5656

5757
Request-ConfigurationValue -configName "prefix" -configValue $configValue
5858

59-
Assert-MockCalled -CommandName Write-InformationColored -Times 3
59+
Should -Invoke -CommandName Write-InformationColored -Times 4 -Exactly
6060

6161
$configValue.Value | Should -BeExactly "alz"
6262
}
@@ -103,13 +103,61 @@ InModuleScope 'ALZ' {
103103
Description = "The prefix that will be added to all resources created by this deployment."
104104
Names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix")
105105
Value = ""
106-
AllowedValues = @("alz", "slz")
106+
AllowedValues = @{
107+
Values = @("alz", "slz")
108+
}
107109
}
108110
Request-ConfigurationValue -configName "prefix" -configValue $configValue -withRetries $false
109111

110112
Should -Invoke -CommandName Write-InformationColored -ParameterFilter { $ForegroundColor -eq "Red" } -Scope It
111113
$configValue.Value | Should -BeExactly ""
112114
}
115+
116+
It 'Prompt user with a calculated list of AllowedValues' {
117+
Mock -CommandName Read-Host -MockWith {
118+
"l"
119+
}
120+
121+
$configValue = @{
122+
Description = "The prefix that will be added to all resources created by this deployment."
123+
Names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix")
124+
Value = ""
125+
AllowedValues = @{
126+
Type = "PSScript"
127+
Values = @()
128+
Script = '"h e l l o" -split " "'
129+
Display = $true
130+
Description = "A collection of values returned by PS Script"
131+
}
132+
}
133+
Request-ConfigurationValue -configName "calculated" -configValue $configValue -withRetries $false
134+
135+
Should -Invoke -CommandName Write-InformationColored -Times 6 -Exactly
136+
$configValue.Value | Should -BeExactly "l"
137+
}
138+
139+
It 'Do not display the calculated list of AllowedValues if Display is false' {
140+
Mock -CommandName Read-Host -MockWith {
141+
"l"
142+
}
143+
144+
$configValue = @{
145+
Description = "The prefix that will be added to all resources created by this deployment."
146+
Names = @("parTopLevelManagementGroupPrefix", "parCompanyPrefix")
147+
Value = ""
148+
AllowedValues = @{
149+
Type = "PSScript"
150+
Values = @()
151+
Script = '"h e l l o" -split " "'
152+
Display = $false
153+
Description = "A collection of values returned by PS Script"
154+
}
155+
}
156+
Request-ConfigurationValue -configName "calculated" -configValue $configValue -withRetries $false
157+
158+
Should -Invoke -CommandName Write-InformationColored -Times 5 -Exactly
159+
$configValue.Value | Should -BeExactly "l"
160+
}
113161
}
114162
}
115163
}

0 commit comments

Comments
 (0)