-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_scripts.go
202 lines (158 loc) · 5.34 KB
/
jamfproapi_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
// jamfproapi_self_service_branding_macos.go
// Jamf Pro Api - Scripts
// api reference: https://developer.jamf.com/jamf-pro/reference/get_v1-scripts
// Jamf Pro Api requires the structs to support an JSON data structure.
package jamfpro
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
const uriScripts = "/api/v1/scripts"
// List
// Struct for paginated response for scripts
type ResponseScriptsList struct {
Size int `json:"totalCount"`
Results []ResourceScript `json:"results"`
}
// Response
// Response format struct for create function
type ResponseScriptCreate struct {
ID string `json:"id"`
Href string `json:"href"`
}
// Resource
// Struct which represents Script object JSON from Pro API
type ResourceScript struct {
ID string `json:"id"`
Name string `json:"name"`
CategoryName string `json:"categoryName,omitempty"`
CategoryId string `json:"categoryId,omitempty"`
Info string `json:"info,omitempty"`
Notes string `json:"notes,omitempty"`
OSRequirements string `json:"osRequirements,omitempty"`
Priority string `json:"priority,omitempty"`
ScriptContents string `json:"scriptContents,omitempty"`
Parameter4 string `json:"parameter4,omitempty"`
Parameter5 string `json:"parameter5,omitempty"`
Parameter6 string `json:"parameter6,omitempty"`
Parameter7 string `json:"parameter7,omitempty"`
Parameter8 string `json:"parameter8,omitempty"`
Parameter9 string `json:"parameter9,omitempty"`
Parameter10 string `json:"parameter10,omitempty"`
Parameter11 string `json:"parameter11,omitempty"`
}
// CRUD
// Gets full list of scripts & handles pagination
func (c *Client) GetScripts(sort_filter string) (*ResponseScriptsList, error) {
resp, err := c.DoPaginatedGet(
uriScripts,
standardPageSize,
startingPageNumber,
sort_filter,
)
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "scripts", err)
}
var out ResponseScriptsList
out.Size = resp.Size
for _, value := range resp.Results {
var newObj ResourceScript
err := mapstructure.Decode(value, &newObj)
if err != nil {
return nil, fmt.Errorf(errMsgFailedMapstruct, "script", err)
}
out.Results = append(out.Results, newObj)
}
return &out, nil
}
// Retrieves script from provided ID & returns ResourceScript
func (c *Client) GetScriptByID(id string) (*ResourceScript, error) {
endpoint := fmt.Sprintf("%s/%s", uriScripts, id)
var script ResourceScript
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &script)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "script", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &script, nil
}
// Retrieves script by Name by leveraging GetScripts(), returns ResourceScript
func (c *Client) GetScriptByName(name string) (*ResourceScript, error) {
scripts, err := c.GetScripts("")
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "scripts", err)
}
for _, value := range scripts.Results {
if value.Name == name {
return &value, nil
}
}
return nil, fmt.Errorf(errMsgFailedGetByName, "script", name, errMsgNoName)
}
// Creates script from ResourceScript struct
func (c *Client) CreateScript(script *ResourceScript) (*ResponseScriptCreate, error) {
endpoint := uriScripts
var ResponseScriptCreate ResponseScriptCreate
resp, err := c.HTTP.DoRequest("POST", endpoint, script, &ResponseScriptCreate)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "script", err)
}
if resp != nil {
defer resp.Body.Close()
}
return &ResponseScriptCreate, nil
}
// Updates script from provided ResourceScript - only updates provided keys
func (c *Client) UpdateScriptByID(id string, scriptUpdate *ResourceScript) (*ResourceScript, error) {
endpoint := fmt.Sprintf("%s/%s", uriScripts, id)
var updatedScript ResourceScript
resp, err := c.HTTP.DoRequest("PUT", endpoint, scriptUpdate, &updatedScript)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "script", id, err)
}
if resp != nil {
defer resp.Body.Close()
}
return &updatedScript, nil
}
// Leverages UpdateScriptByID and GetScripts to update script from provided ResourceScript
func (c *Client) UpdateScriptByName(name string, scriptUpdate *ResourceScript) (*ResourceScript, error) {
target, err := c.GetScriptByName(name)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "script", name, err)
}
target_id := target.ID
resp, err := c.UpdateScriptByID(target_id, scriptUpdate)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "script", name, err)
}
return resp, nil
}
// Deletes script with provided ID
func (c *Client) DeleteScriptByID(id string) error {
endpoint := fmt.Sprintf("%s/%s", uriScripts, id)
var response interface{}
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, &response)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "script", id, err)
}
if resp != nil {
defer resp.Body.Close()
}
return nil
}
// Leverages DeleteScriptByID and GetScripts to delete script by Name
func (c *Client) DeleteScriptByName(name string) error {
target, err := c.GetScriptByName(name)
if err != nil {
return fmt.Errorf(errMsgFailedGetByName, "script", name, err)
}
target_id := target.ID
err = c.DeleteScriptByID(target_id)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "script", name, err)
}
return nil
}