From adb21f43f28fca4624a0e696be6fb3e129a4e68c Mon Sep 17 00:00:00 2001 From: Jared Holgate Date: Fri, 19 Jan 2024 01:02:18 +0000 Subject: [PATCH] Add config file specific code (#100) # Pull Request ## Issue Issue #, if available: N/A ## Description Description of changes: This PR adds the ability to pass a config file path that is then included in the repository. This is to be more concise for users of the accelerator and only prompt for a config file if they are using a starter module that needs one. Also enables them to call the config file whatever name they desire. This change has been tested and testing evidence can be found in the sister PR here: https://github.com/Azure/alz-terraform-accelerator/pull/89 ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license. --- .../Convert-HCLVariablesToUserInputConfig.ps1 | 6 ++---- src/ALZ/Private/Import-ConfigurationFile.ps1 | 21 +++++++++++++++++++ .../Private/New-ALZEnvironmentTerraform.ps1 | 3 ++- .../Private/Request-ConfigurationValue.ps1 | 7 ++++++- src/ALZ/Private/Write-ConfigurationCache.ps1 | 3 +++ src/ALZ/Private/Write-TfvarsFile.ps1 | 7 +++++++ 6 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/ALZ/Private/Import-ConfigurationFile.ps1 diff --git a/src/ALZ/Private/Convert-HCLVariablesToUserInputConfig.ps1 b/src/ALZ/Private/Convert-HCLVariablesToUserInputConfig.ps1 index 4e6576bd..b44083b5 100644 --- a/src/ALZ/Private/Convert-HCLVariablesToUserInputConfig.ps1 +++ b/src/ALZ/Private/Convert-HCLVariablesToUserInputConfig.ps1 @@ -54,8 +54,8 @@ function Convert-HCLVariablesToUserInputConfig { } $inputType = "UserInput" - if($hasValidation -and $validationType -eq "hidden_azure_subscription_ids") { - $inputType = "AzureSubscriptionIds" + if($hasValidation -and $validationType -like "hidden_*") { + $inputType = "ComputedInput" } $sensitive = $false @@ -90,8 +90,6 @@ function Convert-HCLVariablesToUserInputConfig { $starterModuleConfigurationInstance | Add-Member -NotePropertyName "DefaultValue" -NotePropertyValue $defaultValue } - - if($hasValidation) { $validator = $validators.PSObject.Properties[$validationType].Value $description = "$description ($($validator.Description))" diff --git a/src/ALZ/Private/Import-ConfigurationFile.ps1 b/src/ALZ/Private/Import-ConfigurationFile.ps1 new file mode 100644 index 00000000..54fa6c2f --- /dev/null +++ b/src/ALZ/Private/Import-ConfigurationFile.ps1 @@ -0,0 +1,21 @@ +function Import-ConfigurationFileData { + <# + + #> + param( + [Parameter(Mandatory = $false)] + [PSCustomObject] $starterModuleConfiguration, + [Parameter(Mandatory = $false)] + [PSCustomObject] $bootstrapConfiguration + ) + + $configurationFilePathObjects = ($starterModuleConfiguration.PsObject.Properties | Where-Object { $_.Value.Validator -eq "configuration_file_path" }) + + if($configurationFilePathObjects.Count -eq 0) { + return + } + + $configurationFilePath = $configurationFilePathObjects[0].Value.Value + $bootstrapConfigurationFilePathObject = $bootstrapConfiguration.PsObject.Properties | Where-Object { $_.Value.Validator -eq "hidden_configuration_file_path" } + $bootstrapConfigurationFilePathObject.Value.Value = $configurationFilePath +} \ No newline at end of file diff --git a/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 b/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 index 5bbc2f22..d5cd3159 100644 --- a/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 +++ b/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 @@ -87,8 +87,9 @@ function New-ALZEnvironmentTerraform { # Getting the user input for the starter module $starterModuleConfiguration = Request-ALZEnvironmentConfig -configurationParameters $starterModuleParameters -respectOrdering -userInputOverrides $userInputOverrides -userInputDefaultOverrides $cachedStarterModuleConfig -treatEmptyDefaultAsValid $true -autoApprove:$autoApprove.IsPresent - # Getting subscription ids + # Getting computed inputs Import-SubscriptionData -starterModuleConfiguration $starterModuleConfiguration -bootstrapConfiguration $bootstrapConfiguration + Import-ConfigurationFileData -starterModuleConfiguration $starterModuleConfiguration -bootstrapConfiguration $bootstrapConfiguration # Creating the tfvars files for the bootstrap and starter module $bootstrapTfvarsPath = Join-Path -Path $bootstrapPath -ChildPath "override.tfvars" diff --git a/src/ALZ/Private/Request-ConfigurationValue.ps1 b/src/ALZ/Private/Request-ConfigurationValue.ps1 index ee5ff67b..8216b934 100644 --- a/src/ALZ/Private/Request-ConfigurationValue.ps1 +++ b/src/ALZ/Private/Request-ConfigurationValue.ps1 @@ -58,7 +58,12 @@ function Request-ConfigurationValue { $hasNotSpecifiedValue = ($null -eq $configValue.Value -or "" -eq $configValue.Value) -and ($configValue.Value -ne $configValue.DefaultValue) $isDisallowedValue = $hasAllowedValues -and $allowedValues.Contains($configValue.Value) -eq $false - $skipValidationForEmptyDefault = $treatEmptyDefaultAsValid -and $hasDefaultValue -and $defaultValue -eq "" -and $configValue.Value -eq "" + $skipValidationForEmptyDefault = $treatEmptyDefaultAsValid -and $hasDefaultValue -and (($defaultValue -eq "" -and $configValue.Value -eq "") -or ($configValue.Value -eq "-")) + + # Reset the value to empty if we have a default and the user entered a dash (this is to handle cached situations and provide a method to clear a value) + if($skipValidationForEmptyDefault -and $configValue.Value -eq "-") { + $configValue.Value = "" + } if($skipValidationForEmptyDefault) { $isNotValid = $false diff --git a/src/ALZ/Private/Write-ConfigurationCache.ps1 b/src/ALZ/Private/Write-ConfigurationCache.ps1 index b2d3d164..a7fa3f6c 100644 --- a/src/ALZ/Private/Write-ConfigurationCache.ps1 +++ b/src/ALZ/Private/Write-ConfigurationCache.ps1 @@ -16,6 +16,9 @@ function Write-ConfigurationCache { $cache = [PSCustomObject]@{} foreach ($configurationItem in $configuration.PSObject.Properties) { + if($configurationItem.Value.Type -eq "ComputedInput") { + continue + } $cache | Add-Member -NotePropertyName $configurationItem.Name -NotePropertyValue $configurationItem.Value.Value } diff --git a/src/ALZ/Private/Write-TfvarsFile.ps1 b/src/ALZ/Private/Write-TfvarsFile.ps1 index 2c2cc103..ac53c116 100644 --- a/src/ALZ/Private/Write-TfvarsFile.ps1 +++ b/src/ALZ/Private/Write-TfvarsFile.ps1 @@ -16,6 +16,11 @@ function Write-TfvarsFile { foreach($configurationProperty in $configuration.PSObject.Properties) { $configurationValueRaw = $configurationProperty.Value.Value + + if($configurationProperty.Value.Validator -eq "configuration_file_path") { + $configurationValueRaw = [System.IO.Path]::GetFileName($configurationValueRaw) + } + $configurationValue = "`"$($configurationValueRaw)`"" if($configurationProperty.Value.DataType -eq "list(string)") { @@ -30,6 +35,8 @@ function Write-TfvarsFile { if($configurationProperty.Value.DataType -eq "number" -or $configurationProperty.Value.DataType -eq "bool") { $configurationValue = $configurationValueRaw + } else { + $configurationValue = $configurationValue.Replace("\", "\\") } Add-Content -Path $tfvarsFilePath -Value "$($configurationProperty.Name) = $($configurationValue)"