Browse Source

Filter openstack floating IPs by tenant ID

Merge j2sol:filter-openstack-floating and fix up for comments in PR #1809.

Signed-off-by: Matt Vinall <matt.vinall@imgtec.com>
Matt Vinall 3 years ago
parent
commit
2f482ac085
2 changed files with 74 additions and 0 deletions
  1. 42
    0
      drivers/openstack/client.go
  2. 32
    0
      drivers/openstack/openstack.go

+ 42
- 0
drivers/openstack/client.go View File

@@ -17,6 +17,7 @@ import (
17 17
 	"github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
18 18
 	"github.com/rackspace/gophercloud/openstack/compute/v2/images"
19 19
 	"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
20
+	"github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
20 21
 	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
21 22
 	"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
22 23
 	"github.com/rackspace/gophercloud/openstack/networking/v2/ports"
@@ -26,6 +27,7 @@ import (
26 27
 type Client interface {
27 28
 	Authenticate(d *Driver) error
28 29
 	InitComputeClient(d *Driver) error
30
+	InitIdentityClient(d *Driver) error
29 31
 	InitNetworkClient(d *Driver) error
30 32
 
31 33
 	CreateInstance(d *Driver) (string, error)
@@ -45,11 +47,13 @@ type Client interface {
45 47
 	GetFloatingIPs(d *Driver) ([]FloatingIP, error)
46 48
 	GetFloatingIPPoolID(d *Driver) (string, error)
47 49
 	GetInstancePortID(d *Driver) (string, error)
50
+	GetTenantID(d *Driver) (string, error)
48 51
 }
49 52
 
50 53
 type GenericClient struct {
51 54
 	Provider *gophercloud.ProviderClient
52 55
 	Compute  *gophercloud.ServiceClient
56
+	Identity *gophercloud.ServiceClient
53 57
 	Network  *gophercloud.ServiceClient
54 58
 }
55 59
 
@@ -273,6 +277,29 @@ func (c *GenericClient) GetImageID(d *Driver) (string, error) {
273 277
 	return imageID, err
274 278
 }
275 279
 
280
+func (c *GenericClient) GetTenantID(d *Driver) (string, error) {
281
+	pager := tenants.List(c.Identity, nil)
282
+	tenantId := ""
283
+
284
+	err := pager.EachPage(func(page pagination.Page) (bool, error) {
285
+		tenantList, err := tenants.ExtractTenants(page)
286
+		if err != nil {
287
+			return false, err
288
+		}
289
+
290
+		for _, i := range tenantList {
291
+			if i.Name == d.TenantName {
292
+				tenantId = i.ID
293
+				return false, nil
294
+			}
295
+		}
296
+
297
+		return true, nil
298
+	})
299
+
300
+	return tenantId, err
301
+}
302
+
276 303
 func (c *GenericClient) CreateKeyPair(d *Driver, name string, publicKey string) error {
277 304
 	opts := keypairs.CreateOpts{
278 305
 		Name:      name,
@@ -378,8 +405,13 @@ func (c *GenericClient) getNovaNetworkFloatingIPs(d *Driver) ([]FloatingIP, erro
378 405
 }
379 406
 
380 407
 func (c *GenericClient) getNeutronNetworkFloatingIPs(d *Driver) ([]FloatingIP, error) {
408
+	log.Debug("Listing floating IPs", map[string]string{
409
+		"FloatingNetworkId": d.FloatingIpPoolId,
410
+		"TenantID":          d.TenantId,
411
+	})
381 412
 	pager := floatingips.List(c.Network, floatingips.ListOpts{
382 413
 		FloatingNetworkID: d.FloatingIpPoolId,
414
+		TenantID:          d.TenantId,
383 415
 	})
384 416
 
385 417
 	ips := []FloatingIP{}
@@ -446,6 +478,16 @@ func (c *GenericClient) InitComputeClient(d *Driver) error {
446 478
 	return nil
447 479
 }
448 480
 
481
+func (c *GenericClient) InitIdentityClient(d *Driver) error {
482
+	if c.Identity != nil {
483
+		return nil
484
+	}
485
+
486
+	identity := openstack.NewIdentityV2(c.Provider)
487
+	c.Identity = identity
488
+	return nil
489
+}
490
+
449 491
 func (c *GenericClient) InitNetworkClient(d *Driver) error {
450 492
 	if c.Network != nil {
451 493
 		return nil

+ 32
- 0
drivers/openstack/openstack.go View File

@@ -427,6 +427,7 @@ const (
427 427
 	errorUnknownFlavorName       string = "Unable to find flavor named %s"
428 428
 	errorUnknownImageName        string = "Unable to find image named %s"
429 429
 	errorUnknownNetworkName      string = "Unable to find network named %s"
430
+	errorUnknownTenantName       string = "Unable to find tenant named %s"
430 431
 )
431 432
 
432 433
 func (d *Driver) checkConfig() error {
@@ -552,6 +553,27 @@ func (d *Driver) resolveIds() error {
552 553
 		})
553 554
 	}
554 555
 
556
+	if d.TenantId == "" {
557
+		if err := d.initIdentity(); err != nil {
558
+			return err
559
+		}
560
+		tenantId, err := d.client.GetTenantID(d)
561
+
562
+		if err != nil {
563
+			return err
564
+		}
565
+
566
+		if tenantId == "" {
567
+			return fmt.Errorf(errorUnknownTenantName, d.TenantName)
568
+		}
569
+
570
+		d.TenantId = tenantId
571
+		log.Debug("Found tenant id using its name", map[string]string{
572
+			"Name": d.TenantName,
573
+			"ID":   d.TenantId,
574
+		})
575
+	}
576
+
555 577
 	return nil
556 578
 }
557 579
 
@@ -565,6 +587,16 @@ func (d *Driver) initCompute() error {
565 587
 	return nil
566 588
 }
567 589
 
590
+func (d *Driver) initIdentity() error {
591
+	if err := d.client.Authenticate(d); err != nil {
592
+		return err
593
+	}
594
+	if err := d.client.InitIdentityClient(d); err != nil {
595
+		return err
596
+	}
597
+	return nil
598
+}
599
+
568 600
 func (d *Driver) initNetwork() error {
569 601
 	if err := d.client.Authenticate(d); err != nil {
570 602
 		return err

Loading…
Cancel
Save