-
Notifications
You must be signed in to change notification settings - Fork 23
/
blockdevice.go
262 lines (228 loc) · 7.07 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
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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// BlockDevice represents a block device on a machine.
type BlockDevice interface {
Name() string
Links() []string
Label() string
UUID() string
HardwareID() string
SerialID() string
WWN() string
BusAddress() string
Size() uint64
FilesystemType() string
InUse() bool
MountPoint() string
}
type blockdevices struct {
Version int `yaml:"version"`
BlockDevices_ []*blockdevice `yaml:"block-devices"`
}
func (d *blockdevices) add(args BlockDeviceArgs) *blockdevice {
dev := newBlockDevice(args)
d.BlockDevices_ = append(d.BlockDevices_, dev)
return dev
}
type blockdevice struct {
Name_ string `yaml:"name"`
Links_ []string `yaml:"links,omitempty"`
Label_ string `yaml:"label,omitempty"`
UUID_ string `yaml:"uuid,omitempty"`
HardwareID_ string `yaml:"hardware-id,omitempty"`
SerialID_ string `yaml:"serial-id,omitempty"`
WWN_ string `yaml:"wwn,omitempty"`
BusAddress_ string `yaml:"bus-address,omitempty"`
Size_ uint64 `yaml:"size"`
FilesystemType_ string `yaml:"fs-type,omitempty"`
InUse_ bool `yaml:"in-use"`
MountPoint_ string `yaml:"mount-point,omitempty"`
}
// BlockDeviceArgs is an argument struct used to add a block device to a Machine.
type BlockDeviceArgs struct {
Name string
Links []string
Label string
UUID string
HardwareID string
WWN string
BusAddress string
SerialID string
Size uint64
FilesystemType string
InUse bool
MountPoint string
}
func newBlockDevice(args BlockDeviceArgs) *blockdevice {
bd := &blockdevice{
Name_: args.Name,
Links_: make([]string, len(args.Links)),
Label_: args.Label,
UUID_: args.UUID,
HardwareID_: args.HardwareID,
SerialID_: args.SerialID,
WWN_: args.WWN,
BusAddress_: args.BusAddress,
Size_: args.Size,
FilesystemType_: args.FilesystemType,
InUse_: args.InUse,
MountPoint_: args.MountPoint,
}
copy(bd.Links_, args.Links)
return bd
}
// Name implements BlockDevice.
func (b *blockdevice) Name() string {
return b.Name_
}
// Links implements BlockDevice.
func (b *blockdevice) Links() []string {
return b.Links_
}
// Label implements BlockDevice.
func (b *blockdevice) Label() string {
return b.Label_
}
// UUID implements BlockDevice.
func (b *blockdevice) UUID() string {
return b.UUID_
}
// HardwareID implements BlockDevice.
func (b *blockdevice) HardwareID() string {
return b.HardwareID_
}
// SerialID implements BlockDevice.
func (b *blockdevice) SerialID() string {
return b.SerialID_
}
// WWN implements BlockDevice.
func (b *blockdevice) WWN() string {
return b.WWN_
}
// BusAddress implements BlockDevice.
func (b *blockdevice) BusAddress() string {
return b.BusAddress_
}
// Size implements BlockDevice.
func (b *blockdevice) Size() uint64 {
return b.Size_
}
// FilesystemType implements BlockDevice.
func (b *blockdevice) FilesystemType() string {
return b.FilesystemType_
}
// InUse implements BlockDevice.
func (b *blockdevice) InUse() bool {
return b.InUse_
}
// MountPoint implements BlockDevice.
func (b *blockdevice) MountPoint() string {
return b.MountPoint_
}
func importBlockDevices(source interface{}) ([]*blockdevice, error) {
checker := versionedChecker("block-devices")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "block devices version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := blockdeviceDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["block-devices"].([]interface{})
return importBlockDeviceList(sourceList, importFunc)
}
func importBlockDeviceList(sourceList []interface{}, importFunc blockdeviceDeserializationFunc) ([]*blockdevice, error) {
result := make([]*blockdevice, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for block device %d, %T", i, value)
}
device, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "block device %d", i)
}
result = append(result, device)
}
return result, nil
}
type blockdeviceDeserializationFunc func(map[string]interface{}) (*blockdevice, error)
var blockdeviceDeserializationFuncs = map[int]blockdeviceDeserializationFunc{
1: importBlockDeviceV1,
2: importBlockDeviceV2,
}
func blockDeviceV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"name": schema.String(),
"links": schema.List(schema.String()),
"label": schema.String(),
"uuid": schema.String(),
"hardware-id": schema.String(),
"wwn": schema.String(),
"bus-address": schema.String(),
"size": schema.ForceUint(),
"fs-type": schema.String(),
"in-use": schema.Bool(),
"mount-point": schema.String(),
}
defaults := schema.Defaults{
"links": schema.Omit,
"label": "",
"uuid": "",
"hardware-id": "",
"wwn": "",
"bus-address": "",
"fs-type": "",
"mount-point": "",
}
return fields, defaults
}
func blockDeviceV2Fields() (schema.Fields, schema.Defaults) {
fields, defaults := blockDeviceV1Fields()
fields["serial-id"] = schema.String()
defaults["serial-id"] = ""
return fields, defaults
}
func importBlockDeviceV1(source map[string]interface{}) (*blockdevice, error) {
fields, defaults := blockDeviceV1Fields()
return importBlockDevice(fields, defaults, 1, source)
}
func importBlockDeviceV2(source map[string]interface{}) (*blockdevice, error) {
fields, defaults := blockDeviceV2Fields()
return importBlockDevice(fields, defaults, 2, source)
}
func importBlockDevice(fields schema.Fields, defaults schema.Defaults, importVersion int, source map[string]interface{}) (*blockdevice, error) {
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "block device 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 := &blockdevice{
Name_: valid["name"].(string),
Links_: convertToStringSlice(valid["links"]),
Label_: valid["label"].(string),
UUID_: valid["uuid"].(string),
HardwareID_: valid["hardware-id"].(string),
WWN_: valid["wwn"].(string),
BusAddress_: valid["bus-address"].(string),
Size_: valid["size"].(uint64),
FilesystemType_: valid["fs-type"].(string),
InUse_: valid["in-use"].(bool),
MountPoint_: valid["mount-point"].(string),
}
if importVersion >= 2 {
result.SerialID_ = valid["serial-id"].(string)
}
return result, nil
}