Skip to content

Commit 24a93fe

Browse files
feature: treat empty default as valid (#77)
# Pull Request ## Issue Issue #, if available: N/A ## Description Description of changes: For Terraform there are some cases where we want to either provide an empty value or a valid string. E.g. an empty string or a subscription id. The existing implementation did not handle this, so added a parameter to support this scenario. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent cb3e38b commit 24a93fe

4 files changed

+20
-9
lines changed

src/ALZ/Private/New-ALZEnvironmentTerraform.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function New-ALZEnvironmentTerraform {
5353
Write-InformationColored "Got configuration and downloaded alz-terraform-accelerator Terraform module version $release to $alzEnvironmentDestination" -ForegroundColor Green -InformationAction Continue
5454

5555
# Getting the user input for the bootstrap module
56-
$bootstrapConfiguration = Request-ALZEnvironmentConfig -configurationParameters $bootstrapParameters -respectOrdering -userInputOverrides $userInputOverrides
56+
$bootstrapConfiguration = Request-ALZEnvironmentConfig -configurationParameters $bootstrapParameters -respectOrdering -userInputOverrides $userInputOverrides -treatEmptyDefaultAsValid $true
5757

5858
# Getting the configuration for the starter module user input
5959
$starterTemplate = $bootstrapConfiguration.PsObject.Properties["starter_module"].Value.Value
@@ -64,7 +64,7 @@ function New-ALZEnvironmentTerraform {
6464
Write-InformationColored "The following inputs are specific to the '$starterTemplate' starter module that you selected..." -ForegroundColor Green -InformationAction Continue
6565

6666
# Getting the user input for the starter module
67-
$starterModuleConfiguration = Request-ALZEnvironmentConfig -configurationParameters $starterModuleParameters -respectOrdering -userInputOverrides $userInputOverrides
67+
$starterModuleConfiguration = Request-ALZEnvironmentConfig -configurationParameters $starterModuleParameters -respectOrdering -userInputOverrides $userInputOverrides -treatEmptyDefaultAsValid $true
6868

6969
# Getting subscription ids
7070
Import-SubscriptionData -starterModuleConfiguration $starterModuleConfiguration -bootstrapConfiguration $bootstrapConfiguration

src/ALZ/Private/Request-ALZEnvironmentConfig.ps1

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ function Request-ALZEnvironmentConfig {
66
[Parameter(Mandatory = $false)]
77
[switch] $respectOrdering,
88
[Parameter(Mandatory = $false)]
9-
[PSCustomObject] $userInputOverrides = $null
9+
[PSCustomObject] $userInputOverrides = $null,
10+
[Parameter(Mandatory = $false)]
11+
[System.Boolean] $treatEmptyDefaultAsValid = $false
1012
)
1113
<#
1214
.SYNOPSIS
@@ -37,10 +39,10 @@ function Request-ALZEnvironmentConfig {
3739
if($null -ne $userInputOverride) {
3840
$configurationValue.Value.Value = $userInputOverride.Value
3941
} else {
40-
Request-ConfigurationValue $configurationValue.Name $configurationValue.Value
42+
Request-ConfigurationValue -configName $configurationValue.Name -configValue $configurationValue.Value -treatEmptyDefaultAsValid $treatEmptyDefaultAsValid
4143
}
4244
} else {
43-
Request-ConfigurationValue $configurationValue.Name $configurationValue.Value
45+
Request-ConfigurationValue -configName $configurationValue.Name -configValue $configurationValue.Value -treatEmptyDefaultAsValid $treatEmptyDefaultAsValid
4446
}
4547
}
4648
}

src/ALZ/Private/Request-ConfigurationValue.ps1

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ function Request-ConfigurationValue {
77
[object] $configValue,
88

99
[Parameter(Mandatory = $false)]
10-
[System.Boolean] $withRetries = $true
10+
[System.Boolean] $withRetries = $true,
11+
12+
[Parameter(Mandatory = $false)]
13+
[System.Boolean] $treatEmptyDefaultAsValid = $false
1114
)
1215

1316
#if the file has a script - execute it:
@@ -51,7 +54,13 @@ function Request-ConfigurationValue {
5154

5255
$hasNotSpecifiedValue = ($null -eq $configValue.Value -or "" -eq $configValue.Value) -and ($configValue.Value -ne $configValue.DefaultValue)
5356
$isDisallowedValue = $hasAllowedValues -and $allowedValues.Contains($configValue.Value) -eq $false
54-
$isNotValid = $hasValidator -and $configValue.Value -match $configValue.Valid -eq $false
57+
$skipValidationForEmptyDefault = $treatEmptyDefaultAsValid -and $hasDefaultValue -and $defaultValue -eq "" -and $configValue.Value -eq ""
58+
59+
if($skipValidationForEmptyDefault) {
60+
$isNotValid = $false
61+
} else {
62+
$isNotValid = $hasValidator -and $configValue.Value -match $configValue.Valid -eq $false
63+
}
5564

5665
if ($hasNotSpecifiedValue -or $isDisallowedValue -or $isNotValid) {
5766
Write-InformationColored "Please specify a valid value for this field." -ForegroundColor Red -InformationAction Continue

src/ALZ/Public/Get-ALZGithubRelease.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function Get-ALZGithubRelease {
5050

5151
# Get releases on repo
5252
$repoReleasesUrl = "https://api.github.com/repos/$repoOrgPlusRepo/releases"
53-
$allRepoReleases = Invoke-RestMethod $repoReleasesUrl
53+
$allRepoReleases = Invoke-RestMethod $repoReleasesUrl -RetryIntervalSec 3 -MaximumRetryCount 100
5454

5555
Write-Verbose "=====> All available releases on GitHub Repo: $repoOrgPlusRepo"
5656
$allRepoReleases | Select-Object name, tag_name, published_at, prerelease, draft, html_url | Format-Table -AutoSize | Out-String | Write-Verbose
@@ -112,7 +112,7 @@ function Get-ALZGithubRelease {
112112
if ($null -eq $contentInReleaseDirectory) {
113113
Write-Verbose "===> Pulling and extracting release $($release.tag_name) into $releaseDirectory"
114114
New-Item -ItemType Directory -Path "$releaseDirectory/tmp" | Out-String | Write-Verbose
115-
Invoke-WebRequest -Uri "https://github.com/$repoOrgPlusRepo/archive/refs/tags/$($release.tag_name).zip" -OutFile "$releaseDirectory/tmp/$($release.tag_name).zip" | Out-String | Write-Verbose
115+
Invoke-WebRequest -Uri "https://github.com/$repoOrgPlusRepo/archive/refs/tags/$($release.tag_name).zip" -OutFile "$releaseDirectory/tmp/$($release.tag_name).zip" -RetryIntervalSec 3 -MaximumRetryCount 100 | Out-String | Write-Verbose
116116
Expand-Archive -Path "$releaseDirectory/tmp/$($release.tag_name).zip" -DestinationPath "$releaseDirectory/tmp/extracted" | Out-String | Write-Verbose
117117
$extractedSubFolder = Get-ChildItem -Path "$releaseDirectory/tmp/extracted" -Directory
118118

0 commit comments

Comments
 (0)