Skip to content

Add generate_only parameter to skip terraform init/plan/apply #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 "") {
Expand Down
40 changes: 27 additions & 13 deletions src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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
}
}
13 changes: 11 additions & 2 deletions src/ALZ/Public/Deploy-Accelerator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
17 changes: 12 additions & 5 deletions src/Tests/Unit/Public/Deploy-Accelerator.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down