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

Commit f22f710

Browse files
James FisherSamuel Ortiz
authored andcommitted
extract checksum
1 parent da62a53 commit f22f710

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

openstack/imageservice/v2/requests_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func TestGetImage(t *testing.T) {
5555

5656
th.AssertNoErr(t, err)
5757

58+
checksum := "64d7c1cd2b6f60c92c14662941cb7913"
59+
5860
expectedImage := Image{
5961
Id: "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
6062
Name: "cirros-0.3.2-x86_64-disk",
@@ -73,7 +75,7 @@ func TestGetImage(t *testing.T) {
7375
Protected: false,
7476
Visibility: ImageVisibilityPublic,
7577

76-
Checksum: "64d7c1cd2b6f60c92c14662941cb7913",
78+
Checksum: &checksum,
7779
SizeBytes: 13167616,
7880
}
7981

openstack/imageservice/v2/results.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ type Image struct {
2121

2222
Tags []string
2323

24-
ContainerFormat string `mapstructure:"container_format"`
25-
DiskFormat string `mapstructure:"disk_format"`
24+
ContainerFormat string
25+
DiskFormat string
2626

2727
MinDiskGigabytes int `mapstructure:"min_disk"`
2828
MinRamMegabytes int `mapstructure:"min_ram"`
2929

30-
Owner string `mapstructure:"owner"`
30+
Owner string
3131

32-
Protected bool `mapstructure:"protected"`
33-
Visibility ImageVisibility `mapstructure:"visibility"`
32+
Protected bool
33+
Visibility ImageVisibility
3434

35-
Checksum string `mapstructure:"checksum"`
35+
Checksum *string `mapstructure:"checksum"`
3636
SizeBytes int `mapstructure:"size"`
3737

3838
Metadata map[string]string `mapstructure:"metadata"`
@@ -65,6 +65,18 @@ func asString(any interface{}) (string, error) {
6565
}
6666
}
6767

68+
func asNoneableString(any interface{}) (*string, error) {
69+
if str, ok := any.(string); ok {
70+
if str == "None" {
71+
return nil, nil
72+
} else {
73+
return &str, nil
74+
}
75+
} else {
76+
return nil, errors.New(fmt.Sprintf("expected string value, but found: %#v", any))
77+
}
78+
}
79+
6880
func extractBoolAtKey(m map[string]interface{}, k string) (bool, error) {
6981
if any, ok := m[k]; ok {
7082
return asBool(any)
@@ -81,6 +93,14 @@ func extractStringAtKey(m map[string]interface{}, k string) (string, error) {
8193
}
8294
}
8395

96+
func extractNoneableStringAtKey(m map[string]interface{}, k string) (*string, error) {
97+
if any, ok := m[k]; ok {
98+
return asNoneableString(any)
99+
} else {
100+
return nil, errors.New(fmt.Sprintf("expected key \"%s\" in map, but this key is not present", k))
101+
}
102+
}
103+
84104
func extractStringSliceAtKey(m map[string]interface{}, k string) ([]string, error) {
85105
if any, ok := m[k]; ok {
86106
if slice, ok := any.([]interface{}); ok {
@@ -193,6 +213,10 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
193213
if image.Visibility, err = extractImageVisibilityAtKey(body, "visibility"); err != nil {
194214
return nil, err
195215
}
216+
217+
if image.Checksum, err = extractNoneableStringAtKey(body, "checksum"); err != nil {
218+
return nil, err
219+
}
196220

197221
return &image, nil
198222
}

0 commit comments

Comments
 (0)