-
Notifications
You must be signed in to change notification settings - Fork 23
/
remoteendpoint.go
123 lines (104 loc) · 3.28 KB
/
remoteendpoint.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 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// RemoteEndpoint represents a connection point that can be related to
// another application.
type RemoteEndpoint interface {
Name() string
Role() string
Interface() string
}
type remoteEndpoints struct {
Version int `yaml:"version"`
Endpoints []*remoteEndpoint `yaml:"endpoints"`
}
type remoteEndpoint struct {
Name_ string `yaml:"name"`
Role_ string `yaml:"role"`
Interface_ string `yaml:"interface"`
}
// RemoteEndpointArgs is an argument struct used to add a remote
// endpoint to a remote application.
type RemoteEndpointArgs struct {
Name string
Role string
Interface string
}
func newRemoteEndpoint(args RemoteEndpointArgs) *remoteEndpoint {
return &remoteEndpoint{
Name_: args.Name,
Role_: args.Role,
Interface_: args.Interface,
}
}
// Name implements RemoteEndpoint.
func (e *remoteEndpoint) Name() string {
return e.Name_
}
// Role implements RemoteEndpoint.
func (e *remoteEndpoint) Role() string {
return e.Role_
}
// Interface implements RemoteEndpoint.
func (e *remoteEndpoint) Interface() string {
return e.Interface_
}
func importRemoteEndpoints(sourceMap map[string]interface{}) ([]*remoteEndpoint, error) {
checker := versionedChecker("endpoints")
coerced, err := checker.Coerce(sourceMap, nil)
if err != nil {
return nil, errors.Annotatef(err, "remote endpoints version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := remoteEndpointDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["endpoints"].([]interface{})
return importRemoteEndpointList(sourceList, importFunc)
}
func importRemoteEndpointList(sourceList []interface{}, importFunc remoteEndpointDeserializationFunc) ([]*remoteEndpoint, error) {
var result []*remoteEndpoint
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for remote endpoint %d, %T", i, value)
}
device, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "remote endpoint %d", i)
}
result = append(result, device)
}
return result, nil
}
type remoteEndpointDeserializationFunc func(map[string]interface{}) (*remoteEndpoint, error)
var remoteEndpointDeserializationFuncs = map[int]remoteEndpointDeserializationFunc{
1: importRemoteEndpointV1,
}
func importRemoteEndpointV1(source map[string]interface{}) (*remoteEndpoint, error) {
fields := schema.Fields{
"name": schema.String(),
"role": schema.String(),
"interface": schema.String(),
}
checker := schema.FieldMap(fields, nil)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "remote endpoint 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.
result := &remoteEndpoint{
Name_: valid["name"].(string),
Role_: valid["role"].(string),
Interface_: valid["interface"].(string),
}
return result, nil
}