Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit d6b6004

Browse files
committed
Merge pull request #439 from jrperritt/id-from-name
IDFromName for networking resources
2 parents e52d480 + 5d1d835 commit d6b6004

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

openstack/networking/v2/extensions/security/groups/requests.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,36 @@ func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
9191
_, res.Err = c.Delete(resourceURL(c, id), nil)
9292
return res
9393
}
94+
95+
// IDFromName is a convenience function that returns a security group's ID given its name.
96+
func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
97+
securityGroupCount := 0
98+
securityGroupID := ""
99+
if name == "" {
100+
return "", fmt.Errorf("A security group name must be provided.")
101+
}
102+
pager := List(client, ListOpts{})
103+
pager.EachPage(func(page pagination.Page) (bool, error) {
104+
securityGroupList, err := ExtractGroups(page)
105+
if err != nil {
106+
return false, err
107+
}
108+
109+
for _, s := range securityGroupList {
110+
if s.Name == name {
111+
securityGroupCount++
112+
securityGroupID = s.ID
113+
}
114+
}
115+
return true, nil
116+
})
117+
118+
switch securityGroupCount {
119+
case 0:
120+
return "", fmt.Errorf("Unable to find security group: %s", name)
121+
case 1:
122+
return securityGroupID, nil
123+
default:
124+
return "", fmt.Errorf("Found %d security groups matching %s", securityGroupCount, name)
125+
}
126+
}

openstack/networking/v2/networks/requests.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package networks
22

33
import (
4+
"fmt"
5+
46
"github.com/rackspace/gophercloud"
57
"github.com/rackspace/gophercloud/pagination"
68
)
@@ -189,3 +191,36 @@ func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
189191
_, res.Err = c.Delete(deleteURL(c, networkID), nil)
190192
return res
191193
}
194+
195+
// IDFromName is a convenience function that returns a network's ID given its name.
196+
func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
197+
networkCount := 0
198+
networkID := ""
199+
if name == "" {
200+
return "", fmt.Errorf("A network name must be provided.")
201+
}
202+
pager := List(client, nil)
203+
pager.EachPage(func(page pagination.Page) (bool, error) {
204+
networkList, err := ExtractNetworks(page)
205+
if err != nil {
206+
return false, err
207+
}
208+
209+
for _, n := range networkList {
210+
if n.Name == name {
211+
networkCount++
212+
networkID = n.ID
213+
}
214+
}
215+
return true, nil
216+
})
217+
218+
switch networkCount {
219+
case 0:
220+
return "", fmt.Errorf("Unable to find network: %s", name)
221+
case 1:
222+
return networkID, nil
223+
default:
224+
return "", fmt.Errorf("Found %d networks matching %s", networkCount, name)
225+
}
226+
}

openstack/networking/v2/ports/requests.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ports
22

33
import (
4+
"fmt"
5+
46
"github.com/rackspace/gophercloud"
57
"github.com/rackspace/gophercloud/pagination"
68
)
@@ -223,3 +225,36 @@ func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
223225
_, res.Err = c.Delete(deleteURL(c, id), nil)
224226
return res
225227
}
228+
229+
// IDFromName is a convenience function that returns a port's ID given its name.
230+
func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
231+
portCount := 0
232+
portID := ""
233+
if name == "" {
234+
return "", fmt.Errorf("A port name must be provided.")
235+
}
236+
pager := List(client, nil)
237+
pager.EachPage(func(page pagination.Page) (bool, error) {
238+
portList, err := ExtractPorts(page)
239+
if err != nil {
240+
return false, err
241+
}
242+
243+
for _, p := range portList {
244+
if p.Name == name {
245+
portCount++
246+
portID = p.ID
247+
}
248+
}
249+
return true, nil
250+
})
251+
252+
switch portCount {
253+
case 0:
254+
return "", fmt.Errorf("Unable to find port: %s", name)
255+
case 1:
256+
return portID, nil
257+
default:
258+
return "", fmt.Errorf("Found %d ports matching %s", portCount, name)
259+
}
260+
}

openstack/networking/v2/subnets/requests.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package subnets
22

33
import (
4+
"fmt"
5+
46
"github.com/rackspace/gophercloud"
57
"github.com/rackspace/gophercloud/pagination"
68
)
@@ -234,3 +236,36 @@ func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
234236
_, res.Err = c.Delete(deleteURL(c, id), nil)
235237
return res
236238
}
239+
240+
// IDFromName is a convenience function that returns a subnet's ID given its name.
241+
func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
242+
subnetCount := 0
243+
subnetID := ""
244+
if name == "" {
245+
return "", fmt.Errorf("A subnet name must be provided.")
246+
}
247+
pager := List(client, nil)
248+
pager.EachPage(func(page pagination.Page) (bool, error) {
249+
subnetList, err := ExtractSubnets(page)
250+
if err != nil {
251+
return false, err
252+
}
253+
254+
for _, s := range subnetList {
255+
if s.Name == name {
256+
subnetCount++
257+
subnetID = s.ID
258+
}
259+
}
260+
return true, nil
261+
})
262+
263+
switch subnetCount {
264+
case 0:
265+
return "", fmt.Errorf("Unable to find subnet: %s", name)
266+
case 1:
267+
return subnetID, nil
268+
default:
269+
return "", fmt.Errorf("Found %d subnets matching %s", subnetCount, name)
270+
}
271+
}

0 commit comments

Comments
 (0)