1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"github.com/juju/schema"
12
Version int `yaml:"version"`
13
Spaces_ []*space `yaml:"spaces"`
17
Name_ string `yaml:"name"`
18
Public_ bool `yaml:"public"`
19
ProviderID_ string `yaml:"provider-id,omitempty"`
22
// SpaceArgs is an argument struct used to create a new internal space
23
// type that supports the Space interface.
24
type SpaceArgs struct {
30
func newSpace(args SpaceArgs) *space {
34
ProviderID_: args.ProviderID,
38
// Name implements Space.
39
func (s *space) Name() string {
43
// Public implements Space.
44
func (s *space) Public() bool {
48
// ProviderID implements Space.
49
func (s *space) ProviderID() string {
53
func importSpaces(source map[string]interface{}) ([]*space, error) {
54
checker := versionedChecker("spaces")
55
coerced, err := checker.Coerce(source, nil)
57
return nil, errors.Annotatef(err, "spaces version schema check failed")
59
valid := coerced.(map[string]interface{})
61
version := int(valid["version"].(int64))
62
importFunc, ok := spaceDeserializationFuncs[version]
64
return nil, errors.NotValidf("version %d", version)
66
sourceList := valid["spaces"].([]interface{})
67
return importSpaceList(sourceList, importFunc)
70
func importSpaceList(sourceList []interface{}, importFunc spaceDeserializationFunc) ([]*space, error) {
71
result := make([]*space, 0, len(sourceList))
72
for i, value := range sourceList {
73
source, ok := value.(map[string]interface{})
75
return nil, errors.Errorf("unexpected value for space %d, %T", i, value)
77
space, err := importFunc(source)
79
return nil, errors.Annotatef(err, "space %d", i)
81
result = append(result, space)
86
type spaceDeserializationFunc func(map[string]interface{}) (*space, error)
88
var spaceDeserializationFuncs = map[int]spaceDeserializationFunc{
92
func importSpaceV1(source map[string]interface{}) (*space, error) {
93
fields := schema.Fields{
94
"name": schema.String(),
95
"public": schema.Bool(),
96
"provider-id": schema.String(),
98
// Some values don't have to be there.
99
defaults := schema.Defaults{
102
checker := schema.FieldMap(fields, defaults)
104
coerced, err := checker.Coerce(source, nil)
106
return nil, errors.Annotatef(err, "space v1 schema check failed")
108
valid := coerced.(map[string]interface{})
109
// From here we know that the map returned from the schema coercion
110
// contains fields of the right type.
113
Name_: valid["name"].(string),
114
Public_: valid["public"].(bool),
115
ProviderID_: valid["provider-id"].(string),