diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 index 8d5fd1b..163a025 100644 --- a/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1 @@ -19,11 +19,20 @@ function Invoke-Terraform { [Parameter(Mandatory = $false)] [string] $outputFilePath = "", - [Parameter(Mandatory = $false)] - [switch] $silent + [Parameter(Mandatory = $false)] + [switch] $silent, + + [Parameter(Mandatory = $false)] + [switch] $generateOnly ) - if ($PSCmdlet.ShouldProcess("Apply Terraform", "modify")) { + if ($PSCmdlet.ShouldProcess("Apply Terraform", "modify")) { + + if ($generateOnly) { + Write-InformationColored "Generate only mode enabled. Terraform files have been generated but terraform init, plan, and apply have been skipped." -ForegroundColor Green -NewLineBefore -InformationAction Continue + return + } + # Check and Set Subscription ID $removeSubscriptionId = $false if ($null -eq $env:ARM_SUBSCRIPTION_ID -or $env:ARM_SUBSCRIPTION_ID -eq "") { diff --git a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 index 45719d8..17b0c5e 100644 --- a/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 +++ b/src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1 @@ -56,9 +56,13 @@ function New-Bootstrap { [string[]] $inputConfigFilePaths = @(), - [Parameter(Mandatory = $false)] - [string[]] - $starterAdditionalFiles = @() + [Parameter(Mandatory = $false)] + [string[]] + $starterAdditionalFiles = @(), + + [Parameter(Mandatory = $false)] + [switch] + $generateOnly ) if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) { @@ -269,16 +273,26 @@ function New-Bootstrap { Remove-UnrequiredFileSet -path $starterModulePath -foldersOrFilesToRetain $foldersOrFilesToRetain -subFoldersOrFilesToRemove $subFoldersOrFilesToRemove -writeVerboseLogs:$writeVerboseLogs.IsPresent } - # Running terraform init and apply - 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 - } 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 + # Running terraform init and apply + if ($generateOnly) { + Write-InformationColored "Thank you for providing those inputs, we are now generating the Terraform files for your environment..." -ForegroundColor Green -NewLineBefore -InformationAction Continue + } else { + 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 -generateOnly:$generateOnly.IsPresent + } else { + if (!$generateOnly) { + 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 -generateOnly:$generateOnly.IsPresent + } + + if ($generateOnly) { + Write-InformationColored "Terraform files have been generated successfully! You can now use them with your custom pipeline or terraform state configuration." -ForegroundColor Green -NewLineBefore -InformationAction Continue + } else { + 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 } - - 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..f2acb7e 100644 --- a/src/ALZ/Public/Deploy-Accelerator.ps1 +++ b/src/ALZ/Public/Deploy-Accelerator.ps1 @@ -183,7 +183,15 @@ function Deploy-Accelerator { HelpMessage = "[OPTIONAL] Determines whether to skip the requirements check for the ALZ PowerShell Module version only. This is not recommended." )] [Alias("skipAlzModuleVersionRequirementsCheck")] - [switch] $skip_alz_module_version_requirements_check + [switch] $skip_alz_module_version_requirements_check, + + [Parameter( + Mandatory = $false, + HelpMessage = "[OPTIONAL] Only generate Terraform files without running terraform init, plan, or apply. This is useful for custom tfstate configurations or other pipelines. Environment variable: ALZ_generate_only. Config file input: generate_only." + )] + [Alias("g")] + [Alias("generateOnly")] + [switch] $generate_only ) $ProgressPreference = "SilentlyContinue" @@ -380,7 +388,8 @@ function Deploy-Accelerator { -hclParserToolPath $hclParserToolPath ` -convertTfvarsToJson:$inputConfig.convert_tfvars_to_json.Value ` -inputConfigFilePaths $inputConfigFilePaths ` - -starterAdditionalFiles $inputConfig.starter_additional_files.Value + -starterAdditionalFiles $inputConfig.starter_additional_files.Value ` + -generateOnly:$inputConfig.generate_only.Value } $ProgressPreference = "Continue" diff --git a/src/Tests/Unit/Public/Deploy-Accelerator.Tests.ps1 b/src/Tests/Unit/Public/Deploy-Accelerator.Tests.ps1 index 3a8df37..7f40a1d 100644 --- a/src/Tests/Unit/Public/Deploy-Accelerator.Tests.ps1 +++ b/src/Tests/Unit/Public/Deploy-Accelerator.Tests.ps1 @@ -158,11 +158,18 @@ InModuleScope 'ALZ' { Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2 } - It 'should call the correct functions for terraform module configuration' { - Deploy-Accelerator -i "terraform" -b "github" -inputs "example.yml" - Assert-MockCalled -CommandName Get-BootstrapAndStarterConfig -Exactly 1 - Assert-MockCalled -CommandName New-Bootstrap -Exactly 1 - Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2 + It 'should call the correct functions for terraform module configuration' { + Deploy-Accelerator -i "terraform" -b "github" -inputs "example.yml" + Assert-MockCalled -CommandName Get-BootstrapAndStarterConfig -Exactly 1 + Assert-MockCalled -CommandName New-Bootstrap -Exactly 1 + Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2 + } + + It 'should call the correct functions for terraform module configuration with generate_only' { + Deploy-Accelerator -i "terraform" -b "github" -inputs "example.yml" -generate_only + Assert-MockCalled -CommandName Get-BootstrapAndStarterConfig -Exactly 1 + Assert-MockCalled -CommandName New-Bootstrap -Exactly 1 -ParameterFilter { $generateOnly -eq $true } + Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2 } } }