-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_helper.go
369 lines (327 loc) · 9.88 KB
/
controller_helper.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package v2
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/alejandroEsc/golang-maas-client/pkg/api/client"
"github.com/alejandroEsc/golang-maas-client/pkg/api/util"
"github.com/juju/errors"
)
// GetFile returns a single File by its Filename.
func (c *Controller) GetFile(filename string) (*File, error) {
if filename == "" {
return nil, errors.NotValidf("missing Filename")
}
source, err := c.Get("files/"+filename, "", nil)
if err != nil {
if svrErr, ok := errors.Cause(err).(client.ServerError); ok {
if svrErr.StatusCode == http.StatusNotFound {
return nil, errors.Wrap(err, util.NewNoMatchError(svrErr.BodyMessage))
}
}
return nil, util.NewUnexpectedError(err)
}
var file File
err = json.Unmarshal(source, &file)
if err != nil {
return nil, err
}
file.Controller = c
return &file, nil
}
func (c *Controller) ReadFileContent(f *File) ([]byte, error) {
// If the Content is available, it is base64 encoded, so
args := make(url.Values)
args.Add("Filename", f.Filename)
bytes, err := c.Get("files", "Get", args)
if err != nil {
if svrErr, ok := errors.Cause(err).(client.ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound:
return nil, errors.Wrap(err, util.NewNoMatchError(svrErr.BodyMessage))
case http.StatusForbidden:
return nil, errors.Wrap(err, util.NewPermissionError(svrErr.BodyMessage))
}
}
return nil, util.NewUnexpectedError(err)
}
return bytes, nil
}
// getFiles returns all the files that match the specified prefix.
func (c *Controller) getFiles(prefix string) ([]File, error) {
params := util.NewURLParams()
params.MaybeAdd("prefix", prefix)
source, err := c.Get("files", "", params.Values)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
var files []File
results := make([]File, 0)
err = json.Unmarshal(source, &files)
if err != nil {
return nil, err
}
for _, f := range files {
f.Controller = c
results = append(results, f)
}
return results, nil
}
// Delete implements FileInterface.
func (c *Controller) DeleteFile(f *File) error {
err := c.Delete(f.ResourceURI)
if err != nil {
if svrErr, ok := errors.Cause(err).(client.ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound:
return errors.Wrap(err, util.NewNoMatchError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, util.NewPermissionError(svrErr.BodyMessage))
}
}
return util.NewUnexpectedError(err)
}
return nil
}
// Fabrics returns the list of Fabrics defined in the maas ControllerInterface.
func (c *Controller) Fabrics() ([]Fabric, error) {
source, err := c.Get("fabrics", "", nil)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
var fabrics []Fabric
err = json.Unmarshal(source, &fabrics)
if err != nil {
return nil, err
}
return fabrics, nil
}
// Spaces returns the list of Spaces defined in the maas ControllerInterface.
func (c *Controller) Spaces() ([]Space, error) {
source, err := c.Get("spaces", "", nil)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
var spaces []Space
err = json.Unmarshal(source, &spaces)
if err != nil {
return nil, err
}
return spaces, nil
}
// StaticRoutes returns the list of StaticRoutes defined in the maas ControllerInterface.
func (c *Controller) StaticRoutes() ([]StaticRoute, error) {
source, err := c.Get("static-routes", "", nil)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
var staticRoutes []StaticRoute
err = json.Unmarshal(source, &staticRoutes)
if err != nil {
return nil, err
}
return staticRoutes, nil
}
// Zones lists all the zones known to the maas ControllerInterface.
func (c *Controller) Zones() ([]Zone, error) {
source, err := c.Get("zones", "", nil)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
var zones []Zone
err = json.Unmarshal(source, &zones)
if err != nil {
return nil, err
}
return zones, nil
}
// Nodes returns a list of devices that match the params.
func (c *Controller) Nodes(args NodesArgs) ([]Node, error) {
params := NodesParams(args)
source, err := c.Get("nodes", "", params.Values)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
results := make([]Node, 0)
var nodes []Node
err = json.Unmarshal(source, &nodes)
if err != nil {
return nil, err
}
for _, d := range nodes {
d.Controller = c
results = append(results, d)
}
return results, nil
}
// CreateNode creates and returns a new NodeInterface.
func (c *Controller) CreateNode(args CreateNodeArgs) (*Node, error) {
// There must be at least one mac address.
if len(args.MACAddresses) == 0 {
return nil, util.NewBadRequestError("at least one MAC address must be specified")
}
params := CreateNodesParams(args)
result, err := c.Post("nodes", "", params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(client.ServerError); ok {
if svrErr.StatusCode == http.StatusBadRequest {
return nil, errors.Wrap(err, util.NewBadRequestError(svrErr.BodyMessage))
}
}
// Translate http errors.
return nil, util.NewUnexpectedError(err)
}
var d Node
iSet := make([]*NetworkInterface, 0)
err = json.Unmarshal(result, &d)
if err != nil {
return nil, err
}
d.Controller = c
for _, i := range d.InterfaceSet {
i.Controller = c
iSet = append(iSet, i)
}
d.InterfaceSet = iSet
return &d, nil
}
// Machines returns a list of machines that match the params.
func (c *Controller) Machines(args MachinesArgs) ([]Machine, error) {
params := MachinesParams(args)
// At the moment the maas API doesn't support filtering by Owner
// data so we do that ourselves below.
source, err := c.Get("machines", "", params.Values)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
result := make([]Machine, 0)
var machines []Machine
err = json.Unmarshal(source, &machines)
if err != nil {
return nil, err
}
for _, m := range machines {
if ownerDataMatches(m.OwnerData, args.OwnerData) {
m.Controller = c
resultIface := make([]*NetworkInterface, 0)
for _, i := range m.InterfaceSet {
i.Controller = c
resultIface = append(resultIface, i)
}
m.InterfaceSet = resultIface
result = append(result, m)
}
}
return result, nil
}
// AddFile adds or replaces the Content of the specified Filename.
// If or when the maas api is able to return metadata about a single
// File without sending the Content of the File, we can return a FileInterface
// instance here too.
func (c *Controller) AddFile(args AddFileArgs) error {
if err := args.Validate(); err != nil {
return err
}
fileContent := args.Content
if fileContent == nil {
content, err := ioutil.ReadAll(io.LimitReader(args.Reader, args.Length))
if err != nil {
return errors.Annotatef(err, "cannot read File Content")
}
fileContent = content
}
params := url.Values{"Filename": {args.Filename}}
_, err := c.PostFile("files", "", params, fileContent)
if err != nil {
if svrErr, ok := errors.Cause(err).(client.ServerError); ok {
if svrErr.StatusCode == http.StatusBadRequest {
return errors.Wrap(err, util.NewBadRequestError(svrErr.BodyMessage))
}
}
return util.NewUnexpectedError(err)
}
return nil
}
func ownerDataMatches(ownerData, filter map[string]string) bool {
for key, value := range filter {
if ownerData[key] != value {
return false
}
}
return true
}
// BootResources implements ControllerInterface.
func (c *Controller) BootResources() ([]*BootResource, error) {
source, err := c.Get("boot-resources", "", nil)
if err != nil {
return nil, util.NewUnexpectedError(err)
}
var resources []*BootResource
err = json.Unmarshal(source, &resources)
if err != nil {
return nil, err
}
return resources, nil
}
// AllocateMachine will attempt to allocate a MachineInterface to the user.
// If successful, the allocated MachineInterface is returned.
// Returns an error that satisfies IsNoMatchError if the requested
// constraints cannot be met.
func (c *Controller) AllocateMachine(args AllocateMachineArgs) (*Machine, ConstraintMatches, error) {
var matches ConstraintMatches
params := AllocateMachinesParams(args)
result, err := c.Post("machines", "allocate", params.Values)
if err != nil {
// A 409 Status code is "No Matching Machines"
if svrErr, ok := errors.Cause(err).(client.ServerError); ok {
if svrErr.StatusCode == http.StatusConflict {
return nil, matches, errors.Wrap(err, util.NewNoMatchError(svrErr.BodyMessage))
}
}
// Translate http errors.
return nil, matches, util.NewUnexpectedError(err)
}
var machine *Machine
var source map[string]interface{}
err = json.Unmarshal(result, &machine)
if err != nil {
return nil, matches, err
}
machine.Controller = c
err = json.Unmarshal(result, &source)
if err != nil {
return nil, matches, err
}
// Parse the constraint matches.
matches, err = parseAllocateConstraintsResponse(source, machine)
if err != nil {
return nil, matches, err
}
return machine, matches, nil
}
// ReleaseMachines will stop the specified machines, and release them
// from the user making them available to be allocated again.
// Release multiple machines at once. Returns
// - BadRequestError if any of the machines cannot be found
// - PermissionError if the user does not have permission to release any of the machines
// - CannotCompleteError if any of the machines could not be released due to their current state
func (c *Controller) ReleaseMachines(args ReleaseMachinesArgs) error {
params := ReleaseMachinesParams(args)
_, err := c.Post("machines", "release", params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(client.ServerError); ok {
switch svrErr.StatusCode {
case http.StatusBadRequest:
return errors.Wrap(err, util.NewBadRequestError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, util.NewPermissionError(svrErr.BodyMessage))
case http.StatusConflict:
return errors.Wrap(err, util.NewCannotCompleteError(svrErr.BodyMessage))
}
}
return util.NewUnexpectedError(err)
}
return nil
}