-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_ldap_servers.go
359 lines (288 loc) · 12.4 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
// 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.
type ResponseLDAPServersList struct {
Size int `xml:"size"`
LDAPServers []LDAPServerItem `xml:"ldap_server"`
}
type LDAPServerItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// ResponseLDAPServers represents the structure of an individual LDAP server.
type ResponseLDAPServers struct {
Connection LDAPConnection `xml:"connection"`
MappingsForUsers LDAPMappingsForUsers `xml:"mappings_for_users"`
}
type LDAPConnection 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 LDAPAccount `xml:"account"`
OpenCloseTimeout int `xml:"open_close_timeout"`
SearchTimeout int `xml:"search_timeout"`
ReferralResponse string `xml:"referral_response"`
UseWildcards bool `xml:"use_wildcards"`
}
type LDAPAccount struct {
DistinguishedUsername string `xml:"distinguished_username"`
Password string `xml:"password"`
}
type LDAPMappingsForUsers struct {
UserMappings LDAPUserMappings `xml:"user_mappings"`
UserGroupMappings LDAPUserGroupMappings `xml:"user_group_mappings"`
UserGroupMembershipMappings LDAPGroupMembershipMappings `xml:"user_group_membership_mappings"`
}
type LDAPUserMappings 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 LDAPUserGroupMappings 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 LDAPGroupMembershipMappings 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"`
}
// 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("failed to fetch LDAP servers: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriLDAPServers, id)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server by ID: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriLDAPServers, name)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server by Name: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d/user/%s", uriLDAPServers, id, user)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server by ID and User Data Subset: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d/group/%s", uriLDAPServers, id, group)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server by ID and Group Data Subset: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d/group/%s/user/%s", uriLDAPServers, id, group, user)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server user membership in group data: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s/user/%s", uriLDAPServers, name, user)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server by name and user data subset: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s/group/%s", uriLDAPServers, name, group)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server by name and group data subset: %v", 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) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s/group/%s/user/%s", uriLDAPServers, name, group, user)
var ldapServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &ldapServer)
if err != nil {
return nil, fmt.Errorf("failed to fetch LDAP server by name and user membership in group data subset: %v", 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 *ResponseLDAPServers) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/0", uriLDAPServers) // '0' typically used for creation in APIs
// Wrap ldapServer in an anonymous struct to match the expected XML structure
requestBody := struct {
XMLName xml.Name `xml:"ldap_server"`
*ResponseLDAPServers
}{
ResponseLDAPServers: ldapServer,
}
var responseLDAPServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &responseLDAPServer)
if err != nil {
return nil, fmt.Errorf("failed to create LDAP server: %v", 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 *ResponseLDAPServers) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriLDAPServers, id)
requestBody := struct {
XMLName xml.Name `xml:"ldap_server"`
*ResponseLDAPServers
}{
ResponseLDAPServers: ldapServer,
}
var responseLDAPServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &responseLDAPServer)
if err != nil {
return nil, fmt.Errorf("failed to update LDAP server by ID: %v", 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 *ResponseLDAPServers) (*ResponseLDAPServers, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriLDAPServers, name)
requestBody := struct {
XMLName xml.Name `xml:"ldap_server"`
*ResponseLDAPServers
}{
ResponseLDAPServers: ldapServer,
}
var responseLDAPServer ResponseLDAPServers
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &responseLDAPServer)
if err != nil {
return nil, fmt.Errorf("failed to update LDAP server by name: %v", 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("failed to delete LDAP server by ID: %v", 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("failed to delete LDAP server by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}