blob: 5b8b8cf187125b6494c2e2664f90d0a822896ebf [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2016 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package gomaasapi
5
6import (
7 "github.com/juju/errors"
8 "github.com/juju/schema"
9 "github.com/juju/version"
10)
11
12type space struct {
13 // Add the controller in when we need to do things with the space.
14 // controller Controller
15
16 resourceURI string
17
18 id int
19 name string
20
21 subnets []*subnet
22}
23
24// Id implements Space.
25func (s *space) ID() int {
26 return s.id
27}
28
29// Name implements Space.
30func (s *space) Name() string {
31 return s.name
32}
33
34// Subnets implements Space.
35func (s *space) Subnets() []Subnet {
36 var result []Subnet
37 for _, subnet := range s.subnets {
38 result = append(result, subnet)
39 }
40 return result
41}
42
43func readSpaces(controllerVersion version.Number, source interface{}) ([]*space, error) {
44 checker := schema.List(schema.StringMap(schema.Any()))
45 coerced, err := checker.Coerce(source, nil)
46 if err != nil {
47 return nil, errors.Annotatef(err, "space base schema check failed")
48 }
49 valid := coerced.([]interface{})
50
51 var deserialisationVersion version.Number
52 for v := range spaceDeserializationFuncs {
53 if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 {
54 deserialisationVersion = v
55 }
56 }
57 if deserialisationVersion == version.Zero {
58 return nil, errors.Errorf("no space read func for version %s", controllerVersion)
59 }
60 readFunc := spaceDeserializationFuncs[deserialisationVersion]
61 return readSpaceList(valid, readFunc)
62}
63
64// readSpaceList expects the values of the sourceList to be string maps.
65func readSpaceList(sourceList []interface{}, readFunc spaceDeserializationFunc) ([]*space, error) {
66 result := make([]*space, 0, len(sourceList))
67 for i, value := range sourceList {
68 source, ok := value.(map[string]interface{})
69 if !ok {
70 return nil, errors.Errorf("unexpected value for space %d, %T", i, value)
71 }
72 space, err := readFunc(source)
73 if err != nil {
74 return nil, errors.Annotatef(err, "space %d", i)
75 }
76 result = append(result, space)
77 }
78 return result, nil
79}
80
81type spaceDeserializationFunc func(map[string]interface{}) (*space, error)
82
83var spaceDeserializationFuncs = map[version.Number]spaceDeserializationFunc{
84 twoDotOh: space_2_0,
85}
86
87func space_2_0(source map[string]interface{}) (*space, error) {
88 fields := schema.Fields{
89 "resource_uri": schema.String(),
90 "id": schema.ForceInt(),
91 "name": schema.String(),
92 "subnets": schema.List(schema.StringMap(schema.Any())),
93 }
94 checker := schema.FieldMap(fields, nil) // no defaults
95 coerced, err := checker.Coerce(source, nil)
96 if err != nil {
97 return nil, errors.Annotatef(err, "space 2.0 schema check failed")
98 }
99 valid := coerced.(map[string]interface{})
100 // From here we know that the map returned from the schema coercion
101 // contains fields of the right type.
102
103 subnets, err := readSubnetList(valid["subnets"].([]interface{}), subnet_2_0)
104 if err != nil {
105 return nil, errors.Trace(err)
106 }
107
108 result := &space{
109 resourceURI: valid["resource_uri"].(string),
110 id: valid["id"].(int),
111 name: valid["name"].(string),
112 subnets: subnets,
113 }
114 return result, nil
115}