David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package gomaasapi |
| 5 | |
| 6 | import ( |
| 7 | "github.com/juju/errors" |
| 8 | "github.com/juju/schema" |
| 9 | "github.com/juju/version" |
| 10 | ) |
| 11 | |
| 12 | type 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. |
| 25 | func (s *space) ID() int { |
| 26 | return s.id |
| 27 | } |
| 28 | |
| 29 | // Name implements Space. |
| 30 | func (s *space) Name() string { |
| 31 | return s.name |
| 32 | } |
| 33 | |
| 34 | // Subnets implements Space. |
| 35 | func (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 | |
| 43 | func 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. |
| 65 | func 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 | |
| 81 | type spaceDeserializationFunc func(map[string]interface{}) (*space, error) |
| 82 | |
| 83 | var spaceDeserializationFuncs = map[version.Number]spaceDeserializationFunc{ |
| 84 | twoDotOh: space_2_0, |
| 85 | } |
| 86 | |
| 87 | func 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 | } |