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

Commit 29c3e82

Browse files
James FisherSamuel Ortiz
authored andcommitted
extract container_format, disk_format, owner, protected
1 parent dca55ac commit 29c3e82

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

openstack/imageservice/v2/requests_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ func TestCreateImage(t *testing.T) {
3030
expectedImage := Image{
3131
Id: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
3232
Name: "Ubuntu 12.10",
33-
Status: ImageStatusQueued,
3433
Tags: []string{"ubuntu", "quantal"},
34+
35+
Status: ImageStatusQueued,
36+
37+
ContainerFormat: "bare",
38+
DiskFormat: "qcow2",
39+
40+
Owner: "b4eedccc6fb74fa8a7ad6b08382b852b",
3541
}
3642

3743
th.AssertDeepEquals(t, &expectedImage, actualImage)
@@ -50,9 +56,10 @@ func TestGetImage(t *testing.T) {
5056
expectedImage := Image{
5157
Id: "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
5258
Name: "cirros-0.3.2-x86_64-disk",
53-
Status: ImageStatusActive,
5459
Tags: []string{},
5560

61+
Status: ImageStatusActive,
62+
5663
ContainerFormat: "bare",
5764
DiskFormat: "qcow2",
5865

openstack/imageservice/v2/results.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ type CreateResult struct {
4949
gophercloud.ErrResult
5050
}
5151

52+
func asBool(any interface{}) (bool, error) {
53+
if b, ok := any.(bool); ok {
54+
return b, nil
55+
} else {
56+
return false, errors.New(fmt.Sprintf("expected bool value, but found: %#v", any))
57+
}
58+
}
59+
5260
func asString(any interface{}) (string, error) {
5361
if str, ok := any.(string); ok {
5462
return str, nil
@@ -57,6 +65,14 @@ func asString(any interface{}) (string, error) {
5765
}
5866
}
5967

68+
func extractBoolAtKey(m map[string]interface{}, k string) (bool, error) {
69+
if any, ok := m[k]; ok {
70+
return asBool(any)
71+
} else {
72+
return false, errors.New(fmt.Sprintf("expected key \"%s\" in map, but this key is not present", k))
73+
}
74+
}
75+
6076
func extractStringAtKey(m map[string]interface{}, k string) (string, error) {
6177
if any, ok := m[k]; ok {
6278
return asString(any)
@@ -136,6 +152,22 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
136152
return nil, err
137153
}
138154

155+
if image.ContainerFormat, err = extractStringAtKey(body, "container_format"); err != nil {
156+
return nil, err
157+
}
158+
159+
if image.DiskFormat, err = extractStringAtKey(body, "disk_format"); err != nil {
160+
return nil, err
161+
}
162+
163+
if image.Owner, err = extractStringAtKey(body, "owner"); err != nil {
164+
return nil, err
165+
}
166+
167+
if image.Protected, err = extractBoolAtKey(body, "protected"); err != nil {
168+
return nil, err
169+
}
170+
139171
return &image, nil
140172
}
141173

0 commit comments

Comments
 (0)