-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_scripts.go
207 lines (165 loc) · 5.7 KB
/
classicapi_scripts.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
// classicapi_scripts.go
// Jamf Pro Api - Scripts
// api reference: https://developer.jamf.com/jamf-pro/reference/scripts
// Jamf Pro API requires the structs to support an XML data structure.
package jamfpro
import (
"encoding/xml"
"fmt"
)
const uriScripts = "/JSSResource/scripts"
type ResponseScript struct {
ID int `xml:"id"`
Name string `xml:"name"`
Category string `xml:"category,omitempty"`
Filename string `xml:"filename,omitempty"`
Info string `xml:"info,omitempty"`
Notes string `xml:"notes,omitempty"`
Priority string `xml:"priority,omitempty"`
Parameters Parameters `xml:"parameters"`
OSRequirements string `xml:"os_requirements,omitempty"`
ScriptContents string `xml:"script_contents,omitempty"`
ScriptContentsEncoded string `xml:"script_contents_encoded,omitempty"`
}
type Parameters struct {
Parameter4 string `xml:"parameter4,omitempty"`
Parameter5 string `xml:"parameter5,omitempty"`
Parameter6 string `xml:"parameter6,omitempty"`
Parameter7 string `xml:"parameter7,omitempty"`
Parameter8 string `xml:"parameter8,omitempty"`
Parameter9 string `xml:"parameter9,omitempty"`
Parameter10 string `xml:"parameter10,omitempty"`
Parameter11 string `xml:"parameter11,omitempty"`
}
// Scripts List Structs
type ResponseScriptsList struct {
Size int `xml:"size"`
Script []ScriptItem `xml:"script"`
}
type ScriptItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// GetScripts retrieves a list of all scripts.
func (c *Client) GetScripts() (*ResponseScriptsList, error) {
endpoint := uriScripts
var scriptsList ResponseScriptsList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &scriptsList)
if err != nil {
return nil, fmt.Errorf("failed to fetch all scripts: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &scriptsList, nil
}
// GetScriptsByID retrieves the details of a script by its ID.
func (c *Client) GetScriptsByID(id int) (*ResponseScript, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriScripts, id)
var scriptDetails ResponseScript
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &scriptDetails)
if err != nil {
return nil, fmt.Errorf("failed to fetch script by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &scriptDetails, nil
}
// GetScriptsByName retrieves the details of a script by its name.
func (c *Client) GetScriptsByName(name string) (*ResponseScript, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriScripts, name)
var scriptDetails ResponseScript
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &scriptDetails)
if err != nil {
return nil, fmt.Errorf("failed to fetch script by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &scriptDetails, nil
}
// CreateScriptByID creates a new script by its ID.
func (c *Client) CreateScriptByID(script *ResponseScript) (*ResponseScript, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriScripts, script.ID)
// Wrap the script with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"script"`
*ResponseScript
}{
ResponseScript: script,
}
var responseScript ResponseScript
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &responseScript)
if err != nil {
return nil, fmt.Errorf("failed to create script by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseScript, nil
}
// UpdateScriptByID updates an existing script by its ID.
func (c *Client) UpdateScriptByID(script *ResponseScript) (*ResponseScript, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriScripts, script.ID)
// Wrap the script with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"script"`
*ResponseScript
}{
ResponseScript: script,
}
var updatedScript ResponseScript
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &updatedScript)
if err != nil {
return nil, fmt.Errorf("failed to update script by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedScript, nil
}
// UpdateScriptByName updates an existing script by its name.
func (c *Client) UpdateScriptByName(script *ResponseScript) (*ResponseScript, error) {
endpoint := fmt.Sprintf("%s/name/%s", uriScripts, script.Name)
// Wrap the script with the desired XML name using an anonymous struct
requestBody := struct {
XMLName xml.Name `xml:"script"`
*ResponseScript
}{
ResponseScript: script,
}
var updatedScript ResponseScript
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &updatedScript)
if err != nil {
return nil, fmt.Errorf("failed to update script by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedScript, nil
}
// DeleteScriptByID deletes a script by its ID.
func (c *Client) DeleteScriptByID(id int) error {
endpoint := fmt.Sprintf("%s/id/%d", uriScripts, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete script by ID: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteScriptByName deletes a script by its name.
func (c *Client) DeleteScriptByName(name string) error {
endpoint := fmt.Sprintf("%s/name/%s", uriScripts, name)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete script by name: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}