-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_user_extension_attributes.go
196 lines (153 loc) · 6.32 KB
/
classicapi_user_extension_attributes.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
// classicapi_user_extension_attributes.go
// Jamf Pro Classic Api - User Extension Attributes
// api reference: https://developer.jamf.com/jamf-pro/reference/userextensionattributes
// Classic API requires the structs to support an XML data structure.
package jamfpro
import (
"encoding/xml"
"fmt"
)
const uriUserExtensionAttributes = "/JSSResource/userextensionattributes"
// Structs for User Extension Attributes
type ResponseUserExtensionAttributesList struct {
XMLName xml.Name `xml:"user_extension_attributes"`
Size int `xml:"size"`
UserExtensionAttributes []UserExtensionAttributeItem `xml:"user_extension_attribute"`
}
type UserExtensionAttributeItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// ResponseUserExtensionAttributerepresents a single user extension attribute.
type ResponseUserExtensionAttribute struct {
XMLName xml.Name `xml:"user_extension_attribute"`
ID int `xml:"id,omitempty"`
Name string `xml:"name"`
Description string `xml:"description"`
DataType string `xml:"data_type"`
InputType UserExtensionInputType `xml:"input_type"`
}
// UserExtensionInputType represents the input type of a user extension attribute.
type UserExtensionInputType struct {
Type string `xml:"type"`
}
// GetUserExtensionAttributes retrieves a list of all user extension attributes.
func (c *Client) GetUserExtensionAttributes() (*ResponseUserExtensionAttributesList, error) {
endpoint := uriUserExtensionAttributes
var extAttributes ResponseUserExtensionAttributesList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &extAttributes)
if err != nil {
return nil, fmt.Errorf("failed to fetch user extension attributes: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &extAttributes, nil
}
// GetUserExtensionAttributeByID retrieves a user extension attribute by its ID.
func (c *Client) GetUserExtensionAttributeByID(id int) (*ResponseUserExtensionAttribute, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriUserExtensionAttributes, id)
var userExtAttr ResponseUserExtensionAttribute
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &userExtAttr)
if err != nil {
return nil, fmt.Errorf("failed to fetch user extension attribute by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &userExtAttr, nil
}
// GetUserExtensionAttributeByName retrieves a user extension attribute by its name.
func (c *Client) GetUserExtensionAttributeByName(name string) (*ResponseUserExtensionAttribute, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriUserExtensionAttributes, name)
var userExtAttr ResponseUserExtensionAttribute
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &userExtAttr)
if err != nil {
return nil, fmt.Errorf("failed to fetch user extension attribute by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &userExtAttr, nil
}
// CreateUserExtensionAttribute creates a new user extension attribute.
func (c *Client) CreateUserExtensionAttribute(attribute *ResponseUserExtensionAttribute) (*ResponseUserExtensionAttribute, error) {
endpoint := fmt.Sprintf("%s/id/0", uriUserExtensionAttributes) // Using ID 0 for creation
requestBody := struct {
XMLName xml.Name `xml:"user_extension_attribute"`
*ResponseUserExtensionAttribute
}{
ResponseUserExtensionAttribute: attribute,
}
var createdAttribute ResponseUserExtensionAttribute
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &createdAttribute)
if err != nil {
return nil, fmt.Errorf("failed to create user extension attribute: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &createdAttribute, nil
}
// UpdateUserExtensionAttributeByID updates a user extension attribute by its ID.
func (c *Client) UpdateUserExtensionAttributeByID(id int, attribute *ResponseUserExtensionAttribute) (*ResponseUserExtensionAttribute, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriUserExtensionAttributes, id)
requestBody := struct {
XMLName xml.Name `xml:"user_extension_attribute"`
*ResponseUserExtensionAttribute
}{
ResponseUserExtensionAttribute: attribute,
}
var updatedAttribute ResponseUserExtensionAttribute
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &updatedAttribute)
if err != nil {
return nil, fmt.Errorf("failed to update user extension attribute by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedAttribute, nil
}
// UpdateUserExtensionAttributeByName updates a user extension attribute by its name.
func (c *Client) UpdateUserExtensionAttributeByName(name string, attribute *ResponseUserExtensionAttribute) (*ResponseUserExtensionAttribute, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriUserExtensionAttributes, name)
requestBody := struct {
XMLName xml.Name `xml:"user_extension_attribute"`
*ResponseUserExtensionAttribute
}{
ResponseUserExtensionAttribute: attribute,
}
var updatedAttribute ResponseUserExtensionAttribute
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &updatedAttribute)
if err != nil {
return nil, fmt.Errorf("failed to update user extension attribute by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedAttribute, nil
}
// DeleteUserExtensionAttributeByID deletes a user extension attribute by its ID.
func (c *Client) DeleteUserExtensionAttributeByID(id int) error {
endpoint := fmt.Sprintf("%s/id/%d", uriUserExtensionAttributes, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete user extension attribute by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteUserExtensionAttributeByName deletes a user extension attribute by its name.
func (c *Client) DeleteUserExtensionAttributeByName(name string) error {
endpoint := fmt.Sprintf("%s/name/%s", uriUserExtensionAttributes, name)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete user extension attribute by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}