Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions openstack/blockstorage/extensions/backups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion openstack/blockstorage/v1/snapshots/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
2 changes: 1 addition & 1 deletion openstack/blockstorage/v1/volumes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
2 changes: 1 addition & 1 deletion openstack/blockstorage/v2/snapshots/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
2 changes: 1 addition & 1 deletion openstack/blockstorage/v2/volumes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
47 changes: 26 additions & 21 deletions openstack/blockstorage/v3/snapshots/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 26 additions & 21 deletions openstack/blockstorage/v3/volumes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
54 changes: 28 additions & 26 deletions openstack/compute/v2/flavors/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
52 changes: 28 additions & 24 deletions openstack/compute/v2/servers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading