-
Notifications
You must be signed in to change notification settings - Fork 36
/
controller.go
1083 lines (992 loc) · 31.8 KB
/
controller.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package gomaasapi
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"sync/atomic"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/schema"
"github.com/juju/version"
)
var (
logger = loggo.GetLogger("maas")
// The supported versions should be ordered from most desirable version to
// least as they will be tried in order.
supportedAPIVersions = []string{"2.0"}
// Each of the api versions that change the request or response structure
// for any given call should have a value defined for easy definition of
// the deserialization functions.
twoDotOh = version.Number{Major: 2, Minor: 0}
// Current request number. Informational only for logging.
requestNumber int64
)
// ControllerArgs is an argument struct for passing the required parameters
// to the NewController method.
type ControllerArgs struct {
BaseURL string
APIKey string
HTTPClient *http.Client
}
// NewController creates an authenticated client to the MAAS API, and
// checks the capabilities of the server. If the BaseURL specified
// includes the API version, that version of the API will be used,
// otherwise the controller will use the highest supported version
// available.
//
// If the APIKey is not valid, a NotValid error is returned.
// If the credentials are incorrect, a PermissionError is returned.
func NewController(args ControllerArgs) (Controller, error) {
base, apiVersion, includesVersion := SplitVersionedURL(args.BaseURL)
if includesVersion {
if !supportedVersion(apiVersion) {
return nil, NewUnsupportedVersionError("version %s", apiVersion)
}
return newControllerWithVersion(base, apiVersion, args.APIKey, args.HTTPClient)
}
return newControllerUnknownVersion(args)
}
func supportedVersion(value string) bool {
for _, version := range supportedAPIVersions {
if value == version {
return true
}
}
return false
}
func newControllerWithVersion(baseURL, apiVersion, apiKey string, httpClient *http.Client) (Controller, error) {
major, minor, err := version.ParseMajorMinor(apiVersion)
// We should not get an error here. See the test.
if err != nil {
return nil, errors.Errorf("bad version defined in supported versions: %q", apiVersion)
}
client, err := NewAuthenticatedClient(AddAPIVersionToURL(baseURL, apiVersion), apiKey)
if err != nil {
// If the credentials aren't valid, return now.
if errors.IsNotValid(err) {
return nil, errors.Trace(err)
}
// Any other error attempting to create the authenticated client
// is an unexpected error and return now.
return nil, NewUnexpectedError(err)
}
client.HTTPClient = httpClient
controllerVersion := version.Number{
Major: major,
Minor: minor,
}
controller := &controller{client: client, apiVersion: controllerVersion}
_, _, controller.capabilities, err = controller.readAPIVersionInfo()
if err != nil {
logger.Debugf("read version failed: %#v", err)
return nil, errors.Trace(err)
}
if err := controller.checkCreds(); err != nil {
return nil, errors.Trace(err)
}
return controller, nil
}
func newControllerUnknownVersion(args ControllerArgs) (Controller, error) {
// For now we don't need to test multiple versions. It is expected that at
// some time in the future, we will try the most up to date version and then
// work our way backwards.
for _, apiVersion := range supportedAPIVersions {
controller, err := newControllerWithVersion(args.BaseURL, apiVersion, args.APIKey, args.HTTPClient)
switch {
case err == nil:
return controller, nil
case IsUnsupportedVersionError(err):
// This will only come back from APIVersionInfo for 410/404.
continue
default:
return nil, errors.Trace(err)
}
}
return nil, NewUnsupportedVersionError("controller at %s does not support any of %s", args.BaseURL, supportedAPIVersions)
}
type controller struct {
client *Client
apiVersion version.Number
capabilities set.Strings
}
// Capabilities implements Controller.
func (c *controller) Capabilities() set.Strings {
return c.capabilities
}
// BootResources implements Controller.
func (c *controller) BootResources() ([]BootResource, error) {
source, err := c.get("boot-resources")
if err != nil {
return nil, NewUnexpectedError(err)
}
resources, err := readBootResources(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []BootResource
for _, r := range resources {
result = append(result, r)
}
return result, nil
}
// Fabrics implements Controller.
func (c *controller) Fabrics() ([]Fabric, error) {
source, err := c.get("fabrics")
if err != nil {
return nil, NewUnexpectedError(err)
}
fabrics, err := readFabrics(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []Fabric
for _, f := range fabrics {
result = append(result, f)
}
return result, nil
}
// Spaces implements Controller.
func (c *controller) Spaces() ([]Space, error) {
source, err := c.get("spaces")
if err != nil {
return nil, NewUnexpectedError(err)
}
spaces, err := readSpaces(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []Space
for _, space := range spaces {
result = append(result, space)
}
return result, nil
}
// StaticRoutes implements Controller.
func (c *controller) StaticRoutes() ([]StaticRoute, error) {
source, err := c.get("static-routes")
if err != nil {
return nil, NewUnexpectedError(err)
}
staticRoutes, err := readStaticRoutes(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []StaticRoute
for _, staticRoute := range staticRoutes {
result = append(result, staticRoute)
}
return result, nil
}
// Zones implements Controller.
func (c *controller) Zones() ([]Zone, error) {
source, err := c.get("zones")
if err != nil {
return nil, NewUnexpectedError(err)
}
zones, err := readZones(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []Zone
for _, z := range zones {
result = append(result, z)
}
return result, nil
}
// Pools implements Controller.
func (c *controller) Pools() ([]Pool, error) {
var result []Pool
source, err := c.get("pools")
if err != nil {
return nil, NewUnexpectedError(err)
}
pools, err := readPools(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
for _, p := range pools {
result = append(result, p)
}
return result, nil
}
// Domains implements Controller
func (c *controller) Domains() ([]Domain, error) {
source, err := c.get("domains")
if err != nil {
return nil, NewUnexpectedError(err)
}
domains, err := readDomains(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []Domain
for _, domain := range domains {
result = append(result, domain)
}
return result, nil
}
// DevicesArgs is a argument struct for selecting Devices.
// Only devices that match the specified criteria are returned.
type DevicesArgs struct {
Hostname []string
MACAddresses []string
SystemIDs []string
Domain string
Zone string
Pool string
AgentName string
}
// Devices implements Controller.
func (c *controller) Devices(args DevicesArgs) ([]Device, error) {
params := NewURLParams()
params.MaybeAddMany("hostname", args.Hostname)
params.MaybeAddMany("mac_address", args.MACAddresses)
params.MaybeAddMany("id", args.SystemIDs)
params.MaybeAdd("domain", args.Domain)
params.MaybeAdd("zone", args.Zone)
params.MaybeAdd("pool", args.Pool)
params.MaybeAdd("agent_name", args.AgentName)
source, err := c.getQuery("devices", params.Values)
if err != nil {
return nil, NewUnexpectedError(err)
}
devices, err := readDevices(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []Device
for _, d := range devices {
d.controller = c
result = append(result, d)
}
return result, nil
}
// CreateDeviceArgs is a argument struct for passing information into CreateDevice.
type CreateDeviceArgs struct {
Hostname string
MACAddresses []string
Domain string
Parent string
}
// Devices implements Controller.
func (c *controller) CreateDevice(args CreateDeviceArgs) (Device, error) {
// There must be at least one mac address.
if len(args.MACAddresses) == 0 {
return nil, NewBadRequestError("at least one MAC address must be specified")
}
params := NewURLParams()
params.MaybeAdd("hostname", args.Hostname)
params.MaybeAdd("domain", args.Domain)
params.MaybeAddMany("mac_addresses", args.MACAddresses)
params.MaybeAdd("parent", args.Parent)
result, err := c.post("devices", "", params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
if svrErr.StatusCode == http.StatusBadRequest {
return nil, errors.Wrap(err, NewBadRequestError(svrErr.BodyMessage))
}
}
// Translate http errors.
return nil, NewUnexpectedError(err)
}
device, err := readDevice(c.apiVersion, result)
if err != nil {
return nil, errors.Trace(err)
}
device.controller = c
return device, nil
}
// MachinesArgs is a argument struct for selecting Machines.
// Only machines that match the specified criteria are returned.
type MachinesArgs struct {
Hostnames []string
MACAddresses []string
SystemIDs []string
Domain string
Zone string
Pool string
AgentName string
Tags []string
OwnerData map[string]string
}
// Machines implements Controller.
func (c *controller) Machines(args MachinesArgs) ([]Machine, error) {
params := NewURLParams()
params.MaybeAddMany("hostname", args.Hostnames)
params.MaybeAddMany("mac_address", args.MACAddresses)
params.MaybeAddMany("id", args.SystemIDs)
params.MaybeAdd("domain", args.Domain)
params.MaybeAdd("zone", args.Zone)
params.MaybeAdd("pool", args.Pool)
params.MaybeAdd("agent_name", args.AgentName)
params.MaybeAddMany("tags", args.Tags)
// At the moment the MAAS API doesn't support filtering by owner
// data so we do that ourselves below.
source, err := c.getQuery("machines", params.Values)
if err != nil {
return nil, NewUnexpectedError(err)
}
machines, err := readMachines(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []Machine
for _, m := range machines {
m.controller = c
if ownerDataMatches(m.ownerData, args.OwnerData) {
result = append(result, m)
}
}
return result, nil
}
func ownerDataMatches(ownerData, filter map[string]string) bool {
for key, value := range filter {
if ownerData[key] != value {
return false
}
}
return true
}
// StorageSpec represents one element of storage constraints necessary
// to be satisfied to allocate a machine.
type StorageSpec struct {
// Label is optional and an arbitrary string. Labels need to be unique
// across the StorageSpec elements specified in the AllocateMachineArgs.
Label string
// Size is required and refers to the required minimum size in GB.
Size int
// Zero or more tags associated to the disks.
Tags []string
}
// Validate ensures that there is a positive size and that there are no Empty
// tag values.
func (s *StorageSpec) Validate() error {
if s.Size <= 0 {
return errors.NotValidf("Size value %d", s.Size)
}
for _, v := range s.Tags {
if v == "" {
return errors.NotValidf("empty tag")
}
}
return nil
}
// String returns the string representation of the storage spec.
func (s *StorageSpec) String() string {
label := s.Label
if label != "" {
label += ":"
}
tags := strings.Join(s.Tags, ",")
if tags != "" {
tags = "(" + tags + ")"
}
return fmt.Sprintf("%s%d%s", label, s.Size, tags)
}
// InterfaceSpec represents one element of network related constraints.
type InterfaceSpec struct {
// Label is required and an arbitrary string. Labels need to be unique
// across the InterfaceSpec elements specified in the AllocateMachineArgs.
// The label is returned in the ConstraintMatches response from
// AllocateMachine.
Label string
Space string
// NOTE: there are other interface spec values that we are not exposing at
// this stage that can be added on an as needed basis. Other possible values are:
// 'fabric_class', 'not_fabric_class',
// 'subnet_cidr', 'not_subnet_cidr',
// 'vid', 'not_vid',
// 'fabric', 'not_fabric',
// 'subnet', 'not_subnet',
// 'mode'
}
// Validate ensures that a Label is specified and that there is at least one
// Space or NotSpace value set.
func (a *InterfaceSpec) Validate() error {
if a.Label == "" {
return errors.NotValidf("missing Label")
}
// Perhaps at some stage in the future there will be other possible specs
// supported (like vid, subnet, etc), but until then, just space to check.
if a.Space == "" {
return errors.NotValidf("empty Space constraint")
}
return nil
}
// String returns the interface spec as MaaS requires it.
func (a *InterfaceSpec) String() string {
return fmt.Sprintf("%s:space=%s", a.Label, a.Space)
}
// AllocateMachineArgs is an argument struct for passing args into Machine.Allocate.
type AllocateMachineArgs struct {
Hostname string
SystemId string
Architecture string
MinCPUCount int
// MinMemory represented in MB.
MinMemory int
Tags []string
NotTags []string
Zone string
Pool string
NotInZone []string
NotInPool []string
// Storage represents the required disks on the Machine. If any are specified
// the first value is used for the root disk.
Storage []StorageSpec
// Interfaces represents a number of required interfaces on the machine.
// Each InterfaceSpec relates to an individual network interface.
Interfaces []InterfaceSpec
// NotSpace is a machine level constraint, and applies to the entire machine
// rather than specific interfaces.
NotSpace []string
AgentName string
Comment string
DryRun bool
}
// Validate makes sure that any labels specified in Storage or Interfaces
// are unique, and that the required specifications are valid. It
// also makes sure that any pools specified exist.
func (a *AllocateMachineArgs) Validate() error {
storageLabels := set.NewStrings()
for _, spec := range a.Storage {
if err := spec.Validate(); err != nil {
return errors.Annotate(err, "Storage")
}
if spec.Label != "" {
if storageLabels.Contains(spec.Label) {
return errors.NotValidf("reusing storage label %q", spec.Label)
}
storageLabels.Add(spec.Label)
}
}
interfaceLabels := set.NewStrings()
for _, spec := range a.Interfaces {
if err := spec.Validate(); err != nil {
return errors.Annotate(err, "Interfaces")
}
if interfaceLabels.Contains(spec.Label) {
return errors.NotValidf("reusing interface label %q", spec.Label)
}
interfaceLabels.Add(spec.Label)
}
for _, v := range a.NotSpace {
if v == "" {
return errors.NotValidf("empty NotSpace constraint")
}
}
return nil
}
func (a *AllocateMachineArgs) storage() string {
var values []string
for _, spec := range a.Storage {
values = append(values, spec.String())
}
return strings.Join(values, ",")
}
func (a *AllocateMachineArgs) interfaces() string {
var values []string
for _, spec := range a.Interfaces {
values = append(values, spec.String())
}
return strings.Join(values, ";")
}
func (a *AllocateMachineArgs) notSubnets() []string {
var values []string
for _, v := range a.NotSpace {
values = append(values, "space:"+v)
}
return values
}
// ConstraintMatches provides a way for the caller of AllocateMachine to determine
//.how the allocated machine matched the storage and interfaces constraints specified.
// The labels that were used in the constraints are the keys in the maps.
type ConstraintMatches struct {
// Interface is a mapping of the constraint label specified to the Interfaces
// that match that constraint.
Interfaces map[string][]Interface
// Storage is a mapping of the constraint label specified to the StorageDevice
// that match that constraint.
Storage map[string][]StorageDevice
}
// AllocateMachine implements Controller.
//
// 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 := NewURLParams()
params.MaybeAdd("name", args.Hostname)
params.MaybeAdd("system_id", args.SystemId)
params.MaybeAdd("arch", args.Architecture)
params.MaybeAddInt("cpu_count", args.MinCPUCount)
params.MaybeAddInt("mem", args.MinMemory)
params.MaybeAddMany("tags", args.Tags)
params.MaybeAddMany("not_tags", args.NotTags)
params.MaybeAdd("storage", args.storage())
params.MaybeAdd("interfaces", args.interfaces())
params.MaybeAddMany("not_subnets", args.notSubnets())
params.MaybeAdd("zone", args.Zone)
params.MaybeAdd("pool", args.Pool)
params.MaybeAddMany("not_in_zone", args.NotInZone)
params.MaybeAddMany("not_in_pool", args.NotInPool)
params.MaybeAdd("agent_name", args.AgentName)
params.MaybeAdd("comment", args.Comment)
params.MaybeAddBool("dry_run", args.DryRun)
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).(ServerError); ok {
if svrErr.StatusCode == http.StatusConflict {
return nil, matches, errors.Wrap(err, NewNoMatchError(svrErr.BodyMessage))
}
}
// Translate http errors.
return nil, matches, NewUnexpectedError(err)
}
machine, err := readMachine(c.apiVersion, result)
if err != nil {
return nil, matches, errors.Trace(err)
}
machine.controller = c
// Parse the constraint matches.
matches, err = parseAllocateConstraintsResponse(result, machine)
if err != nil {
return nil, matches, errors.Trace(err)
}
return machine, matches, nil
}
// ReleaseMachinesArgs is an argument struct for passing the machine system IDs
// and an optional comment into the ReleaseMachines method.
type ReleaseMachinesArgs struct {
SystemIDs []string
Comment string
}
// ReleaseMachines implements Controller.
//
// 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 := NewURLParams()
params.MaybeAddMany("machines", args.SystemIDs)
params.MaybeAdd("comment", args.Comment)
_, err := c.post("machines", "release", params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
switch svrErr.StatusCode {
case http.StatusBadRequest:
return errors.Wrap(err, NewBadRequestError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
case http.StatusConflict:
return errors.Wrap(err, NewCannotCompleteError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}
return nil
}
// Files implements Controller.
func (c *controller) Files(prefix string) ([]File, error) {
params := NewURLParams()
params.MaybeAdd("prefix", prefix)
source, err := c.getQuery("files", params.Values)
if err != nil {
return nil, NewUnexpectedError(err)
}
files, err := readFiles(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
var result []File
for _, f := range files {
f.controller = c
result = append(result, f)
}
return result, nil
}
// GetFile implements Controller.
func (c *controller) GetFile(filename string) (File, error) {
if filename == "" {
return nil, errors.NotValidf("missing filename")
}
source, err := c.get("files/" + filename)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
if svrErr.StatusCode == http.StatusNotFound {
return nil, errors.Wrap(err, NewNoMatchError(svrErr.BodyMessage))
}
}
return nil, NewUnexpectedError(err)
}
file, err := readFile(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}
file.controller = c
return file, nil
}
// AddFileArgs is a argument struct for passing information into AddFile.
// One of Content or (Reader, Length) must be specified.
type AddFileArgs struct {
Filename string
Content []byte
Reader io.Reader
Length int64
}
// Validate checks to make sure the filename has no slashes, and that one of
// Content or (Reader, Length) is specified.
func (a *AddFileArgs) Validate() error {
dir, _ := path.Split(a.Filename)
if dir != "" {
return errors.NotValidf("paths in Filename %q", a.Filename)
}
if a.Filename == "" {
return errors.NotValidf("missing Filename")
}
if a.Content == nil {
if a.Reader == nil {
return errors.NotValidf("missing Content or Reader")
}
if a.Length == 0 {
return errors.NotValidf("missing Length")
}
} else {
if a.Reader != nil {
return errors.NotValidf("specifying Content and Reader")
}
if a.Length != 0 {
return errors.NotValidf("specifying Length and Content")
}
}
return nil
}
// AddFile implements Controller.
func (c *controller) AddFile(args AddFileArgs) error {
if err := args.Validate(); err != nil {
return errors.Trace(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).(ServerError); ok {
if svrErr.StatusCode == http.StatusBadRequest {
return errors.Wrap(err, NewBadRequestError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}
return nil
}
func (c *controller) checkCreds() error {
if _, err := c.getOp("users", "whoami"); err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
if svrErr.StatusCode == http.StatusUnauthorized {
return errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}
return nil
}
func (c *controller) put(path string, params url.Values) (interface{}, error) {
path = EnsureTrailingSlash(path)
requestID := nextRequestID()
logger.Tracef("request %x: PUT %s%s, params: %s", requestID, c.client.APIURL, path, params.Encode())
bytes, err := c.client.Put(&url.URL{Path: path}, params, nil)
if err != nil {
logger.Tracef("response %x: error: %q", requestID, err.Error())
logger.Tracef("error detail: %#v", err)
return nil, errors.Trace(err)
}
logger.Tracef("response %x: %s", requestID, string(bytes))
var parsed interface{}
err = json.Unmarshal(bytes, &parsed)
if err != nil {
return nil, errors.Trace(err)
}
return parsed, nil
}
func (c *controller) post(path, op string, params url.Values) (interface{}, error) {
bytes, err := c._postRaw(path, op, params, nil)
if err != nil {
return nil, errors.Trace(err)
}
var parsed interface{}
err = json.Unmarshal(bytes, &parsed)
if err != nil {
return nil, errors.Trace(err)
}
return parsed, nil
}
func (c *controller) postFile(path, op string, params url.Values, fileContent []byte) (interface{}, error) {
// Only one file is ever sent at a time.
files := map[string][]byte{"file": fileContent}
return c._postRaw(path, op, params, files)
}
func (c *controller) _postRaw(path, op string, params url.Values, files map[string][]byte) ([]byte, error) {
path = EnsureTrailingSlash(path)
requestID := nextRequestID()
if logger.IsTraceEnabled() {
opArg := ""
if op != "" {
opArg = "?op=" + op
}
logger.Tracef("request %x: POST %s%s%s, params=%s", requestID, c.client.APIURL, path, opArg, params.Encode())
}
bytes, err := c.client.Post(&url.URL{Path: path}, op, params, files)
if err != nil {
logger.Tracef("response %x: error: %q", requestID, err.Error())
logger.Tracef("error detail: %#v", err)
return nil, errors.Trace(err)
}
logger.Tracef("response %x: %s", requestID, string(bytes))
return bytes, nil
}
func (c *controller) delete(path string) error {
path = EnsureTrailingSlash(path)
requestID := nextRequestID()
logger.Tracef("request %x: DELETE %s%s", requestID, c.client.APIURL, path)
err := c.client.Delete(&url.URL{Path: path})
if err != nil {
logger.Tracef("response %x: error: %q", requestID, err.Error())
logger.Tracef("error detail: %#v", err)
return errors.Trace(err)
}
logger.Tracef("response %x: complete", requestID)
return nil
}
func (c *controller) getQuery(path string, params url.Values) (interface{}, error) {
return c._get(path, "", params)
}
func (c *controller) get(path string) (interface{}, error) {
return c._get(path, "", nil)
}
func (c *controller) getOp(path, op string) (interface{}, error) {
return c._get(path, op, nil)
}
func (c *controller) _get(path, op string, params url.Values) (interface{}, error) {
bytes, err := c._getRaw(path, op, params)
if err != nil {
return nil, errors.Trace(err)
}
var parsed interface{}
err = json.Unmarshal(bytes, &parsed)
if err != nil {
return nil, errors.Trace(err)
}
return parsed, nil
}
func (c *controller) _getRaw(path, op string, params url.Values) ([]byte, error) {
path = EnsureTrailingSlash(path)
requestID := nextRequestID()
if logger.IsTraceEnabled() {
var query string
if params != nil {
query = "?" + params.Encode()
}
logger.Tracef("request %x: GET %s%s%s", requestID, c.client.APIURL, path, query)
}
bytes, err := c.client.Get(&url.URL{Path: path}, op, params)
if err != nil {
logger.Tracef("response %x: error: %q", requestID, err.Error())
logger.Tracef("error detail: %#v", err)
return nil, errors.Trace(err)
}
logger.Tracef("response %x: %s", requestID, string(bytes))
return bytes, nil
}
func nextRequestID() int64 {
return atomic.AddInt64(&requestNumber, 1)
}
func indicatesUnsupportedVersion(err error) bool {
if err == nil {
return false
}
if serverErr, ok := errors.Cause(err).(ServerError); ok {
code := serverErr.StatusCode
return code == http.StatusNotFound || code == http.StatusGone
}
// Workaround for bug in MAAS 1.9.4 - instead of a 404 we get a
// redirect to the HTML login page, which doesn't parse as JSON.
// https://bugs.launchpad.net/maas/+bug/1583715
if syntaxErr, ok := errors.Cause(err).(*json.SyntaxError); ok {
message := "invalid character '<' looking for beginning of value"
return syntaxErr.Offset == 1 && syntaxErr.Error() == message
}
return false
}
// APIVersionInfo returns the version and subversion strings for the MAAS
// controller.
func (c *controller) APIVersionInfo() (string, string, error) {
version, subversion, _, err := c.readAPIVersionInfo()
return version, subversion, err
}
func (c *controller) readAPIVersionInfo() (string, string, set.Strings, error) {
parsed, err := c.get("version")
if indicatesUnsupportedVersion(err) {
return "", "", nil, WrapWithUnsupportedVersionError(err)
} else if err != nil {
return "", "", nil, errors.Trace(err)
}
fields := schema.Fields{
"capabilities": schema.List(schema.String()),
"version": schema.String(),
"subversion": schema.String(),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(parsed, nil)
if err != nil {
return "", "", nil, WrapWithDeserializationError(err, "version response")
}
// For now, we don't append any subversion, but as it becomes used, we
// should parse and check.
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
capabilities := set.NewStrings()
capabilityValues := valid["capabilities"].([]interface{})
for _, value := range capabilityValues {
capabilities.Add(value.(string))
}
version := valid["version"].(string)
subversion := valid["subversion"].(string)
return version, subversion, capabilities, nil
}
func parseAllocateConstraintsResponse(source interface{}, machine *machine) (ConstraintMatches, error) {
var empty ConstraintMatches
matchFields := schema.Fields{
"storage": schema.StringMap(schema.List(schema.Any())),
"interfaces": schema.StringMap(schema.List(schema.ForceInt())),
}
matchDefaults := schema.Defaults{
"storage": schema.Omit,
"interfaces": schema.Omit,
}
fields := schema.Fields{
"constraints_by_type": schema.FieldMap(matchFields, matchDefaults),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return empty, WrapWithDeserializationError(err, "allocation constraints response schema check failed")
}
valid := coerced.(map[string]interface{})
constraintsMap := valid["constraints_by_type"].(map[string]interface{})
result := ConstraintMatches{
Interfaces: make(map[string][]Interface),
Storage: make(map[string][]StorageDevice),
}
if interfaceMatches, found := constraintsMap["interfaces"]; found {
matches := convertConstraintMatchesInt(interfaceMatches)
for label, ids := range matches {
interfaces := make([]Interface, len(ids))
for index, id := range ids {
iface := machine.Interface(id)
if iface == nil {
return empty, NewDeserializationError("constraint match interface %q: %d does not match an interface for the machine", label, id)
}
interfaces[index] = iface
}
result.Interfaces[label] = interfaces
}
}
if storageMatches, found := constraintsMap["storage"]; found {
matches := convertConstraintMatchesAny(storageMatches)
for label, ids := range matches {