Skip to content
Merged
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
38 changes: 37 additions & 1 deletion Public/Get-AzRetirementRecommendation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,45 @@ Gets recommendations using the REST API method

$recommendations = if ($SubscriptionId) {
# Query specific subscriptions
# Store the current context to restore later
$originalContext = Get-AzContext

foreach ($subId in $SubscriptionId) {
Write-Verbose "Querying subscription via Az.Advisor: $subId"
Get-AzAdvisorRecommendation -Filter $filter | Where-Object $subcategoryFilter

# Set context to the specific subscription
try {
$context = Set-AzContext -SubscriptionId $subId -ErrorAction Stop

# Verify that the context was actually set to the intended subscription
if (-not $context -or -not $context.Subscription -or $context.Subscription.Id -ne $subId) {
Write-Warning "Azure context for subscription $($subId) could not be verified. Skipping recommendation query for this subscription."
continue
}

}
catch {
Write-Warning "Failed to set Azure context for subscription $($subId): $_"
continue
}

# Query Advisor recommendations for the current subscription
try {
Get-AzAdvisorRecommendation -Filter $filter | Where-Object $subcategoryFilter
}
catch {
Write-Warning "Failed to query Advisor recommendations for subscription $($subId): $_"
}
}

# Restore the original context
if ($originalContext) {
try {
$null = Set-AzContext -Context $originalContext -ErrorAction Stop
}
catch {
Write-Warning "Failed to restore original Azure context: $_"
}
}
}
else {
Expand Down
56 changes: 56 additions & 0 deletions Tests/AzRetirementMonitor.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,62 @@ Describe "Get-AzRetirementRecommendation" {
}
}

Describe "Get-AzRetirementRecommendation Context Switching Logic" {
It "Should have context management code in the function" {
# Verify that the function source contains the necessary context management logic
$functionDef = (Get-Command Get-AzRetirementRecommendation).Definition

# Check for Get-AzContext call to save original context
$functionDef | Should -Match 'Get-AzContext'

# Check for Set-AzContext with SubscriptionId parameter
$functionDef | Should -Match 'Set-AzContext\s+-SubscriptionId'

# Check for context verification logic
$functionDef | Should -Match 'could not be verified'

# Check for context restoration with Context parameter
$functionDef | Should -Match 'Set-AzContext\s+-Context'
}

It "Should have error handling for Set-AzContext failures" {
$functionDef = (Get-Command Get-AzRetirementRecommendation).Definition

# Check for try-catch around Set-AzContext
$functionDef | Should -Match 'try\s*\{[^}]*Set-AzContext'
$functionDef | Should -Match 'Failed to set Azure context for subscription'
}

It "Should have error handling for context restoration" {
$functionDef = (Get-Command Get-AzRetirementRecommendation).Definition

# Check for error handling around context restoration
$functionDef | Should -Match 'Failed to restore original Azure context'
}

It "Should have error handling for Get-AzAdvisorRecommendation failures" {
$functionDef = (Get-Command Get-AzRetirementRecommendation).Definition

# Check for error handling around Get-AzAdvisorRecommendation
$functionDef | Should -Match 'Failed to query Advisor recommendations'
}

It "Should verify subscription context after setting it" {
$functionDef = (Get-Command Get-AzRetirementRecommendation).Definition

# Check that the function verifies the context was set correctly
$functionDef | Should -Match 'context\.Subscription\.Id'
$functionDef | Should -Match '\$subId'
}

It "Should use continue statement to skip failed subscriptions" {
$functionDef = (Get-Command Get-AzRetirementRecommendation).Definition

# Check for continue statements in error handling
$functionDef | Should -Match 'continue'
}
}

Describe "Get-AzRetirementMetadataItem" {
It "Should have no parameters" {
$cmd = Get-Command Get-AzRetirementMetadataItem
Expand Down