Skip to content

Commit 288c9ef

Browse files
committed
app deployment templates
1 parent d3b92b7 commit 288c9ef

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function Invoke-ListAppDeploymentTemplates {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint,AnyTenant
5+
.ROLE
6+
Tenant.Application.Read
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+
17+
try {
18+
# Use the templates table with AppDeploymentTemplate partition key
19+
$filter = "PartitionKey eq 'AppDeploymentTemplate'"
20+
21+
$Templates = Get-CIPPAzDataTableEntity @Table -Filter $filter
22+
23+
$Body = $Templates | ForEach-Object {
24+
try {
25+
# Safely parse the JSON data - handle potential invalid JSON format
26+
$TemplateData = $null
27+
if ($_.JSON) {
28+
$TemplateData = $_.JSON | ConvertFrom-Json -ErrorAction Stop
29+
}
30+
31+
# Create a base object with properties directly from the table entity
32+
$templateObject = [PSCustomObject]@{
33+
TemplateId = $_.RowKey
34+
Timestamp = $_.Timestamp.DateTime.ToString('yyyy-MM-ddTHH:mm:ssZ')
35+
}
36+
37+
# Add all properties from the JSON data if available
38+
if ($TemplateData) {
39+
foreach ($property in $TemplateData.PSObject.Properties) {
40+
$templateObject | Add-Member -NotePropertyName $property.Name -NotePropertyValue $property.Value -Force
41+
}
42+
}
43+
44+
return $templateObject
45+
} catch {
46+
Write-LogMessage -headers $Headers -API $APIName -message "Error processing template $($_.RowKey): $($_.Exception.Message)" -Sev 'Error'
47+
return [PSCustomObject]@{
48+
TemplateId = $_.RowKey
49+
TemplateName = 'Error parsing template data'
50+
Error = $_.Exception.Message
51+
Timestamp = $_.Timestamp.DateTime.ToString('yyyy-MM-ddTHH:mm:ssZ')
52+
}
53+
}
54+
}
55+
56+
Write-LogMessage -headers $Headers -API $APIName -message "Listed App Deployment Templates: $($Body.Count) templates found" -Sev 'Info'
57+
} catch {
58+
$Body = @{
59+
Results = "Failed to list app deployment templates: $($_.Exception.Message)"
60+
}
61+
Write-LogMessage -headers $Headers -API $APIName -message "Failed to list App Deployment Templates: $($_.Exception.Message)" -Sev 'Error'
62+
}
63+
64+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
65+
StatusCode = [HttpStatusCode]::OK
66+
Body = ConvertTo-Json -Depth 10 -InputObject @($Body)
67+
})
68+
}

0 commit comments

Comments
 (0)