Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefajers authored Feb 29, 2024
1 parent a1f7829 commit 735b620
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
9 changes: 1 addition & 8 deletions src/functions/Invoke-AzOpsPush.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,7 @@
# Check each failed removal and attempt to get the associated resource
foreach ($fail in $removeActionFail) {
$resource = $null
Set-AzOpsContext -ScopeObject $fail.ScopeObject
# Determine if the resource is a lock or a regular resource
if ($fail.FullyQualifiedResourceId -match '^/subscriptions/.*/providers/Microsoft.Authorization/locks' -or $fail.FullyQualifiedResourceId -match '^/subscriptions/.*/resourceGroups/.*/providers/Microsoft.Authorization/locks') {
$resource = Get-AzResourceLock | Where-Object { $_.ResourceId -eq $fail.FullyQualifiedResourceId } -ErrorAction SilentlyContinue
}
else {
$resource = Get-AzResource -ResourceId $fail.FullyQualifiedResourceId -ErrorAction SilentlyContinue
}
$resource = Get-AzOpsResource -ResourceId $fail.FullyQualifiedResourceId -ScopeObject $fail.ScopeObject
# If the resource is found, log the failure
if ($resource) {
$throwFail = $true
Expand Down
39 changes: 39 additions & 0 deletions src/internal/functions/Get-AzOpsResource.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function Get-AzOpsResource {

<#
.SYNOPSIS
Check if the Azure resource exists.
.DESCRIPTION
Check if the Azure resource exists.
.PARAMETER ResourceId
The ResourceId to check.
.PARAMETER ScopeObject
Object used to set Azure context for operation.
.EXAMPLE
> Get-AzOpsResource -ResourceId /subscriptions/6a35d1cc-ae17-4c0c-9a66-1d9a25647b19/resourceGroups/NetworkWatcherRG -ScopeObject $ScopeObject
#>

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$ResourceId,
[Parameter(Mandatory = $true)]
[AzOpsScope]
$ScopeObject
)

process {
Set-AzOpsContext -ScopeObject $ScopeObject
# Check if the resource exists
if ($ResourceId -match '^/subscriptions/.*/providers/Microsoft.Authorization/locks' -or $ResourceId -match '^/subscriptions/.*/resourceGroups/.*/providers/Microsoft.Authorization/locks') {
$resource = Get-AzResourceLock | Where-Object { $_.ResourceId -eq $ResourceId } -ErrorAction SilentlyContinue
}
else {
$resource = Get-AzResource -ResourceId $ResourceId -ErrorAction SilentlyContinue
}
if ($resource) {
return $resource
}
}
}
3 changes: 1 addition & 2 deletions src/internal/functions/Remove-AzOpsDeployment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@
Set-AzOpsWhatIfOutput -FilePath $TemplateFilePath -Results $results -RemoveAzOpsFlag $true
}
if ($PSCmdlet.ShouldProcess("Remove $($scopeObject.Scope)?")) {
$null = Remove-AzResource -ResourceId $scopeObject.Scope -Force
Start-Sleep -Seconds 5
$null = Remove-AzResourceRaw -FullyQualifiedResourceId $scopeObject.Scope -ScopeObject $scopeObject -TemplateFilePath $TemplateFilePath -TemplateParameterFilePath $TemplateParameterFilePath
}
else {
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Remove-AzOpsDeployment.SkipDueToWhatIf'
Expand Down
24 changes: 15 additions & 9 deletions src/internal/functions/Remove-AzResourceRaw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,26 @@
ScopeObject = $scopeObject
Status = 'success'
}
# Set Azure context for removal operation
Set-AzOpsContext -ScopeObject $ScopeObject
# Check if the resource exists
if ($FullyQualifiedResourceId -match '^/subscriptions/.*/providers/Microsoft.Authorization/locks' -or $FullyQualifiedResourceId -match '^/subscriptions/.*/resourceGroups/.*/providers/Microsoft.Authorization/locks') {
$resource = Get-AzResourceLock | Where-Object { $_.ResourceId -eq $FullyQualifiedResourceId } -ErrorAction SilentlyContinue
}
else {
$resource = Get-AzResource -ResourceId $FullyQualifiedResourceId -ErrorAction SilentlyContinue
}
$resource = Get-AzOpsResource -ResourceId $FullyQualifiedResourceId -ScopeObject $ScopeObject
# Remove the resource if it exists
if ($resource) {
try {
# Set Azure context for removal operation
Set-AzOpsContext -ScopeObject $ScopeObject
$null = Remove-AzResource -ResourceId $FullyQualifiedResourceId -Force -ErrorAction Stop
Start-Sleep -Seconds 5
$maxAttempts = 4
$attempt = 1
$gone = $false
while ($gone -eq $false -and $attempt -le $maxAttempts) {
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Remove-AzResourceRaw.Resource.CheckExistence' -LogStringValues $FullyQualifiedResourceId
Start-Sleep -Seconds 10
$tryResource = Get-AzOpsResource -ResourceId $FullyQualifiedResourceId -ScopeObject $ScopeObject
if (-not $tryResource) {
$gone = $true
}
$attempt++
}
}
catch {
# Log failure message
Expand Down
2 changes: 1 addition & 1 deletion src/localized/en-us/Strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@
'Remove-AzOpsDeployment.ResourceNotFound' = 'Unable to find resource of type {0} with id {1}.'# $scopeObject.resource, $scopeObject.scope, $resultsError
'Remove-AzOpsDeployment.SkipUnsupportedResource' = 'Deletion of AzOps generated file resources is only supported for locks, policyAssignments, policyDefinitions, policyExemptions, policySetDefinitions and roleAssignments. Will NOT proceed with deletion of resource in file {0}'# $TemplateFilePath

'Remove-AzResourceRaw.Resource.CheckExistence' = 'Checking existence after deletion of: [{0}]'# $FullyQualifiedResourceId
'Remove-AzResourceRaw.Resource.Failed' = 'Unable to delete resource of type {0} with id {1}'# $scopeObject.scope, $FullyQualifiedResourceId

'Remove-AzResourceRawRecursive.Processing' = 'Recursive retry processing to delete resource of type {0} with id {1}'# $item.ScopeObject.resource, $item.FullyQualifiedResourceId

'Remove-AzOpsInvalidCharacter.Completed' = 'Valid string: {0}'# $String
Expand Down

0 comments on commit 735b620

Please sign in to comment.