|
| 1 | +function Invoke-ExecDiagnosticsPresets { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + CIPP.SuperAdmin.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param ( |
| 10 | + $Request, |
| 11 | + $TriggerMetadata |
| 12 | + ) |
| 13 | + |
| 14 | + try { |
| 15 | + $Table = Get-CIPPTable -TableName 'DiagnosticsPresets' |
| 16 | + $Action = $Request.Body.action |
| 17 | + $GUID = $Request.Body.GUID |
| 18 | + |
| 19 | + if ($Action -eq 'delete') { |
| 20 | + if (-not $GUID) { |
| 21 | + return [HttpResponseContext]@{ |
| 22 | + StatusCode = [HttpStatusCode]::BadRequest |
| 23 | + Body = @{ |
| 24 | + Results = 'GUID is required for delete action' |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + Remove-AzDataTableEntity @Table -Entity @{ |
| 30 | + PartitionKey = 'Preset' |
| 31 | + RowKey = $GUID |
| 32 | + } |
| 33 | + |
| 34 | + return [HttpResponseContext]@{ |
| 35 | + StatusCode = [HttpStatusCode]::OK |
| 36 | + Body = @{ |
| 37 | + Results = 'Preset deleted successfully' |
| 38 | + } |
| 39 | + } |
| 40 | + } else { |
| 41 | + # Save or update preset |
| 42 | + $Name = $Request.Body.name |
| 43 | + $Query = $Request.Body.query |
| 44 | + |
| 45 | + if (-not $Name -or -not $Query) { |
| 46 | + return [HttpResponseContext]@{ |
| 47 | + StatusCode = [HttpStatusCode]::BadRequest |
| 48 | + Body = @{ |
| 49 | + Results = 'Name and query are required' |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + # Use provided GUID or generate new one |
| 55 | + if (-not $GUID) { |
| 56 | + $GUID = (New-Guid).Guid |
| 57 | + } |
| 58 | + |
| 59 | + # Convert query to compressed JSON for storage |
| 60 | + $QueryJson = ConvertTo-Json -InputObject @{ query = $Query } -Compress |
| 61 | + |
| 62 | + $Entity = @{ |
| 63 | + PartitionKey = 'Preset' |
| 64 | + RowKey = [string]$GUID |
| 65 | + name = [string]$Name |
| 66 | + data = [string]$QueryJson |
| 67 | + } |
| 68 | + |
| 69 | + Add-CIPPAzDataTableEntity @Table -Entity $Entity -Force |
| 70 | + |
| 71 | + return [HttpResponseContext]@{ |
| 72 | + StatusCode = [HttpStatusCode]::OK |
| 73 | + Body = @{ |
| 74 | + Results = 'Preset saved successfully' |
| 75 | + Metadata = @{ |
| 76 | + GUID = $GUID |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } catch { |
| 82 | + return [HttpResponseContext]@{ |
| 83 | + StatusCode = [HttpStatusCode]::InternalServerError |
| 84 | + Body = @{ |
| 85 | + Error = "Failed to manage diagnostics preset: $($_.Exception.Message)" |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments