Skip to content

Commit 6c6953c

Browse files
Feature: add auto-approve for terraform (#72)
# Pull Request ## Issue N/A ## Description This PR adds the capability to override the Terraform approval step. This is to support automated testing, but may also benefit customers. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent 48f178e commit 6c6953c

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/ALZ/Private/Invoke-Terraform.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ function Invoke-Terraform {
55
[string] $moduleFolderPath,
66

77
[Parameter(Mandatory = $false)]
8-
[string] $tfvarsFileName
8+
[string] $tfvarsFileName,
9+
10+
[Parameter(Mandatory = $false)]
11+
[switch] $autoApprove
912
)
1013

1114
if ($PSCmdlet.ShouldProcess("Apply Terraform", "modify")) {
1215
terraform -chdir="$moduleFolderPath" init
13-
terraform -chdir="$moduleFolderPath" apply -var-file="$tfvarsFileName"
16+
Write-InformationColored "Terraform init has completed, now running the apply..." -ForegroundColor Green -InformationAction Continue
17+
if($autoApprove) {
18+
terraform -chdir="$moduleFolderPath" apply -var-file="$tfvarsFileName" -auto-approve
19+
} else {
20+
terraform -chdir="$moduleFolderPath" apply -var-file="$tfvarsFileName"
21+
}
1422
}
1523
}

src/ALZ/Private/New-ALZEnvironmentTerraform.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ function New-ALZEnvironmentTerraform {
1717

1818
[Parameter(Mandatory = $false)]
1919
[Alias("inputs")]
20-
[string] $userInputOverridePath = ""
20+
[string] $userInputOverridePath = "",
21+
22+
[Parameter(Mandatory = $false)]
23+
[switch] $autoApprove
2124
)
2225

2326
$terraformModuleUrl = "https://github.com/Azure/alz-terraform-accelerator"
@@ -74,8 +77,12 @@ function New-ALZEnvironmentTerraform {
7477

7578
# Running terraform init and apply
7679
Write-InformationColored "Thank you for providing those inputs, we are now initializing and applying Terraform to bootstrap your environment..." -ForegroundColor Green -InformationAction Continue
77-
Write-InformationColored "Once the plan is complete you will be prompted to confirm the apply. You must enter 'yes' to apply." -ForegroundColor Green -InformationAction Continue
7880

79-
Invoke-Terraform -moduleFolderPath $bootstrapPath -tfvarsFileName "override.tfvars"
81+
if($autoApprove) {
82+
Invoke-Terraform -moduleFolderPath $bootstrapPath -tfvarsFileName "override.tfvars" -autoApprove
83+
} else {
84+
Write-InformationColored "Once the plan is complete you will be prompted to confirm the apply. You must enter 'yes' to apply." -ForegroundColor Green -InformationAction Continue
85+
Invoke-Terraform -moduleFolderPath $bootstrapPath -tfvarsFileName "override.tfvars"
86+
}
8087
}
8188
}

src/ALZ/Public/New-ALZEnvironment.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ function New-ALZEnvironment {
1414
The IaC provider to use for the ALZ environment.
1515
.PARAMETER userInputOverridePath
1616
A json file containing user input overrides for the user input prompts. This will cause the tool to by pass requesting user input for that field and use the value(s) provided. E.g { "starter_module": "basic", "azure_location": "uksouth" }
17+
.PARAMETER autoApprove
18+
Automatically approve the terraform apply.
1719
.EXAMPLE
1820
New-ALZEnvironment
1921
.EXAMPLE
@@ -47,7 +49,10 @@ function New-ALZEnvironment {
4749

4850
[Parameter(Mandatory = $false)]
4951
[Alias("inputs")]
50-
[string] $userInputOverridePath = ""
52+
[string] $userInputOverridePath = "",
53+
54+
[Parameter(Mandatory = $false)]
55+
[switch] $autoApprove
5156
)
5257

5358
Write-InformationColored "Getting ready to create a new ALZ environment with you..." -ForegroundColor Green -InformationAction Continue
@@ -64,7 +69,11 @@ function New-ALZEnvironment {
6469
if($alzVersion -eq "") {
6570
$alzVersion = "latest"
6671
}
67-
New-ALZEnvironmentTerraform -alzEnvironmentDestination $alzEnvironmentDestination -alzVersion $alzVersion -alzCicdPlatform $alzCicdPlatform -userInputOverridePath $userInputOverridePath
72+
if($autoApprove) {
73+
New-ALZEnvironmentTerraform -alzEnvironmentDestination $alzEnvironmentDestination -alzVersion $alzVersion -alzCicdPlatform $alzCicdPlatform -userInputOverridePath $userInputOverridePath -autoApprove
74+
} else {
75+
New-ALZEnvironmentTerraform -alzEnvironmentDestination $alzEnvironmentDestination -alzVersion $alzVersion -alzCicdPlatform $alzCicdPlatform -userInputOverridePath $userInputOverridePath
76+
}
6877
}
6978
}
7079
}

0 commit comments

Comments
 (0)