diff --git a/openstack/blockstorage/extensions/backups/utils.go b/openstack/blockstorage/extensions/backups/utils.go index 46a60b25..3f9d55c9 100644 --- a/openstack/blockstorage/extensions/backups/utils.go +++ b/openstack/blockstorage/extensions/backups/utils.go @@ -5,38 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/backups" ) -// IDFromName is a convienience function that returns a backups's ID given its name. +// IDFromName is a convenience function that returns a backup's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } - listOpts := backups.ListOpts{ - Name: name, + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "backup"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "backup"} } +} - pages, err := backups.List(client, listOpts).AllPages() +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := backups.List(client, backups.ListOpts{ + Name: name, + }).AllPages() if err != nil { - return "", err + return nil, err } all, err := backups.ExtractBackups(pages) if err != nil { - return "", err + return nil, err } - for _, s := range all { - if s.Name == name { - count++ - id = s.ID - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "backup"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "backup"} - } + return IDs, nil } diff --git a/openstack/blockstorage/v1/snapshots/utils.go b/openstack/blockstorage/v1/snapshots/utils.go index c8e9903c..414a1693 100644 --- a/openstack/blockstorage/v1/snapshots/utils.go +++ b/openstack/blockstorage/v1/snapshots/utils.go @@ -5,7 +5,7 @@ import ( "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/snapshots" ) -// IDFromName is a convienience function that returns a snapshot's ID given its name. +// IDFromName is a convenience function that returns a snapshot's ID given its name. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" diff --git a/openstack/blockstorage/v1/volumes/utils.go b/openstack/blockstorage/v1/volumes/utils.go index 53d8ac9e..64d6d7c4 100644 --- a/openstack/blockstorage/v1/volumes/utils.go +++ b/openstack/blockstorage/v1/volumes/utils.go @@ -5,7 +5,7 @@ import ( "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes" ) -// IDFromName is a convienience function that returns a volume's ID given its name. +// IDFromName is a convenience function that returns a volume's ID given its name. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" diff --git a/openstack/blockstorage/v2/snapshots/utils.go b/openstack/blockstorage/v2/snapshots/utils.go index 2e2c2fa3..869a9fc1 100644 --- a/openstack/blockstorage/v2/snapshots/utils.go +++ b/openstack/blockstorage/v2/snapshots/utils.go @@ -5,7 +5,7 @@ import ( "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/snapshots" ) -// IDFromName is a convienience function that returns a snapshot's ID given its name. +// IDFromName is a convenience function that returns a snapshot's ID given its name. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" diff --git a/openstack/blockstorage/v2/volumes/utils.go b/openstack/blockstorage/v2/volumes/utils.go index cd8ad605..fb6206ec 100644 --- a/openstack/blockstorage/v2/volumes/utils.go +++ b/openstack/blockstorage/v2/volumes/utils.go @@ -5,7 +5,7 @@ import ( "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes" ) -// IDFromName is a convienience function that returns a volume's ID given its name. +// IDFromName is a convenience function that returns a volume's ID given its name. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { count := 0 id := "" diff --git a/openstack/blockstorage/v3/snapshots/utils.go b/openstack/blockstorage/v3/snapshots/utils.go index 955d496d..58d68e85 100644 --- a/openstack/blockstorage/v3/snapshots/utils.go +++ b/openstack/blockstorage/v3/snapshots/utils.go @@ -5,38 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/snapshots" ) -// IDFromName is a convienience function that returns a snapshot's ID given its name. +// IDFromName is a convenience function that returns a snapshot's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } - listOpts := snapshots.ListOpts{ - Name: name, + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"} } +} - pages, err := snapshots.List(client, listOpts).AllPages() +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := snapshots.List(client, snapshots.ListOpts{ + Name: name, + }).AllPages() if err != nil { - return "", err + return nil, err } all, err := snapshots.ExtractSnapshots(pages) if err != nil { - return "", err + return nil, err } - for _, s := range all { - if s.Name == name { - count++ - id = s.ID - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"} - } + return IDs, nil } diff --git a/openstack/blockstorage/v3/volumes/utils.go b/openstack/blockstorage/v3/volumes/utils.go index 6afc7d77..9babed78 100644 --- a/openstack/blockstorage/v3/volumes/utils.go +++ b/openstack/blockstorage/v3/volumes/utils.go @@ -5,38 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes" ) -// IDFromName is a convienience function that returns a volume's ID given its name. +// IDFromName is a convenience function that returns a volume's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } - listOpts := volumes.ListOpts{ - Name: name, + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"} } +} - pages, err := volumes.List(client, listOpts).AllPages() +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := volumes.List(client, volumes.ListOpts{ + Name: name, + }).AllPages() if err != nil { - return "", err + return nil, err } all, err := volumes.ExtractVolumes(pages) if err != nil { - return "", err + return nil, err } - for _, s := range all { - if s.Name == name { - count++ - id = s.ID - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "volume"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "volume"} - } + return IDs, nil } diff --git a/openstack/compute/v2/flavors/utils.go b/openstack/compute/v2/flavors/utils.go index 909c1a1c..90826e23 100644 --- a/openstack/compute/v2/flavors/utils.go +++ b/openstack/compute/v2/flavors/utils.go @@ -5,41 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors" ) -// IDFromName is a convienience function that returns a flavor's ID given its -// name. +// IDFromName is a convenience function that returns a flavor's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" - allPages, err := flavors.ListDetail(client, nil).AllPages() + IDs, err := IDsFromName(client, name) if err != nil { return "", err } - all, err := flavors.ExtractFlavors(allPages) + switch count := len(IDs); count { + case 0: + return "", &gophercloud.ErrResourceNotFound{Name: name, ResourceType: "flavor"} + case 1: + return IDs[0], nil + default: + return "", &gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "flavor"} + } +} + +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := flavors.ListDetail(client, nil).AllPages() if err != nil { - return "", err + return nil, err } - for _, f := range all { - if f.Name == name { - count++ - id = f.ID - } + all, err := flavors.ExtractFlavors(pages) + if err != nil { + return nil, err } - switch count { - case 0: - err := &gophercloud.ErrResourceNotFound{} - err.ResourceType = "flavor" - err.Name = name - return "", err - case 1: - return id, nil - default: - err := &gophercloud.ErrMultipleResourcesFound{} - err.ResourceType = "flavor" - err.Name = name - err.Count = count - return "", err + IDs := make([]string, 0, len(all)) + for _, s := range all { + if s.Name == name { + IDs = append(IDs, s.ID) + } } + + return IDs, nil } diff --git a/openstack/compute/v2/servers/utils.go b/openstack/compute/v2/servers/utils.go index 46c9b2fc..4a71923c 100644 --- a/openstack/compute/v2/servers/utils.go +++ b/openstack/compute/v2/servers/utils.go @@ -7,40 +7,44 @@ import ( "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" ) -// IDFromName is a convienience function that returns a server's ID given its -// name. +// IDFromName is a convenience function that returns a server's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } - listOpts := servers.ListOpts{ - // nova list uses a name field as an regexp - Name: fmt.Sprintf("^%s$", name), + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"} } +} - allPages, err := servers.List(client, listOpts).AllPages() +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := servers.List(client, servers.ListOpts{ + // nova list uses a name field as a regexp + Name: fmt.Sprintf("^%s$", name), + }).AllPages() if err != nil { - return "", err + return nil, err } - all, err := servers.ExtractServers(allPages) + all, err := servers.ExtractServers(pages) if err != nil { - return "", err + return nil, err } - for _, f := range all { - if f.Name == name { - count++ - id = f.ID - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"} - } + return IDs, nil } diff --git a/openstack/imageservice/v2/images/utils.go b/openstack/imageservice/v2/images/utils.go index 438d9491..62038aa7 100644 --- a/openstack/imageservice/v2/images/utils.go +++ b/openstack/imageservice/v2/images/utils.go @@ -5,33 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" ) -// IDFromName is a convienience function that returns an image's ID given its name. +// IDFromName is a convenience function that returns an image's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - allPages, err := images.List(client, images.ListOpts{ + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } + + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "image"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "image"} + } +} + +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := images.List(client, images.ListOpts{ Name: name, }).AllPages() if err != nil { - return "", err + return nil, err } - allImages, err := images.ExtractImages(allPages) + all, err := images.ExtractImages(pages) if err != nil { - return "", err + return nil, err } - switch count := len(allImages); count { - case 0: - return "", gophercloud.ErrResourceNotFound{ - Name: name, - ResourceType: "image", - } - case 1: - return allImages[0].ID, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{ - Name: name, - Count: count, - ResourceType: "image", - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } + + return IDs, nil } diff --git a/openstack/networking/v2/networks/utils.go b/openstack/networking/v2/networks/utils.go index 39b120c8..54584828 100644 --- a/openstack/networking/v2/networks/utils.go +++ b/openstack/networking/v2/networks/utils.go @@ -5,39 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" ) -// IDFromName is a convenience function that returns a network's ID, given -// its name. +// IDFromName is a convenience function that returns a network's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } - listOpts := networks.ListOpts{ - Name: name, + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "network"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"} } +} - pages, err := networks.List(client, listOpts).AllPages() +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := networks.List(client, networks.ListOpts{ + Name: name, + }).AllPages() if err != nil { - return "", err + return nil, err } all, err := networks.ExtractNetworks(pages) if err != nil { - return "", err + return nil, err } - for _, s := range all { - if s.Name == name { - count++ - id = s.ID - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "network"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "network"} - } + return IDs, nil } diff --git a/openstack/networking/v2/ports/utils.go b/openstack/networking/v2/ports/utils.go index 6a5ed0ec..6d7f44bd 100644 --- a/openstack/networking/v2/ports/utils.go +++ b/openstack/networking/v2/ports/utils.go @@ -5,39 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" ) -// IDFromName is a convenience function that returns a port's ID, -// given its name. +// IDFromName is a convenience function that returns a port's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } - listOpts := ports.ListOpts{ - Name: name, + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "port"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "port"} } +} - pages, err := ports.List(client, listOpts).AllPages() +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := ports.List(client, ports.ListOpts{ + Name: name, + }).AllPages() if err != nil { - return "", err + return nil, err } all, err := ports.ExtractPorts(pages) if err != nil { - return "", err + return nil, err } - for _, s := range all { - if s.Name == name { - count++ - id = s.ID - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "port"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "port"} - } + return IDs, nil } diff --git a/openstack/networking/v2/subnets/utils.go b/openstack/networking/v2/subnets/utils.go index e908039f..02332bfa 100644 --- a/openstack/networking/v2/subnets/utils.go +++ b/openstack/networking/v2/subnets/utils.go @@ -5,39 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" ) -// IDFromName is a convenience function that returns a subnet's ID, -// given its name. +// IDFromName is a convenience function that returns a subnet's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - count := 0 - id := "" + IDs, err := IDsFromName(client, name) + if err != nil { + return "", err + } - listOpts := subnets.ListOpts{ - Name: name, + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "subnet"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "subnet"} } +} - pages, err := subnets.List(client, listOpts).AllPages() +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := subnets.List(client, subnets.ListOpts{ + Name: name, + }).AllPages() if err != nil { - return "", err + return nil, err } all, err := subnets.ExtractSubnets(pages) if err != nil { - return "", err + return nil, err } - for _, s := range all { - if s.Name == name { - count++ - id = s.ID - } + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "subnet"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "subnet"} - } + return IDs, nil } diff --git a/openstack/sharedfilesystems/v2/shares/utils.go b/openstack/sharedfilesystems/v2/shares/utils.go index 9a5f342d..82a00c7a 100644 --- a/openstack/sharedfilesystems/v2/shares/utils.go +++ b/openstack/sharedfilesystems/v2/shares/utils.go @@ -5,28 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/shares" ) -// IDFromName is a convenience function that returns a share's ID given its name. +// IDFromName is a convenience function that returns a share's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - listOpts := shares.ListOpts{ - Name: name, - } - - r, err := shares.ListDetail(client, listOpts).AllPages() + IDs, err := IDsFromName(client, name) if err != nil { return "", err } - ss, err := shares.ExtractShares(r) - if err != nil { - return "", err - } - - switch len(ss) { + switch count := len(IDs); count { case 0: return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "share"} case 1: - return ss[0].ID, nil + return IDs[0], nil default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: len(ss), ResourceType: "share"} + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "share"} + } +} + +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := shares.ListDetail(client, shares.ListOpts{ + Name: name, + }).AllPages() + if err != nil { + return nil, err } + + all, err := shares.ExtractShares(pages) + if err != nil { + return nil, err + } + + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID + } + + return IDs, nil } diff --git a/openstack/sharedfilesystems/v2/sharetypes/utils.go b/openstack/sharedfilesystems/v2/sharetypes/utils.go index 60d80fb4..0a8b1f38 100644 --- a/openstack/sharedfilesystems/v2/sharetypes/utils.go +++ b/openstack/sharedfilesystems/v2/sharetypes/utils.go @@ -5,36 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/sharetypes" ) -// IDFromName is a convenience function that returns a share-type's ID given its name. +// IDFromName is a convenience function that returns a share type's ID given +// its name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - r, err := sharetypes.List(client, nil).AllPages() + IDs, err := IDsFromName(client, name) if err != nil { - return "", nil + return "", err } - ss, err := sharetypes.ExtractShareTypes(r) + switch count := len(IDs); count { + case 0: + return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "share type"} + case 1: + return IDs[0], nil + default: + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "share type"} + } +} + +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := sharetypes.List(client, nil).AllPages() if err != nil { - return "", err + return nil, err } - var ( - count int - id string - ) + all, err := sharetypes.ExtractShareTypes(pages) + if err != nil { + return nil, err + } - for _, s := range ss { + IDs := make([]string, 0, len(all)) + for _, s := range all { if s.Name == name { - count++ - id = s.ID + IDs = append(IDs, s.ID) } } - switch count { - case 0: - return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "share type"} - case 1: - return id, nil - default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "share type"} - } + return IDs, nil } diff --git a/openstack/sharedfilesystems/v2/snapshots/utils.go b/openstack/sharedfilesystems/v2/snapshots/utils.go index c812d4e3..fc42d009 100644 --- a/openstack/sharedfilesystems/v2/snapshots/utils.go +++ b/openstack/sharedfilesystems/v2/snapshots/utils.go @@ -5,28 +5,43 @@ import ( "github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/snapshots" ) -// IDFromName is a convenience function that returns a snapshot's ID given its name. +// IDFromName is a convenience function that returns a network's ID given its +// name. Errors when the number of items found is not one. func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) { - listOpts := snapshots.ListOpts{ - Name: name, - } - - r, err := snapshots.ListDetail(client, listOpts).AllPages() + IDs, err := IDsFromName(client, name) if err != nil { return "", err } - ss, err := snapshots.ExtractSnapshots(r) - if err != nil { - return "", err - } - - switch len(ss) { + switch count := len(IDs); count { case 0: return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"} case 1: - return ss[0].ID, nil + return IDs[0], nil default: - return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: len(ss), ResourceType: "snapshot"} + return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"} + } +} + +// IDsFromName returns zero or more IDs corresponding to a name. The returned +// error is only non-nil in case of failure. +func IDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) { + pages, err := snapshots.ListDetail(client, snapshots.ListOpts{ + Name: name, + }).AllPages() + if err != nil { + return nil, err } + + all, err := snapshots.ExtractSnapshots(pages) + if err != nil { + return nil, err + } + + IDs := make([]string, len(all)) + for i := range all { + IDs[i] = all[i].ID + } + + return IDs, nil }