Skip to content

Commit f3d24b1

Browse files
committed
Update functions
1 parent 175cf89 commit f3d24b1

File tree

5 files changed

+68
-53
lines changed

5 files changed

+68
-53
lines changed

PowerShellModules/AzureCliLogin.psm1

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -97,71 +97,70 @@ function Connect-ToAzureCliOidc
9797
_LogMessage -Level 'INFO' -Message 'OIDC login OK.' -InvocationName $MyInvocation.MyCommand.Name
9898
}
9999

100-
function Connect-ToAzureCliDeviceCode
101-
{
100+
function Connect-ToAzureCliDeviceCode {
102101
param(
103102
[string]$TenantId,
104103
[string]$SubscriptionId
105104
)
106105

107-
# ── 1. Is the CLI already logged in? ────────────────────────────────────
108-
$currentId = az account show --query id -o tsv 2> $null
109-
if ($LASTEXITCODE -eq 0 -and $currentId)
110-
{
111-
_LogMessage -Level 'INFO' -Message "Azure CLI already authenticated (subscription id: $currentId) – skipping device-code login." -InvocationName $MyInvocation.MyCommand.Name
106+
$invocation = $MyInvocation.MyCommand.Name
112107

113-
# caller may still want to switch subscription
114-
if ($SubscriptionId)
115-
{
116-
az account set --subscription $SubscriptionId
117-
_LogMessage -Level 'DEBUG' -Message "az account set exit-code: $LASTEXITCODE" -InvocationName $MyInvocation.MyCommand.Name
118-
if ($LASTEXITCODE -ne 0)
119-
{
120-
_LogMessage -Level 'WARN' -Message "Unable to switch to subscription $SubscriptionId." -InvocationName $MyInvocation.MyCommand.Name
108+
try {
109+
# ── Check if already logged in and with correct tenant/sub ──
110+
$accountInfo = az account show --output json | ConvertFrom-Json
111+
112+
if ($accountInfo -and $accountInfo.id) {
113+
$currentSubId = $accountInfo.id
114+
$currentTenant = $accountInfo.tenantId
115+
116+
$isSubMatch = -not $SubscriptionId -or ($SubscriptionId -eq $currentSubId)
117+
$isTenantMatch = -not $TenantId -or ($TenantId -eq $currentTenant)
118+
119+
if ($isSubMatch -and $isTenantMatch) {
120+
_LogMessage -Level 'INFO' -Message "Azure CLI already authenticated with correct subscription and tenant (sub: $currentSubId, tenant: $currentTenant) – skipping login." -InvocationName $invocation
121+
return
122+
}
123+
124+
if (-not $isSubMatch -and $SubscriptionId) {
125+
_LogMessage -Level 'INFO' -Message "Switching subscription to $SubscriptionId..." -InvocationName $invocation
126+
az account set --subscription $SubscriptionId
127+
if ($LASTEXITCODE -ne 0) {
128+
_LogMessage -Level 'WARN' -Message "Unable to switch to subscription $SubscriptionId." -InvocationName $invocation
129+
}
130+
return
121131
}
122132
}
123-
return
124-
}
125133

126-
# ── 2. Perform interactive login ───────────────────────────────────────
127-
try
128-
{
129-
_LogMessage -Level 'INFO' -Message 'Azure CLI device-code login…' -InvocationName $MyInvocation.MyCommand.Name
134+
# ── Perform interactive login ──
135+
_LogMessage -Level 'INFO' -Message 'Azure CLI device-code login…' -InvocationName $invocation
130136

131-
if ($TenantId)
132-
{
137+
if ($TenantId) {
133138
az login --use-device-code --tenant $TenantId --allow-no-subscriptions
134-
}
135-
else
136-
{
139+
} else {
137140
az login --use-device-code --allow-no-subscriptions
138141
}
139-
_LogMessage -Level 'DEBUG' -Message "az login exit-code: $LASTEXITCODE" -InvocationName $MyInvocation.MyCommand.Name
140-
if ($LASTEXITCODE -ne 0)
141-
{
142+
143+
if ($LASTEXITCODE -ne 0) {
142144
throw 'az login failed (device-code).'
143145
}
144146

145-
if ($SubscriptionId)
146-
{
147+
if ($SubscriptionId) {
147148
az account set --subscription $SubscriptionId
148-
_LogMessage -Level 'DEBUG' -Message "az account set exit-code: $LASTEXITCODE" -InvocationName $MyInvocation.MyCommand.Name
149-
if ($LASTEXITCODE -ne 0)
150-
{
149+
if ($LASTEXITCODE -ne 0) {
151150
throw "Unable to set subscription $SubscriptionId."
152151
}
153152
}
154153

155-
_LogMessage -Level 'INFO' -Message 'Device-code login OK.' -InvocationName $MyInvocation.MyCommand.Name
154+
_LogMessage -Level 'INFO' -Message 'Device-code login OK.' -InvocationName $invocation
156155
}
157-
catch
158-
{
159-
_LogMessage -Level 'ERROR' -Message "Device-code login failed: $( $_.Exception.Message )" -InvocationName $MyInvocation.MyCommand.Name
156+
catch {
157+
_LogMessage -Level 'ERROR' -Message "Device-code login failed: $($_.Exception.Message)" -InvocationName $invocation
160158
throw
161159
}
162160
}
163161

164162

163+
165164
function Test-AzureCliConnection
166165
{
167166
try

PowerShellModules/Homebrew.psm1

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
function Assert-HomebrewPath
2-
{
3-
_LogMessage -Level "INFO" -Message "Ensuring Homebrew is available in the PATH..." -InvocationName "$( $MyInvocation.MyCommand.Name )"
1+
function Assert-HomebrewPath {
2+
_LogMessage -Level "INFO" -Message "Ensuring Homebrew is available in the PATH..." -InvocationName $MyInvocation.MyCommand.Name
3+
4+
# Check if 'brew' is already available in the current session
5+
if (Get-Command brew -ErrorAction SilentlyContinue) {
6+
_LogMessage -Level "INFO" -Message "Homebrew is already available in the PATH. Skipping shellenv import." -InvocationName $MyInvocation.MyCommand.Name
7+
return
8+
}
49

510
# Get the output of the shellenv command from Homebrew
611
$brewShellEnv = & /home/linuxbrew/.linuxbrew/bin/brew shellenv
7-
$brewShellEnvString = $brewShellEnv -join "`n"
12+
if (-not $brewShellEnv) {
13+
_LogMessage -Level "ERROR" -Message "brew shellenv returned no output. Cannot update environment." -InvocationName $MyInvocation.MyCommand.Name
14+
exit 1
15+
}
816

917
# Apply the environment changes using Invoke-Expression
18+
$brewShellEnvString = $brewShellEnv -join "`n"
1019
Invoke-Expression $brewShellEnvString
1120

12-
# Test if brew is now available in the session
13-
if (Get-Command brew -ErrorAction SilentlyContinue)
14-
{
15-
_LogMessage -Level "INFO" -Message "Homebrew is now available in the PATH." -InvocationName "$( $MyInvocation.MyCommand.Name )"
21+
# Re-check if brew is now available
22+
if (Get-Command brew -ErrorAction SilentlyContinue) {
23+
_LogMessage -Level "INFO" -Message "Homebrew is now available in the PATH." -InvocationName $MyInvocation.MyCommand.Name
1624
}
17-
else
18-
{
19-
_LogMessage -Level "ERROR" -Message "Homebrew is not available. Something went wrong." -InvocationName "$( $MyInvocation.MyCommand.Name )"
25+
else {
26+
_LogMessage -Level "ERROR" -Message "Homebrew is still not available after applying shellenv." -InvocationName $MyInvocation.MyCommand.Name
2027
exit 1
2128
}
2229
}
2330

2431
Export-ModuleMember -Function Assert-HomebrewPath
32+
33+
34+
Export-ModuleMember -Function Assert-HomebrewPath

Run-AzTerraform.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ param (
1919
[string]$TerraformPlanFileName = "tfplan.plan",
2020
[string]$TerraformDestroyPlanFileName = "tfplan-destroy.plan",
2121
[string]$TerraformCodeLocation = "terraform",
22-
[string[]]$TerraformStackToRun = @('all'),
22+
[string[]]$TerraformStackToRun = @('rg'),
2323
[string]$CreateTerraformWorkspace = "true",
2424
[string]$TerraformWorkspace = "dev",
2525
[string]$InstallAzureCli = "false",
2626
[string]$AttemptAzureLogin = "true",
27-
[string]$UseAzureClientSecretLogin = "true",
27+
[string]$UseAzureClientSecretLogin = "false",
2828
[string]$UseAzureOidcLogin = "false",
29-
[string]$UseAzureUserLogin = "false",
29+
[string]$UseAzureUserLogin = "true",
3030
[string]$UseAzureManagedIdentityLogin = "false"
3131
)
3232

@@ -235,7 +235,7 @@ try
235235
# ── INIT ──────────────────────────────────────────────────────────────
236236
if ($convertedRunTerraformInit)
237237
{
238-
Invoke-TerraformInit -CodePath $folder -InitArgs '-input=false','-upgrade=true'
238+
Invoke-TerraformInit -CodePath $folder -InitArgs $TerraformInitExtraArgs
239239
}
240240

241241
# workspace (needs an init first)

terraform/0_rg/.terraform.lock.hcl

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/1_network/.terraform.lock.hcl

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)