-
Notifications
You must be signed in to change notification settings - Fork 23
/
relationnetworks.go
123 lines (107 loc) · 3.46 KB
/
relationnetworks.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
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// RelationNetwork instances describe the ingress or egress
// networks required for a cross model relation.
type RelationNetwork interface {
ID() string
RelationKey() string
CIDRS() []string
}
type relationNetworks struct {
Version int `yaml:"version"`
RelationNetworks []*relationNetwork `yaml:"relation-networks"`
}
type relationNetwork struct {
ID_ string `yaml:"id"`
RelationKey_ string `yaml:"relation-key"`
CIDRS_ []string `yaml:"cidrs"`
}
// RelationNetworkArgs is an argument struct used to add a relation network
// to a model.
type RelationNetworkArgs struct {
ID string
RelationKey string
CIDRS []string
}
func newRelationNetwork(args RelationNetworkArgs) *relationNetwork {
r := &relationNetwork{
ID_: args.ID,
RelationKey_: args.RelationKey,
CIDRS_: args.CIDRS,
}
return r
}
// ID implements RelationNetwork
func (r *relationNetwork) ID() string {
return r.ID_
}
// RelationKey implements RelationNetwork
func (r *relationNetwork) RelationKey() string {
return r.RelationKey_
}
// CIDRS implements RelationNetwork
func (r *relationNetwork) CIDRS() []string {
return r.CIDRS_
}
func importRelationNetworks(source interface{}) ([]*relationNetwork, error) {
checker := versionedChecker("relation-networks")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "relation networks version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
getFields, ok := relationNetworksFieldsFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["relation-networks"].([]interface{})
return importRelationNetworkList(sourceList, schema.FieldMap(getFields()), version)
}
func importRelationNetworkList(sourceList []interface{}, checker schema.Checker, version int) ([]*relationNetwork, error) {
result := make([]*relationNetwork, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for relation network %d, %T", i, value)
}
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "relation network %d v%d schema check failed", i, version)
}
valid := coerced.(map[string]interface{})
relationNetw, err := newRelationNetworkFromValid(valid, version)
if err != nil {
return nil, errors.Annotatef(err, "relation network %d", i)
}
result[i] = relationNetw
}
return result, nil
}
func newRelationNetworkFromValid(valid map[string]interface{}, version int) (*relationNetwork, error) {
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &relationNetwork{
ID_: valid["id"].(string),
RelationKey_: valid["relation-key"].(string),
CIDRS_: convertToStringSlice(valid["cidrs"]),
}
return result, nil
}
var relationNetworksFieldsFuncs = map[int]fieldsFunc{
1: relationNetworksV1Fields,
}
func relationNetworksV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"id": schema.String(),
"relation-key": schema.String(),
"cidrs": schema.List(schema.String()),
}
defaults := schema.Defaults{}
return fields, defaults
}