@@ -13,21 +13,76 @@ Function Invoke-ListTransportRules {
13
13
$APIName = $Request.Params.CIPPEndpoint
14
14
$Headers = $Request.Headers
15
15
Write-LogMessage - headers $Headers - API $APIName - message ' Accessed this API' - Sev ' Debug'
16
- $TenantFilter = $request.Query.tenantFilter
16
+
17
+ # Interact with query parameters or the body of the request.
18
+ $TenantFilter = $Request.Query.tenantFilter
17
19
18
20
try {
19
- $GraphRequest = New-ExoRequest - tenantid $TenantFilter - cmdlet ' Get-TransportRule'
21
+ $Results = if ($TenantFilter -ne ' AllTenants' ) {
22
+ # Single tenant functionality
23
+ New-ExoRequest - tenantid $TenantFilter - cmdlet ' Get-TransportRule'
24
+ } else {
25
+ # AllTenants functionality
26
+ $Table = Get-CIPPTable - TableName CacheTransportRules
27
+ $PartitionKey = ' TransportRule'
28
+ $Filter = " PartitionKey eq '$PartitionKey '"
29
+ $Rows = Get-CIPPAzDataTableEntity @Table - filter $Filter | Where-Object - Property Timestamp -GT (Get-Date ).AddMinutes(-60 )
30
+ $QueueReference = ' {0}-{1}' -f $TenantFilter , $PartitionKey
31
+ $RunningQueue = Invoke-ListCippQueue | Where-Object { $_.Reference -eq $QueueReference -and $_.Status -notmatch ' Completed' -and $_.Status -notmatch ' Failed' }
32
+
33
+ # If a queue is running, we will not start a new one
34
+ if ($RunningQueue ) {
35
+ $Metadata = [PSCustomObject ]@ {
36
+ QueueMessage = ' Still loading transport rules for all tenants. Please check back in a few more minutes'
37
+ }
38
+ } elseif (! $Rows -and ! $RunningQueue ) {
39
+ # If no rows are found and no queue is running, we will start a new one
40
+ $TenantList = Get-Tenants - IncludeErrors
41
+ $Queue = New-CippQueueEntry - Name ' Transport Rules - All Tenants' - Link ' /email/transport/list-rules?tenantFilter=AllTenants' - Reference $QueueReference - TotalTasks ($TenantList | Measure-Object ).Count
42
+ $Metadata = [PSCustomObject ]@ {
43
+ QueueMessage = ' Loading transport rules for all tenants. Please check back in a few minutes'
44
+ }
45
+ $InputObject = [PSCustomObject ]@ {
46
+ OrchestratorName = ' TransportRuleOrchestrator'
47
+ QueueFunction = @ {
48
+ FunctionName = ' GetTenants'
49
+ QueueId = $Queue.RowKey
50
+ TenantParams = @ {
51
+ IncludeErrors = $true
52
+ }
53
+ DurableName = ' ListTransportRulesAllTenants'
54
+ }
55
+ SkipLog = $true
56
+ }
57
+ Start-NewOrchestration - FunctionName ' CIPPOrchestrator' - InputObject ($InputObject | ConvertTo-Json - Depth 5 - Compress) | Out-Null
58
+ } else {
59
+ # Return cached data
60
+ $Rules = $Rows
61
+ foreach ($rule in $Rules ) {
62
+ $RuleObj = $rule.TransportRule | ConvertFrom-Json
63
+ $RuleObj | Add-Member - MemberType NoteProperty - Name Tenant - Value $rule.Tenant - Force
64
+ $RuleObj
65
+ }
66
+ }
67
+ }
20
68
$StatusCode = [HttpStatusCode ]::OK
21
69
} catch {
22
70
$ErrorMessage = Get-NormalizedError - Message $_.Exception.Message
23
71
$StatusCode = [HttpStatusCode ]::Forbidden
24
- $GraphRequest = $ErrorMessage
72
+ $Body = $ErrorMessage
73
+ }
74
+
75
+ # If the body is not set by an error, we will set it here
76
+ if (! $Body ) {
77
+ $Body = [PSCustomObject ]@ {
78
+ Results = @ ($Results )
79
+ Metadata = $Metadata
80
+ }
25
81
}
26
82
27
83
# Associate values to output bindings by calling 'Push-OutputBinding'.
28
84
Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
29
85
StatusCode = $StatusCode
30
- Body = @ ( $GraphRequest )
86
+ Body = $Body
31
87
})
32
-
33
88
}
0 commit comments