-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_vpp_assignments.go
184 lines (147 loc) · 5.03 KB
/
classicapi_vpp_assignments.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
// classicapi_vpp_assignments.go
// Jamf Pro Classic Api - VPP Assignments
// api reference: https://developer.jamf.com/jamf-pro/reference/vppassignments
// Jamf Pro Classic Api requires the structs to support an XML data structure.
// classicapi_vpp_assignments.go
package jamfpro
import (
"encoding/xml"
"fmt"
)
const uriVPPAssignments = "/JSSResource/vppassignments"
// Struct for the list of VPP assignments
type ResponseVPPAssignmentsList struct {
VPPAssignments []VPPAssignment `xml:"vpp_assignment"`
}
// Struct for a single VPP assignment
type VPPAssignment struct {
ID int `xml:"id"`
VPPAdminAccountID int `xml:"vpp_admin_account_id"`
Name string `xml:"name"`
}
// Structs for the detailed VPP assignment response
type ResponseVPPAssignment struct {
General VPPAssignmentGeneral `xml:"general"`
IOSApps []VPPApp `xml:"ios_apps>ios_app"`
MacApps []VPPApp `xml:"mac_apps>mac_app"`
EBooks []VPPApp `xml:"ebooks>ebook"`
Scope VPPAssignmentScope `xml:"scope"`
}
type VPPAssignmentGeneral struct {
ID int `xml:"id"`
Name string `xml:"name"`
VPPAdminAccountID int `xml:"vpp_admin_account_id"`
VPPAdminAccountName string `xml:"vpp_admin_account_name"`
}
type VPPApp struct {
AdamID int `xml:"adam_id"`
Name string `xml:"name"`
}
// Struct for VPP assignment scope
type VPPAssignmentScope struct {
AllJSSUsers bool `xml:"all_jss_users"`
JSSUsers []VPPUser `xml:"jss_users>user"`
JSSUserGroups []VPPUserGroup `xml:"jss_user_groups>user_group"`
Limitations VPPLimitations `xml:"limitations"`
Exclusions VPPExclusions `xml:"exclusions"`
}
// Struct for VPP user
type VPPUser struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// Struct for VPP user group
type VPPUserGroup struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// Struct for VPP limitations
type VPPLimitations struct {
UserGroups []VPPUserGroup `xml:"user_groups>user_group"`
}
// Struct for VPP exclusions
type VPPExclusions struct {
JSSUsers []VPPUser `xml:"jss_users>user"`
UserGroups []VPPUserGroup `xml:"user_groups>user_group"`
JSSUserGroups []VPPUserGroup `xml:"jss_user_groups>user_group"`
}
// GetVPPAssignments fetches a list of VPP assignments
func (c *Client) GetVPPAssignments() (*ResponseVPPAssignmentsList, error) {
endpoint := uriVPPAssignments
var assignments ResponseVPPAssignmentsList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &assignments)
if err != nil {
return nil, fmt.Errorf("failed to fetch VPP assignments: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &assignments, nil
}
// GetVPPAssignmentByID fetches a VPP assignment by its ID
func (c *Client) GetVPPAssignmentByID(id int) (*ResponseVPPAssignment, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriVPPAssignments, id)
var assignment ResponseVPPAssignment
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &assignment)
if err != nil {
return nil, fmt.Errorf("failed to fetch VPP assignment by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &assignment, nil
}
// CreateVPPAssignment creates a new VPP assignment
func (c *Client) CreateVPPAssignment(assignment *ResponseVPPAssignment) error {
endpoint := fmt.Sprintf("%s/id/0", uriVPPAssignments) // '0' indicates creation
// Wrap the assignment with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"vpp_assignment"`
*ResponseVPPAssignment
}{
ResponseVPPAssignment: assignment,
}
// Create a dummy struct for the response
var handleResponse struct{}
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &handleResponse)
if err != nil {
return fmt.Errorf("failed to create VPP assignment: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// UpdateVPPAssignmentByID updates a VPP assignment by its ID
func (c *Client) UpdateVPPAssignmentByID(id int, assignment *ResponseVPPAssignment) error {
endpoint := fmt.Sprintf("%s/id/%d", uriVPPAssignments, id)
// Wrap the assignment with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"vpp_assignment"`
*ResponseVPPAssignment
}{
ResponseVPPAssignment: assignment,
}
// Create a dummy struct for the response
var handleResponse struct{}
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &handleResponse)
if err != nil {
return fmt.Errorf("failed to update VPP assignment by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteVPPAssignmentByID deletes a VPP assignment by its ID
func (c *Client) DeleteVPPAssignmentByID(id int) error {
endpoint := fmt.Sprintf("%s/id/%d", uriVPPAssignments, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete VPP assignment by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}