Skip to content

Commit

Permalink
Save changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredfholgate committed Nov 18, 2024
1 parent 34a8b3e commit 739bcfd
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ function Convert-ParametersToInputConfig {
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 | Add-Member -NotePropertyName $parameterKey -NotePropertyValue @{
Value = $configItem.Value
Source = "parameter"
}
$inputConfig.PSObject.Properties.Remove($configItem.Name)
continue
}
Expand All @@ -34,7 +37,10 @@ function Convert-ParametersToInputConfig {
$variableValue = [bool]::Parse($variableValue)
}
Write-Verbose "Adding parameter $parameterKey with value $variableValue"
$inputConfig | Add-Member -NotePropertyName $parameterKey -NotePropertyValue $variableValue
$inputConfig | Add-Member -NotePropertyName $parameterKey -NotePropertyValue @{
Value = $variableValue
Source = "parameter"
}
}
}

Expand Down
19 changes: 16 additions & 3 deletions src/ALZ/Private/Config-Helpers/Get-ALZConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ function Get-ALZConfig {
[Parameter(Mandatory = $false)]
[string] $configFilePath = "",
[Parameter(Mandatory = $false)]
[PSCustomObject] $inputConfig = $null
[PSCustomObject] $inputConfig = $null,
[Parameter(Mandatory = $false)]
[string] $hclParserToolPath = ""
)

if(!(Test-Path $configFilePath)) {
Expand Down Expand Up @@ -39,14 +41,25 @@ function Get-ALZConfig {
Write-Error $errorMessage
throw $errorMessage
}
} elseif($extension -eq ".tfvars") {
try {
$config = [PSCustomObject](& $hclParserToolPath $configFilePath | ConvertFrom-Json)
} catch {
$errorMessage = "Failed to parse HCL inputs. Please check the HCL file for errors and try again. $_"
Write-Error $errorMessage
throw $errorMessage
}
} else {
throw "The config file must be a json or yaml/yml file"
throw "The config file must be a json, yaml/yml or tfvars file"
}

Write-Verbose "Config file loaded from $configFilePath with $($config.PSObject.Properties.Name.Count) properties."

foreach($property in $config.PSObject.Properties) {
$inputConfig | Add-Member -NotePropertyName $property.Name -NotePropertyValue $property.Value
$inputConfig | Add-Member -NotePropertyName $property.Name -NotePropertyValue @{
Value = $property.Value
Source = $extension
}
}

return $inputConfig
Expand Down
2 changes: 1 addition & 1 deletion src/ALZ/Private/Config-Helpers/Set-Config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function Set-Config {
# Look for input config match
$inputConfigItem = $inputConfig.PsObject.Properties | Where-Object { $_.Name -eq $inputConfigName }
if($null -ne $inputConfigItem) {
$configurationValue.Value.Value = $inputConfigItem.Value
$configurationValue.Value.Value = $inputConfigItem.Value.Value
continue
}

Expand Down
10 changes: 9 additions & 1 deletion src/ALZ/Private/Config-Helpers/Write-TfvarsJsonFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ function Write-TfvarsJsonFile {
[string] $tfvarsFilePath,

[Parameter(Mandatory = $false)]
[PSObject] $configuration
[PSObject] $configuration,

[Parameter(Mandatory = $false)]
[string[]] $skipItems = @()
)

if ($PSCmdlet.ShouldProcess("Download Terraform Tools", "modify")) {
Expand All @@ -17,6 +20,11 @@ function Write-TfvarsJsonFile {
$jsonObject = [ordered]@{}

foreach($configurationProperty in $configuration.PSObject.Properties | Sort-Object Name) {
if($skipItems -contains $configurationProperty.Name) {
Write-Verbose "Skipping configuration property: $($configurationProperty.Name)"
continue
}

$configurationValue = $configurationProperty.Value.Value

if($null -ne $configurationValue -and $configurationValue.ToString() -eq "sourced-from-env") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function Get-BootstrapAndStarterConfig {
[Parameter(Mandatory = $false)]
[string]$bootstrapConfigPath,
[Parameter(Mandatory = $false)]
[PSCustomObject]$inputConfig,
[Parameter(Mandatory = $false)]
[string]$toolsPath
)

Expand Down
71 changes: 54 additions & 17 deletions src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ function New-Bootstrap {

[Parameter(Mandatory = $false)]
[string]
$hclParserToolPath
$hclParserToolPath,

[Parameter(Mandatory = $false)]
[switch]
$convertTfvarsToJson,

[Parameter(Mandatory = $false)]
[string[]]
$inputConfigFilePaths = @()
)

if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) {
Expand All @@ -71,13 +79,16 @@ function New-Bootstrap {
$starterFoldersToRetain = @()

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

$chosenStarterConfig = $starterConfig.starter_modules.$($inputConfig.starter_module_name)
$chosenStarterConfig = $starterConfig.starter_modules.$($inputConfig.starter_module_name.Value)

Write-Verbose "Selected Starter: $($inputConfig.starter_module_name))"
Write-Verbose "Selected Starter: $($inputConfig.starter_module_name.Value))"
$starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $chosenStarterConfig.location)).Path
$starterRootModuleFolderPath = $starterModulePath
Write-Verbose "Starter Module Path: $starterModulePath"
Expand All @@ -94,7 +105,10 @@ function New-Bootstrap {
$starterFoldersToRetain += $starterRootModuleFolder

# Add the root module folder to bootstrap input config
$inputConfig | Add-Member -NotePropertyName "root_module_folder_relative_path" -NotePropertyValue $starterRootModuleFolder
$inputConfig | Add-Member -NotePropertyName "root_module_folder_relative_path" -NotePropertyValue @{
Value = $starterRootModuleFolder
Source = "caluated"
}

# Set the starter root module folder full path
$starterRootModuleFolderPath = Join-Path -Path $starterModulePath -ChildPath $starterRootModuleFolder
Expand Down Expand Up @@ -126,25 +140,37 @@ function New-Bootstrap {
}

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

# Set computed inputs
$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)
$inputConfig | Add-Member -NotePropertyName "module_folder_path" -NotePropertyValue @{
Value = $starterModulePath
Source = "calculated"
}
$inputConfig | Add-Member -NotePropertyName "availability_zones_bootstrap" -NotePropertyValue @{
Value = @(Get-AvailabilityZonesSupport -region $inputConfig.bootstrap_location.Value -zonesSupport $zonesSupport)
Source = "calculated"
}

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)
Write-Verbose "Converting starter_location $($inputConfig.starter_location.Value) to starter_locations..."
$inputConfig | Add-Member -NotePropertyName "starter_locations" -NotePropertyValue @{
Value = @($inputConfig.starter_location.Value)
Source = "calculated"
}
}

if($inputConfig.PSObject.Properties.Name -contains "starter_locations") {
$availabilityZonesStarter = @()
foreach($region in $inputConfig.starter_locations) {
foreach($region in $inputConfig.starter_locations.Value) {
$availabilityZonesStarter += , @(Get-AvailabilityZonesSupport -region $region -zonesSupport $zonesSupport)
}
$inputConfig | Add-Member -NotePropertyName "availability_zones_starter" -NotePropertyValue $availabilityZonesStarter
$inputConfig | Add-Member -NotePropertyName "availability_zones_starter" -NotePropertyValue @{
Value = $availabilityZonesStarter
Source = "calculated"
}
}

Write-Verbose "Final Input config: $(ConvertTo-Json $inputConfig -Depth 100)"
Expand Down Expand Up @@ -188,7 +214,18 @@ function New-Bootstrap {
}
}
Remove-TerraformMetaFileSet -path $starterModulePath -writeVerboseLogs:$writeVerboseLogs.IsPresent
Write-TfvarsJsonFile -tfvarsFilePath $starterTfvarsPath -configuration $starterConfiguration
if($convertTfvarsToJson) {
Write-TfvarsJsonFile -tfvarsFilePath $starterTfvarsPath -configuration $starterConfiguration
} else {
$inputsFromTfvars = $inputConfig.PSObject.Properties | Where-Object { $_.Value.Source -eq ".tfvars" } | Select-Object -ExpandProperty Name
Write-TfvarsJsonFile -tfvarsFilePath $starterTfvarsPath -configuration $starterConfiguration -skipItems $inputsFromTfvars
foreach($inputConfigFilePath in $inputConfigFilePaths | Where-Object { $_ -like "*.tfvars" }) {
$fileName = [System.IO.Path]::GetFileName($inputConfigFilePath)
$fileName = $fileName.Replace(".tfvars", ".auto.tfvars")
$destination = Join-Path -Path $starterModulePath -ChildPath $fileName
Copy-Item -Path $inputConfigFilePath -Destination $destination
}
}
}

if($iac -eq "bicep") {
Expand All @@ -198,16 +235,16 @@ function New-Bootstrap {
Write-JsonFile -jsonFilePath $starterBicepVarsPath -configuration $starterConfiguration

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

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

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

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

0 comments on commit 739bcfd

Please sign in to comment.