-
Notifications
You must be signed in to change notification settings - Fork 8
/
mobileGroups.go
143 lines (115 loc) · 4.19 KB
/
mobileGroups.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
package jamf
import (
"fmt"
"github.com/bradfitz/slice"
)
const uriMobileDeviceGroups = "/JSSResource/mobiledevicegroups"
type MobileDeviceGroupsResponse struct {
Size int `xml:"size"`
Results []MobileDeviceGroupListResponse `xml:"mobile_device_group"`
}
type MobileDeviceGroupListResponse struct {
ID int `xml:"id,omitempty"`
Name string `xml:"name,omitempty"`
IsSmart bool `xml:"is_smart,omitempty"`
}
type MobileDeviceGroup struct {
ID int `xml:"id"`
Name string `xml:"name"`
IsSmart bool `xml:"is_smart"`
Site Site `xml:"site"`
Criteria []MobileDeviceGroupCriterion `xml:"criteria>criterion"`
CriteriaSize int `xml:"criteria>size"`
Devices []MobileDeviceGroupDeviceEntry `xml:"mobile_devices>mobile_device"`
DeviceSize int `xml:"mobile_devices>size"`
}
type MobileDeviceGroupRequest struct {
Name string `xml:"name"`
IsSmart bool `xml:"is_smart"`
Site Site `xml:"site"`
Criteria []MobileDeviceGroupCriterion `xml:"criteria>criterion"`
Devices []MobileDeviceGroupDeviceEntry `xml:"mobile_devices>mobile_device,omitempty"`
}
type MobileDeviceGroupDeviceEntry struct {
ID int `json:"id,omitempty" xml:"id,omitempty"`
Name string `json:"name,omitempty" xml:"name,omitempty"`
SerialNumber string `json:"serial_number,omitempty" xml:"serial_number,omitempty"`
}
type MobileDeviceGroupCriterion struct {
Name string `xml:"name"`
Priority int `xml:"priority"`
AndOr DeviceGroupAndOr `xml:"and_or"`
SearchType string `xml:"search_type"`
SearchValue string `xml:"value"`
OpeningParen bool `xml:"opening_paren"`
ClosingParen bool `xml:"closing_paren"`
}
func (c *Client) GetMobileDeviceGroupIdByName(name string) (int, error) {
var id int
d, err := c.GetMobileDeviceGroups()
if err != nil {
return -1, err
}
for _, v := range d.Results {
if v.Name == name {
id = v.ID
break
}
}
return id, err
}
func (c *Client) GetMobileDeviceGroupByName(name string) (*MobileDeviceGroup, error) {
id, err := c.GetMobileDeviceGroupIdByName(name)
if err != nil {
return nil, err
}
return c.GetMobileDeviceGroup(id)
}
func (c *Client) GetMobileDeviceGroup(id int) (*MobileDeviceGroup, error) {
var out *MobileDeviceGroup
uri := fmt.Sprintf("%s/id/%v", uriMobileDeviceGroups, id)
err := c.DoRequest("GET", uri, nil, nil, &out)
return out, err
}
func (c *Client) GetMobileDeviceGroups() (*MobileDeviceGroupsResponse, error) {
out := &MobileDeviceGroupsResponse{}
err := c.DoRequest("GET", uriMobileDeviceGroups, nil, nil, out)
return out, err
}
func (c *Client) CreateMobileDeviceGroup(d *MobileDeviceGroupRequest) (int, error) {
group := &MobileDeviceGroup{}
d.Criteria = validateMobileDeviceGroupCriteriaOrder(d.Criteria)
reqBody := &struct {
*MobileDeviceGroupRequest
XMLName struct{} `xml:"mobile_device_group"`
}{
MobileDeviceGroupRequest: d,
}
err := c.DoRequest("POST", fmt.Sprintf("%s/id/0", uriMobileDeviceGroups), reqBody, nil, group)
return group.ID, err
}
func (c *Client) UpdateMobileDeviceGroup(d *MobileDeviceGroup) (int, error) {
group := &MobileDeviceGroup{}
d.Criteria = validateMobileDeviceGroupCriteriaOrder(d.Criteria)
uri := fmt.Sprintf("%s/id/%v", uriMobileDeviceGroups, d.ID)
reqBody := &struct {
*MobileDeviceGroup
XMLName struct{} `xml:"mobile_device_group"`
}{
MobileDeviceGroup: d,
}
err := c.DoRequest("PUT", uri, reqBody, nil, group)
return group.ID, err
}
func (c *Client) DeleteMobileDeviceGroup(id int) (int, error) {
group := &MobileDeviceGroup{}
uri := fmt.Sprintf("%s/id/%v", uriMobileDeviceGroups, id)
err := c.DoRequest("DELETE", uri, nil, nil, group)
return group.ID, err
}
func validateMobileDeviceGroupCriteriaOrder(criteria []MobileDeviceGroupCriterion) []MobileDeviceGroupCriterion {
slice.Sort(criteria[:], func(i, j int) bool {
return criteria[i].Priority < criteria[j].Priority
})
return criteria
}