-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_mobile_device_groups.go
231 lines (186 loc) · 7.52 KB
/
classicapi_mobile_device_groups.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
223
224
225
226
227
228
229
230
231
// classicapi_mobile_device_enrollment_groups.go
// Jamf Pro Classic Api - Mobile Device Groups
// API reference: https://developer.jamf.com/jamf-pro/reference/mobiledevicegroups
// Jamf Pro Classic API requires the structs to support an XML data structure.
package jamfpro
import (
"encoding/xml"
"fmt"
)
const uriMobileDeviceGroups = "/JSSResource/mobiledevicegroups"
// ResponseMobileDeviceGroupsList represents the response for a list of mobile device groups.
type ResponseMobileDeviceGroupsList struct {
Size int `xml:"size"`
MobileDeviceGroup []MobileDeviceGroupItem `xml:"mobile_device_group"`
}
// MobileDeviceGroupItem represents a single mobile device group item.
type MobileDeviceGroupItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
IsSmart bool `xml:"is_smart"`
}
// ResponseMobileDeviceGroup represents the response for a single mobile device group.
type ResponseMobileDeviceGroup struct {
ID int `xml:"id"`
Name string `xml:"name"`
IsSmart bool `xml:"is_smart"`
Criteria []MobileDeviceGroupCriteriaItem `xml:"criteria>criterion,omitempty"`
Site MobileDeviceGroupSite `xml:"site"`
MobileDevices []MobileDeviceGroupDeviceItem `xml:"mobile_devices>mobile_device,omitempty"`
MobileDeviceAdditions []MobileDeviceGroupDeviceItem `xml:"mobile_device_additions>mobile_device,omitempty"`
MobileDeviceDeletions []MobileDeviceGroupDeviceItem `xml:"mobile_device_deletions>mobile_device,omitempty"`
}
// MobileDeviceGroupCriteriaItem represents a single criterion within a mobile device group.
type MobileDeviceGroupCriteriaItem struct {
Name string `xml:"name"`
Priority int `xml:"priority"`
AndOr string `xml:"and_or"`
SearchType string `xml:"search_type"`
Value string `xml:"value"`
OpeningParen bool `xml:"opening_paren,omitempty"`
ClosingParen bool `xml:"closing_paren,omitempty"`
}
// MobileDeviceGroupSite represents the site information for a mobile device group.
type MobileDeviceGroupSite struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// MobileDeviceGroupDeviceItem represents a single mobile device within a group.
type MobileDeviceGroupDeviceItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
MacAddress string `xml:"mac_address,omitempty"`
UDID string `xml:"udid"`
WifiMacAddress string `xml:"wifi_mac_address,omitempty"`
SerialNumber string `xml:"serial_number,omitempty"`
}
// GetMobileDeviceGroups retrieves a serialized list of mobile device groups.
func (c *Client) GetMobileDeviceGroups() (*ResponseMobileDeviceGroupsList, error) {
endpoint := uriMobileDeviceGroups
var groups ResponseMobileDeviceGroupsList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &groups)
if err != nil {
return nil, fmt.Errorf("failed to fetch mobile device groups: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &groups, nil
}
// GetMobileDeviceGroupsByID retrieves a single mobile device group by its ID.
func (c *Client) GetMobileDeviceGroupsByID(id int) (*ResponseMobileDeviceGroup, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriMobileDeviceGroups, id)
var group ResponseMobileDeviceGroup
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &group)
if err != nil {
return nil, fmt.Errorf("failed to fetch mobile device group by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &group, nil
}
// GetMobileDeviceGroupsByName retrieves a single mobile device group by its name.
func (c *Client) GetMobileDeviceGroupsByName(name string) (*ResponseMobileDeviceGroup, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriMobileDeviceGroups, name)
var group ResponseMobileDeviceGroup
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &group)
if err != nil {
return nil, fmt.Errorf("failed to fetch mobile device group by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &group, nil
}
// CreateMobileDeviceGroup creates a new mobile device group on the Jamf Pro server.
func (c *Client) CreateMobileDeviceGroup(group *ResponseMobileDeviceGroup) (*ResponseMobileDeviceGroup, error) {
endpoint := fmt.Sprintf("%s/id/0", uriMobileDeviceGroups)
// Set default values for site if not included within request
if group.Site.ID == 0 && group.Site.Name == "" {
group.Site = MobileDeviceGroupSite{
ID: -1,
Name: "None",
}
}
// Wrap the group with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"mobile_device_group"`
*ResponseMobileDeviceGroup
}{
ResponseMobileDeviceGroup: group,
}
var responseGroup ResponseMobileDeviceGroup
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &responseGroup)
if err != nil {
return nil, fmt.Errorf("failed to create mobile device group: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseGroup, nil
}
// UpdateMobileDeviceGroupByID updates a mobile device group by its ID.
func (c *Client) UpdateMobileDeviceGroupByID(id int, group *ResponseMobileDeviceGroup) (*ResponseMobileDeviceGroup, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriMobileDeviceGroups, id)
// Wrap the group with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"mobile_device_group"`
*ResponseMobileDeviceGroup
}{
ResponseMobileDeviceGroup: group,
}
var updatedGroup ResponseMobileDeviceGroup
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &updatedGroup)
if err != nil {
return nil, fmt.Errorf("failed to update mobile device group by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedGroup, nil
}
// UpdateMobileDeviceGroupByName updates a mobile device group by its name.
func (c *Client) UpdateMobileDeviceGroupByName(name string, group *ResponseMobileDeviceGroup) (*ResponseMobileDeviceGroup, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriMobileDeviceGroups, name)
// Wrap the group with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"mobile_device_group"`
*ResponseMobileDeviceGroup
}{
ResponseMobileDeviceGroup: group,
}
var updatedGroup ResponseMobileDeviceGroup
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &updatedGroup)
if err != nil {
return nil, fmt.Errorf("failed to update mobile device group by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedGroup, nil
}
// DeleteMobileDeviceGroupByID deletes a mobile device group by its ID.
func (c *Client) DeleteMobileDeviceGroupByID(id int) error {
endpoint := fmt.Sprintf("%s/id/%d", uriMobileDeviceGroups, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete mobile device group by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteMobileDeviceGroupByName deletes a mobile device group by its name.
func (c *Client) DeleteMobileDeviceGroupByName(name string) error {
endpoint := fmt.Sprintf("%s/name/%s", uriMobileDeviceGroups, name)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete mobile device group by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}