-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_adue_access_group.go
182 lines (140 loc) · 6.66 KB
/
jamfproapi_adue_access_group.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
// jamfproapi_adue_access_group.go
// Jamf Pro Api - Enrollment
// api reference: https://developer.jamf.com/jamf-pro/reference/get_v3-enrollment-access-groups
// Jamf Pro API requires the structs to support a JSON data structure.
package jamfpro
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
const uriAccountDrivenUserEnrollment = "/api/v3/enrollment"
// List
// ResponseAccountDrivenUserEnrollmentAccessGroups represents the structure of the response for a list of access groups
type ResponseAccountDrivenUserEnrollmentAccessGroupsList struct {
TotalCount int `json:"totalCount"`
Results []ResourceAccountDrivenUserEnrollmentAccessGroup `json:"results"`
}
type ResponseAccountDrivenUserEnrollmentAccessGroupCreateAndUpdate struct {
ID string `json:"id"`
Href string `json:"href"`
}
// Resource
type ResourceAccountDrivenUserEnrollmentAccessGroup struct {
ID string `json:"id"`
GroupID string `json:"groupId"`
LdapServerID string `json:"ldapServerId"`
Name string `json:"name"`
SiteID string `json:"siteId"`
EnterpriseEnrollmentEnabled bool `json:"enterpriseEnrollmentEnabled"`
PersonalEnrollmentEnabled bool `json:"personalEnrollmentEnabled"`
AccountDrivenUserEnrollmentEnabled bool `json:"accountDrivenUserEnrollmentEnabled"`
RequireEula bool `json:"requireEula"`
}
// CRUD
// GetAccountDrivenUserEnrollmentAccessGroups fetches all ADUE access groups
func (c *Client) GetAccountDrivenUserEnrollmentAccessGroups(sort_filter string) (*ResponseAccountDrivenUserEnrollmentAccessGroupsList, error) {
endpoint := uriAccountDrivenUserEnrollment
resp, err := c.DoPaginatedGet(endpoint, standardPageSize, 0, sort_filter)
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "ADUE Access Group List", err)
}
var OutStruct ResponseAccountDrivenUserEnrollmentAccessGroupsList
OutStruct.TotalCount = resp.Size
for _, value := range resp.Results {
var newObj ResourceAccountDrivenUserEnrollmentAccessGroup
err := mapstructure.Decode(value, &newObj)
if err != nil {
return nil, fmt.Errorf(errMsgFailedMapstruct, "ADUE Access Group List", err)
}
OutStruct.Results = append(OutStruct.Results, newObj)
}
return &OutStruct, nil
}
// Retrieves AccountDrivenUserEnrollmentAccessGroup from provided ID & returns ResourceAccountDrivenUserEnrollmentAccessGroup
func (c *Client) GetAccountDrivenUserEnrollmentAccessGroupByID(id string) (*ResourceAccountDrivenUserEnrollmentAccessGroup, error) {
endpoint := fmt.Sprintf("%s/access-groups/%s", uriAccountDrivenUserEnrollment, id)
var ADUEGroup ResourceAccountDrivenUserEnrollmentAccessGroup
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ADUEGroup)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "ADUE Access Group", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ADUEGroup, nil
}
// GetAccountDrivenUserEnrollmentAccessGroupByName retrieves an Account Driven User Enrollment Access Group by its name
func (c *Client) GetAccountDrivenUserEnrollmentAccessGroupByName(name string) (*ResourceAccountDrivenUserEnrollmentAccessGroup, error) {
accessGroupsList, err := c.GetAccountDrivenUserEnrollmentAccessGroups("")
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "ADUE access group", err)
}
for _, group := range accessGroupsList.Results {
if group.Name == name {
return &group, nil
}
}
return nil, fmt.Errorf(errMsgFailedGetByName, "ADUE access group", name, errMsgNoName)
}
// Creates Account Driven User Enrollment Access Group from ResourceScript struct
func (c *Client) CreateAccountDrivenUserEnrollmentAccessGroup(script *ResourceAccountDrivenUserEnrollmentAccessGroup) (*ResponseAccountDrivenUserEnrollmentAccessGroupCreateAndUpdate, error) {
endpoint := uriScripts
var out ResponseAccountDrivenUserEnrollmentAccessGroupCreateAndUpdate
resp, err := c.HTTP.DoRequest("POST", endpoint, script, &out)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "ADUE access group", err)
}
if resp != nil {
defer resp.Body.Close()
}
return &out, nil
}
// UpdateAccountDrivenUserEnrollmentAccessGroupByID updates an ADUE access group by resource ID
func (c *Client) UpdateAccountDrivenUserEnrollmentAccessGroupByID(id string, groupUpdate *ResourceAccountDrivenUserEnrollmentAccessGroup) (*ResourceAccountDrivenUserEnrollmentAccessGroup, error) {
endpoint := fmt.Sprintf("%s/access-groups/%s", uriAccountDrivenUserEnrollment, id)
var out ResourceAccountDrivenUserEnrollmentAccessGroup
resp, err := c.HTTP.DoRequest("PUT", endpoint, groupUpdate, &out)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "ADUE Access Group", id, err)
}
if resp != nil {
defer resp.Body.Close()
}
return &out, nil
}
// UpdateAccountDrivenUserEnrollmentAccessGroupByName updates an ADUE access group by resource name
func (c *Client) UpdateAccountDrivenUserEnrollmentAccessGroupByName(targetName string, groupUpdate *ResourceAccountDrivenUserEnrollmentAccessGroup) (*ResourceAccountDrivenUserEnrollmentAccessGroup, error) {
target, err := c.GetAccountDrivenUserEnrollmentAccessGroupByName(targetName)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "ADUE access group", targetName, err)
}
resp, err := c.UpdateAccountDrivenUserEnrollmentAccessGroupByID(target.ID, groupUpdate)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "ADUE access group", targetName, err)
}
return resp, nil
}
// DeleteAccountDrivenUserEnrollmentAccessGroupByID deletes an ADUE access group with given id
func (c *Client) DeleteAccountDrivenUserEnrollmentAccessGroupByID(id string) error {
endpoint := fmt.Sprintf("%s/access-groups/%s", uriAccountDrivenUserEnrollment, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil || resp.StatusCode != 204 {
return fmt.Errorf(errMsgFailedDeleteByID, "ADUE access group", id, err)
}
if resp != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteAccountDrivenUserEnrollmentAccessGroupByName deletes an ADUE access group with given name, leverages GetAccountDrivenUserEnrollmentAccessGroupByName
func (c *Client) DeleteAccountDrivenUserEnrollmentAccessGroupByName(targetName string) error {
target, err := c.GetAccountDrivenUserEnrollmentAccessGroupByName(targetName)
if err != nil {
return fmt.Errorf(errMsgFailedGetByName, "ADUE access group", targetName, err)
}
err = c.DeleteAccountDrivenUserEnrollmentAccessGroupByID(target.ID)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "ADUE access group", targetName, err)
}
return nil
}