-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_accounts.go
260 lines (206 loc) · 8.06 KB
/
classicapi_accounts.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
// classicapi_accounts.go
// Jamf Pro Classic Api - Accounts
// api reference: https://developer.jamf.com/jamf-pro/reference/accounts
// Classic API requires the structs to support an XML data structure.
/*
Shared Resources in this Endpoint:
- SharedResourceSite
*/
package jamfpro
import (
"fmt"
)
const uriAPIAccounts = "/JSSResource/accounts"
// List
type ResponseAccountsList struct {
Users []AccountsListSubsetUsers `xml:"users>user,omitempty"`
Groups []AccountsListSubsetGroups `xml:"groups>group,omitempty"`
}
// Resource
type ResourceAccount struct {
ID int `json:"id,omitempty" xml:"id,omitempty"`
Name string `json:"name,omitempty" xml:"name,omitempty"`
DirectoryUser bool `json:"directory_user,omitempty" xml:"directory_user,omitempty"`
FullName string `json:"full_name,omitempty" xml:"full_name,omitempty"`
Email string `json:"email,omitempty" xml:"email,omitempty"`
EmailAddress string `json:"email_address,omitempty" xml:"email_address,omitempty"`
Enabled string `json:"enabled,omitempty" xml:"enabled,omitempty"`
LdapServer AccountSubsetLdapServer `json:"ldap_server,omitempty" xml:"ldap_server,omitempty"`
ForcePasswordChange bool `json:"force_password_change,omitempty" xml:"force_password_change,omitempty"`
AccessLevel string `json:"access_level,omitempty" xml:"access_level,omitempty"`
Password string `json:"password,omitempty" xml:"password,omitempty"`
PrivilegeSet string `json:"privilege_set,omitempty" xml:"privilege_set,omitempty"`
Site SharedResourceSite `json:"site,omitempty" xml:"site,omitempty"`
Privileges AccountSubsetPrivileges `json:"privileges,omitempty" xml:"privileges,omitempty"`
Groups []AccountsListSubsetGroups `json:"groups,omitempty" xml:"groups>group"`
}
// Responses
type ResponseAccountCreatedAndUpdated struct {
ID int `json:"id,omitempty" xml:"id,omitempty"`
}
// Subsets
type AccountsListSubsetUsers struct {
ID int `xml:"id,omitempty"`
Name string `xml:"name,omitempty"`
}
type AccountsListSubsetGroups struct {
ID int `json:"id,omitempty" xml:"id,omitempty"`
Name string `json:"name,omitempty" xml:"name,omitempty"`
Privileges AccountSubsetPrivileges `json:"privileges,omitempty" xml:"privileges,omitempty"`
}
type AccountSubsetLdapServer struct {
ID int `json:"id,omitempty" xml:"id,omitempty"`
Name string `json:"name,omitempty" xml:"name,omitempty"`
}
type AccountSubsetPrivileges struct {
JSSObjects []string `json:"jss_objects,omitempty" xml:"jss_objects>privilege,omitempty"`
JSSSettings []string `json:"jss_settings,omitempty" xml:"jss_settings>privilege,omitempty"`
JSSActions []string `json:"jss_actions,omitempty" xml:"jss_actions>privilege,omitempty"`
Recon []string `json:"recon,omitempty" xml:"recon>privilege,omitempty"`
CasperAdmin []string `json:"casper_admin,omitempty" xml:"casper_admin>privilege,omitempty"`
CasperRemote []string `json:"casper_remote,omitempty" xml:"casper_remote>privilege,omitempty"`
CasperImaging []string `json:"casper_imaging,omitempty" xml:"casper_imaging>privilege,omitempty"`
}
// CRUD
// GetAccounts retrieves a list of all accounts (both users and groups).
func (c *Client) GetAccounts() (*ResponseAccountsList, error) {
endpoint := uriAPIAccounts
var accountsList ResponseAccountsList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &accountsList)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGet, "accounts", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &accountsList, nil
}
// GetAccountByID retrieves the Account by its ID
func (c *Client) GetAccountByID(id int) (*ResourceAccount, error) {
endpoint := fmt.Sprintf("%s/userid/%d", uriAPIAccounts, id)
var account ResourceAccount
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &account)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "account", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &account, nil
}
// GetAccountByName retrieves the Account by its name
func (c *Client) GetAccountByName(name string) (*ResourceAccount, error) {
endpoint := fmt.Sprintf("%s/username/%s", uriAPIAccounts, name)
var account ResourceAccount
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &account)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "account", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &account, nil
}
// CreateAccountByID creates an Account using its ID
func (c *Client) CreateAccount(account *ResourceAccount) (*ResponseAccountCreatedAndUpdated, error) {
// Use a placeholder ID for creating a new account
placeholderID := 0
endpoint := fmt.Sprintf("%s/userid/%d", uriAPIAccounts, placeholderID)
// Check if site is not provided and set default values
if account.Site.ID == 0 && account.Site.Name == "" {
account.Site = SharedResourceSite{
ID: -1,
Name: "None",
}
}
// Define XML requestBody structure
requestBody := &struct {
XMLName struct{} `xml:"account"`
*ResourceAccount
}{
ResourceAccount: account,
}
var returnedAccount ResponseAccountCreatedAndUpdated
resp, err := c.HTTP.DoRequest("POST", endpoint, requestBody, &returnedAccount)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "account", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &returnedAccount, nil
}
// UpdateAccountByID updates an Account using its ID
func (c *Client) UpdateAccountByID(id int, account *ResourceAccount) (*ResponseAccountCreatedAndUpdated, error) {
endpoint := fmt.Sprintf("%s/userid/%d", uriAPIAccounts, id)
if account.Site.ID == 0 && account.Site.Name == "" {
account.Site = SharedResourceSite{
ID: -1,
Name: "None",
}
}
requestBody := &struct {
XMLName struct{} `xml:"account"`
*ResourceAccount
}{
ResourceAccount: account,
}
var updatedAccount ResponseAccountCreatedAndUpdated
resp, err := c.HTTP.DoRequest("PUT", endpoint, requestBody, &updatedAccount)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "account", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedAccount, nil
}
// UpdateAccountByName updates an Account using its name.
func (c *Client) UpdateAccountByName(name string, account *ResourceAccount) (*ResponseAccountCreatedAndUpdated, error) {
endpoint := fmt.Sprintf("%s/username/%s", uriAPIAccounts, name)
if account.Site.ID == 0 && account.Site.Name == "" {
account.Site = SharedResourceSite{
ID: -1,
Name: "None",
}
}
requestBody := &struct {
XMLName struct{} `xml:"account"`
*ResourceAccount
}{
ResourceAccount: account,
}
var updatedAccount ResponseAccountCreatedAndUpdated
resp, err := c.HTTP.DoRequest("PUT", endpoint, requestBody, &updatedAccount)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "account", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedAccount, nil
}
// DeleteAccountByID deletes an Account using its ID
func (c *Client) DeleteAccountByID(id int) error {
endpoint := fmt.Sprintf("%s/userid/%d", uriAPIAccounts, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "account", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteAccountByName deletes an Account using its name.
func (c *Client) DeleteAccountByName(name string) error {
endpoint := fmt.Sprintf("%s/username/%s", uriAPIAccounts, name)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "account", name, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}