Skip to content

Commit 1633dcd

Browse files
Fix Log analytics workspace removal (#841)
* change st ins config default name * law & order * output * purge law * Update utilities/pipelines/resourceRemoval/helper/Invoke-ResourcePostRemoval.ps1 Co-authored-by: Alexander Sehr <ASehr@hotmail.de> * add check purge * add break * remove if Co-authored-by: Erika Gressi <erikag.microsoft.com> Co-authored-by: Alexander Sehr <ASehr@hotmail.de>
1 parent 89de262 commit 1633dcd

7 files changed

Lines changed: 42 additions & 5 deletions

File tree

arm/Microsoft.OperationalInsights/workspaces/deploy.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ module logAnalyticsWorkspace_rbac '.bicep/nested_rbac.bicep' = [for (roleAssignm
251251
@description('The resource ID of the deployed log analytics workspace')
252252
output logAnalyticsResourceId string = logAnalyticsWorkspace.id
253253

254-
@description('The resource group where the log analytics will be deployed')
254+
@description('The resource group of the deployed log analytics workspace')
255255
output logAnalyticsResourceGroup string = resourceGroup().name
256256

257257
@description('The name of the deployed log analytics workspace')

arm/Microsoft.OperationalInsights/workspaces/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,18 @@ Tag names and tag values can be provided as needed. A tag can be left without a
123123
| Output Name | Type | Description |
124124
| :-- | :-- | :-- |
125125
| `logAnalyticsName` | string | The name of the deployed log analytics workspace |
126-
| `logAnalyticsResourceGroup` | string | The resource group where the log analytics will be deployed |
126+
| `logAnalyticsResourceGroup` | string | The resource group of the deployed log analytics workspace |
127127
| `logAnalyticsResourceId` | string | The resource ID of the deployed log analytics workspace |
128128
| `logAnalyticsWorkspaceId` | string | The ID associated with the workspace |
129129

130130
## Template references
131131

132+
- [Diagnosticsettings](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Insights/2021-05-01-preview/diagnosticSettings)
132133
- [Locks](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Authorization/2016-09-01/locks)
133134
- [Roleassignments](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Authorization/2020-04-01-preview/roleAssignments)
134-
- [Diagnosticsettings](https://docs.microsoft.com/en-us/azure/templates/Microsoft.Insights/2021-05-01-preview/diagnosticSettings)
135+
- [Solutions](https://docs.microsoft.com/en-us/azure/templates/Microsoft.OperationsManagement/2015-11-01-preview/solutions)
135136
- [Workspaces](https://docs.microsoft.com/en-us/azure/templates/Microsoft.OperationalInsights/2020-08-01/workspaces)
136137
- [Workspaces/Datasources](https://docs.microsoft.com/en-us/azure/templates/Microsoft.OperationalInsights/2020-08-01/workspaces/dataSources)
137138
- [Workspaces/Linkedservices](https://docs.microsoft.com/en-us/azure/templates/Microsoft.OperationalInsights/2020-08-01/workspaces/linkedServices)
138139
- [Workspaces/Savedsearches](https://docs.microsoft.com/en-us/azure/templates/Microsoft.OperationalInsights/2020-08-01/workspaces/savedSearches)
139140
- [Workspaces/Storageinsightconfigs](https://docs.microsoft.com/en-us/azure/templates/Microsoft.OperationalInsights/2020-08-01/workspaces/storageInsightConfigs)
140-
- [Solutions](https://docs.microsoft.com/en-us/azure/templates/Microsoft.OperationsManagement/2015-11-01-preview/solutions)

arm/Microsoft.OperationalInsights/workspaces/storageInsightConfigs/deploy.bicep

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
param logAnalyticsWorkspaceName string
33

44
@description('Optional. The name of the storage insights config')
5-
param name string = last(split(storageAccountId, '/'))
5+
param name string = '${last(split(storageAccountId, '/'))}-stinsconfig'
66

77
@description('Required. The Azure Resource Manager ID of the storage account resource.')
88
param storageAccountId string

utilities/pipelines/resourceRemoval/Initialize-DeploymentRemoval.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ function Initialize-DeploymentRemoval {
7272
)
7373
break
7474
}
75+
'workspaces' {
76+
$removalSequence += @(
77+
'Microsoft.OperationsManagement/solutions',
78+
'Microsoft.OperationalInsights/workspaces/linkedServices'
79+
)
80+
break
81+
}
7582
### CODE LOCATION: Add custom removal sequence here
7683
}
7784

utilities/pipelines/resourceRemoval/helper/Invoke-ResourcePostRemoval.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function Invoke-ResourcePostRemoval {
3838
$null = Remove-AzKeyVault -ResourceId $matchingKeyVault.Id -InRemovedState -Force -Location $matchingKeyVault.Location
3939
}
4040
}
41+
break
4142
}
4243
'Microsoft.CognitiveServices/accounts' {
4344
$resourceGroupName = $resourceId.Split('/')[4]
@@ -49,6 +50,7 @@ function Invoke-ResourcePostRemoval {
4950
$null = Remove-AzCognitiveServicesAccount -InRemovedState -Force -Location $matchingAccount.Location -ResourceGroupName $resourceGroupName -Name $matchingAccount.AccountName
5051
}
5152
}
53+
break
5254
}
5355
'Microsoft.ApiManagement/service' {
5456
$subscriptionId = $resourceId.Split('/')[2]
@@ -73,6 +75,31 @@ function Invoke-ResourcePostRemoval {
7375
$null = Invoke-AzRestMethod @purgeRequestInputObject
7476
}
7577
}
78+
break
79+
}
80+
'Microsoft.OperationalInsights/workspaces' {
81+
$subscriptionId = $resourceId.Split('/')[2]
82+
$resourceGroupName = $resourceId.Split('/')[4]
83+
$resourceName = Split-Path $ResourceId -Leaf
84+
# Fetch service in soft-delete state
85+
$getPath = '/subscriptions/{0}/providers/Microsoft.OperationalInsights/deletedWorkspaces?api-version=2020-03-01-preview' -f $subscriptionId
86+
$getRequestInputObject = @{
87+
Method = 'GET'
88+
Path = $getPath
89+
}
90+
$softDeletedService = ((Invoke-AzRestMethod @getRequestInputObject).Content | ConvertFrom-Json).value | Where-Object { $_.id -eq $resourceId -and $_.name -eq $resourceName }
91+
if ($softDeletedService) {
92+
# Recover service
93+
$location = $softDeletedService.location
94+
if ($PSCmdlet.ShouldProcess(('Log analytics workspace [{0}]' -f $resourceId), 'Recover')) {
95+
$recoveredWorkspace = New-AzOperationalInsightsWorkspace -ResourceGroupName $resourceGroupName -Name $resourceName -Location $location
96+
}
97+
# Purge service
98+
if ($PSCmdlet.ShouldProcess(('Log analytics workspace with ID [{0}]' -f $resourceId), 'Purge')) {
99+
$recoveredWorkspace | Remove-AzOperationalInsightsWorkspace -ForceDelete -Force
100+
}
101+
}
102+
break
76103
}
77104
'Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems' {
78105
# Remove protected VM
@@ -108,6 +135,7 @@ function Invoke-ResourcePostRemoval {
108135

109136
# Undo a potential soft delete state change
110137
$null = Set-AzRecoveryServicesVaultProperty -VaultId $vaultId -SoftDeleteFeatureState $softDeleteStatus.TrimEnd('d')
138+
break
111139
}
112140
### CODE LOCATION: Add custom post-removal operation here
113141
}

utilities/pipelines/resourceRemoval/helper/Invoke-ResourceRemoval.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function Invoke-ResourceRemoval {
9494
# Actual removal
9595
# --------------
9696
$null = Remove-AzResource -ResourceId $resourceId -Force -ErrorAction 'Stop'
97+
break
9798
}
9899
### CODE LOCATION: Add custom removal action here
99100
Default {

utilities/pipelines/sharedScripts/Set-EnvironmentOnAgent.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ function Set-EnvironmentOnAgent {
106106
@{ Name = 'Az.RecoveryServices' },
107107
@{ Name = 'Az.Monitor' },
108108
@{ Name = 'Az.CognitiveServices' },
109+
@{ Name = 'Az.OperationalInsights' },
109110
@{ Name = 'Pester'; Version = '5.3.0' }
110111
)
111112
)

0 commit comments

Comments
 (0)