IDFromName for networking resources
diff --git a/openstack/networking/v2/extensions/security/groups/requests.go b/openstack/networking/v2/extensions/security/groups/requests.go
index 55e4b3b..3afcf24 100644
--- a/openstack/networking/v2/extensions/security/groups/requests.go
+++ b/openstack/networking/v2/extensions/security/groups/requests.go
@@ -91,3 +91,36 @@
_, res.Err = c.Delete(resourceURL(c, id), nil)
return res
}
+
+// IDFromName is a convienience function that returns a securitygroup's ID given its name.
+func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
+ securityGroupCount := 0
+ securityGroupID := ""
+ if name == "" {
+ return "", fmt.Errorf("A network name must be provided.")
+ }
+ pager := List(client, ListOpts{})
+ pager.EachPage(func(page pagination.Page) (bool, error) {
+ securityGroupList, err := ExtractGroups(page)
+ if err != nil {
+ return false, err
+ }
+
+ for _, s := range securityGroupList {
+ if s.Name == name {
+ securityGroupCount++
+ securityGroupID = s.ID
+ }
+ }
+ return true, nil
+ })
+
+ switch securityGroupCount {
+ case 0:
+ return "", fmt.Errorf("Unable to find network: %s", name)
+ case 1:
+ return securityGroupID, nil
+ default:
+ return "", fmt.Errorf("Found %d networks matching %s", securityGroupCount, name)
+ }
+}
diff --git a/openstack/networking/v2/networks/requests.go b/openstack/networking/v2/networks/requests.go
index 7be3227..1df95de 100644
--- a/openstack/networking/v2/networks/requests.go
+++ b/openstack/networking/v2/networks/requests.go
@@ -1,6 +1,8 @@
package networks
import (
+ "fmt"
+
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -189,3 +191,36 @@
_, res.Err = c.Delete(deleteURL(c, networkID), nil)
return res
}
+
+// IDFromName is a convienience function that returns a network's ID given its name.
+func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
+ networkCount := 0
+ networkID := ""
+ if name == "" {
+ return "", fmt.Errorf("A network name must be provided.")
+ }
+ pager := List(client, nil)
+ pager.EachPage(func(page pagination.Page) (bool, error) {
+ networkList, err := ExtractNetworks(page)
+ if err != nil {
+ return false, err
+ }
+
+ for _, n := range networkList {
+ if n.Name == name {
+ networkCount++
+ networkID = n.ID
+ }
+ }
+ return true, nil
+ })
+
+ switch networkCount {
+ case 0:
+ return "", fmt.Errorf("Unable to find network: %s", name)
+ case 1:
+ return networkID, nil
+ default:
+ return "", fmt.Errorf("Found %d networks matching %s", networkCount, name)
+ }
+}
diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go
index 781a3c3..bba8b71 100644
--- a/openstack/networking/v2/ports/requests.go
+++ b/openstack/networking/v2/ports/requests.go
@@ -1,6 +1,8 @@
package ports
import (
+ "fmt"
+
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -223,3 +225,36 @@
_, res.Err = c.Delete(deleteURL(c, id), nil)
return res
}
+
+// IDFromName is a convienience function that returns a port's ID given its name.
+func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
+ portCount := 0
+ portID := ""
+ if name == "" {
+ return "", fmt.Errorf("A network name must be provided.")
+ }
+ pager := List(client, nil)
+ pager.EachPage(func(page pagination.Page) (bool, error) {
+ portList, err := ExtractPorts(page)
+ if err != nil {
+ return false, err
+ }
+
+ for _, p := range portList {
+ if p.Name == name {
+ portCount++
+ portID = p.ID
+ }
+ }
+ return true, nil
+ })
+
+ switch portCount {
+ case 0:
+ return "", fmt.Errorf("Unable to find network: %s", name)
+ case 1:
+ return portID, nil
+ default:
+ return "", fmt.Errorf("Found %d networks matching %s", portCount, name)
+ }
+}
diff --git a/openstack/networking/v2/subnets/requests.go b/openstack/networking/v2/subnets/requests.go
index 6e01f05..c3f6364 100644
--- a/openstack/networking/v2/subnets/requests.go
+++ b/openstack/networking/v2/subnets/requests.go
@@ -1,6 +1,8 @@
package subnets
import (
+ "fmt"
+
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
@@ -234,3 +236,36 @@
_, res.Err = c.Delete(deleteURL(c, id), nil)
return res
}
+
+// IDFromName is a convienience function that returns a subnet's ID given its name.
+func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
+ subnetCount := 0
+ subnetID := ""
+ if name == "" {
+ return "", fmt.Errorf("A network name must be provided.")
+ }
+ pager := List(client, nil)
+ pager.EachPage(func(page pagination.Page) (bool, error) {
+ subnetList, err := ExtractSubnets(page)
+ if err != nil {
+ return false, err
+ }
+
+ for _, s := range subnetList {
+ if s.Name == name {
+ subnetCount++
+ subnetID = s.ID
+ }
+ }
+ return true, nil
+ })
+
+ switch subnetCount {
+ case 0:
+ return "", fmt.Errorf("Unable to find network: %s", name)
+ case 1:
+ return subnetID, nil
+ default:
+ return "", fmt.Errorf("Found %d networks matching %s", subnetCount, name)
+ }
+}