forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offerconnections.go
158 lines (131 loc) · 4.67 KB
/
offerconnections.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// OfferConnection represents an offer connection for a an application's endpoints.
type OfferConnection interface {
OfferUUID() string
RelationID() int
RelationKey() string
UserName() string
SourceModelUUID() string
}
var _ OfferConnection = (*offerConnection)(nil)
type offerConnections struct {
Version int `yaml:"version"`
OfferConnections []*offerConnection `yaml:"offer-connections"`
}
type offerConnection struct {
OfferUUID_ string `yaml:"offer-uuid"`
RelationID_ int `yaml:"relation-id"`
RelationKey_ string `yaml:"relation-key"`
UserName_ string `yaml:"user-name"`
SourceModelUUID_ string `yaml:"source-model-uuid"`
}
// OfferConnectionArgs is an argument struct used to add a offer connection to
// the model.
type OfferConnectionArgs struct {
OfferUUID string
RelationID int
RelationKey string
UserName string
SourceModelUUID string
}
func newOfferConnection(args OfferConnectionArgs) *offerConnection {
return &offerConnection{
OfferUUID_: args.OfferUUID,
RelationID_: args.RelationID,
RelationKey_: args.RelationKey,
UserName_: args.UserName,
SourceModelUUID_: args.SourceModelUUID,
}
}
// OfferUUID returns the offer uuid for the connection.
func (c *offerConnection) OfferUUID() string {
return c.OfferUUID_
}
// RelationID returns the relation id for the connection.
func (c *offerConnection) RelationID() int {
return c.RelationID_
}
// RelationKey returns the relation key for the connection.
func (c *offerConnection) RelationKey() string {
return c.RelationKey_
}
// UserName returns the user name for the connection.
func (c *offerConnection) UserName() string {
return c.UserName_
}
// SourceModelUUID returns the user name for the connection.
func (c *offerConnection) SourceModelUUID() string {
return c.SourceModelUUID_
}
var offerConnectionDeserializationFuncs = map[int]offerConnectionDeserializationFunc{
1: importOfferConnectionV1,
}
func importOfferConnections(source interface{}) ([]*offerConnection, error) {
checker := versionedChecker("offer-connections")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "offer connections version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := offerConnectionDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["offer-connections"].([]interface{})
return importOfferConnectionList(sourceList, importFunc)
}
type offerConnectionDeserializationFunc func(interface{}) (*offerConnection, error)
func importOfferConnectionList(sourceList []interface{}, importFunc offerConnectionDeserializationFunc) ([]*offerConnection, error) {
result := make([]*offerConnection, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for offer connection %d, %T", i, value)
}
offerConnection, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "offer connection %d", i)
}
result[i] = offerConnection
}
return result, nil
}
func offerConnectionV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"offer-uuid": schema.String(),
"relation-id": schema.Int(),
"relation-key": schema.String(),
"user-name": schema.String(),
"source-model-uuid": schema.String(),
}
return fields, schema.Defaults{}
}
func importOfferConnection(fields schema.Fields, defaults schema.Defaults, importVersion int, source interface{}) (*offerConnection, error) {
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "offer connection v%d schema check failed", importVersion)
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &offerConnection{
OfferUUID_: valid["offer-uuid"].(string),
RelationID_: int(valid["relation-id"].(int64)),
RelationKey_: valid["relation-key"].(string),
SourceModelUUID_: valid["source-model-uuid"].(string),
UserName_: valid["user-name"].(string),
}
return result, nil
}
func importOfferConnectionV1(source interface{}) (*offerConnection, error) {
fields, defaults := offerConnectionV1Fields()
return importOfferConnection(fields, defaults, 1, source)
}