Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit d0109db

Browse files
James FisherSamuel Ortiz
authored andcommitted
MinDiskGigabytes, MinRamMegabytes, SizeBytes
1 parent f22f710 commit d0109db

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

openstack/imageservice/v2/requests_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func TestGetImage(t *testing.T) {
5656
th.AssertNoErr(t, err)
5757

5858
checksum := "64d7c1cd2b6f60c92c14662941cb7913"
59+
sizebytes := 13167616
5960

6061
expectedImage := Image{
6162
Id: "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
@@ -76,7 +77,7 @@ func TestGetImage(t *testing.T) {
7677
Visibility: ImageVisibilityPublic,
7778

7879
Checksum: &checksum,
79-
SizeBytes: 13167616,
80+
SizeBytes: &sizebytes,
8081
}
8182

8283
th.AssertDeepEquals(t, &expectedImage, actualImage)

openstack/imageservice/v2/results.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v2
33
import (
44
"errors"
55
"fmt"
6+
"reflect"
67

78
"github.com/rackspace/gophercloud"
89
//"github.com/rackspace/gophercloud/pagination"
@@ -33,7 +34,7 @@ type Image struct {
3334
Visibility ImageVisibility
3435

3536
Checksum *string `mapstructure:"checksum"`
36-
SizeBytes int `mapstructure:"size"`
37+
SizeBytes *int `mapstructure:"size"`
3738

3839
Metadata map[string]string `mapstructure:"metadata"`
3940
Properties map[string]string `mapstructure:"properties"`
@@ -57,6 +58,16 @@ func asBool(any interface{}) (bool, error) {
5758
}
5859
}
5960

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+
6071
func asString(any interface{}) (string, error) {
6172
if str, ok := any.(string); ok {
6273
return str, nil
@@ -77,6 +88,23 @@ func asNoneableString(any interface{}) (*string, error) {
7788
}
7889
}
7990

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+
80108
func extractBoolAtKey(m map[string]interface{}, k string) (bool, error) {
81109
if any, ok := m[k]; ok {
82110
return asBool(any)
@@ -85,6 +113,14 @@ func extractBoolAtKey(m map[string]interface{}, k string) (bool, error) {
85113
}
86114
}
87115

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+
88124
func extractStringAtKey(m map[string]interface{}, k string) (string, error) {
89125
if any, ok := m[k]; ok {
90126
return asString(any)
@@ -101,6 +137,14 @@ func extractNoneableStringAtKey(m map[string]interface{}, k string) (*string, er
101137
}
102138
}
103139

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+
104148
func extractStringSliceAtKey(m map[string]interface{}, k string) ([]string, error) {
105149
if any, ok := m[k]; ok {
106150
if slice, ok := any.([]interface{}); ok {
@@ -177,7 +221,7 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
177221
var image Image
178222

179223
var err error
180-
224+
181225
if image.Id, err = extractStringAtKey(body, "id"); err != nil {
182226
return nil, err
183227
}
@@ -202,6 +246,14 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
202246
return nil, err
203247
}
204248

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+
205257
if image.Owner, err = extractStringAtKey(body, "owner"); err != nil {
206258
return nil, err
207259
}
@@ -217,7 +269,14 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
217269
if image.Checksum, err = extractNoneableStringAtKey(body, "checksum"); err != nil {
218270
return nil, err
219271
}
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+
221280
return &image, nil
222281
}
223282

0 commit comments

Comments
 (0)