Skip to content

Adding dynamic allowed value creation, hiding allowed values and filtering invalid dynamically created allowed values. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
84 changes: 8 additions & 76 deletions src/ALZ/Assets/alz-bicep-config/v0.13.0.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
},
"Location": {
"Type": "UserInput",
"Description": "Deployment location.",
"Description": "Deployment location. (e.g. 'uksouth')",
"Value": "",
"Targets": [
{
Expand All @@ -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",
Expand Down
17 changes: 13 additions & 4 deletions src/ALZ/Private/Request-ConfigurationValue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ 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") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script execution might be a bit weird... Happy to discuss...

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

$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
}

Expand Down Expand Up @@ -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
}
54 changes: 51 additions & 3 deletions src/Tests/Unit/Private/Request-ConfigurationValue.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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"
}
Expand Down Expand Up @@ -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"
}
}
}
}