forked from huaweicloud/huaweicloud-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
273 lines (224 loc) · 7.45 KB
/
results.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
package images
import (
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/internal"
"github.com/gophercloud/gophercloud/pagination"
)
// Image represents an image found in the OpenStack Image service.
type Image struct {
// ID is the image UUID.
ID string `json:"id"`
// Name is the human-readable display name for the image.
Name string `json:"name"`
// Status is the image status. It can be "queued" or "active"
// See imageservice/v2/images/type.go
Status ImageStatus `json:"status"`
// Tags is a list of image tags. Tags are arbitrarily defined strings
// attached to an image.
Tags []string `json:"tags"`
// ContainerFormat is the format of the container.
// Valid values are ami, ari, aki, bare, and ovf.
ContainerFormat string `json:"container_format"`
// DiskFormat is the format of the disk.
// If set, valid values are ami, ari, aki, vhd, vmdk, raw, qcow2, vdi,
// and iso.
DiskFormat string `json:"disk_format"`
// MinDiskGigabytes is the amount of disk space in GB that is required to
// boot the image.
MinDiskGigabytes int `json:"min_disk"`
// MinRAMMegabytes [optional] is the amount of RAM in MB that is required to
// boot the image.
MinRAMMegabytes int `json:"min_ram"`
// Owner is the tenant ID the image belongs to.
Owner string `json:"owner"`
// Protected is whether the image is deletable or not.
Protected bool `json:"protected"`
// Visibility defines who can see/use the image.
Visibility ImageVisibility `json:"visibility"`
// Checksum is the checksum of the data that's associated with the image.
Checksum string `json:"checksum"`
// SizeBytes is the size of the data that's associated with the image.
SizeBytes int64 `json:"size"`
// Metadata is a set of metadata associated with the image.
// Image metadata allow for meaningfully define the image properties
// and tags.
// See http://docs.openstack.org/developer/glance/metadefs-concepts.html.
Metadata map[string]string `json:"metadata"`
// Properties is a set of key-value pairs, if any, that are associated with
// the image.
Properties map[string]interface{} `json:"-"`
// CreatedAt is the date when the image has been created.
CreatedAt time.Time `json:"created_at"`
// UpdatedAt is the date when the last change has been made to the image or
// it's properties.
UpdatedAt time.Time `json:"updated_at"`
// File is the trailing path after the glance endpoint that represent the
// location of the image or the path to retrieve it.
File string `json:"file"`
// Schema is the path to the JSON-schema that represent the image or image
// entity.
Schema string `json:"schema"`
// VirtualSize is the virtual size of the image
VirtualSize int64 `json:"virtual_size"`
// Specifies the image URL.
Self string `json:"self"`
// Specifies whether the image has been deleted.
// The value can be true or false.
Deleted bool `json:"deleted"`
// Specifies the environment where the image is used.
// The value can be FusionCompute,Ironic, or DataImage.
// For an ECS image, the value is FusionCompute.
// For a data disk image, the value is DataImage.
// For a BMS image, the value is Ironic.
VirtualEnvType ImageVirtualEnvType `json:"virtual_env_type"`
// Specifies the time when the image was deleted.
// The value is in UTC format.
DeletedAt string `json:"deleted_at"`
}
func (r *Image) UnmarshalJSON(b []byte) error {
type tmp Image
var s struct {
tmp
SizeBytes interface{} `json:"size"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = Image(s.tmp)
switch t := s.SizeBytes.(type) {
case nil:
return nil
case float32:
r.SizeBytes = int64(t)
case float64:
r.SizeBytes = int64(t)
default:
return fmt.Errorf("Unknown type for SizeBytes: %v (value: %v)", reflect.TypeOf(t), t)
}
// Bundle all other fields into Properties
var result interface{}
err = json.Unmarshal(b, &result)
if err != nil {
return err
}
if resultMap, ok := result.(map[string]interface{}); ok {
delete(resultMap, "self")
r.Properties = internal.RemainingKeys(Image{}, resultMap)
}
return err
}
type commonResult struct {
gophercloud.Result
}
// Extract interprets any commonResult as an Image.
func (r commonResult) Extract() (*Image, error) {
var s *Image
err := r.ExtractInto(&s)
return s, err
}
// CreateResult represents the result of a Create operation. Call its Extract
// method to interpret it as an Image.
type CreateResult struct {
commonResult
}
// UpdateResult represents the result of an Update operation. Call its Extract
// method to interpret it as an Image.
type UpdateResult struct {
commonResult
}
// GetResult represents the result of a Get operation. Call its Extract
// method to interpret it as an Image.
type GetResult struct {
commonResult
}
// DeleteResult represents the result of a Delete operation. Call its
// ExtractErr method to interpret it as an Image.
type DeleteResult struct {
gophercloud.ErrResult
}
// PutTagResult represents the result of a put tag operation.
type PutTagResult struct {
gophercloud.ErrResult
}
// DeleteTagResult represents the result of a delete tag operation.
type DeleteTagResult struct {
gophercloud.ErrResult
}
// ImagePage represents the results of a List request.
type ImagePage struct {
pagination.LinkedPageBase
}
// IsEmpty returns true if an ImagePage contains no Images results.
func (r ImagePage) IsEmpty() (bool, error) {
images, err := ExtractImages(r)
return len(images) == 0, err
}
// NextPageURL uses the response's embedded link reference to navigate to
// the next page of results.
func (r ImagePage) NextPageURL() (string, error) {
var s struct {
Next string `json:"next"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
if s.Next == "" {
return "", nil
}
return nextPageURL(r.URL.String(), s.Next)
}
// ExtractImages interprets the results of a single page from a List() call,
// producing a slice of Image entities.
func ExtractImages(r pagination.Page) ([]Image, error) {
var s struct {
Images []Image `json:"images"`
}
err := (r.(ImagePage)).ExtractInto(&s)
return s.Images, err
}
// ImageSchemas presents the result of getting image schemas request
type ImageSchemas struct {
// AdditionalProperties presents the additional properties
AdditionalProperties map[string]string `json:"additionalProperties"`
// Name is the name of schemas
Name string `json:"name"`
// Links is the links of schemas
Links []map[string]string `json:"links"`
// Properties is the explaination of schemas properties
Properties *json.RawMessage `json:"properties"`
}
// ImageSchemasResult represents the result of Image schemas request
type ImageSchemasResult struct {
gophercloud.Result
}
// Extract interprets the result as an ImageSchemas
func (r ImageSchemasResult) Extract() (*ImageSchemas, error) {
var s *ImageSchemas
err := r.ExtractInto(&s)
return s, err
}
// ImagesSchemas presents the result of getting images schemas request
type ImagesSchemas struct {
// Name is the name of schemas
Name string `json:"name"`
// Links is the links of schemas
Links []map[string]string `json:"links"`
// Properties is the explaination of schemas properties
Properties *json.RawMessage `json:"properties"`
}
// ImagesSchemasResult represents the result of Images schemas request
type ImagesSchemasResult struct {
gophercloud.Result
}
// Extract interprets the result as an ImagesSchemas
func (r ImagesSchemasResult) Extract() (*ImagesSchemas, error) {
var s *ImagesSchemas
err := r.ExtractInto(&s)
return s, err
}