-
Notifications
You must be signed in to change notification settings - Fork 24
/
storageconstraint.go
111 lines (93 loc) · 2.94 KB
/
storageconstraint.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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// StorageConstraintArgs is an argument struct used to create a new internal
// storageconstraint type that supports the StorageConstraint interface.
type StorageConstraintArgs struct {
Pool string
Size uint64
Count uint64
}
func newStorageConstraint(args StorageConstraintArgs) *storageconstraint {
return &storageconstraint{
Version: 1,
Pool_: args.Pool,
Size_: args.Size,
Count_: args.Count,
}
}
type storageconstraint struct {
Version int `yaml:"version"`
Pool_ string `yaml:"pool"`
Size_ uint64 `yaml:"size"`
Count_ uint64 `yaml:"count"`
}
// Pool implements StorageConstraint.
func (s *storageconstraint) Pool() string {
return s.Pool_
}
// Size implements StorageConstraint.
func (s *storageconstraint) Size() uint64 {
return s.Size_
}
// Count implements StorageConstraint.
func (s *storageconstraint) Count() uint64 {
return s.Count_
}
func importStorageConstraints(sourceMap map[string]interface{}) (map[string]*storageconstraint, error) {
result := make(map[string]*storageconstraint)
for key, value := range sourceMap {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for storageconstraint %q, %T", key, value)
}
constraint, err := importStorageConstraint(source)
if err != nil {
return nil, errors.Trace(err)
}
result[key] = constraint
}
return result, nil
}
// importStorageConstraint constructs a new StorageConstraint from a map representing a serialised
// StorageConstraint instance.
func importStorageConstraint(source map[string]interface{}) (*storageconstraint, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "storageconstraint version schema check failed")
}
importFunc, ok := storageconstraintDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type storageconstraintDeserializationFunc func(map[string]interface{}) (*storageconstraint, error)
var storageconstraintDeserializationFuncs = map[int]storageconstraintDeserializationFunc{
1: importStorageConstraintV1,
}
func importStorageConstraintV1(source map[string]interface{}) (*storageconstraint, error) {
fields := schema.Fields{
"pool": schema.String(),
"size": schema.Uint(),
"count": schema.Uint(),
}
checker := schema.FieldMap(fields, nil)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "storageconstraint v1 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
return &storageconstraint{
Version: 1,
Pool_: valid["pool"].(string),
Size_: valid["size"].(uint64),
Count_: valid["count"].(uint64),
}, nil
}