Skip to content

Commit 5558622

Browse files
committed
manila share-types: added IDFromName convenience function
1 parent 8014bd3 commit 5558622

File tree

1 file changed

+34
-0
lines changed
  • openstack/sharedfilesystems/v2/sharetypes

1 file changed

+34
-0
lines changed

openstack/sharedfilesystems/v2/sharetypes/results.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ func (r commonResult) Extract() (*ShareType, error) {
3333
return s.ShareType, err
3434
}
3535

36+
// IDFromName is a convenience function that returns a share-type's ID given its name.
37+
func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
38+
r, err := List(client, &ListOpts{}).AllPages()
39+
if err != nil {
40+
return "", nil
41+
}
42+
43+
ss, err := ExtractShareTypes(r)
44+
if err != nil {
45+
return "", err
46+
}
47+
48+
var (
49+
count int
50+
id string
51+
)
52+
53+
for _, s := range ss {
54+
if s.Name == name {
55+
count++
56+
id = s.ID
57+
}
58+
}
59+
60+
switch count {
61+
case 0:
62+
return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "share type"}
63+
case 1:
64+
return id, nil
65+
default:
66+
return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "share type"}
67+
}
68+
}
69+
3670
// CreateResult contains the response body and error from a Create request.
3771
type CreateResult struct {
3872
commonResult

0 commit comments

Comments
 (0)