-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_packages.go
222 lines (175 loc) · 6.48 KB
/
classicapi_packages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// classicapi_packages.go
// Jamf Pro Classic Api - Packages
// api reference: https://developer.jamf.com/jamf-pro/reference/packages
// Jamf Pro Classic Api requires the structs to support an XML data structure.
/*
Shared Resources in this Endpoint:
- SharedResourceSelfServiceIcon
*/
package jamfpro
import (
"encoding/xml"
"fmt"
)
// URI for Packages in the Jamf Pro Classic API
const uriPackages = "/JSSResource/packages"
// ResponsePackagesList struct to capture the XML response for packages list
type ResponsePackagesList struct {
Size int `xml:"size"` // The size attribute
Package []PackageListItem `xml:"package"` // The package element
}
// PackageListItem struct to capture individual package items in the list
type PackageListItem struct {
ID int `xml:"id"` // The ID element
Name string `xml:"name"` // The Name element
}
// Response
type ResponsePackageCreatedAndUpdated struct {
ID int `json:"id,omitempty" xml:"id,omitempty"`
}
// ResourcePackage struct to capture the XML response for a single package detail
type ResourcePackage struct {
ID int `xml:"id"`
Name string `xml:"name"`
Category string `xml:"category,omitempty"`
Filename string `xml:"filename,omitempty"`
Info string `xml:"info,omitempty"`
Notes string `xml:"notes,omitempty"`
Priority int `xml:"priority,omitempty"`
RebootRequired bool `xml:"reboot_required"`
FillUserTemplate bool `xml:"fill_user_template"`
FillExistingUsers bool `xml:"fill_existing_users"`
BootVolumeRequired bool `xml:"boot_volume_required"`
AllowUninstalled bool `xml:"allow_uninstalled"`
OSRequirements string `xml:"os_requirements,omitempty"`
RequiredProcessor string `xml:"required_processor,omitempty"`
SwitchWithPackage string `xml:"switch_with_package,omitempty"`
InstallIfReportedAvailable bool `xml:"install_if_reported_available"`
ReinstallOption string `xml:"reinstall_option,omitempty"`
TriggeringFiles string `xml:"triggering_files,omitempty"`
SendNotification bool `xml:"send_notification"`
}
// CRUD
// GetPackages retrieves a list of packages.
func (c *Client) GetPackages() (*ResponsePackagesList, error) {
endpoint := uriPackages
var response ResponsePackagesList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGet, "package", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// GetPackageByID retrieves details of a specific package by its ID.
func (c *Client) GetPackageByID(id int) (*ResourcePackage, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriPackages, id)
var response ResourcePackage
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "package", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// GetPackageByName retrieves details of a specific package by its name.
func (c *Client) GetPackageByName(name string) (*ResourcePackage, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriPackages, name)
var response ResourcePackage
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "package", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// CreatePackage creates a new package in Jamf Pro
func (c *Client) CreatePackage(pkg ResourcePackage) (*ResponsePackageCreatedAndUpdated, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriPackages, pkg.ID)
requestBody := struct {
XMLName xml.Name `xml:"package"`
ResourcePackage
}{
ResourcePackage: pkg,
}
var response ResponsePackageCreatedAndUpdated
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "package", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// UpdatePackageByID updates an existing package by its ID on the Jamf Pro server
// and returns the response with the ID of the updated package.
func (c *Client) UpdatePackageByID(id int, pkg *ResourcePackage) (*ResponsePackageCreatedAndUpdated, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriPackages, id)
requestBody := struct {
XMLName xml.Name `xml:"package"`
*ResourcePackage
}{
ResourcePackage: pkg,
}
var response ResponsePackageCreatedAndUpdated
// Use PUT method for updating the package
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "package", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// UpdatePackageByName updates an existing package by its ID on the Jamf Pro server
// and returns the response with the ID of the updated package.
func (c *Client) UpdatePackageByName(name string, pkg *ResourcePackage) (*ResponsePackageCreatedAndUpdated, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriPackages, name)
requestBody := struct {
XMLName xml.Name `xml:"package"`
*ResourcePackage
}{
ResourcePackage: pkg,
}
var response ResponsePackageCreatedAndUpdated
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "package", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// DeletePackageByID deletes a package by its ID from the Jamf Pro server.
func (c *Client) DeletePackageByID(id int) error {
endpoint := fmt.Sprintf("%s/id/%d", uriPackages, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "package", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeletePackageByName deletes a package by its name from the Jamf Pro server.
func (c *Client) DeletePackageByName(name string) error {
endpoint := fmt.Sprintf("%s/name/%s", uriPackages, name)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "package", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}