Skip to content

Commit 281fb16

Browse files
DrDrraeCalebAlbers
andauthored
Add support for creating, updating, and removing related items (#88)
* Add support for creating, updating, and removing related items * faster processing and cleaner method for nulling the output * Little cleanup of [CmdletBinding()] * Update params to be idiomatic The other functions all use $id, so updated $related_items_id to match Co-authored-by: Caleb Albers <[email protected]>
1 parent 3601ba8 commit 281fb16

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

ITGlueAPI/ITGlueAPI.psd1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ NestedModules = 'Internal/BaseURI.ps1',
9797
'Resources/Passwords.ps1',
9898
'Resources/Platforms.ps1',
9999
'Resources/Regions.ps1',
100+
'Resources/RelatedItems.ps1',
100101
'Resources/UserMetrics.ps1',
101102
'Resources/Users.ps1'
102103

@@ -205,6 +206,10 @@ FunctionsToExport = 'Add-ITGlueAPIKey',
205206

206207
'Get-ITGlueRegions',
207208

209+
'New-ITGlueRelatedItems',
210+
'Set-ITGlueRelatedItems',
211+
'Remove-ITGlueRelatedItems',
212+
208213
'Get-ITGlueUserMetrics',
209214

210215
'Get-ITGlueUsers',

ITGlueAPI/Resources/RelatedItems.ps1

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
function New-ITGlueRelatedItems {
2+
[CmdletBinding()]
3+
Param (
4+
[Parameter(Mandatory = $true)]
5+
[ValidateSet( 'checklists', 'checklist_templates', 'configurations', 'contacts', 'documents', `
6+
'domains', 'locations', 'passwords', 'ssl_certificates', 'flexible_assets', 'tickets')]
7+
[string]$resource_type,
8+
9+
[Parameter(Mandatory = $true)]
10+
[int64]$resource_id,
11+
12+
[Parameter(Mandatory = $true)]
13+
$data
14+
)
15+
16+
$resource_uri = ('/{0}/{1}/relationships/related_items' -f $resource_type, $resource_id)
17+
18+
$body = @{}
19+
20+
$body += @{'data'= $data}
21+
22+
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
23+
24+
try {
25+
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
26+
$rest_output = Invoke-RestMethod -method 'POST' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
27+
-body $body -ErrorAction Stop -ErrorVariable $web_error
28+
} catch {
29+
Write-Error $_
30+
} finally {
31+
[void] $ITGlue_Headers.Remove('x-api-key') # Quietly clean up scope so the API key doesn't persist
32+
}
33+
34+
$data = @{}
35+
$data = $rest_output
36+
return $data
37+
}
38+
39+
function Set-ITGlueRelatedItems {
40+
[CmdletBinding()]
41+
Param (
42+
[Parameter(Mandatory = $true)]
43+
[ValidateSet( 'checklists', 'checklist_templates', 'configurations', 'contacts', 'documents', `
44+
'domains', 'locations', 'passwords', 'ssl_certificates', 'flexible_assets', 'tickets')]
45+
[string]$resource_type,
46+
47+
[Parameter(Mandatory = $true)]
48+
[int64]$resource_id,
49+
50+
[Parameter(Mandatory = $true)]
51+
[int64]$id,
52+
53+
[Parameter(Mandatory = $true)]
54+
$data
55+
56+
)
57+
58+
$resource_uri = ('/{0}/{1}/relationships/related_items/{2}' -f $resource_type, $resource_id, $id)
59+
60+
$body = @{}
61+
62+
$body += @{'data' = $data}
63+
64+
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
65+
66+
try {
67+
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
68+
$rest_output = Invoke-RestMethod -method 'PATCH' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
69+
-body $body -ErrorAction Stop -ErrorVariable $web_error
70+
} catch {
71+
Write-Error $_
72+
} finally {
73+
[void] $ITGlue_Headers.Remove('x-api-key') # Quietly clean up scope so the API key doesn't persist
74+
}
75+
76+
$data = @{}
77+
$data = $rest_output
78+
return $data
79+
}
80+
81+
function Remove-ITGlueRelatedItems {
82+
[CmdletBinding()]
83+
Param (
84+
[Parameter(Mandatory = $true)]
85+
[ValidateSet( 'checklists', 'checklist_templates', 'configurations', 'contacts', 'documents', `
86+
'domains', 'locations', 'passwords', 'ssl_certificates', 'flexible_assets', 'tickets')]
87+
[string]$resource_type,
88+
89+
[Parameter(Mandatory = $true)]
90+
[int64]$resource_id,
91+
92+
[Parameter(Mandatory = $true)]
93+
$data
94+
)
95+
96+
$resource_uri = ('/{0}/{1}/relationships/related_items' -f $resource_type, $resource_id)
97+
98+
$body = @{}
99+
100+
$body += @{'data' = $data}
101+
102+
$body = ConvertTo-Json -InputObject $body -Depth $ITGlue_JSON_Conversion_Depth
103+
104+
try {
105+
$ITGlue_Headers.Add('x-api-key', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'N/A', $ITGlue_API_Key).GetNetworkCredential().Password)
106+
$rest_output = Invoke-RestMethod -method 'DELETE' -uri ($ITGlue_Base_URI + $resource_uri) -headers $ITGlue_Headers `
107+
-body $body -ErrorAction Stop -ErrorVariable $web_error
108+
} catch {
109+
Write-Error $_
110+
} finally {
111+
[void] $ITGlue_Headers.Remove('x-api-key') # Quietly clean up scope so the API key doesn't persist
112+
}
113+
114+
$data = @{}
115+
$data = $rest_output
116+
return $data
117+
}

0 commit comments

Comments
 (0)