Skip to content

Commit

Permalink
Add dynamic paramters and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredfholgate committed Sep 30, 2024
1 parent 765fd9e commit 4ddcd99
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 167 deletions.
42 changes: 42 additions & 0 deletions src/ALZ/Private/Config-Helpers/Convert-ParametersToInputConfig.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function Convert-ParametersToInputConfig {
param(
[Parameter(Mandatory = $false)]
[PSCustomObject] $inputConfig,
[Parameter(Mandatory = $false)]
[hashtable] $parameters
)

foreach($parameterKey in $parameters.Keys) {
$parameter = $parameters[$parameterKey]
Write-Verbose "Processing parameter $parameterKey $(ConvertTo-Json $parameter -Depth 100)"

foreach($parameterAlias in $parameter.aliases) {
if($inputConfig.PsObject.Properties.Name -contains $parameterAlias) {
Write-Verbose "Alias $parameterAlias exists in input config, renaming..."
$configItem = $inputConfig.PSObject.Properties | Where-Object { $_.Name -eq $parameterAlias }
$inputConfig | Add-Member -NotePropertyName $parameterKey -NotePropertyValue $configItem.Value
$inputConfig.PSObject.Properties.Remove($configItem.Name)
continue
}
}

if($inputConfig.PsObject.Properties.Name -notcontains $parameterKey) {
$variableValue = [Environment]::GetEnvironmentVariable("ALZ_$($parameterKey)")
if($null -eq $variableValue) {
if($parameter.type -eq "SwitchParameter") {
$variableValue = $parameter.value.IsPresent
} else {
$variableValue = $parameter.value
}
}

if($parameter.type -eq "SwitchParameter") {
$variableValue = [bool]::Parse($variableValue)
}
Write-Verbose "Adding parameter $parameterKey with value $variableValue"
$inputConfig | Add-Member -NotePropertyName $parameterKey -NotePropertyValue $variableValue
}
}

return $inputConfig
}
17 changes: 1 addition & 16 deletions src/ALZ/Private/Config-Helpers/Request-SpecialInput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ function Request-SpecialInput {
[PSCustomObject] $starterConfig,

[Parameter(Mandatory = $false)]
[PSCustomObject] $bootstrapModules,

[Parameter(Mandatory = $false)]
[PSCustomObject] $inputConfig = $null
[PSCustomObject] $bootstrapModules
)

if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) {
Expand Down Expand Up @@ -98,18 +95,6 @@ function Request-SpecialInput {
}
}

if($null -ne $inputConfig) {
$userInputOverride = $inputConfig.PSObject.Properties | Where-Object { $_.Name -eq $type }
if($null -ne $userInputOverride) {
$result = $userInputOverride.Value
if($options.key -notcontains $result -and $aliasOptions.key -notcontains $result) {
Write-InformationColored "The $typeDescription '$result' that you have selected does not exist. Please try again with a valid $typeDescription..." -ForegroundColor Red -InformationAction Continue
throw "The $typeDescription '$result' that you have selected does not exist. Please try again with a valid $typeDescription..."
}
return $result
}
}

# Add the options to the choices array
$choices = @()
$usedLetters = @()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ function Get-BootstrapAndStarterConfig {
# Get the available bootstrap modules
$bootstrapModules = $bootstrapConfig.bootstrap_modules

# Request the bootstrap type if not already specified
if($bootstrap -eq "") {
$bootstrap = Request-SpecialInput -type "bootstrap" -bootstrapModules $bootstrapModules -inputConfig $inputConfig
}

# Get the bootstrap details and validate it exists (use alias for legacy values)
$bootstrapDetails = $bootstrapModules.PsObject.Properties | Where-Object { $_.Name -eq $bootstrap -or $bootstrap -in $_.Value.aliases }
if($null -eq $bootstrapDetails) {
Expand Down
31 changes: 10 additions & 21 deletions src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ function New-Bootstrap {
[Parameter(Mandatory = $false)]
[switch] $destroy,

[Parameter(Mandatory = $false)]
[string] $starter = "",

[Parameter(Mandatory = $false)]
[PSCustomObject] $zonesSupport = $null,

Expand Down Expand Up @@ -71,14 +68,12 @@ function New-Bootstrap {
$starterModulePath = ""

if($hasStarter) {
if($starter -eq "") {
$starter = Request-SpecialInput -type "starter" -starterConfig $starterConfig -inputConfig $inputConfig
if($inputConfig.starter_module_name -eq "") {
$inputConfig.starter_module_name = Request-SpecialInput -type "starter" -starterConfig $starterConfig
}

Write-Verbose "Selected Starter: $starter"

$starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $starterConfig.starter_modules.$starter.location)).Path

Write-Verbose "Selected Starter: $($inputConfig.starter_module_name))"
$starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $starterConfig.starter_modules.$($inputConfig.starter_module_name).location)).Path
Write-Verbose "Starter Module Path: $starterModulePath"
}

Expand All @@ -90,7 +85,6 @@ function New-Bootstrap {
foreach($terraformFile in $terraformFiles) {
$bootstrapParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -validators $validationConfig -appendToObject $bootstrapParameters
}
#Write-Verbose "Bootstrap Config: $(ConvertTo-Json $bootstrapParameters -Depth 100)"

# Getting the configuration for the starter module user input
$starterParameters = [PSCustomObject]@{}
Expand All @@ -105,14 +99,11 @@ function New-Bootstrap {
}

if($iac -eq "bicep") {
$starterParameters = Convert-BicepConfigToInputConfig -bicepConfig $starterConfig.starter_modules.$starter -validators $validationConfig
$starterParameters = Convert-BicepConfigToInputConfig -bicepConfig $starterConfig.starter_modules.$($inputConfig.starter_module_name) -validators $validationConfig
}
}

#Write-Verbose "Starter config: $(ConvertTo-Json $starterParameters -Depth 100)"

# Set computed inputs
$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)

Expand All @@ -129,7 +120,7 @@ function New-Bootstrap {
$inputConfig | Add-Member -NotePropertyName "availability_zones_starter" -NotePropertyValue $availabilityZonesStarter
}

#Write-Verbose "Input config: $(ConvertTo-Json $inputConfig -Depth 100)"
Write-Verbose "Final Input config: $(ConvertTo-Json $inputConfig -Depth 100)"

# Getting the input for the bootstrap module
Write-Verbose "Setting the configuration for the bootstrap module..."
Expand All @@ -144,8 +135,6 @@ function New-Bootstrap {
-inputConfig $inputConfig `
-copyEnvVarToConfig



# Creating the tfvars files for the bootstrap and starter module
$tfVarsFileName = "terraform.tfvars.json"
$bootstrapTfvarsPath = Join-Path -Path $bootstrapModulePath -ChildPath $tfVarsFileName
Expand All @@ -161,22 +150,22 @@ function New-Bootstrap {
}

if($iac -eq "bicep") {
Copy-ParametersFileCollection -starterPath $starterModulePath -configFiles $starterConfig.starter_modules.$starter.deployment_files
Copy-ParametersFileCollection -starterPath $starterModulePath -configFiles $starterConfig.starter_modules.$($inputConfig.starter_module_name).deployment_files
Set-ComputedConfiguration -configuration $starterConfiguration
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination $starterModulePath -configuration $starterConfiguration
Write-JsonFile -jsonFilePath $starterBicepVarsPath -configuration $starterConfiguration

# Remove unrequired files
$foldersOrFilesToRetain = $starterConfig.starter_modules.$starter.folders_or_files_to_retain
$foldersOrFilesToRetain = $starterConfig.starter_modules.$($inputConfig.starter_module_name).folders_or_files_to_retain
$foldersOrFilesToRetain += "parameters.json"
$foldersOrFilesToRetain += "config"
$foldersOrFilesToRetain += "starter-cache.json"

foreach($deployment_file in $starterConfig.starter_modules.$starter.deployment_files) {
foreach($deployment_file in $starterConfig.starter_modules.$($inputConfig.starter_module_name).deployment_files) {
$foldersOrFilesToRetain += $deployment_file.templateParametersSourceFilePath
}

$subFoldersOrFilesToRemove = $starterConfig.starter_modules.$starter.subfolders_or_files_to_remove
$subFoldersOrFilesToRemove = $starterConfig.starter_modules.$($inputConfig.starter_module_name).subfolders_or_files_to_remove

Remove-UnrequiredFileSet -path $starterModulePath -foldersOrFilesToRetain $foldersOrFilesToRetain -subFoldersOrFilesToRemove $subFoldersOrFilesToRemove -writeVerboseLogs:$writeVerboseLogs.IsPresent
}
Expand Down
Loading

0 comments on commit 4ddcd99

Please sign in to comment.