-
Notifications
You must be signed in to change notification settings - Fork 23
/
filesystem.go
393 lines (338 loc) · 11.2 KB
/
filesystem.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/schema"
)
type filesystems struct {
Version int `yaml:"version"`
Filesystems_ []*filesystem `yaml:"filesystems"`
}
type filesystem struct {
ID_ string `yaml:"id"`
StorageID_ string `yaml:"storage-id,omitempty"`
VolumeID_ string `yaml:"volume-id,omitempty"`
Provisioned_ bool `yaml:"provisioned"`
Size_ uint64 `yaml:"size"`
Pool_ string `yaml:"pool,omitempty"`
FilesystemID_ string `yaml:"filesystem-id,omitempty"`
Status_ *status `yaml:"status"`
StatusHistory_ `yaml:"status-history"`
Attachments_ filesystemAttachments `yaml:"attachments"`
}
type filesystemAttachments struct {
Version int `yaml:"version"`
Attachments_ []*filesystemAttachment `yaml:"attachments"`
}
type filesystemAttachment struct {
HostID_ string `yaml:"host-id"`
Provisioned_ bool `yaml:"provisioned"`
MountPoint_ string `yaml:"mount-point,omitempty"`
ReadOnly_ bool `yaml:"read-only"`
}
// FilesystemArgs is an argument struct used to add a filesystem to the Model.
type FilesystemArgs struct {
Tag names.FilesystemTag
Storage names.StorageTag
Volume names.VolumeTag
Provisioned bool
Size uint64
Pool string
FilesystemID string
}
func newFilesystem(args FilesystemArgs) *filesystem {
f := &filesystem{
ID_: args.Tag.Id(),
StorageID_: args.Storage.Id(),
VolumeID_: args.Volume.Id(),
Provisioned_: args.Provisioned,
Size_: args.Size,
Pool_: args.Pool,
FilesystemID_: args.FilesystemID,
StatusHistory_: newStatusHistory(),
}
f.setAttachments(nil)
return f
}
// Tag implements Filesystem.
func (f *filesystem) Tag() names.FilesystemTag {
return names.NewFilesystemTag(f.ID_)
}
// Volume implements Filesystem.
func (f *filesystem) Volume() names.VolumeTag {
if f.VolumeID_ == "" {
return names.VolumeTag{}
}
return names.NewVolumeTag(f.VolumeID_)
}
// Storage implements Filesystem.
func (f *filesystem) Storage() names.StorageTag {
if f.StorageID_ == "" {
return names.StorageTag{}
}
return names.NewStorageTag(f.StorageID_)
}
// Provisioned implements Filesystem.
func (f *filesystem) Provisioned() bool {
return f.Provisioned_
}
// Size implements Filesystem.
func (f *filesystem) Size() uint64 {
return f.Size_
}
// Pool implements Filesystem.
func (f *filesystem) Pool() string {
return f.Pool_
}
// FilesystemID implements Filesystem.
func (f *filesystem) FilesystemID() string {
return f.FilesystemID_
}
// Status implements Filesystem.
func (f *filesystem) Status() Status {
// To avoid typed nils check nil here.
if f.Status_ == nil {
return nil
}
return f.Status_
}
// SetStatus implements Filesystem.
func (f *filesystem) SetStatus(args StatusArgs) {
f.Status_ = newStatus(args)
}
func (f *filesystem) setAttachments(attachments []*filesystemAttachment) {
f.Attachments_ = filesystemAttachments{
Version: 2,
Attachments_: attachments,
}
}
// Attachments implements Filesystem.
func (f *filesystem) Attachments() []FilesystemAttachment {
var result []FilesystemAttachment
for _, attachment := range f.Attachments_.Attachments_ {
result = append(result, attachment)
}
return result
}
// AddAttachment implements Filesystem.
func (f *filesystem) AddAttachment(args FilesystemAttachmentArgs) FilesystemAttachment {
a := newFilesystemAttachment(args)
f.Attachments_.Attachments_ = append(f.Attachments_.Attachments_, a)
return a
}
// Validate implements Filesystem.
func (f *filesystem) Validate() error {
if f.ID_ == "" {
return errors.NotValidf("filesystem missing id")
}
if f.Size_ == 0 {
return errors.NotValidf("filesystem %q missing size", f.ID_)
}
if f.Status_ == nil {
return errors.NotValidf("filesystem %q missing status", f.ID_)
}
return nil
}
func importFilesystems(source map[string]interface{}) ([]*filesystem, error) {
checker := versionedChecker("filesystems")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "filesystems version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := filesystemDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["filesystems"].([]interface{})
return importFilesystemList(sourceList, importFunc)
}
func importFilesystemList(sourceList []interface{}, importFunc filesystemDeserializationFunc) ([]*filesystem, error) {
result := make([]*filesystem, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for filesystem %d, %T", i, value)
}
filesystem, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "filesystem %d", i)
}
result = append(result, filesystem)
}
return result, nil
}
type filesystemDeserializationFunc func(map[string]interface{}) (*filesystem, error)
var filesystemDeserializationFuncs = map[int]filesystemDeserializationFunc{
1: importFilesystemV1,
}
func importFilesystemV1(source map[string]interface{}) (*filesystem, error) {
fields := schema.Fields{
"id": schema.String(),
"storage-id": schema.String(),
"volume-id": schema.String(),
"provisioned": schema.Bool(),
"size": schema.ForceUint(),
"pool": schema.String(),
"filesystem-id": schema.String(),
"status": schema.StringMap(schema.Any()),
"attachments": schema.StringMap(schema.Any()),
}
defaults := schema.Defaults{
"storage-id": "",
"volume-id": "",
"pool": "",
"filesystem-id": "",
"attachments": schema.Omit,
}
addStatusHistorySchema(fields)
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "filesystem 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 := &filesystem{
ID_: valid["id"].(string),
StorageID_: valid["storage-id"].(string),
VolumeID_: valid["volume-id"].(string),
Provisioned_: valid["provisioned"].(bool),
Size_: valid["size"].(uint64),
Pool_: valid["pool"].(string),
FilesystemID_: valid["filesystem-id"].(string),
StatusHistory_: newStatusHistory(),
}
if err := result.importStatusHistory(valid); err != nil {
return nil, errors.Trace(err)
}
status, err := importStatus(valid["status"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.Status_ = status
attachments, err := importFilesystemAttachments(valid["attachments"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
result.setAttachments(attachments)
return result, nil
}
// FilesystemAttachmentArgs is an argument struct used to add information about the
// cloud instance to a Filesystem.
type FilesystemAttachmentArgs struct {
Host names.Tag
Provisioned bool
ReadOnly bool
MountPoint string
}
func newFilesystemAttachment(args FilesystemAttachmentArgs) *filesystemAttachment {
return &filesystemAttachment{
HostID_: args.Host.Id(),
Provisioned_: args.Provisioned,
ReadOnly_: args.ReadOnly,
MountPoint_: args.MountPoint,
}
}
// Host implements FilesystemAttachment
func (a *filesystemAttachment) Host() names.Tag {
return storageAttachmentHost(a.HostID_)
}
// Provisioned implements FilesystemAttachment
func (a *filesystemAttachment) Provisioned() bool {
return a.Provisioned_
}
// ReadOnly implements FilesystemAttachment
func (a *filesystemAttachment) ReadOnly() bool {
return a.ReadOnly_
}
// MountPoint implements FilesystemAttachment
func (a *filesystemAttachment) MountPoint() string {
return a.MountPoint_
}
func importFilesystemAttachments(source map[string]interface{}) ([]*filesystemAttachment, error) {
checker := versionedChecker("attachments")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "filesystem attachments version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := filesystemAttachmentDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["attachments"].([]interface{})
return importFilesystemAttachmentList(sourceList, importFunc)
}
func importFilesystemAttachmentList(sourceList []interface{}, importFunc filesystemAttachmentDeserializationFunc) ([]*filesystemAttachment, error) {
result := make([]*filesystemAttachment, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for filesystemAttachment %d, %T", i, value)
}
filesystemAttachment, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "filesystemAttachment %d", i)
}
result = append(result, filesystemAttachment)
}
return result, nil
}
type filesystemAttachmentDeserializationFunc func(map[string]interface{}) (*filesystemAttachment, error)
var filesystemAttachmentDeserializationFuncs = map[int]filesystemAttachmentDeserializationFunc{
1: importFilesystemAttachmentV1,
2: importFilesystemAttachmentV2,
}
func importFilesystemAttachmentV1(source map[string]interface{}) (*filesystemAttachment, error) {
fields, defaults := filesystemAttachmentV1Fields()
return importFilesystemAttachment(fields, defaults, 1, source)
}
func importFilesystemAttachmentV2(source map[string]interface{}) (*filesystemAttachment, error) {
fields, defaults := filesystemAttachmentV2Fields()
return importFilesystemAttachment(fields, defaults, 2, source)
}
func filesystemAttachmentV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"machine-id": schema.String(),
"provisioned": schema.Bool(),
"read-only": schema.Bool(),
"mount-point": schema.String(),
}
defaults := schema.Defaults{
"mount-point": "",
}
return fields, defaults
}
func filesystemAttachmentV2Fields() (schema.Fields, schema.Defaults) {
fields, defaults := filesystemAttachmentV1Fields()
fields["host-id"] = schema.String()
delete(fields, "machine-id")
return fields, defaults
}
func importFilesystemAttachment(fields schema.Fields, defaults schema.Defaults, importVersion int, source map[string]interface{}) (*filesystemAttachment, error) {
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "filesystemAttachment 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 := &filesystemAttachment{
Provisioned_: valid["provisioned"].(bool),
ReadOnly_: valid["read-only"].(bool),
MountPoint_: valid["mount-point"].(string),
}
if importVersion >= 2 {
result.HostID_ = valid["host-id"].(string)
} else {
result.HostID_ = valid["machine-id"].(string)
}
return result, nil
}