-
Notifications
You must be signed in to change notification settings - Fork 23
/
cloudimagemetadata.go
250 lines (217 loc) · 7.57 KB
/
cloudimagemetadata.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
// 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"
)
type cloudimagemetadataset struct {
Version int `yaml:"version"`
CloudImageMetadata_ []*cloudimagemetadata `yaml:"cloudimagemetadata"`
}
type cloudimagemetadata struct {
Stream_ string `yaml:"stream"`
Region_ string `yaml:"region"`
Version_ string `yaml:"version"`
Arch_ string `yaml:"arch"`
VirtType_ string `yaml:"virt-type"`
RootStorageType_ string `yaml:"root-storage-type"`
RootStorageSize_ *uint64 `yaml:"root-storage-size,omitempty"`
DateCreated_ int64 `yaml:"date-created"`
Source_ string `yaml:"source"`
Priority_ int `yaml:"priority"`
ImageId_ string `yaml:"image-id"`
ExpireAt_ *time.Time `yaml:"expire-at,omitempty"`
}
// Stream implements CloudImageMetadata.
func (i *cloudimagemetadata) Stream() string {
return i.Stream_
}
// Region implements CloudImageMetadata.
func (i *cloudimagemetadata) Region() string {
return i.Region_
}
// Version implements CloudImageMetadata.
func (i *cloudimagemetadata) Version() string {
return i.Version_
}
// Arch implements CloudImageMetadata.
func (i *cloudimagemetadata) Arch() string {
return i.Arch_
}
// VirtType implements CloudImageMetadata.
func (i *cloudimagemetadata) VirtType() string {
return i.VirtType_
}
// RootStorageType implements CloudImageMetadata.
func (i *cloudimagemetadata) RootStorageType() string {
return i.RootStorageType_
}
// RootStorageSize implements CloudImageMetadata.
func (i *cloudimagemetadata) RootStorageSize() (uint64, bool) {
if i.RootStorageSize_ == nil {
return 0, false
}
return *i.RootStorageSize_, true
}
// DateCreated implements CloudImageMetadata.
func (i *cloudimagemetadata) DateCreated() int64 {
return i.DateCreated_
}
// Source implements CloudImageMetadata.
func (i *cloudimagemetadata) Source() string {
return i.Source_
}
// Priority implements CloudImageMetadata.
func (i *cloudimagemetadata) Priority() int {
return i.Priority_
}
// ImageId implements CloudImageMetadata.
func (i *cloudimagemetadata) ImageId() string {
return i.ImageId_
}
// ExpireAt implements CloudImageMetadata.
func (i *cloudimagemetadata) ExpireAt() *time.Time {
return i.ExpireAt_
}
// CloudImageMetadataArgs is an argument struct used to create a
// new internal cloudimagemetadata type that supports the CloudImageMetadata interface.
type CloudImageMetadataArgs struct {
Stream string
Region string
Version string
Arch string
VirtType string
RootStorageType string
RootStorageSize *uint64
DateCreated int64
Source string
Priority int
ImageId string
ExpireAt *time.Time
}
func newCloudImageMetadata(args CloudImageMetadataArgs) *cloudimagemetadata {
cloudimagemetadata := &cloudimagemetadata{
Stream_: args.Stream,
Region_: args.Region,
Version_: args.Version,
Arch_: args.Arch,
VirtType_: args.VirtType,
RootStorageType_: args.RootStorageType,
RootStorageSize_: args.RootStorageSize,
DateCreated_: args.DateCreated,
Source_: args.Source,
Priority_: args.Priority,
ImageId_: args.ImageId,
ExpireAt_: args.ExpireAt,
}
return cloudimagemetadata
}
func importCloudImageMetadatas(source map[string]interface{}) ([]*cloudimagemetadata, error) {
checker := versionedChecker("cloudimagemetadata")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "cloudimagemetadata version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := cloudimagemetadataDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["cloudimagemetadata"].([]interface{})
return importCloudImageMetadataList(sourceList, importFunc)
}
func importCloudImageMetadataList(sourceList []interface{}, importFunc cloudimagemetadataDeserializationFunc) ([]*cloudimagemetadata, error) {
result := make([]*cloudimagemetadata, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected type for cloudimagemetadata %d, %#v", i, value)
}
cloudimagemetadata, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "cloudimagemetadata %d", i)
}
result = append(result, cloudimagemetadata)
}
return result, nil
}
func cloudImageMetadataV1Fields() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"stream": schema.String(),
"region": schema.String(),
"series": schema.String(),
"version": schema.String(),
"arch": schema.String(),
"virt-type": schema.String(),
"root-storage-type": schema.String(),
"root-storage-size": schema.Uint(),
"date-created": schema.Int(),
"source": schema.String(),
"priority": schema.Int(),
"image-id": schema.String(),
"expire-at": schema.Time(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"root-storage-size": schema.Omit,
"expire-at": schema.Omit,
}
return fields, defaults
}
func cloudImageMetadataV2Fields() (schema.Fields, schema.Defaults) {
fields, defaults := cloudImageMetadataV1Fields()
delete(fields, "series")
defaults["series"] = schema.Omit
return fields, defaults
}
type cloudimagemetadataDeserializationFunc func(map[string]interface{}) (*cloudimagemetadata, error)
var cloudimagemetadataDeserializationFuncs = map[int]cloudimagemetadataDeserializationFunc{
1: importCloudImageMetadataV1,
2: importCloudImageMetadataV2,
}
func importCloudImageMetadataV1(source map[string]interface{}) (*cloudimagemetadata, error) {
fields, defaults := cloudImageMetadataV1Fields()
return importCloudImageMetadata(fields, defaults, source, 1)
}
func importCloudImageMetadataV2(source map[string]interface{}) (*cloudimagemetadata, error) {
fields, defaults := cloudImageMetadataV2Fields()
return importCloudImageMetadata(fields, defaults, source, 2)
}
func importCloudImageMetadata(fields schema.Fields, defaults schema.Defaults, source map[string]interface{}, importVersion int) (*cloudimagemetadata, error) {
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "cloudimagemetadata v1 schema check failed")
}
valid := coerced.(map[string]interface{})
_, ok := valid["root-storage-size"]
var pointerSize *uint64
if ok {
rootStorageSize := valid["root-storage-size"].(uint64)
pointerSize = &rootStorageSize
}
_, ok = valid["expire-at"]
var expireAtPtr *time.Time
if ok {
expireAt := valid["expire-at"].(time.Time)
expireAtPtr = &expireAt
}
cloudimagemetadata := &cloudimagemetadata{
Stream_: valid["stream"].(string),
Region_: valid["region"].(string),
Version_: valid["version"].(string),
Arch_: valid["arch"].(string),
VirtType_: valid["virt-type"].(string),
RootStorageType_: valid["root-storage-type"].(string),
RootStorageSize_: pointerSize,
DateCreated_: valid["date-created"].(int64),
Source_: valid["source"].(string),
Priority_: int(valid["priority"].(int64)),
ImageId_: valid["image-id"].(string),
ExpireAt_: expireAtPtr,
}
return cloudimagemetadata, nil
}