Skip to content

Commit

Permalink
Scope IP and Port listings to current project
Browse files Browse the repository at this point in the history
  • Loading branch information
kgube committed Oct 20, 2023
1 parent d7121d9 commit 3c7a4ef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
10 changes: 6 additions & 4 deletions internal/openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ type Config struct {
}

type OpenStackClient struct {
provider *gophercloud.ProviderClient
region string
provider *gophercloud.ProviderClient
region string
projectID string
}

func (cfg AuthOpts) ToAuthOptions() gophercloud.AuthOptions {
Expand Down Expand Up @@ -144,8 +145,9 @@ func NewClient(cfg *AuthOpts) (*OpenStackClient, error) {
}

return &OpenStackClient{
provider: provider,
region: cfg.Region,
provider: provider,
region: cfg.Region,
projectID: cfg.ProjectID,
}, nil
}

Expand Down
6 changes: 5 additions & 1 deletion internal/openstack/port_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (opts CustomCreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
type OpenStackL3PortManager struct {
client *gophercloud.ServiceClient
networkID string
projectID string
cfg *NetworkingOpts
ports PortClient
}
Expand All @@ -84,10 +85,12 @@ func (client *OpenStackClient) NewOpenStackL3PortManager(networkConfig *Networki
client: networkingclient,
cfg: networkConfig,
networkID: networkID,
projectID: client.projectID,
ports: NewPortClient(
networkingclient,
TagLBManagedPort,
networkConfig.UseFloatingIPs,
client.projectID,
),
}, nil
}
Expand Down Expand Up @@ -205,7 +208,8 @@ func (pm *OpenStackL3PortManager) deleteUnusedFloatingIPs() error {
pager := floatingipsv2.List(
pm.client,
floatingipsv2.ListOpts{
Tags: TagLBManagedPort,
Tags: TagLBManagedPort,
ProjectID: pm.projectID,
},
)

Expand Down
8 changes: 5 additions & 3 deletions internal/openstack/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ type UncachedClient struct {
client *gophercloud.ServiceClient
tag string
useFloatingIPs bool
projectID string
}

type PortClient interface {
GetPorts() ([]portsv2.Port, error)
GetPortByID(ID string) (*portsv2.Port, *floatingipsv2.FloatingIP, error)
}

func NewPortClient(networkingclient *gophercloud.ServiceClient, tag string, useFloatingIPs bool) *UncachedClient {
func NewPortClient(networkingclient *gophercloud.ServiceClient, tag string, useFloatingIPs bool, projectID string) *UncachedClient {
return &UncachedClient{
client: networkingclient,
tag: tag,
useFloatingIPs: useFloatingIPs,
projectID: projectID,
}
}

func (pc *UncachedClient) GetPorts() (ports []portsv2.Port, err error) {
err = portsv2.List(
pc.client,
portsv2.ListOpts{Tags: pc.tag},
portsv2.ListOpts{Tags: pc.tag, ProjectID: pc.projectID},
).EachPage(func(page pagination.Page) (bool, error) {
fetched_ports, err := portsv2.ExtractPorts(page)
if err != nil {
Expand Down Expand Up @@ -79,7 +81,7 @@ func (pc *UncachedClient) GetPortByID(ID string) (port *portsv2.Port, fip *float
if pc.useFloatingIPs {
err = floatingipsv2.List(
pc.client,
floatingipsv2.ListOpts{Tags: pc.tag, PortID: ID},
floatingipsv2.ListOpts{Tags: pc.tag, PortID: ID, ProjectID: pc.projectID},
).EachPage(func(page pagination.Page) (bool, error) {
fips, err := floatingipsv2.ExtractFloatingIPs(page)
if err != nil {
Expand Down

0 comments on commit 3c7a4ef

Please sign in to comment.