-
Notifications
You must be signed in to change notification settings - Fork 23
/
remoteentity.go
125 lines (109 loc) · 3.29 KB
/
remoteentity.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
124
125
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// RemoteEntity represents the internal state of a remote entity.
// Remote entities may be exported local entities, or imported
// remote entities
type RemoteEntity interface {
ID() string
Token() string
Macaroon() string
}
type remoteEntities struct {
Version int `yaml:"version"`
RemoteEntities []*remoteEntity `yaml:"remote-entities"`
}
type remoteEntity struct {
ID_ string `yaml:"id"`
Token_ string `yaml:"token"`
Macaroon_ string `yaml:"macaroon"`
}
// RemoteEntityArgs is an argument struct used to add a remote entity.
type RemoteEntityArgs struct {
ID string
Token string
Macaroon string
}
func newRemoteEntity(args RemoteEntityArgs) *remoteEntity {
f := &remoteEntity{
ID_: args.ID,
Token_: args.Token,
Macaroon_: args.Macaroon,
}
return f
}
// ID implements RemoteEntity
func (f *remoteEntity) ID() string {
return f.ID_
}
// Token implements RemoteEntity
func (f *remoteEntity) Token() string {
return f.Token_
}
// Macaroon implements RemoteEntity
func (f *remoteEntity) Macaroon() string {
return f.Macaroon_
}
func importRemoteEntities(source interface{}) ([]*remoteEntity, error) {
checker := versionedChecker("remote-entities")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "remote entities version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
getFields, ok := remoteEntityFieldsFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["remote-entities"].([]interface{})
return importRemoteEntityList(sourceList, schema.FieldMap(getFields()), version)
}
func importRemoteEntityList(sourceList []interface{}, checker schema.Checker, version int) ([]*remoteEntity, error) {
result := make([]*remoteEntity, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for remote entity %d, %T", i, value)
}
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "remote entity %d v%d schema check failed", i, version)
}
valid := coerced.(map[string]interface{})
remoteEnt, err := newRemoteEntityFromValid(valid, version)
if err != nil {
return nil, errors.Annotatef(err, "remote entity %d", i)
}
result[i] = remoteEnt
}
return result, nil
}
func newRemoteEntityFromValid(valid map[string]interface{}, version int) (*remoteEntity, error) {
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &remoteEntity{
ID_: valid["id"].(string),
Token_: valid["token"].(string),
Macaroon_: valid["macaroon"].(string),
}
return result, nil
}
var remoteEntityFieldsFuncs = map[int]fieldsFunc{
1: remoteEntityV1Fields,
}
func remoteEntityV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"id": schema.String(),
"token": schema.String(),
"macaroon": schema.String(),
}
defaults := schema.Defaults{
"macaroon": schema.Omit,
}
return fields, defaults
}