-
Notifications
You must be signed in to change notification settings - Fork 23
/
resource.go
319 lines (277 loc) · 8.93 KB
/
resource.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"time"
"github.com/juju/errors"
"github.com/juju/schema"
)
// Resource represents an application resource.
type Resource interface {
// Name returns the name of the resource.
Name() string
// SetApplicationRevision sets the application revision of the
// resource.
SetApplicationRevision(ResourceRevisionArgs) ResourceRevision
// ApplicationRevision returns the revision of the resource as set
// on the application. May return nil if SetApplicationRevision
// hasn't been called yet.
ApplicationRevision() ResourceRevision
// SetCharmStoreRevision sets the application revision of the
// resource.
SetCharmStoreRevision(ResourceRevisionArgs) ResourceRevision
// CharmStoreRevision returns the revision the charmstore has, as
// seen at the last poll. May return nil if SetCharmStoreRevision
// hasn't been called yet.
CharmStoreRevision() ResourceRevision
// Validate checks the consistency of the resource and its
// revisions.
Validate() error
}
// ResourceRevision represents a revision of an application resource.
type ResourceRevision interface {
Revision() int
Type() string
Path() string
Description() string
Origin() string
FingerprintHex() string
Size() int64
Timestamp() time.Time
Username() string
}
// ResourceArgs is an argument struct used to create a new internal
// resource type that supports the Resource interface.
type ResourceArgs struct {
Name string
}
// newResource returns a new *resource (which implements the Resource
// interface).
func newResource(args ResourceArgs) *resource {
return &resource{
Name_: args.Name,
}
}
type resources struct {
Version int `yaml:"version"`
Resources_ []*resource `yaml:"resources"`
}
type resource struct {
Name_ string `yaml:"name"`
ApplicationRevision_ *resourceRevision `yaml:"application-revision"`
CharmStoreRevision_ *resourceRevision `yaml:"charmstore-revision,omitempty"`
}
// ResourceRevisionArgs is an argument struct used to add a new
// internal resource revision to a Resource.
type ResourceRevisionArgs struct {
Revision int
Type string
Path string
Description string
Origin string
FingerprintHex string
Size int64
Timestamp time.Time
Username string
}
// Name implements Resource.
func (r *resource) Name() string {
return r.Name_
}
// SetApplicationRevision implements Resource.
func (r *resource) SetApplicationRevision(args ResourceRevisionArgs) ResourceRevision {
r.ApplicationRevision_ = newResourceRevision(args)
return r.ApplicationRevision_
}
// ApplicationRevision implements Resource.
func (r *resource) ApplicationRevision() ResourceRevision {
if r.ApplicationRevision_ == nil {
return nil // Return untyped nil when not set
}
return r.ApplicationRevision_
}
// SetCharmStoreRevision implements Resource.
func (r *resource) SetCharmStoreRevision(args ResourceRevisionArgs) ResourceRevision {
r.CharmStoreRevision_ = newResourceRevision(args)
return r.CharmStoreRevision_
}
// CharmStoreRevision implements Resource.
func (r *resource) CharmStoreRevision() ResourceRevision {
if r.CharmStoreRevision_ == nil {
return nil // Return untyped nil when not set
}
return r.CharmStoreRevision_
}
// Validate implements Resource.
func (r *resource) Validate() error {
if r.ApplicationRevision_ == nil {
return errors.New("no application revision set")
}
return nil
}
func newResourceRevision(args ResourceRevisionArgs) *resourceRevision {
return &resourceRevision{
Revision_: args.Revision,
Type_: args.Type,
Path_: args.Path,
Description_: args.Description,
Origin_: args.Origin,
FingerprintHex_: args.FingerprintHex,
Size_: args.Size,
Timestamp_: timePtr(args.Timestamp),
Username_: args.Username,
}
}
type resourceRevision struct {
Revision_ int `yaml:"revision"`
Type_ string `yaml:"type"`
Path_ string `yaml:"path"`
Description_ string `yaml:"description"`
Origin_ string `yaml:"origin"`
FingerprintHex_ string `yaml:"fingerprint"`
Size_ int64 `yaml:"size"`
Timestamp_ *time.Time `yaml:"timestamp,omitempty"`
Username_ string `yaml:"username,omitempty"`
}
// Revision implements ResourceRevision.
func (r *resourceRevision) Revision() int {
return r.Revision_
}
// Type implements ResourceRevision.
func (r *resourceRevision) Type() string {
return r.Type_
}
// Path implements ResourceRevision.
func (r *resourceRevision) Path() string {
return r.Path_
}
// Description implements ResourceRevision.
func (r *resourceRevision) Description() string {
return r.Description_
}
// Origin implements ResourceRevision.
func (r *resourceRevision) Origin() string {
return r.Origin_
}
// FingerprintHex implements ResourceRevision.
func (r *resourceRevision) FingerprintHex() string {
return r.FingerprintHex_
}
// Size implements ResourceRevision.
func (r *resourceRevision) Size() int64 {
return r.Size_
}
// Timestamp implements ResourceRevision.
func (r *resourceRevision) Timestamp() time.Time {
if r.Timestamp_ == nil {
return time.Time{}
}
return *r.Timestamp_
}
// Username implements ResourceRevision.
func (r *resourceRevision) Username() string {
return r.Username_
}
func importResources(source map[string]interface{}) ([]*resource, error) {
checker := versionedChecker("resources")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotate(err, "resources version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := resourceDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["resources"].([]interface{})
return importResourceList(sourceList, importFunc)
}
func importResourceList(sourceList []interface{}, importFunc resourceDeserializationFunc) ([]*resource, error) {
result := make([]*resource, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for resource %d, %T", i, value)
}
resource, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "resource %d", i)
}
result = append(result, resource)
}
return result, nil
}
type resourceDeserializationFunc func(map[string]interface{}) (*resource, error)
var resourceDeserializationFuncs = map[int]resourceDeserializationFunc{
1: importResourceV1,
}
func importResourceV1(source map[string]interface{}) (*resource, error) {
fields := schema.Fields{
"name": schema.String(),
"application-revision": schema.StringMap(schema.Any()),
"charmstore-revision": schema.StringMap(schema.Any()),
}
defaults := schema.Defaults{
"charmstore-revision": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "resource 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.
r := newResource(ResourceArgs{
Name: valid["name"].(string),
})
appRev, err := importResourceRevisionV1(valid["application-revision"])
if err != nil {
return nil, errors.Annotatef(err, "resource %s: application revision", r.Name_)
}
r.ApplicationRevision_ = appRev
if source, exists := valid["charmstore-revision"]; exists {
csRev, err := importResourceRevisionV1(source)
if err != nil {
return nil, errors.Annotatef(err, "resource %s: charmstore revision", r.Name_)
}
r.CharmStoreRevision_ = csRev
}
return r, nil
}
func importResourceRevisionV1(source interface{}) (*resourceRevision, error) {
fields := schema.Fields{
"revision": schema.Int(),
"type": schema.String(),
"path": schema.String(),
"description": schema.String(),
"origin": schema.String(),
"fingerprint": schema.String(),
"size": schema.Int(),
"timestamp": schema.Time(),
"username": schema.String(),
}
defaults := schema.Defaults{
"timestamp": schema.Omit,
"username": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "resource revision schema check failed")
}
valid := coerced.(map[string]interface{})
rev := &resourceRevision{
Revision_: int(valid["revision"].(int64)),
Type_: valid["type"].(string),
Path_: valid["path"].(string),
Description_: valid["description"].(string),
Origin_: valid["origin"].(string),
FingerprintHex_: valid["fingerprint"].(string),
Size_: valid["size"].(int64),
Timestamp_: fieldToTimePtr(valid, "timestamp"),
Username_: valid["username"].(string),
}
return rev, nil
}