Skip to content

Commit ad46b9d

Browse files
Merge pull request KelvinTegelaar#1638 from PeterVive/tenant-clear-capabilities-cache
Added Invoke-RemoveTenantCapabilitiesCache for use on tenants page.
2 parents 0209e61 + 1d5b954 commit ad46b9d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using namespace System.Net
2+
3+
function Invoke-RemoveTenantCapabilitiesCache {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint,AnyTenant
7+
.ROLE
8+
Tenant.Administration.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $Request.Params.CIPPEndpoint
14+
$Headers = $Request.Headers
15+
Write-LogMessage -Headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
16+
17+
# Get the tenant identifier from query parameters
18+
$DefaultDomainName = $Request.Query.defaultDomainName
19+
if (-not $DefaultDomainName) {
20+
$body = [pscustomobject]@{'Results' = 'Missing required parameter: defaultDomainName' }
21+
$StatusCode = [HttpStatusCode]::BadRequest
22+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
23+
StatusCode = $StatusCode
24+
Body = $body
25+
})
26+
return
27+
}
28+
29+
try {
30+
# Get the CacheCapabilities table
31+
$Table = Get-CippTable -tablename 'CacheCapabilities'
32+
33+
# Find the cache entry for this tenant
34+
$Filter = "PartitionKey eq 'Capabilities' and RowKey eq '$DefaultDomainName'"
35+
$CacheEntry = Get-CIPPAzDataTableEntity @Table -Filter $Filter -Property PartitionKey, RowKey
36+
37+
if ($CacheEntry) {
38+
# Remove the cache entry
39+
Remove-AzDataTableEntity -Force @Table -Entity $CacheEntry
40+
Write-LogMessage -Headers $Headers -API $APIName -message "Removed capabilities cache for tenant $DefaultDomainName." -Sev 'Info'
41+
$body = [pscustomobject]@{'Results' = "Successfully removed capabilities cache for tenant $DefaultDomainName" }
42+
$StatusCode = [HttpStatusCode]::OK
43+
} else {
44+
Write-LogMessage -Headers $Headers -API $APIName -message "No capabilities cache found for tenant $DefaultDomainName." -Sev 'Info'
45+
$body = [pscustomobject]@{'Results' = "No capabilities cache found for tenant $DefaultDomainName" }
46+
$StatusCode = [HttpStatusCode]::OK
47+
}
48+
} catch {
49+
$ErrorMessage = Get-CippException -Exception $_
50+
Write-LogMessage -Headers $Headers -API $APIName -message "Failed to remove capabilities cache for tenant $DefaultDomainName. $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
51+
$StatusCode = [HttpStatusCode]::InternalServerError
52+
$body = [pscustomobject]@{'Results' = "Failed to remove capabilities cache: $($ErrorMessage.NormalizedError)" }
53+
}
54+
55+
# Associate values to output bindings by calling 'Push-OutputBinding'.
56+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
57+
StatusCode = $StatusCode
58+
Body = $body
59+
})
60+
}

0 commit comments

Comments
 (0)