|
| 1 | +function Invoke-ExecAppDeploymentTemplate { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint,AnyTenant |
| 5 | + .ROLE |
| 6 | + Tenant.Application.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + $Headers = $Request.Headers |
| 13 | + Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug' |
| 14 | + |
| 15 | + $Table = Get-CIPPTable -TableName 'templates' |
| 16 | + $User = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Request.Headers.'x-ms-client-principal')) | ConvertFrom-Json |
| 17 | + |
| 18 | + $Action = $Request.Query.Action ?? $Request.Body.Action |
| 19 | + |
| 20 | + switch ($Action) { |
| 21 | + 'Save' { |
| 22 | + try { |
| 23 | + $GUID = $Request.Body.TemplateId ?? (New-Guid).GUID |
| 24 | + |
| 25 | + # Create structured object for the template |
| 26 | + $templateObject = $Request.Body | Select-Object -Property * -ExcludeProperty Action, TemplateId |
| 27 | + |
| 28 | + # Add additional metadata |
| 29 | + $templateObject | Add-Member -NotePropertyName 'GUID' -NotePropertyValue $GUID |
| 30 | + $templateObject | Add-Member -NotePropertyName 'CreatedBy' -NotePropertyValue ($User.UserDetails ?? 'CIPP-API') |
| 31 | + $templateObject | Add-Member -NotePropertyName 'CreatedOn' -NotePropertyValue (Get-Date).ToString('o') |
| 32 | + |
| 33 | + # If updating an existing template, add UpdatedBy and UpdatedOn |
| 34 | + if ($Request.Body.TemplateId) { |
| 35 | + $templateObject | Add-Member -NotePropertyName 'UpdatedBy' -NotePropertyValue ($User.UserDetails ?? 'CIPP-API') |
| 36 | + $templateObject | Add-Member -NotePropertyName 'UpdatedOn' -NotePropertyValue (Get-Date).ToString('o') |
| 37 | + } |
| 38 | + |
| 39 | + # Convert to JSON, preserving the original structure |
| 40 | + $templateJson = $templateObject | ConvertTo-Json -Depth 10 -Compress |
| 41 | + |
| 42 | + # Add to templates table with AppDeploymentTemplate partition key |
| 43 | + $Table.Force = $true |
| 44 | + Add-CIPPAzDataTableEntity @Table -Entity @{ |
| 45 | + JSON = [string]$templateJson |
| 46 | + RowKey = "$GUID" |
| 47 | + PartitionKey = 'AppDeploymentTemplate' |
| 48 | + } |
| 49 | + |
| 50 | + # Return a proper array with ONE element containing the TemplateId |
| 51 | + $Body = @( |
| 52 | + [PSCustomObject]@{ |
| 53 | + 'Results' = 'Template Saved' |
| 54 | + 'Metadata' = @{ |
| 55 | + 'TemplateName' = $Request.Body.TemplateName |
| 56 | + 'TemplateId' = $GUID |
| 57 | + } |
| 58 | + } |
| 59 | + ) |
| 60 | + |
| 61 | + Write-LogMessage -headers $Request.Headers -API $APIName -message "App Deployment Template Saved: $($Request.Body.TemplateName)" -Sev 'Info' |
| 62 | + } catch { |
| 63 | + $Body = @{ |
| 64 | + 'Results' = $_.Exception.Message |
| 65 | + } |
| 66 | + Write-LogMessage -headers $Request.Headers -API $APIName -message "App Deployment Template Save failed: $($_.Exception.Message)" -Sev 'Error' |
| 67 | + } |
| 68 | + } |
| 69 | + 'Delete' { |
| 70 | + try { |
| 71 | + $TemplateId = $Request.Body.TemplateId |
| 72 | + |
| 73 | + # Get the template to delete |
| 74 | + $Template = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'AppDeploymentTemplate' and RowKey eq '$TemplateId'" |
| 75 | + |
| 76 | + if ($Template) { |
| 77 | + $TemplateData = $Template.JSON | ConvertFrom-Json |
| 78 | + $TemplateName = $TemplateData.TemplateName |
| 79 | + |
| 80 | + # Remove the template |
| 81 | + $null = Remove-AzDataTableEntity @Table -Entity $Template -Force |
| 82 | + |
| 83 | + $Body = @{ |
| 84 | + 'Results' = "Successfully deleted template '$TemplateName'" |
| 85 | + } |
| 86 | + Write-LogMessage -headers $Request.Headers -API $APIName -message "App Deployment Template deleted: $TemplateName" -Sev 'Info' |
| 87 | + } else { |
| 88 | + $Body = @{ |
| 89 | + 'Results' = 'No template found with the provided ID' |
| 90 | + } |
| 91 | + } |
| 92 | + } catch { |
| 93 | + $Body = @{ |
| 94 | + 'Results' = "Failed to delete template: $($_.Exception.Message)" |
| 95 | + } |
| 96 | + Write-LogMessage -headers $Request.Headers -API $APIName -message "App Deployment Template Delete failed: $($_.Exception.Message)" -Sev 'Error' |
| 97 | + } |
| 98 | + } |
| 99 | + 'Get' { |
| 100 | + # Check if TemplateId is provided to filter results |
| 101 | + $filter = "PartitionKey eq 'AppDeploymentTemplate'" |
| 102 | + if ($Request.Query.TemplateId) { |
| 103 | + $templateId = $Request.Query.TemplateId |
| 104 | + $filter = "PartitionKey eq 'AppDeploymentTemplate' and RowKey eq '$templateId'" |
| 105 | + Write-LogMessage -headers $Request.Headers -API $APIName -message "Retrieved specific template: $templateId" -Sev 'Info' |
| 106 | + } |
| 107 | + |
| 108 | + $Templates = Get-CIPPAzDataTableEntity @Table -Filter $filter |
| 109 | + |
| 110 | + $Body = $Templates | ForEach-Object { |
| 111 | + # Parse the JSON |
| 112 | + $templateData = $_.JSON | ConvertFrom-Json |
| 113 | + |
| 114 | + # Create output object preserving original structure |
| 115 | + $outputObject = $templateData | Select-Object -Property * |
| 116 | + |
| 117 | + # Add the TemplateId (RowKey) to the output |
| 118 | + $outputObject | Add-Member -NotePropertyName 'TemplateId' -NotePropertyValue $_.RowKey -Force |
| 119 | + |
| 120 | + # Add timestamp from the table entity |
| 121 | + $outputObject | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $_.Timestamp.DateTime.ToString('yyyy-MM-ddTHH:mm:ssZ') -Force |
| 122 | + |
| 123 | + return $outputObject |
| 124 | + } |
| 125 | + } |
| 126 | + default { |
| 127 | + # Default action - list all templates |
| 128 | + $filter = "PartitionKey eq 'AppDeploymentTemplate'" |
| 129 | + |
| 130 | + $Templates = Get-CIPPAzDataTableEntity @Table -Filter $filter |
| 131 | + |
| 132 | + $Body = $Templates | ForEach-Object { |
| 133 | + # Parse the JSON |
| 134 | + $templateData = $_.JSON | ConvertFrom-Json |
| 135 | + |
| 136 | + # Create output object preserving original structure |
| 137 | + $outputObject = $templateData | Select-Object -Property * |
| 138 | + |
| 139 | + # Add the TemplateId (RowKey) to the output |
| 140 | + $outputObject | Add-Member -NotePropertyName 'TemplateId' -NotePropertyValue $_.RowKey -Force |
| 141 | + |
| 142 | + # Add timestamp from the table entity |
| 143 | + $outputObject | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $_.Timestamp.DateTime.ToString('yyyy-MM-ddTHH:mm:ssZ') -Force |
| 144 | + |
| 145 | + return $outputObject |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 151 | + StatusCode = [HttpStatusCode]::OK |
| 152 | + Body = ConvertTo-Json -Depth 10 -InputObject @($Body) |
| 153 | + }) |
| 154 | +} |
0 commit comments