Skip to content

Commit 576eab2

Browse files
committed
Update resources
1 parent 19d8d5e commit 576eab2

File tree

14 files changed

+589
-225
lines changed

14 files changed

+589
-225
lines changed

Local-DevelopmentScript.ps1

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ param (
99
[string]$TerraformVersion = "latest",
1010
[string]$RunCheckov = "false",
1111
[string]$TfPlanFileName = "tfplan.plan",
12-
[string]$TerraformCodeLocation = "terraform/0_rg",
12+
[string]$TerraformCodeLocation = "terraform",
13+
[string[]]$TerraformStackToRun = @('all'),
14+
[string]$CreateTerraformWorkspace = "true",
15+
[string]$TerraformWorkspace = "dev",
16+
[string]$AttemptAzureLogin = "true",
1317
[string]$UseAzureClientSecretLogin = "true",
1418
[string]$UseAzureOidcLogin = "false",
1519
[string]$UseAzureUserLogin = "false",
@@ -20,16 +24,11 @@ $ErrorActionPreference = 'Stop'
2024
$currentWorkingDirectory = (Get-Location).path
2125
$fullTerraformCodePath = Join-Path -Path $currentWorkingDirectory -ChildPath $TerraformCodeLocation
2226

23-
# Get timestamp in "HH:mm:ss" format
24-
$timestamp = Get-Date -Format "HH:mm:ss"
25-
26-
## Setup script modules etc
27-
2827
# Get script directory
2928
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
3029

3130
# Import all required modules
32-
$modules = @("Logger", "Utils", "AzureCliLogin", "AzurePwshLogin", "Nsg", "Terraform", "Storage", "Homebrew", "Checkov", "Tenv", "Choco")
31+
$modules = @("Logger", "Utils", "AzureCliLogin", "Terraform", "Storage", "Homebrew", "Checkov", "Tenv", "Choco")
3332
foreach ($module in $modules)
3433
{
3534
$modulePath = Join-Path -Path $scriptDir -ChildPath "PowerShellModules/$module.psm1"
@@ -39,18 +38,17 @@ foreach ($module in $modules)
3938
}
4039
else
4140
{
42-
Write-Host "ERROR: $timestamp - [$( $MyInvocation.MyCommand.Name )] Module not found: $modulePath" -ForegroundColor Red
41+
Write-Host "ERROR: [$( $MyInvocation.MyCommand.Name )] Module not found: $modulePath" -ForegroundColor Red
4342
exit 1
4443
}
4544
}
4645

4746
# Log that modules were loaded
48-
_LogMessage -Level "INFO" -Message "$timestamp - [$( $MyInvocation.MyCommand.Name )] Modules loaded successfully" -InvocationName "$( $MyInvocation.MyCommand.Name )"
47+
_LogMessage -Level "INFO" -Message "[$( $MyInvocation.MyCommand.Name )] Modules loaded successfully" -InvocationName "$( $MyInvocation.MyCommand.Name )"
4948

5049
$convertedDebugMode = ConvertTo-Boolean $DebugMode
5150
_LogMessage -Level 'DEBUG' -Message "DebugMode: `"$DebugMode`"$convertedDebugMode" -InvocationName "$( $MyInvocation.MyCommand.Name )"
5251

53-
5452
# Enable debug mode if DebugMode is set to $true
5553
if ($true -eq $convertedDebugMode)
5654
{
@@ -85,6 +83,9 @@ try
8583

8684
# Convert the string flags to Boolean and log the results at DEBUG level
8785

86+
$convertedAttemptAzureLogin = ConvertTo-Boolean $AttemptAzureLogin
87+
_LogMessage -Level 'DEBUG' -Message "AttemptAzureLogin: `"$AttemptAzureLogin`"$convertedAttemptAzureLogin" -InvocationName $MyInvocation.MyCommand.Name
88+
8889
$convertedUseAzureClientSecretLogin = ConvertTo-Boolean $UseAzureClientSecretLogin
8990
_LogMessage -Level 'DEBUG' -Message "UseAzureClientSecretLogin: `"$UseAzureClientSecretLogin`"$convertedUseAzureClientSecretLogin" -InvocationName $MyInvocation.MyCommand.Name
9091

@@ -118,6 +119,10 @@ try
118119
$convertedRunCheckov = ConvertTo-Boolean $RunCheckov
119120
_LogMessage -Level 'DEBUG' -Message "RunCheckov: `"$RunCheckov`"$convertedRunCheckov" -InvocationName "$( $MyInvocation.MyCommand.Name )"
120121

122+
$convertedCreateTerraformWorkspace = ConvertTo-Boolean $CreateTerraformWorkspace
123+
_LogMessage -Level 'DEBUG' -Message "CreateTerraformWorkspace: `"$CreateTerraformWorkspace`"$convertedCreateTerraformWorkspace" -InvocationName "$( $MyInvocation.MyCommand.Name )"
124+
125+
121126
# ── Chicken-and-egg / mutual exclusivity checks ───────────────────────────────
122127
if (-not $convertedRunTerraformInit -and (
123128
$convertedRunTerraformPlan -or
@@ -160,11 +165,31 @@ try
160165
try
161166
{
162167

163-
Connect-AzureCli `
164-
-UseClientSecret $convertedUseAzureClientSecretLogin `
165-
-UseOidc $convertedUseAzureOidcLogin `
166-
-UseUserDeviceCode $convertedUseAzureUserLogin `
167-
-UseManagedIdentity $convertedUseAzureManagedIdentityLogin
168+
if ($convertedAttemptAzureLogin)
169+
{
170+
171+
Connect-AzureCli `
172+
-UseClientSecret $convertedUseAzureClientSecretLogin `
173+
-UseOidc $convertedUseAzureOidcLogin `
174+
-UseUserDeviceCode $convertedUseAzureUserLogin `
175+
-UseManagedIdentity $convertedUseAzureManagedIdentityLogin
176+
}
177+
178+
$stackFolders = Get-TerraformStackFolders `
179+
-CodeRoot $fullTerraformCodePath `
180+
-StacksToRun $TerraformStackToRun
181+
182+
foreach ($folder in $stackFolders)
183+
{
184+
# Example: validate + fmt-check for each stack
185+
Invoke-TerraformFmtCheck -CodePath $folder
186+
Invoke-TerraformInit -CodePath $folder
187+
if ($convertedCreateTerraformWorkspace -and -not [string]::IsNullOrWhiteSpace($TerraformWorkspace))
188+
{
189+
Invoke-TerraformWorkspaceSelect -CodePath $folder -WorkspaceName $TerraformWorkspace
190+
}
191+
Invoke-TerraformValidate -CodePath $folder
192+
}
168193
}
169194
catch
170195
{
@@ -174,7 +199,7 @@ try
174199
}
175200
catch
176201
{
177-
_LogMessage -Level "ERROR" -Message "$timestamp - [$( $MyInvocation.MyCommand.Name )] Error: $( $_.Exception.Message )" -InvocationName "$( $MyInvocation.MyCommand.Name )"
202+
_LogMessage -Level "ERROR" -Message "Error: $( $_.Exception.Message )" -InvocationName "$( $MyInvocation.MyCommand.Name )"
178203
exit 1
179204
}
180205

@@ -215,6 +240,7 @@ finally
215240
Disconnect-AzureCli -IsUserDeviceLogin $false
216241
}
217242

243+
$Env:TF_LOG = $null
218244
Set-Location $currentWorkingDirectory
219245
}
220246

PowerShellModules/AzureCliLogin.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Connect-ToAzureCliClientSecret {
1313
--username $ClientId `
1414
--password $ClientSecret `
1515
--tenant $TenantId `
16-
--allow-no-subscriptions
16+
--allow-no-subscriptions | Out-Null
1717
_LogMessage -Level 'DEBUG' -Message "az login exit-code: $LASTEXITCODE" -InvocationName $MyInvocation.MyCommand.Name
1818
if ($LASTEXITCODE -ne 0) {
1919
_LogMessage -Level 'ERROR' -Message 'az login failed (client-secret).' -InvocationName $MyInvocation.MyCommand.Name
@@ -47,7 +47,7 @@ function Connect-ToAzureCliOidc {
4747
--username $ClientId `
4848
--tenant $TenantId `
4949
--allow-no-subscriptions `
50-
--federated-token $OidcToken
50+
--federated-token $OidcToken | Out-Null
5151
_LogMessage -Level 'DEBUG' -Message "az login exit-code: $LASTEXITCODE" -InvocationName $MyInvocation.MyCommand.Name
5252
if ($LASTEXITCODE -ne 0) {
5353
_LogMessage -Level 'ERROR' -Message 'az login failed (OIDC).' -InvocationName $MyInvocation.MyCommand.Name
@@ -218,7 +218,7 @@ function Disconnect-AzureCli {
218218
}
219219

220220
try {
221-
_LogMessage -Level 'INFO' -Message 'azure-cli logout …' -InvocationName $MyInvocation.MyCommand.Name
221+
_LogMessage -Level 'INFO' -Message 'Attempting Azure-Cli logout to cleanup' -InvocationName $MyInvocation.MyCommand.Name
222222

223223
az logout | Out-Null
224224
$code = $LASTEXITCODE
@@ -230,7 +230,7 @@ function Disconnect-AzureCli {
230230

231231
}
232232
catch {
233-
_LogMessage -Level 'ERROR' -Message "error during azure-cli logout: $($_.Exception.Message)" -InvocationName $MyInvocation.MyCommand.Name
233+
_LogMessage -Level 'ERROR' -Message "Error: Azure-Cli logout failed: $($_.Exception.Message)" -InvocationName $MyInvocation.MyCommand.Name
234234
throw
235235
}
236236
}

PowerShellModules/AzurePwshLogin.psm1

Lines changed: 0 additions & 72 deletions
This file was deleted.

PowerShellModules/Nsg.psm1

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)