IDFromName functions
diff --git a/openstack/blockstorage/v1/volumes/requests.go b/openstack/blockstorage/v1/volumes/requests.go
index 253aaf7..3e9243a 100644
--- a/openstack/blockstorage/v1/volumes/requests.go
+++ b/openstack/blockstorage/v1/volumes/requests.go
@@ -201,3 +201,36 @@
})
return res
}
+
+// IDFromName is a convienience function that returns a server's ID given its name.
+func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
+ volumeCount := 0
+ volumeID := ""
+ if name == "" {
+ return "", fmt.Errorf("A volume name must be provided.")
+ }
+ pager := List(client, nil)
+ pager.EachPage(func(page pagination.Page) (bool, error) {
+ volumeList, err := ExtractVolumes(page)
+ if err != nil {
+ return false, err
+ }
+
+ for _, s := range volumeList {
+ if s.Name == name {
+ volumeCount++
+ volumeID = s.ID
+ }
+ }
+ return true, nil
+ })
+
+ switch volumeCount {
+ case 0:
+ return "", fmt.Errorf("Unable to find volume: %s", name)
+ case 1:
+ return volumeID, nil
+ default:
+ return "", fmt.Errorf("Found %d volumes matching %s", volumeCount, name)
+ }
+}