-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_buildings.go
272 lines (211 loc) · 7.75 KB
/
jamfproapi_buildings.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
// jamfapi_buildings.go
// Jamf Pro Api - Buildings
// api reference: https://developer.jamf.com/jamf-pro/reference/get_v1-buildings
// Jamf Pro API requires the structs to support a JSON data structure.
package jamfpro
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
const uriBuildings = "/api/v1/buildings"
// List
// ResponseBuildings represents the structure of the response for the buildings list.
type ResponseBuildingsList struct {
TotalCount int `json:"totalCount"`
Results []ResourceBuilding `json:"results"`
}
// Responses
// ResponseBuildingResourceHistoryList represents the structure of the response for the building resource history list.
type ResponseBuildingResourceHistoryList struct {
Size int `json:"totalCount"`
Results []ResourceBuildingResourceHistory `json:"results"`
}
// ResponseBuildingCreate represents the response structure for creating a building.
type ResponseBuildingCreate struct {
ID string `json:"id"`
Href string `json:"href"`
}
// Resource
// ResponseBuilding represents the structure of each building item in the response.
type ResourceBuilding struct {
ID string `json:"id"`
Name string `json:"name"`
StreetAddress1 string `json:"streetAddress1"`
StreetAddress2 string `json:"streetAddress2"`
City string `json:"city"`
StateProvince string `json:"stateProvince"`
ZipPostalCode string `json:"zipPostalCode"`
Country string `json:"country"`
}
// ResponseBuildingResourceHistory represents the structure of each resource history item in the response.
type ResourceBuildingResourceHistory struct {
ID int `json:"id"`
Username string `json:"username"`
Date string `json:"date"`
Note string `json:"note"`
Details string `json:"details"`
}
// CRUD
// GetBuildings retrieves all building information with optional sorting.
func (c *Client) GetBuildings(sort_filter string) (*ResponseBuildingsList, error) {
resp, err := c.DoPaginatedGet(
uriBuildings,
standardPageSize,
startingPageNumber,
sort_filter,
)
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "buildings", err)
}
var out ResponseBuildingsList
out.TotalCount = resp.Size
for _, value := range resp.Results {
var newObj ResourceBuilding
err := mapstructure.Decode(value, &newObj)
if err != nil {
return nil, fmt.Errorf(errMsgFailedMapstruct, "building", err)
}
out.Results = append(out.Results, newObj)
}
return &out, nil
}
// GetBuildingByID retrieves a single building information by its ID.
func (c *Client) GetBuildingByID(id string) (*ResourceBuilding, error) {
endpoint := fmt.Sprintf("%s/%s", uriBuildings, id)
var building ResourceBuilding
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &building)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "building", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &building, nil
}
// GetBuildingByNameByID retrieves a single building information by its name using GetBuildingByID.
func (c *Client) GetBuildingByName(name string) (*ResourceBuilding, error) {
buildings, err := c.GetBuildings("")
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "building", err)
}
for _, value := range buildings.Results {
if value.Name == name {
return &value, nil
}
}
return nil, fmt.Errorf(errMsgFailedGetByName, "building", name, errMsgNoName)
}
// CreateBuilding creates a new building in Jamf Pro
func (c *Client) CreateBuilding(building *ResourceBuilding) (*ResponseBuildingCreate, error) {
endpoint := uriBuildings
var responseBuildingCreate ResponseBuildingCreate
resp, err := c.HTTP.DoRequest("POST", endpoint, building, &responseBuildingCreate)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "building", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseBuildingCreate, nil
}
// UpdateBuildingByID updates a building's information in Jamf Pro by its ID.
func (c *Client) UpdateBuildingByID(id string, buildingUpdate *ResourceBuilding) (*ResourceBuilding, error) {
endpoint := fmt.Sprintf("%s/%s", uriBuildings, id)
var updatedBuilding ResourceBuilding
resp, err := c.HTTP.DoRequest("PUT", endpoint, buildingUpdate, &updatedBuilding)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "building", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedBuilding, nil
}
// UpdateBuildingByNameByID updates a building's information in Jamf Pro by its name.
func (c *Client) UpdateBuildingByName(name string, buildingUpdate *ResourceBuilding) (*ResourceBuilding, error) {
target, err := c.GetBuildingByName(name)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "building", name, err)
}
target_id := target.ID
resp, err := c.UpdateBuildingByID(target_id, buildingUpdate)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "building", name, err)
}
return resp, nil
}
// DeleteBuildingByID deletes a building in Jamf Pro by its ID.
func (c *Client) DeleteBuildingByID(id string) error {
endpoint := fmt.Sprintf("%s/%s", uriBuildings, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "buidling", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteBuildingByNameByID deletes a building in Jamf Pro by its name.
func (c *Client) DeleteBuildingByName(name string) error {
target, err := c.GetBuildingByName(name)
if err != nil {
return fmt.Errorf(errMsgFailedGetByName, "building", name, err)
}
target_id := target.ID
err = c.DeleteBuildingByID(target_id)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "building", name, err)
}
return nil
}
// DeleteMultipleBuildingsByID deletes multiple buildings in Jamf Pro by their IDs.
func (c *Client) DeleteMultipleBuildingsByID(ids []string) error {
endpoint := "/api/v1/buildings/delete-multiple"
payload := struct {
IDs []string `json:"ids"`
}{
IDs: ids,
}
resp, err := c.HTTP.DoRequest("POST", endpoint, payload, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteMultiple, "buildings", ids, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// GetBuildingResourceHistoryByID retrieves the resource history of a specific building by its ID.
func (c *Client) GetBuildingResourceHistoryByID(id, sort_filter string) (*ResponseBuildingResourceHistoryList, error) {
// Construct the URL with the provided ID
endpoint := fmt.Sprintf("%s/%s/history", uriBuildings, id)
resp, err := c.DoPaginatedGet(endpoint, standardPageSize, startingPageNumber, sort_filter)
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "building histories", err)
}
var out ResponseBuildingResourceHistoryList
out.Size = resp.Size
for _, value := range resp.Results {
var newObj ResourceBuildingResourceHistory
err := mapstructure.Decode(value, &newObj)
if err != nil {
return nil, fmt.Errorf(errMsgFailedMapstruct, "buidling histories", err)
}
out.Results = append(out.Results, newObj)
}
return &out, nil
}
// CreateBuildingResourceHistoryByID updates the resource history of a building in Jamf Pro by its ID.
func (c *Client) CreateBuildingResourceHistoryByID(id string, historyUpdate *ResourceBuildingResourceHistory) (*ResourceBuildingResourceHistory, error) {
endpoint := fmt.Sprintf("%s/%s/history", uriBuildings, id)
var updatedHistory ResourceBuildingResourceHistory
resp, err := c.HTTP.DoRequest("POST", endpoint, historyUpdate, &updatedHistory)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "building histories", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedHistory, nil
}