From 07ac0704f988e243d491f3ab287c3a42ea12ec63 Mon Sep 17 00:00:00 2001 From: Rhys Ash Date: Wed, 19 Mar 2025 12:35:55 +0000 Subject: [PATCH 1/2] feat: add planOnly parameter to Invoke-Terraform and New-Bootstrap functions --- .../Invoke-Terraform.ps1 | 12 +++++++++- .../New-Bootstrap.ps1 | 7 ++++-- src/ALZ/Public/Deploy-Accelerator.ps1 | 24 ++++++++++++------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 index 4d96d8d..6e2dd6a 100644 --- a/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 @@ -13,6 +13,9 @@ function Invoke-Terraform { [Parameter(Mandatory = $false)] [switch] $destroy, + [Parameter(Mandatory = $false)] + [switch] $planOnly, + [Parameter(Mandatory = $false)] [string] $output = "", @@ -40,6 +43,11 @@ function Invoke-Terraform { terraform -chdir="$moduleFolderPath" init $action = "apply" + + if($planOnly) { + $action = "plan" + } + if($destroy) { $action = "destroy" } @@ -89,7 +97,9 @@ function Invoke-Terraform { throw "Terraform plan failed with exit code $exitCode. Please review the error and try again or raise an issue." } - if(!$autoApprove) { + if($planOnly) { + return + } elseif(!$autoApprove) { Write-InformationColored "Terraform plan has completed, please review the plan and confirm you wish to continue." -ForegroundColor Yellow -NewLineBefore -InformationAction Continue $choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Yes", "&No") $message = "Please confirm you wish to apply the plan." diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 index 7b70bd9..aa8f595 100644 --- a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 @@ -37,6 +37,9 @@ function New-Bootstrap { [Parameter(Mandatory = $false)] [switch] $destroy, + [Parameter(Mandatory = $false)] + [switch] $planOnly, + [Parameter(Mandatory = $false)] [PSCustomObject] $zonesSupport = $null, @@ -273,10 +276,10 @@ function New-Bootstrap { Write-InformationColored "Thank you for providing those inputs, we are now initializing and applying Terraform to bootstrap your environment..." -ForegroundColor Green -NewLineBefore -InformationAction Continue if($autoApprove) { - Invoke-Terraform -moduleFolderPath $bootstrapModulePath -autoApprove -destroy:$destroy.IsPresent + Invoke-Terraform -moduleFolderPath $bootstrapModulePath -autoApprove -destroy:$destroy.IsPresent -planOnly:$planOnly.IsPresent } else { Write-InformationColored "Once the plan is complete you will be prompted to confirm the apply." -ForegroundColor Green -NewLineBefore -InformationAction Continue - Invoke-Terraform -moduleFolderPath $bootstrapModulePath -destroy:$destroy.IsPresent + Invoke-Terraform -moduleFolderPath $bootstrapModulePath -destroy:$destroy.IsPresent -planOnly:$planOnly.IsPresent } Write-InformationColored "Bootstrap has completed successfully! Thanks for using our tool. Head over to Phase 3 in the documentation to continue..." -ForegroundColor Green -NewLineBefore -InformationAction Continue diff --git a/src/ALZ/Public/Deploy-Accelerator.ps1 b/src/ALZ/Public/Deploy-Accelerator.ps1 index 5f9afc8..d23f472 100644 --- a/src/ALZ/Public/Deploy-Accelerator.ps1 +++ b/src/ALZ/Public/Deploy-Accelerator.ps1 @@ -86,11 +86,18 @@ function Deploy-Accelerator { [Parameter( Mandatory = $false, - HelpMessage = "[OPTIONAL] Determines that this run is to destroup the bootstrap. This is used to cleanup experiments. Environment variable: ALZ_destroy. Config file input: destroy." + HelpMessage = "[OPTIONAL] Determines that this run is to destroy the bootstrap. This is used to cleanup experiments. Environment variable: ALZ_destroy. Config file input: destroy." )] [Alias("d")] [switch] $destroy, + [Parameter( + Mandatory = $false, + HelpMessage = "[OPTIONAL] Determines that this run is to plan the bootstrap rather than apply. Environment variable: ALZ_planOnly. Config file input: planOnly." + )] + [Alias("p")] + [switch] $planOnly, + [Parameter( Mandatory = $false, HelpMessage = "[OPTIONAL] The bootstrap modules reposiotry url. This can be overridden for custom modules. Environment variable: ALZ_bootstrap_module_url. Config file input: bootstrap_module_url." @@ -188,7 +195,7 @@ function Deploy-Accelerator { $ProgressPreference = "SilentlyContinue" - if($skip_requirements_check.IsPresent) { + if ($skip_requirements_check.IsPresent) { Write-InformationColored "WARNING: Skipping the software requirements check..." -ForegroundColor Yellow -InformationAction Continue } else { Write-InformationColored "Checking the software requirements for the Accelerator..." -ForegroundColor Green -InformationAction Continue @@ -201,7 +208,7 @@ function Deploy-Accelerator { # Check and install tools needed $toolsPath = Join-Path -Path $output_folder_path -ChildPath ".tools" - if($skipInternetChecks) { + if ($skipInternetChecks) { Write-InformationColored "Skipping Terraform tool check as you used the skipInternetCheck parameter. Please ensure you have the most recent version of Terraform installed" -ForegroundColor Yellow -InformationAction Continue } else { Write-InformationColored "Checking you have the latest version of Terraform installed..." -ForegroundColor Green -NewLineBefore -InformationAction Continue @@ -213,7 +220,7 @@ function Deploy-Accelerator { $inputConfig = $null if ($inputConfigFilePaths.Length -eq 0) { $envInputConfigPaths = $env:ALZ_input_config_path - if($null -ne $envInputConfigPaths -and $envInputConfigPaths -ne "") { + if ($null -ne $envInputConfigPaths -and $envInputConfigPaths -ne "") { $inputConfigFilePaths = $envInputConfigPaths -split "," } else { Write-InformationColored "No input configuration file path has been provided. Please provide the path(s) to your configuration file(s)..." -ForegroundColor Yellow -InformationAction Continue @@ -222,7 +229,7 @@ function Deploy-Accelerator { } # Get the input config from yaml and json files - foreach($inputConfigFilePath in $inputConfigFilePaths) { + foreach ($inputConfigFilePath in $inputConfigFilePaths) { $inputConfig = Get-ALZConfig -configFilePath $inputConfigFilePath -inputConfig $inputConfig -hclParserToolPath $hclParserToolPath } @@ -231,13 +238,13 @@ function Deploy-Accelerator { $parametersWithValues = @{} foreach ($parameterKey in $parameters.Keys) { $parameter = $parameters[$parameterKey] - if($parameter.IsDynamic) { + if ($parameter.IsDynamic) { continue } $parameterValue = Get-Variable -Name $parameterKey -ValueOnly -ErrorAction SilentlyContinue - if($null -ne $parameterValue) { + if ($null -ne $parameterValue) { $parametersWithValues[$parameterKey] = @{ type = $parameters[$parameterKey].ParameterType.Name value = $parameterValue @@ -298,7 +305,7 @@ function Deploy-Accelerator { $zonesSupport = $null # Request the bootstrap type if not already specified - if($inputConfig.bootstrap_module_name.Value -eq "") { + if ($inputConfig.bootstrap_module_name.Value -eq "") { $inputConfig.bootstrap_module_name = @{ Value = Request-SpecialInput -type "bootstrap" -bootstrapModules $bootstrapModules Source = "user" @@ -375,6 +382,7 @@ function Deploy-Accelerator { -starterConfig $starterConfig ` -autoApprove:$inputConfig.auto_approve.Value ` -destroy:$inputConfig.destroy.Value ` + -planOnly:$inputConfig.planOnly.Value ` -zonesSupport $zonesSupport ` -writeVerboseLogs:$inputConfig.write_verbose_logs.Value ` -hclParserToolPath $hclParserToolPath ` From 68a5e426e04f0a5b9edb0ef9c84e5df683ff8e39 Mon Sep 17 00:00:00 2001 From: Rhys Ash Date: Wed, 19 Mar 2025 12:41:50 +0000 Subject: [PATCH 2/2] fix: remove unnecessary spaces in Deploy-Accelerator.ps1 --- src/ALZ/Public/Deploy-Accelerator.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ALZ/Public/Deploy-Accelerator.ps1 b/src/ALZ/Public/Deploy-Accelerator.ps1 index d23f472..f7024d7 100644 --- a/src/ALZ/Public/Deploy-Accelerator.ps1 +++ b/src/ALZ/Public/Deploy-Accelerator.ps1 @@ -195,7 +195,7 @@ function Deploy-Accelerator { $ProgressPreference = "SilentlyContinue" - if ($skip_requirements_check.IsPresent) { + if($skip_requirements_check.IsPresent) { Write-InformationColored "WARNING: Skipping the software requirements check..." -ForegroundColor Yellow -InformationAction Continue } else { Write-InformationColored "Checking the software requirements for the Accelerator..." -ForegroundColor Green -InformationAction Continue @@ -208,7 +208,7 @@ function Deploy-Accelerator { # Check and install tools needed $toolsPath = Join-Path -Path $output_folder_path -ChildPath ".tools" - if ($skipInternetChecks) { + if($skipInternetChecks) { Write-InformationColored "Skipping Terraform tool check as you used the skipInternetCheck parameter. Please ensure you have the most recent version of Terraform installed" -ForegroundColor Yellow -InformationAction Continue } else { Write-InformationColored "Checking you have the latest version of Terraform installed..." -ForegroundColor Green -NewLineBefore -InformationAction Continue @@ -220,7 +220,7 @@ function Deploy-Accelerator { $inputConfig = $null if ($inputConfigFilePaths.Length -eq 0) { $envInputConfigPaths = $env:ALZ_input_config_path - if ($null -ne $envInputConfigPaths -and $envInputConfigPaths -ne "") { + if($null -ne $envInputConfigPaths -and $envInputConfigPaths -ne "") { $inputConfigFilePaths = $envInputConfigPaths -split "," } else { Write-InformationColored "No input configuration file path has been provided. Please provide the path(s) to your configuration file(s)..." -ForegroundColor Yellow -InformationAction Continue @@ -229,7 +229,7 @@ function Deploy-Accelerator { } # Get the input config from yaml and json files - foreach ($inputConfigFilePath in $inputConfigFilePaths) { + foreach($inputConfigFilePath in $inputConfigFilePaths) { $inputConfig = Get-ALZConfig -configFilePath $inputConfigFilePath -inputConfig $inputConfig -hclParserToolPath $hclParserToolPath } @@ -238,13 +238,13 @@ function Deploy-Accelerator { $parametersWithValues = @{} foreach ($parameterKey in $parameters.Keys) { $parameter = $parameters[$parameterKey] - if ($parameter.IsDynamic) { + if($parameter.IsDynamic) { continue } $parameterValue = Get-Variable -Name $parameterKey -ValueOnly -ErrorAction SilentlyContinue - if ($null -ne $parameterValue) { + if($null -ne $parameterValue) { $parametersWithValues[$parameterKey] = @{ type = $parameters[$parameterKey].ParameterType.Name value = $parameterValue @@ -305,7 +305,7 @@ function Deploy-Accelerator { $zonesSupport = $null # Request the bootstrap type if not already specified - if ($inputConfig.bootstrap_module_name.Value -eq "") { + if($inputConfig.bootstrap_module_name.Value -eq "") { $inputConfig.bootstrap_module_name = @{ Value = Request-SpecialInput -type "bootstrap" -bootstrapModules $bootstrapModules Source = "user"