Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Filter openstack floating IPs by tenant ID #1809

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions drivers/openstack/client.go
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
"github.com/rackspace/gophercloud/openstack/compute/v2/images"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"github.com/rackspace/gophercloud/openstack/identity/v2/tenants"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
"github.com/rackspace/gophercloud/openstack/networking/v2/ports"
Expand All @@ -25,6 +26,7 @@ import (
type Client interface {
Authenticate(d *Driver) error
InitComputeClient(d *Driver) error
InitIdentityClient(d *Driver) error
InitNetworkClient(d *Driver) error

CreateInstance(d *Driver) (string, error)
Expand All @@ -44,11 +46,13 @@ type Client interface {
GetFloatingIPs(d *Driver) ([]FloatingIp, error)
GetFloatingIpPoolId(d *Driver) (string, error)
GetInstancePortId(d *Driver) (string, error)
GetTenantId(d *Driver) (string, error)
}

type GenericClient struct {
Provider *gophercloud.ProviderClient
Compute *gophercloud.ServiceClient
Identity *gophercloud.ServiceClient
Network *gophercloud.ServiceClient
}

Expand Down Expand Up @@ -261,6 +265,29 @@ func (c *GenericClient) GetImageId(d *Driver) (string, error) {
return imageId, err
}

func (c *GenericClient) GetTenantId(d *Driver) (string, error) {
pager := tenants.List(c.Identity, nil)
tenantId := ""

err := pager.EachPage(func(page pagination.Page) (bool, error) {
tenantList, err := tenants.ExtractTenants(page)
if err != nil {
return false, err
}

for _, i := range tenantList {
if i.Name == d.TenantName {
tenantId = i.ID
return false, nil
}
}

return true, nil
})

return tenantId, err
}

func (c *GenericClient) CreateKeyPair(d *Driver, name string, publicKey string) error {
opts := keypairs.CreateOpts{
Name: name,
Expand Down Expand Up @@ -312,8 +339,13 @@ func (c *GenericClient) AssignFloatingIP(d *Driver, floatingIp *FloatingIp, port
}

func (c *GenericClient) GetFloatingIPs(d *Driver) ([]FloatingIp, error) {
log.WithFields(log.Fields{
"FloatingNetworkId": d.FloatingIpPoolId,
"TenantID": d.TenantId,
}).Debug("Listing floating IPs")
pager := floatingips.List(c.Network, floatingips.ListOpts{
FloatingNetworkID: d.FloatingIpPoolId,
TenantID: d.TenantId,
})

ips := []FloatingIp{}
Expand Down Expand Up @@ -380,6 +412,16 @@ func (c *GenericClient) InitComputeClient(d *Driver) error {
return nil
}

func (c *GenericClient) InitIdentityClient(d *Driver) error {
if c.Identity != nil {
return nil
}

identity := openstack.NewIdentityV2(c.Provider)
c.Identity = identity
return nil
}

func (c *GenericClient) InitNetworkClient(d *Driver) error {
if c.Network != nil {
return nil
Expand Down
32 changes: 32 additions & 0 deletions drivers/openstack/openstack.go
Expand Up @@ -391,6 +391,7 @@ const (
errorUnknownFlavorName string = "Unable to find flavor named %s"
errorUnknownImageName string = "Unable to find image named %s"
errorUnknownNetworkName string = "Unable to find network named %s"
errorUnknownTenantName string = "Unable to find tenant named %s"
)

func (d *Driver) checkConfig() error {
Expand Down Expand Up @@ -516,6 +517,27 @@ func (d *Driver) resolveIds() error {
}).Debug("Found floating IP pool id using its name")
}

if d.TenantName != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If think it would be better to check if d.TenantId == "" rather than if d.TenantName != "" because it's common to have an OpenStack rc file containing both the tenant id and the tenant name. In such a situation it would be a pity to compute the id from the name.

if err := d.initIdentity(); err != nil {
return err
}
tenantId, err := d.client.GetTenantId(d)

if err != nil {
return err
}

if tenantId == "" {
return fmt.Errorf(errorUnknownTenantName, d.TenantName)
}

d.TenantId = tenantId
log.WithFields(log.Fields{
"Name": d.TenantName,
"ID": d.TenantId,
}).Debug("Found tenant id using its name")
}

return nil
}

Expand All @@ -529,6 +551,16 @@ func (d *Driver) initCompute() error {
return nil
}

func (d *Driver) initIdentity() error {
if err := d.client.Authenticate(d); err != nil {
return err
}
if err := d.client.InitIdentityClient(d); err != nil {
return err
}
return nil
}

func (d *Driver) initNetwork() error {
if err := d.client.Authenticate(d); err != nil {
return err
Expand Down