-
Notifications
You must be signed in to change notification settings - Fork 36
/
partition.go
158 lines (133 loc) · 4.05 KB
/
partition.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 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 partition struct {
resourceURI string
id int
path string
uuid string
usedFor string
size uint64
tags []string
filesystem *filesystem
}
// Type implements Partition.
func (p *partition) Type() string {
return "partition"
}
// ID implements Partition.
func (p *partition) ID() int {
return p.id
}
// Path implements Partition.
func (p *partition) Path() string {
return p.path
}
// FileSystem implements Partition.
func (p *partition) FileSystem() FileSystem {
if p.filesystem == nil {
return nil
}
return p.filesystem
}
// UUID implements Partition.
func (p *partition) UUID() string {
return p.uuid
}
// UsedFor implements Partition.
func (p *partition) UsedFor() string {
return p.usedFor
}
// Size implements Partition.
func (p *partition) Size() uint64 {
return p.size
}
// Tags implements Partition.
func (p *partition) Tags() []string {
return p.tags
}
func readPartitions(controllerVersion version.Number, source interface{}) ([]*partition, error) {
checker := schema.List(schema.StringMap(schema.Any()))
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "partition base schema check failed")
}
valid := coerced.([]interface{})
var deserialisationVersion version.Number
for v := range partitionDeserializationFuncs {
if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 {
deserialisationVersion = v
}
}
if deserialisationVersion == version.Zero {
return nil, NewUnsupportedVersionError("no partition read func for version %s", controllerVersion)
}
readFunc := partitionDeserializationFuncs[deserialisationVersion]
return readPartitionList(valid, readFunc)
}
// readPartitionList expects the values of the sourceList to be string maps.
func readPartitionList(sourceList []interface{}, readFunc partitionDeserializationFunc) ([]*partition, error) {
result := make([]*partition, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, NewDeserializationError("unexpected value for partition %d, %T", i, value)
}
partition, err := readFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "partition %d", i)
}
result = append(result, partition)
}
return result, nil
}
type partitionDeserializationFunc func(map[string]interface{}) (*partition, error)
var partitionDeserializationFuncs = map[version.Number]partitionDeserializationFunc{
twoDotOh: partition_2_0,
}
func partition_2_0(source map[string]interface{}) (*partition, error) {
fields := schema.Fields{
"resource_uri": schema.String(),
"id": schema.ForceInt(),
"path": schema.String(),
"uuid": schema.OneOf(schema.Nil(""), schema.String()),
"used_for": schema.String(),
"size": schema.ForceUint(),
"tags": schema.List(schema.String()),
"filesystem": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())),
}
defaults := schema.Defaults{
"tags": []string{},
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "partition 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)
}
}
uuid, _ := valid["uuid"].(string)
result := &partition{
resourceURI: valid["resource_uri"].(string),
id: valid["id"].(int),
path: valid["path"].(string),
uuid: uuid,
usedFor: valid["used_for"].(string),
size: valid["size"].(uint64),
tags: convertToStringSlice(valid["tags"]),
filesystem: filesystem,
}
return result, nil
}