Skip to content

Commit

Permalink
Add indexable input capability and bug fix Bicep
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredfholgate committed Sep 29, 2024
1 parent 552eca8 commit 765fd9e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Convert-BicepConfigToInmputConfig {
function Convert-BicepConfigToInputConfig {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $false)]
Expand Down Expand Up @@ -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
Expand Down
45 changes: 31 additions & 14 deletions src/ALZ/Private/Config-Helpers/Set-Config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
38 changes: 22 additions & 16 deletions src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand Down
11 changes: 4 additions & 7 deletions src/ALZ/Public/New-ALZEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -242,7 +240,6 @@ function New-ALZEnvironment {
-destroy:$destroy.IsPresent `
-starter $starter `
-zonesSupport $zonesSupport `
-computedInputs $computedInputs `
-writeVerboseLogs:$writeVerboseLogs.IsPresent `
-hclParserToolPath $hclParserToolPath
}
Expand Down

0 comments on commit 765fd9e

Please sign in to comment.