/
unitresources.go
111 lines (93 loc) · 2.87 KB
/
unitresources.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"
)
// UnitResource represents the revision of a resource used by a unit.
type UnitResource interface {
// Name returns the name of the resource.
Name() string
// Revision returns the revision of the resource as used by a
// particular unit.
Revision() ResourceRevision
}
// UnitResourceArgs is an argument struct used to specify the revision
// of a resource used by a unit.
type UnitResourceArgs struct {
Name string
RevisionArgs ResourceRevisionArgs
}
func newUnitResource(args UnitResourceArgs) *unitResource {
return &unitResource{
Name_: args.Name,
Revision_: newResourceRevision(args.RevisionArgs),
}
}
type unitResources struct {
Version int `yaml:"version"`
Resources_ []*unitResource `yaml:"resources"`
}
type unitResource struct {
Name_ string `yaml:"name"`
Revision_ *resourceRevision `yaml:"revision"`
}
// Name implements UnitResource.
func (ur *unitResource) Name() string {
return ur.Name_
}
// Revision implements UnitResource.
func (ur *unitResource) Revision() ResourceRevision {
return ur.Revision_
}
func importUnitResources(source map[string]interface{}) ([]*unitResource, error) {
checker := versionedChecker("resources")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "unit resources version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
if version != 1 {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["resources"].([]interface{})
return importUnitResourceList(sourceList)
}
func importUnitResourceList(sourceList []interface{}) ([]*unitResource, error) {
result := make([]*unitResource, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for resource %d, %T", i, value)
}
r, err := importUnitResourceV1(source)
if err != nil {
return nil, errors.Annotatef(err, "unit resource %d", i)
}
result = append(result, r)
}
return result, nil
}
func importUnitResourceV1(source map[string]interface{}) (*unitResource, error) {
fields := schema.Fields{
"name": schema.String(),
"revision": schema.StringMap(schema.Any()),
}
checker := schema.FieldMap(fields, nil)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "unit resource v1 schema check failed")
}
valid := coerced.(map[string]interface{})
r := &unitResource{
Name_: valid["name"].(string),
}
rev, err := importResourceRevisionV1(valid["revision"])
if err != nil {
return nil, errors.Annotatef(err, "unit resource %s", r.Name_)
}
r.Revision_ = rev
return r, nil
}