-
Notifications
You must be signed in to change notification settings - Fork 59
/
payloads.go
73 lines (58 loc) · 1.45 KB
/
payloads.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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"fmt"
"github.com/juju/names/v5"
"github.com/juju/schema"
)
var payloadClassSchema = schema.FieldMap(
schema.Fields{
"type": schema.String(),
},
schema.Defaults{},
)
// PayloadClass holds the information about a payload class, as stored
// in a charm's metadata.
type PayloadClass struct {
// Name identifies the payload class.
Name string
// Type identifies the type of payload (e.g. kvm, docker).
Type string
}
func parsePayloadClasses(data interface{}) map[string]PayloadClass {
if data == nil {
return nil
}
result := make(map[string]PayloadClass)
for name, val := range data.(map[string]interface{}) {
result[name] = parsePayloadClass(name, val)
}
return result
}
func parsePayloadClass(name string, data interface{}) PayloadClass {
payloadClass := PayloadClass{
Name: name,
}
if data == nil {
return payloadClass
}
pcMap := data.(map[string]interface{})
if val := pcMap["type"]; val != nil {
payloadClass.Type = val.(string)
}
return payloadClass
}
// Validate checks the payload class to ensure its data is valid.
func (pc PayloadClass) Validate() error {
if pc.Name == "" {
return fmt.Errorf("payload class missing name")
}
if !names.IsValidPayload(pc.Name) {
return fmt.Errorf("invalid payload class %q", pc.Name)
}
if pc.Type == "" {
return fmt.Errorf("payload class missing type")
}
return nil
}