-
Notifications
You must be signed in to change notification settings - Fork 59
/
resources.go
86 lines (71 loc) · 1.8 KB
/
resources.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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/charm/v12/resource"
)
var resourceSchema = schema.FieldMap(
schema.Fields{
"type": schema.String(),
"filename": schema.String(), // TODO(ericsnow) Change to "path"?
"description": schema.String(),
},
schema.Defaults{
"type": resource.TypeFile.String(),
"filename": "",
"description": "",
},
)
func parseMetaResources(data interface{}) (map[string]resource.Meta, error) {
if data == nil {
return nil, nil
}
result := make(map[string]resource.Meta)
for name, val := range data.(map[string]interface{}) {
meta, err := parseResourceMeta(name, val)
if err != nil {
return nil, err
}
result[name] = meta
}
return result, nil
}
func validateMetaResources(resources map[string]resource.Meta) error {
for name, res := range resources {
if res.Name != name {
return fmt.Errorf("mismatch on resource name (%q != %q)", res.Name, name)
}
if err := res.Validate(); err != nil {
return err
}
}
return nil
}
// parseResourceMeta parses the provided data into a Meta, assuming
// that the data has first been checked with resourceSchema.
func parseResourceMeta(name string, data interface{}) (resource.Meta, error) {
meta := resource.Meta{
Name: name,
}
if data == nil {
return meta, nil
}
rMap := data.(map[string]interface{})
if val := rMap["type"]; val != nil {
var err error
meta.Type, err = resource.ParseType(val.(string))
if err != nil {
return meta, errors.Trace(err)
}
}
if val := rMap["filename"]; val != nil {
meta.Path = val.(string)
}
if val := rMap["description"]; val != nil {
meta.Description = val.(string)
}
return meta, nil
}