Skip to content

Commit 3cbaf96

Browse files
committed
Optimize tenant info retrieval and trigger CPV refresh
Replaces multiple Microsoft Graph API calls with a single batch request to retrieve organization and domain information when adding a tenant. Adds logic to trigger a CPV permissions refresh for the new tenant by starting the appropriate orchestrator.
1 parent f153a18 commit 3cbaf96

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Setup/Invoke-ExecAddTenant.ps1

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,35 @@ function Invoke-ExecAddTenant {
3131
} else {
3232
# Create new tenant entry
3333
try {
34-
# Get tenant information from Microsoft Graph
34+
# Get tenant information from Microsoft Graph using bulk request
3535
$headers = @{ Authorization = "Bearer $($request.body.accessToken)" }
36-
$Organization = (Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/organization' -Headers $headers -Method GET -ContentType 'application/json' -ErrorAction Stop).value
37-
$displayName = $Organization.displayName
38-
$Domains = (Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/domains?$top=999' -Headers $headers -Method GET -ContentType 'application/json' -ErrorAction Stop).value
39-
$defaultDomainName = ($Domains | Where-Object { $_.isDefault -eq $true }).id
40-
$initialDomainName = ($Domains | Where-Object { $_.isInitial -eq $true }).id
36+
37+
$BulkRequests = @(
38+
@{
39+
id = 'organization'
40+
method = 'GET'
41+
url = '/organization?$select=id,displayName'
42+
}
43+
@{
44+
id = 'domains'
45+
method = 'GET'
46+
url = '/domains?$top=999'
47+
}
48+
)
49+
50+
$BulkBody = @{
51+
requests = $BulkRequests
52+
} | ConvertTo-Json -Depth 10
53+
54+
$BulkResponse = Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/$batch' -Headers $headers -Method POST -Body $BulkBody -ContentType 'application/json' -ErrorAction Stop
55+
56+
# Parse bulk response
57+
$OrgResponse = ($BulkResponse.responses | Where-Object { $_.id -eq 'organization' }).body.value
58+
$DomainsResponse = ($BulkResponse.responses | Where-Object { $_.id -eq 'domains' }).body.value
59+
60+
$displayName = $OrgResponse.displayName
61+
$defaultDomainName = ($DomainsResponse | Where-Object { $_.isDefault -eq $true }).id
62+
$initialDomainName = ($DomainsResponse | Where-Object { $_.isInitial -eq $true }).id
4163
} catch {
4264
Write-LogMessage -API 'Add-Tenant' -message "Failed to get information for tenant $tenantId - $($_.Exception.Message)" -Sev 'Critical'
4365
throw "Failed to get information for tenant $tenantId. Make sure the tenant is properly authenticated."
@@ -66,6 +88,27 @@ function Invoke-ExecAddTenant {
6688
Add-CIPPAzDataTableEntity @TenantsTable -Entity $NewTenant -Force | Out-Null
6789
$Results = @{'message' = "Successfully added tenant $displayName ($defaultDomainName) to the tenant list with Direct Tenant status."; 'severity' = 'success' }
6890
Write-LogMessage -tenant $defaultDomainName -tenantid $tenantId -API 'Add-Tenant' -message "Added tenant $displayName ($defaultDomainName) with Direct Tenant status." -Sev 'Info'
91+
92+
# Trigger CPV refresh to push remaining permissions to this specific tenant
93+
try {
94+
$Queue = New-CippQueueEntry -Name "Update Permissions - $displayName" -TotalTasks 1
95+
$TenantBatch = @([PSCustomObject]@{
96+
defaultDomainName = $defaultDomainName
97+
customerId = $tenantId
98+
displayName = $displayName
99+
FunctionName = 'UpdatePermissionsQueue'
100+
QueueId = $Queue.RowKey
101+
})
102+
$InputObject = [PSCustomObject]@{
103+
OrchestratorName = 'UpdatePermissionsOrchestrator'
104+
Batch = @($TenantBatch)
105+
}
106+
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
107+
Write-Information "Started permissions update orchestrator for $displayName"
108+
} catch {
109+
Write-Warning "Failed to start permissions orchestrator: $($_.Exception.Message)"
110+
}
111+
69112
}
70113
} catch {
71114
$Results = @{'message' = "Failed to add tenant: $($_.Exception.Message)"; 'state' = 'error'; 'severity' = 'error' }

0 commit comments

Comments
 (0)