Skip to content

Commit 6d55536

Browse files
Add generate_only parameter to skip terraform init/plan/apply
Co-authored-by: jaredfholgate <1612200+jaredfholgate@users.noreply.github.com>
1 parent 4b463d8 commit 6d55536

4 files changed

Lines changed: 62 additions & 23 deletions

File tree

src/ALZ/Private/Deploy-Accelerator-Helpers/Invoke-Terraform.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,20 @@ function Invoke-Terraform {
1919
[Parameter(Mandatory = $false)]
2020
[string] $outputFilePath = "",
2121

22-
[Parameter(Mandatory = $false)]
23-
[switch] $silent
22+
[Parameter(Mandatory = $false)]
23+
[switch] $silent,
24+
25+
[Parameter(Mandatory = $false)]
26+
[switch] $generateOnly
2427
)
2528

26-
if ($PSCmdlet.ShouldProcess("Apply Terraform", "modify")) {
29+
if ($PSCmdlet.ShouldProcess("Apply Terraform", "modify")) {
30+
31+
if ($generateOnly) {
32+
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
33+
return
34+
}
35+
2736
# Check and Set Subscription ID
2837
$removeSubscriptionId = $false
2938
if ($null -eq $env:ARM_SUBSCRIPTION_ID -or $env:ARM_SUBSCRIPTION_ID -eq "") {

src/ALZ/Private/Deploy-Accelerator-Helpers/New-Bootstrap.ps1

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ function New-Bootstrap {
5656
[string[]]
5757
$inputConfigFilePaths = @(),
5858

59-
[Parameter(Mandatory = $false)]
60-
[string[]]
61-
$starterAdditionalFiles = @()
59+
[Parameter(Mandatory = $false)]
60+
[string[]]
61+
$starterAdditionalFiles = @(),
62+
63+
[Parameter(Mandatory = $false)]
64+
[switch]
65+
$generateOnly
6266
)
6367

6468
if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) {
@@ -269,16 +273,26 @@ function New-Bootstrap {
269273
Remove-UnrequiredFileSet -path $starterModulePath -foldersOrFilesToRetain $foldersOrFilesToRetain -subFoldersOrFilesToRemove $subFoldersOrFilesToRemove -writeVerboseLogs:$writeVerboseLogs.IsPresent
270274
}
271275

272-
# Running terraform init and apply
273-
Write-InformationColored "Thank you for providing those inputs, we are now initializing and applying Terraform to bootstrap your environment..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
274-
275-
if($autoApprove) {
276-
Invoke-Terraform -moduleFolderPath $bootstrapModulePath -autoApprove -destroy:$destroy.IsPresent
277-
} else {
278-
Write-InformationColored "Once the plan is complete you will be prompted to confirm the apply." -ForegroundColor Green -NewLineBefore -InformationAction Continue
279-
Invoke-Terraform -moduleFolderPath $bootstrapModulePath -destroy:$destroy.IsPresent
276+
# Running terraform init and apply
277+
if ($generateOnly) {
278+
Write-InformationColored "Thank you for providing those inputs, we are now generating the Terraform files for your environment..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
279+
} else {
280+
Write-InformationColored "Thank you for providing those inputs, we are now initializing and applying Terraform to bootstrap your environment..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
281+
}
282+
283+
if($autoApprove) {
284+
Invoke-Terraform -moduleFolderPath $bootstrapModulePath -autoApprove -destroy:$destroy.IsPresent -generateOnly:$generateOnly.IsPresent
285+
} else {
286+
if (!$generateOnly) {
287+
Write-InformationColored "Once the plan is complete you will be prompted to confirm the apply." -ForegroundColor Green -NewLineBefore -InformationAction Continue
288+
}
289+
Invoke-Terraform -moduleFolderPath $bootstrapModulePath -destroy:$destroy.IsPresent -generateOnly:$generateOnly.IsPresent
290+
}
291+
292+
if ($generateOnly) {
293+
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
294+
} else {
295+
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
280296
}
281-
282-
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
283297
}
284298
}

src/ALZ/Public/Deploy-Accelerator.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,15 @@ function Deploy-Accelerator {
183183
HelpMessage = "[OPTIONAL] Determines whether to skip the requirements check for the ALZ PowerShell Module version only. This is not recommended."
184184
)]
185185
[Alias("skipAlzModuleVersionRequirementsCheck")]
186-
[switch] $skip_alz_module_version_requirements_check
186+
[switch] $skip_alz_module_version_requirements_check,
187+
188+
[Parameter(
189+
Mandatory = $false,
190+
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."
191+
)]
192+
[Alias("g")]
193+
[Alias("generateOnly")]
194+
[switch] $generate_only
187195
)
188196

189197
$ProgressPreference = "SilentlyContinue"
@@ -380,7 +388,8 @@ function Deploy-Accelerator {
380388
-hclParserToolPath $hclParserToolPath `
381389
-convertTfvarsToJson:$inputConfig.convert_tfvars_to_json.Value `
382390
-inputConfigFilePaths $inputConfigFilePaths `
383-
-starterAdditionalFiles $inputConfig.starter_additional_files.Value
391+
-starterAdditionalFiles $inputConfig.starter_additional_files.Value `
392+
-generateOnly:$inputConfig.generate_only.Value
384393
}
385394

386395
$ProgressPreference = "Continue"

src/Tests/Unit/Public/Deploy-Accelerator.Tests.ps1

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,18 @@ InModuleScope 'ALZ' {
158158
Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2
159159
}
160160

161-
It 'should call the correct functions for terraform module configuration' {
162-
Deploy-Accelerator -i "terraform" -b "github" -inputs "example.yml"
163-
Assert-MockCalled -CommandName Get-BootstrapAndStarterConfig -Exactly 1
164-
Assert-MockCalled -CommandName New-Bootstrap -Exactly 1
165-
Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2
161+
It 'should call the correct functions for terraform module configuration' {
162+
Deploy-Accelerator -i "terraform" -b "github" -inputs "example.yml"
163+
Assert-MockCalled -CommandName Get-BootstrapAndStarterConfig -Exactly 1
164+
Assert-MockCalled -CommandName New-Bootstrap -Exactly 1
165+
Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2
166+
}
167+
168+
It 'should call the correct functions for terraform module configuration with generate_only' {
169+
Deploy-Accelerator -i "terraform" -b "github" -inputs "example.yml" -generate_only
170+
Assert-MockCalled -CommandName Get-BootstrapAndStarterConfig -Exactly 1
171+
Assert-MockCalled -CommandName New-Bootstrap -Exactly 1 -ParameterFilter { $generateOnly -eq $true }
172+
Assert-MockCalled -CommandName New-ModuleSetup -Exactly 2
166173
}
167174
}
168175
}

0 commit comments

Comments
 (0)