From 765fd9e7be92db80d7492c7db85bfab82228cfd7 Mon Sep 17 00:00:00 2001 From: Jared Holgate Date: Sun, 29 Sep 2024 12:51:46 +0100 Subject: [PATCH] Add indexable input capability and bug fix Bicep --- .../Convert-BicepConfigToInputConfig.ps1 | 6 ++- src/ALZ/Private/Config-Helpers/Set-Config.ps1 | 45 +++++++++++++------ .../New-Bootstrap.ps1 | 38 +++++++++------- src/ALZ/Public/New-ALZEnvironment.ps1 | 11 ++--- 4 files changed, 62 insertions(+), 38 deletions(-) diff --git a/src/ALZ/Private/Config-Helpers/Convert-BicepConfigToInputConfig.ps1 b/src/ALZ/Private/Config-Helpers/Convert-BicepConfigToInputConfig.ps1 index 51538af0..2980f54f 100644 --- a/src/ALZ/Private/Config-Helpers/Convert-BicepConfigToInputConfig.ps1 +++ b/src/ALZ/Private/Config-Helpers/Convert-BicepConfigToInputConfig.ps1 @@ -1,4 +1,4 @@ -function Convert-BicepConfigToInmputConfig { +function Convert-BicepConfigToInputConfig { [CmdletBinding(SupportsShouldProcess = $true)] param ( [Parameter(Mandatory = $false)] @@ -36,6 +36,10 @@ function Convert-BicepConfigToInmputConfig { $configItem | Add-Member -NotePropertyName "Pattern" -NotePropertyValue $variable.Value.pattern } + if($variable.Value.PSObject.Properties.Name -contains "process") { + $configItem | Add-Member -NotePropertyName "Process" -NotePropertyValue $variable.Value.process + } + if($variable.Value.PSObject.Properties.Name -contains "default") { $defaultValue = $variable.Value.default $configItem | Add-Member -NotePropertyName "DefaultValue" -NotePropertyValue $defaultValue diff --git a/src/ALZ/Private/Config-Helpers/Set-Config.ps1 b/src/ALZ/Private/Config-Helpers/Set-Config.ps1 index 83330867..a7f95225 100644 --- a/src/ALZ/Private/Config-Helpers/Set-Config.ps1 +++ b/src/ALZ/Private/Config-Helpers/Set-Config.ps1 @@ -7,12 +7,11 @@ function Set-Config { [Parameter(Mandatory = $false)] [PSCustomObject] $inputConfig = $null, [Parameter(Mandatory = $false)] - [hashtable] $computedInputs, - [Parameter(Mandatory = $false)] [switch] $copyEnvVarToConfig ) if ($PSCmdlet.ShouldProcess("Set Configuration.", "Set configuration values.")) { + $configurations = $configurationParameters.PsObject.Properties foreach ($configurationValue in $configurations) { @@ -23,20 +22,11 @@ function Set-Config { continue } - # Look for computed input match - if($null -ne $computedInputs) { - $computedInput = $computedInputs[$configurationValue.Name] - - if($null -ne $computedInput) { - $configurationValue.Value.Value = $computedInput - continue - } - } - # Get input config name $inputConfigName = $configurationValue.Name - if($configurationValue.PSObject.Properties.Name -contains "SourceInput") { - $inputConfigName = $configurationValue.SourceInput + if($configurationValue.Value.PSObject.Properties.Name -contains "SourceInput") { + $inputConfigName = $configurationValue.Value.SourceInput + Write-Verbose "Using source input $inputConfigName for $($configurationValue.Name)" } # Look for environment variables @@ -52,6 +42,33 @@ function Set-Config { continue } + # Look for array config match + if($inputConfigName.EndsWith("]")) { + $indexSplit = $inputConfigName.Split([char[]]@('[', ']'), [System.StringSplitOptions]::RemoveEmptyEntries) + $inputConfigItem = $inputConfig.PsObject.Properties | Where-Object { $_.Name -eq $indexSplit[0] } + if($null -ne $inputConfigItem) { + if(!$inputConfigItem.Value.GetType().ImplementedInterfaces.Contains([System.Collections.ICollection])) { + Write-Error "Input config item $($inputConfigName) is not an array, but an index was specified." + throw "Input config item $($inputConfigName) is not an array, but an index was specified." + } + $index = [int]$indexSplit[1] + if($inputConfigItem.Value.Length -le $index) { + Write-Verbose "Input config item $($inputConfigName) does not have an index of $index." + } else { + $inputConfigItemValue = $inputConfigItem.Value[$index] + if($null -ne $inputConfigItemValue) { + $configurationValue.Value.Value = $inputConfigItemValue + continue + } else { + Write-Verbose "Input config item $($inputConfigName) with index $index is null." + } + } + } else { + Write-Error "Input config item $($inputConfigName) not found." + throw "Input config item $($inputConfigName) not found." + } + } + # Look for input config match $inputConfigItem = $inputConfig.PsObject.Properties | Where-Object { $_.Name -eq $inputConfigName } if($null -ne $inputConfigItem) { diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 index b9c685e9..dae06119 100644 --- a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 @@ -43,9 +43,6 @@ function New-Bootstrap { [Parameter(Mandatory = $false)] [PSCustomObject] $zonesSupport = $null, - [Parameter(Mandatory = $false)] - [hashtable] $computedInputs, - [Parameter(Mandatory = $false, HelpMessage = "An extra level of logging that is turned off by default for easier debugging.")] [switch] $writeVerboseLogs, @@ -88,7 +85,7 @@ function New-Bootstrap { # Getting configuration for the bootstrap module user input $bootstrapParameters = [PSCustomObject]@{} - Write-Verbose "Getting the bootstrap configuration for user input..." + Write-Verbose "Getting the bootstrap configuration..." $terraformFiles = Get-ChildItem -Path $bootstrapModulePath -Filter "*.tf" -File foreach($terraformFile in $terraformFiles) { $bootstrapParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -validators $validationConfig -appendToObject $bootstrapParameters @@ -99,6 +96,7 @@ function New-Bootstrap { $starterParameters = [PSCustomObject]@{} if($hasStarter) { + Write-Verbose "Getting the starter configuration..." if($iac -eq "terraform") { $terraformFiles = Get-ChildItem -Path $starterModulePath -Filter "*.tf" -File foreach($terraformFile in $terraformFiles) { @@ -107,39 +105,47 @@ function New-Bootstrap { } if($iac -eq "bicep") { - $starterParameters = Convert-BicepConfigToInputConfig -inputConfig $starterConfig.starter_modules.$starter -validators $validationConfig + $starterParameters = Convert-BicepConfigToInputConfig -bicepConfig $starterConfig.starter_modules.$starter -validators $validationConfig } } + #Write-Verbose "Starter config: $(ConvertTo-Json $starterParameters -Depth 100)" + # Set computed inputs - #Write-Verbose "Input config: $(ConvertTo-Json $inputConfig -Depth 100)" - $computedInputs["starter_module_name"] = $starter - $computedInputs["module_folder_path"] = $starterModulePath - $computedInputs["availability_zones_bootstrap"] = @(Get-AvailabilityZonesSupport -region $inputConfig.bootstrap_location -zonesSupport $zonesSupport) + $inputConfig | Add-Member -NotePropertyName "starter_module_name" -NotePropertyValue $starter + $inputConfig | Add-Member -NotePropertyName "module_folder_path" -NotePropertyValue $starterModulePath + $inputConfig | Add-Member -NotePropertyName "availability_zones_bootstrap" -NotePropertyValue @(Get-AvailabilityZonesSupport -region $inputConfig.bootstrap_location -zonesSupport $zonesSupport) + + if($inputConfig.PSObject.Properties.Name -contains "starter_location" -and $inputConfig.PSObject.Properties.Name -notcontains "starter_locations") { + Write-Verbose "Converting starter_location $($inputConfig.starter_location) to starter_locations..." + $inputConfig | Add-Member -NotePropertyName "starter_locations" -NotePropertyValue @($inputConfig.starter_location) + } if($inputConfig.PSObject.Properties.Name -contains "starter_locations") { - $computedInputs["availability_zones_starter"] = @() + $availabilityZonesStarter = @() foreach($region in $inputConfig.starter_locations) { - $computedInputs["availability_zones_starter"] += @(Get-AvailabilityZonesSupport -region $region -zonesSupport $zonesSupport) + $availabilityZonesStarter += , @(Get-AvailabilityZonesSupport -region $region -zonesSupport $zonesSupport) } - Write-Verbose "Computed availability zones for starter: $(ConvertTo-Json $computedInputs["availability_zones_starter"] -Depth 100)" + $inputConfig | Add-Member -NotePropertyName "availability_zones_starter" -NotePropertyValue $availabilityZonesStarter } - Write-Verbose "Computed Inputs: $(ConvertTo-Json $computedInputs -Depth 100)" + #Write-Verbose "Input config: $(ConvertTo-Json $inputConfig -Depth 100)" # Getting the input for the bootstrap module + Write-Verbose "Setting the configuration for the bootstrap module..." $bootstrapConfiguration = Set-Config ` -configurationParameters $bootstrapParameters ` - -inputConfig $inputConfig ` - -computedInputs $computedInputs + -inputConfig $inputConfig # Getting the input for the starter module + Write-Verbose "Setting the configuration for the starter module..." $starterConfiguration = Set-Config ` -configurationParameters $starterParameters ` -inputConfig $inputConfig ` - -computedInputs $computedInputs ` -copyEnvVarToConfig + + # Creating the tfvars files for the bootstrap and starter module $tfVarsFileName = "terraform.tfvars.json" $bootstrapTfvarsPath = Join-Path -Path $bootstrapModulePath -ChildPath $tfVarsFileName diff --git a/src/ALZ/Public/New-ALZEnvironment.ps1 b/src/ALZ/Public/New-ALZEnvironment.ps1 index 552b4426..f03e4dc5 100644 --- a/src/ALZ/Public/New-ALZEnvironment.ps1 +++ b/src/ALZ/Public/New-ALZEnvironment.ps1 @@ -216,12 +216,10 @@ function New-ALZEnvironment { } # Set computed interface inputs - $computedInputs = @{ - "iac_type" = $iac - "on_demand_folder_repository" = $starterModuleUrl - "on_demand_folder_artifact_name" = $starterReleaseArtifactName - "release_version" = $starterReleaseTag -eq "local" ? $starterRelease : $starterReleaseTag - } + $inputConfig | Add-Member -MemberType NoteProperty -Name "iac_type" -Value $iac + $inputConfig | Add-Member -MemberType NoteProperty -Name "on_demand_folder_repository" -Value $starterModuleUrl + $inputConfig | Add-Member -MemberType NoteProperty -Name "on_demand_folder_artifact_name" -Value $starterReleaseArtifactName + $inputConfig | Add-Member -MemberType NoteProperty -Name "release_version" -Value ($starterReleaseTag -eq "local" ? $starterRelease : $starterReleaseTag) # Run the bootstrap $bootstrapTargetPath = Join-Path $targetDirectory $bootstrapTargetFolder @@ -242,7 +240,6 @@ function New-ALZEnvironment { -destroy:$destroy.IsPresent ` -starter $starter ` -zonesSupport $zonesSupport ` - -computedInputs $computedInputs ` -writeVerboseLogs:$writeVerboseLogs.IsPresent ` -hclParserToolPath $hclParserToolPath }