@@ -3,6 +3,7 @@ package v2
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "reflect"
6
7
7
8
"github.com/rackspace/gophercloud"
8
9
//"github.com/rackspace/gophercloud/pagination"
@@ -33,7 +34,7 @@ type Image struct {
33
34
Visibility ImageVisibility
34
35
35
36
Checksum * string `mapstructure:"checksum"`
36
- SizeBytes int `mapstructure:"size"`
37
+ SizeBytes * int `mapstructure:"size"`
37
38
38
39
Metadata map [string ]string `mapstructure:"metadata"`
39
40
Properties map [string ]string `mapstructure:"properties"`
@@ -57,6 +58,16 @@ func asBool(any interface{}) (bool, error) {
57
58
}
58
59
}
59
60
61
+ func asInt (any interface {}) (int , error ) {
62
+ // FIXME integers decoded as float64s
63
+ if f , ok := any .(float64 ); ok {
64
+ i := int (f )
65
+ return i , nil
66
+ } else {
67
+ return 0 , errors .New (fmt .Sprintf ("expected int value, but found: %#v" , any ))
68
+ }
69
+ }
70
+
60
71
func asString (any interface {}) (string , error ) {
61
72
if str , ok := any .(string ); ok {
62
73
return str , nil
@@ -77,6 +88,23 @@ func asNoneableString(any interface{}) (*string, error) {
77
88
}
78
89
}
79
90
91
+ func asNoneableInteger (any interface {}) (* int , error ) {
92
+ // FIXME problem here is that provider_client.go uses: json.NewDecoder(resp.Body).Decode(options.JSONResponse)
93
+ // which apparently converts integers in JSON to float64 values
94
+ if f , ok := any .(float64 ); ok {
95
+ i := int (f )
96
+ return & i , nil
97
+ } else if s , ok := any .(string ); ok {
98
+ if s == "None" {
99
+ return nil , nil
100
+ } else {
101
+ return nil , errors .New (fmt .Sprintf ("expected \" None\" or integer value, but found unexpected string: \" %s\" " , s ))
102
+ }
103
+ } else {
104
+ return nil , errors .New (fmt .Sprintf ("expected \" None\" or integer value, but found: %#v of type %s" , any , reflect .TypeOf (any )))
105
+ }
106
+ }
107
+
80
108
func extractBoolAtKey (m map [string ]interface {}, k string ) (bool , error ) {
81
109
if any , ok := m [k ]; ok {
82
110
return asBool (any )
@@ -85,6 +113,14 @@ func extractBoolAtKey(m map[string]interface{}, k string) (bool, error) {
85
113
}
86
114
}
87
115
116
+ func extractIntAtKey (m map [string ]interface {}, k string ) (int , error ) {
117
+ if any , ok := m [k ]; ok {
118
+ return asInt (any )
119
+ } else {
120
+ return 0 , errors .New (fmt .Sprintf ("expected key \" %s\" in map, but this key is not present" , k ))
121
+ }
122
+ }
123
+
88
124
func extractStringAtKey (m map [string ]interface {}, k string ) (string , error ) {
89
125
if any , ok := m [k ]; ok {
90
126
return asString (any )
@@ -101,6 +137,14 @@ func extractNoneableStringAtKey(m map[string]interface{}, k string) (*string, er
101
137
}
102
138
}
103
139
140
+ func extractNoneableIntegerAtKey (m map [string ]interface {}, k string ) (* int , error ) {
141
+ if any , ok := m [k ]; ok {
142
+ return asNoneableInteger (any )
143
+ } else {
144
+ return nil , errors .New (fmt .Sprintf ("expected key \" %s\" in map, but this key is not present" , k ))
145
+ }
146
+ }
147
+
104
148
func extractStringSliceAtKey (m map [string ]interface {}, k string ) ([]string , error ) {
105
149
if any , ok := m [k ]; ok {
106
150
if slice , ok := any .([]interface {}); ok {
@@ -177,7 +221,7 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
177
221
var image Image
178
222
179
223
var err error
180
-
224
+
181
225
if image .Id , err = extractStringAtKey (body , "id" ); err != nil {
182
226
return nil , err
183
227
}
@@ -202,6 +246,14 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
202
246
return nil , err
203
247
}
204
248
249
+ if image .MinDiskGigabytes , err = extractIntAtKey (body , "min_disk" ); err != nil {
250
+ return nil , err
251
+ }
252
+
253
+ if image .MinRamMegabytes , err = extractIntAtKey (body , "min_ram" ); err != nil {
254
+ return nil , err
255
+ }
256
+
205
257
if image .Owner , err = extractStringAtKey (body , "owner" ); err != nil {
206
258
return nil , err
207
259
}
@@ -217,7 +269,14 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
217
269
if image .Checksum , err = extractNoneableStringAtKey (body , "checksum" ); err != nil {
218
270
return nil , err
219
271
}
220
-
272
+
273
+ if image .SizeBytes , err = extractNoneableIntegerAtKey (body , "size" ); err != nil {
274
+ return nil , err
275
+ }
276
+
277
+ // TODO Metadata map[string]string `mapstructure:"metadata"`
278
+ // TODO Properties map[string]string `mapstructure:"properties"`
279
+
221
280
return & image , nil
222
281
}
223
282
0 commit comments