-
Notifications
You must be signed in to change notification settings - Fork 36
/
blockdevice.go
213 lines (179 loc) · 5.49 KB
/
blockdevice.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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package gomaasapi
import (
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
)
type blockdevice struct {
resourceURI string
id int
uuid string
name string
model string
idPath string
path string
usedFor string
tags []string
blockSize uint64
usedSize uint64
size uint64
filesystem *filesystem
partitions []*partition
}
// Type implements BlockDevice
func (b *blockdevice) Type() string {
return "blockdevice"
}
// ID implements BlockDevice.
func (b *blockdevice) ID() int {
return b.id
}
// UUID implements BlockDevice.
func (b *blockdevice) UUID() string {
return b.uuid
}
// Name implements BlockDevice.
func (b *blockdevice) Name() string {
return b.name
}
// Model implements BlockDevice.
func (b *blockdevice) Model() string {
return b.model
}
// IDPath implements BlockDevice.
func (b *blockdevice) IDPath() string {
return b.idPath
}
// Path implements BlockDevice.
func (b *blockdevice) Path() string {
return b.path
}
// UsedFor implements BlockDevice.
func (b *blockdevice) UsedFor() string {
return b.usedFor
}
// Tags implements BlockDevice.
func (b *blockdevice) Tags() []string {
return b.tags
}
// BlockSize implements BlockDevice.
func (b *blockdevice) BlockSize() uint64 {
return b.blockSize
}
// UsedSize implements BlockDevice.
func (b *blockdevice) UsedSize() uint64 {
return b.usedSize
}
// Size implements BlockDevice.
func (b *blockdevice) Size() uint64 {
return b.size
}
// FileSystem implements BlockDevice.
func (b *blockdevice) FileSystem() FileSystem {
return b.filesystem
}
// Partitions implements BlockDevice.
func (b *blockdevice) Partitions() []Partition {
result := make([]Partition, len(b.partitions))
for i, v := range b.partitions {
result[i] = v
}
return result
}
func readBlockDevices(controllerVersion version.Number, source interface{}) ([]*blockdevice, error) {
checker := schema.List(schema.StringMap(schema.Any()))
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "blockdevice base schema check failed")
}
valid := coerced.([]interface{})
var deserialisationVersion version.Number
for v := range blockdeviceDeserializationFuncs {
if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 {
deserialisationVersion = v
}
}
if deserialisationVersion == version.Zero {
return nil, NewUnsupportedVersionError("no blockdevice read func for version %s", controllerVersion)
}
readFunc := blockdeviceDeserializationFuncs[deserialisationVersion]
return readBlockDeviceList(valid, readFunc)
}
// readBlockDeviceList expects the values of the sourceList to be string maps.
func readBlockDeviceList(sourceList []interface{}, readFunc blockdeviceDeserializationFunc) ([]*blockdevice, error) {
result := make([]*blockdevice, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, NewDeserializationError("unexpected value for blockdevice %d, %T", i, value)
}
blockdevice, err := readFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "blockdevice %d", i)
}
result = append(result, blockdevice)
}
return result, nil
}
type blockdeviceDeserializationFunc func(map[string]interface{}) (*blockdevice, error)
var blockdeviceDeserializationFuncs = map[version.Number]blockdeviceDeserializationFunc{
twoDotOh: blockdevice_2_0,
}
func blockdevice_2_0(source map[string]interface{}) (*blockdevice, error) {
fields := schema.Fields{
"resource_uri": schema.String(),
"id": schema.ForceInt(),
"uuid": schema.OneOf(schema.Nil(""), schema.String()),
"name": schema.String(),
"model": schema.OneOf(schema.Nil(""), schema.String()),
"id_path": schema.OneOf(schema.Nil(""), schema.String()),
"path": schema.String(),
"used_for": schema.String(),
"tags": schema.List(schema.String()),
"block_size": schema.ForceUint(),
"used_size": schema.ForceUint(),
"size": schema.ForceUint(),
"filesystem": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())),
"partitions": schema.List(schema.StringMap(schema.Any())),
}
checker := schema.FieldMap(fields, nil)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "blockdevice 2.0 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.
var filesystem *filesystem
if fsSource, ok := valid["filesystem"].(map[string]interface{}); ok {
if filesystem, err = filesystem2_0(fsSource); err != nil {
return nil, errors.Trace(err)
}
}
partitions, err := readPartitionList(valid["partitions"].([]interface{}), partition_2_0)
if err != nil {
return nil, errors.Trace(err)
}
uuid, _ := valid["uuid"].(string)
model, _ := valid["model"].(string)
idPath, _ := valid["id_path"].(string)
result := &blockdevice{
resourceURI: valid["resource_uri"].(string),
id: valid["id"].(int),
uuid: uuid,
name: valid["name"].(string),
model: model,
idPath: idPath,
path: valid["path"].(string),
usedFor: valid["used_for"].(string),
tags: convertToStringSlice(valid["tags"]),
blockSize: valid["block_size"].(uint64),
usedSize: valid["used_size"].(uint64),
size: valid["size"].(uint64),
filesystem: filesystem,
partitions: partitions,
}
return result, nil
}