-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_ldap_servers.go
371 lines (293 loc) · 12.7 KB
/
classicapi_ldap_servers.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// classicapi_ldap_servers.go
// Jamf Pro Classic Api - LDAP Servers
// api reference: https://developer.jamf.com/jamf-pro/reference/ldapservers
// Classic API requires the structs to support an XML data structure.
package jamfpro
import (
"encoding/xml"
"fmt"
)
const uriLDAPServers = "/JSSResource/ldapservers"
// ResponseLDAPServersList represents the response structure for a list of LDAP servers.
// List
type ResponseLDAPServersList struct {
Size int `xml:"size"`
LDAPServers []LDAPServersListItem `xml:"ldap_server"`
}
type LDAPServersListItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// Resource
// ResourceLDAPServers represents the structure of an individual LDAP server.
type ResourceLDAPServers struct {
Connection LDAPServerSubsetConnection `xml:"connection"`
MappingsForUsers LDAPServerContainerMapping `xml:"mappings_for_users"`
}
// Subsets & Containers
// Connection
type LDAPServerSubsetConnection struct {
ID int `xml:"id"`
Name string `xml:"name"`
Hostname string `xml:"hostname"`
ServerType string `xml:"server_type"`
Port int `xml:"port"`
UseSSL bool `xml:"use_ssl"`
AuthenticationType string `xml:"authentication_type"`
Account LDAPServerSubsetConnectionAccount `xml:"account"`
OpenCloseTimeout int `xml:"open_close_timeout"`
SearchTimeout int `xml:"search_timeout"`
ReferralResponse string `xml:"referral_response"`
UseWildcards bool `xml:"use_wildcards"`
}
type LDAPServerSubsetConnectionAccount struct {
DistinguishedUsername string `xml:"distinguished_username"`
Password string `xml:"password"`
}
// MappingsForUsers
type LDAPServerContainerMapping struct {
UserMappings LDAPServerSubsetMappingUsers `xml:"user_mappings"`
UserGroupMappings LDAPServerSubsetMappingUserGroups `xml:"user_group_mappings"`
UserGroupMembershipMappings LDAPServerSubsetMappingUserGroupMemberships `xml:"user_group_membership_mappings"`
}
type LDAPServerSubsetMappingUsers struct {
MapObjectClassToAnyOrAll string `xml:"map_object_class_to_any_or_all"`
ObjectClasses string `xml:"object_classes"`
SearchBase string `xml:"search_base"`
SearchScope string `xml:"search_scope"`
MapUserID string `xml:"map_user_id"`
MapUsername string `xml:"map_username"`
MapRealName string `xml:"map_realname"`
MapEmailAddress string `xml:"map_email_address"`
AppendToEmailResults string `xml:"append_to_email_results"`
MapDepartment string `xml:"map_department"`
MapBuilding string `xml:"map_building"`
MapRoom string `xml:"map_room"`
MapTelephone string `xml:"map_telephone"`
MapPosition string `xml:"map_position"`
MapUserUUID string `xml:"map_user_uuid"`
}
type LDAPServerSubsetMappingUserGroups struct {
MapObjectClassToAnyOrAll string `xml:"map_object_class_to_any_or_all"`
ObjectClasses string `xml:"object_classes"`
SearchBase string `xml:"search_base"`
SearchScope string `xml:"search_scope"`
MapGroupID string `xml:"map_group_id"`
MapGroupName string `xml:"map_group_name"`
MapGroupUUID string `xml:"map_group_uuid"`
}
type LDAPServerSubsetMappingUserGroupMemberships struct {
UserGroupMembershipStoredIn string `xml:"user_group_membership_stored_in"`
MapGroupMembershipToUserField string `xml:"map_group_membership_to_user_field"`
AppendToUsername string `xml:"append_to_username"`
UseDN bool `xml:"use_dn"`
RecursiveLookups bool `xml:"recursive_lookups"`
MapUserMembershipToGroupField bool `xml:"map_user_membership_to_group_field"`
MapUserMembershipUseDN bool `xml:"map_user_membership_use_dn"`
MapObjectClassToAnyOrAll string `xml:"map_object_class_to_any_or_all"`
ObjectClasses string `xml:"object_classes"`
SearchBase string `xml:"search_base"`
SearchScope string `xml:"search_scope"`
Username string `xml:"username"`
GroupID string `xml:"group_id"`
UserGroupMembershipUseLDAPCompare bool `xml:"user_group_membership_use_ldap_compare"`
}
// CRUD
// GetLDAPServers retrieves a serialized list of LDAP servers.
func (c *Client) GetLDAPServers() (*ResponseLDAPServersList, error) {
endpoint := uriLDAPServers
var ldapServers ResponseLDAPServersList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServers)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGet, "ldap servers", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServers, nil
}
// GetLDAPServerByID retrieves the details of a specific LDAP server by its ID.
func (c *Client) GetLDAPServerByID(id int) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriLDAPServers, id)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "ldap server", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// GetLDAPServerByName retrieves the details of a specific LDAP server by its name.
func (c *Client) GetLDAPServerByName(name string) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriLDAPServers, name)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "ldap server", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// GetLDAPServerByIDAndUserDataSubset retrieves information about matching users for a specific LDAP server by its ID.
func (c *Client) GetLDAPServerByIDAndUserDataSubset(id int, user string) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d/user/%s", uriLDAPServers, id, user)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "ldap server and user data", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// GetLDAPServerByIDAndGroupDataSubset retrieves information about matching groups for a specific LDAP server by its ID.
func (c *Client) GetLDAPServerByIDAndGroupDataSubset(id int, group string) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d/group/%s", uriLDAPServers, id, group)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "ldap server and group data", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// GetLDAPServerByIDAndUserMembershipInGroupDataSubset retrieves information about user membership in a group for an LDAP server specified by its ID.
func (c *Client) GetLDAPServerByIDAndUserMembershipInGroupDataSubset(id int, group, user string) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d/group/%s/user/%s", uriLDAPServers, id, group, user)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "ldap server and user membership", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// GetLDAPServerByNameAndUserDataSubset retrieves information about matching users for a specific LDAP server specified by its name.
func (c *Client) GetLDAPServerByNameAndUserDataSubset(name, user string) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s/user/%s", uriLDAPServers, name, user)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "ldap server and user data", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// GetLDAPServerByNameAndGroupDataSubset retrieves information about groups for a specific LDAP server specified by its name.
func (c *Client) GetLDAPServerByNameAndGroupDataSubset(name, group string) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s/group/%s", uriLDAPServers, name, group)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "ldap server and group data", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// GetLDAPServerByNameAndUserMembershipInGroupDataSubset retrieves information about user membership in a group for a specific LDAP server by its name.
func (c *Client) GetLDAPServerByNameAndUserMembershipInGroupDataSubset(name, group, user string) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s/group/%s/user/%s", uriLDAPServers, name, group, user)
var ldapServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "ldap server and user membership data", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &ldapServer, nil
}
// CreateLDAPServer creates a new LDAP server in Jamf Pro.
func (c *Client) CreateLDAPServer(ldapServer *ResourceLDAPServers) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/0", uriLDAPServers)
requestBody := struct {
XMLName xml.Name `xml:"ldap_server"`
*ResourceLDAPServers
}{
ResourceLDAPServers: ldapServer,
}
var responseLDAPServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &responseLDAPServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "ldap server", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseLDAPServer, nil
}
// UpdateLDAPServerByID updates an existing LDAP server identified by its ID.
func (c *Client) UpdateLDAPServerByID(id int, ldapServer *ResourceLDAPServers) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriLDAPServers, id)
requestBody := struct {
XMLName xml.Name `xml:"ldap_server"`
*ResourceLDAPServers
}{
ResourceLDAPServers: ldapServer,
}
var responseLDAPServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &responseLDAPServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "ldap server", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseLDAPServer, nil
}
// UpdateLDAPServerByName updates an existing LDAP server identified by its name.
func (c *Client) UpdateLDAPServerByName(name string, ldapServer *ResourceLDAPServers) (*ResourceLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriLDAPServers, name)
requestBody := struct {
XMLName xml.Name `xml:"ldap_server"`
*ResourceLDAPServers
}{
ResourceLDAPServers: ldapServer,
}
var responseLDAPServer ResourceLDAPServers
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &responseLDAPServer)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "ldap server", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseLDAPServer, nil
}
// DeleteLDAPServerByID deletes an LDAP server identified by its ID.
func (c *Client) DeleteLDAPServerByID(id int) error {
endpoint := fmt.Sprintf("%s/id/%d", uriLDAPServers, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "ldap server", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteLDAPServerByName deletes an LDAP server identified by its name.
func (c *Client) DeleteLDAPServerByName(name string) error {
endpoint := fmt.Sprintf("%s/name/%s", uriLDAPServers, name)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "ldap server", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}