This repository was archived by the owner on Aug 1, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +138
-0
lines changed
extensions/security/groups Expand file tree Collapse file tree 4 files changed +138
-0
lines changed Original file line number Diff line number Diff line change @@ -91,3 +91,36 @@ func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
91
91
_ , res .Err = c .Delete (resourceURL (c , id ), nil )
92
92
return res
93
93
}
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
+ }
Original file line number Diff line number Diff line change 1
1
package networks
2
2
3
3
import (
4
+ "fmt"
5
+
4
6
"github.com/rackspace/gophercloud"
5
7
"github.com/rackspace/gophercloud/pagination"
6
8
)
@@ -189,3 +191,36 @@ func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
189
191
_ , res .Err = c .Delete (deleteURL (c , networkID ), nil )
190
192
return res
191
193
}
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
+ }
Original file line number Diff line number Diff line change 1
1
package ports
2
2
3
3
import (
4
+ "fmt"
5
+
4
6
"github.com/rackspace/gophercloud"
5
7
"github.com/rackspace/gophercloud/pagination"
6
8
)
@@ -223,3 +225,36 @@ func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
223
225
_ , res .Err = c .Delete (deleteURL (c , id ), nil )
224
226
return res
225
227
}
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
+ }
Original file line number Diff line number Diff line change 1
1
package subnets
2
2
3
3
import (
4
+ "fmt"
5
+
4
6
"github.com/rackspace/gophercloud"
5
7
"github.com/rackspace/gophercloud/pagination"
6
8
)
@@ -234,3 +236,36 @@ func Delete(c *gophercloud.ServiceClient, id string) DeleteResult {
234
236
_ , res .Err = c .Delete (deleteURL (c , id ), nil )
235
237
return res
236
238
}
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
+ }
You can’t perform that action at this time.
0 commit comments