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

Commit da62a53

Browse files
James FisherSamuel Ortiz
authored andcommitted
extract image visibility
1 parent 29c3e82 commit da62a53

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

openstack/imageservice/v2/requests_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func TestCreateImage(t *testing.T) {
3838
DiskFormat: "qcow2",
3939

4040
Owner: "b4eedccc6fb74fa8a7ad6b08382b852b",
41+
42+
Visibility: ImageVisibilityPrivate,
4143
}
4244

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

openstack/imageservice/v2/results.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func stringToImageStatus(s string) (ImageStatus, error) {
106106
} else if s == "active" {
107107
return ImageStatusActive, nil
108108
} else {
109-
return "", errors.New(fmt.Sprintf("expected \"active\" as image status, but found: \"%s\"", s))
109+
return "", errors.New(fmt.Sprintf("expected \"queued\" or \"active\" as image status, but found: \"%s\"", s))
110110
}
111111
}
112112

@@ -122,6 +122,28 @@ func extractImageStatusAtKey(m map[string]interface{}, k string) (ImageStatus, e
122122
}
123123
}
124124

125+
func stringToImageVisibility(s string) (ImageVisibility, error) {
126+
if s == "public" {
127+
return ImageVisibilityPublic, nil
128+
} else if s == "private" {
129+
return ImageVisibilityPrivate, nil
130+
} else {
131+
return "", errors.New(fmt.Sprintf("expected \"public\" or \"private\" as image status, but found: \"%s\"", s))
132+
}
133+
}
134+
135+
func extractImageVisibilityAtKey(m map[string]interface{}, k string) (ImageVisibility, error) {
136+
if any, ok := m[k]; ok {
137+
if str, ok := any.(string); ok {
138+
return stringToImageVisibility(str)
139+
} else {
140+
return "", errors.New(fmt.Sprintf("expected string as \"%s\" value, but found: %#v", k, any))
141+
}
142+
} else {
143+
return "", errors.New(fmt.Sprintf("expected key \"%s\" in map, but this key is not present", k))
144+
}
145+
}
146+
125147
func extractImage(res gophercloud.ErrResult) (*Image, error) {
126148
if res.Err != nil {
127149
return nil, res.Err
@@ -167,6 +189,10 @@ func extractImage(res gophercloud.ErrResult) (*Image, error) {
167189
if image.Protected, err = extractBoolAtKey(body, "protected"); err != nil {
168190
return nil, err
169191
}
192+
193+
if image.Visibility, err = extractImageVisibilityAtKey(body, "visibility"); err != nil {
194+
return nil, err
195+
}
170196

171197
return &image, nil
172198
}

0 commit comments

Comments
 (0)