Persist Floating IP UUIDs for direct Neutron operations
This commit is contained in:
1 parent
89ac4d5bcd
commit
94124747ce
24 files changed
+918
-116
No files matched your search
@@ -68,6 +68,8 @@ type ClientConfig struct {
|
||||
type Client struct {
|
||||
networking *gophercloud.ServiceClient
|
||||
retry pageRetry
|
||||
projectID string
|
||||
region string
|
||||
}
|
||||
|
||||
// buildAuthOptions translates ClientConfig into gophercloud.AuthOptions. It
|
||||
@@ -152,11 +154,25 @@ func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
|
||||
if retries < 0 {
|
||||
retries = DefaultListPageRetries
|
||||
}
|
||||
return &Client{networking: networking, retry: pageRetry{Retries: retries}}, nil
|
||||
region := cfg.Region
|
||||
if region == "" {
|
||||
// Endpoint selection can fall back to a catalog entry with no region
|
||||
// configured. Keep mappings for different endpoints isolated.
|
||||
region = networking.Endpoint
|
||||
}
|
||||
return &Client{networking: networking, retry: pageRetry{Retries: retries}, projectID: cfg.ProjectID, region: region}, nil
|
||||
}
|
||||
|
||||
func (c *Client) FloatingIPScope() (projectID, region string) {
|
||||
return c.projectID, c.region
|
||||
}
|
||||
|
||||
func (c *Client) GetFloatingIPByAddress(ctx context.Context, address string) (*FloatingIP, error) {
|
||||
pages, err := floatingips.List(c.networking, floatingips.ListOpts{FloatingIP: address}).AllPages(ctx)
|
||||
opts := floatingips.ListOpts{FloatingIP: address, ProjectID: c.projectID}
|
||||
pages, err := floatingips.List(c.networking, opts).AllPages(ctx)
|
||||
if gophercloud.ResponseCodeIs(err, 404) {
|
||||
return nil, ErrNotFound(address)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("openstack: list floating ips: %w", err)
|
||||
}
|
||||
@@ -164,11 +180,41 @@ func (c *Client) GetFloatingIPByAddress(ctx context.Context, address string) (*F
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("openstack: extract floating ips: %w", err)
|
||||
}
|
||||
if len(list) == 0 {
|
||||
var found *FloatingIP
|
||||
for _, raw := range list {
|
||||
f := extractFloatingIP(raw)
|
||||
if f.Address != address || f.ID == "" || (c.projectID != "" && f.ProjectID != c.projectID) {
|
||||
continue
|
||||
}
|
||||
if found != nil && found.ID != f.ID {
|
||||
return nil, fmt.Errorf("openstack: multiple floating ips match address %s in project %s", address, c.projectID)
|
||||
}
|
||||
found = &f
|
||||
}
|
||||
if found == nil {
|
||||
return nil, ErrNotFound(address)
|
||||
}
|
||||
f := list[0]
|
||||
return &FloatingIP{ID: f.ID, Address: f.FloatingIP, PortID: f.PortID, ProjectID: f.TenantID}, nil
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetFloatingIP(ctx context.Context, id string) (*FloatingIP, error) {
|
||||
f, err := floatingips.Get(ctx, c.networking, id).Extract()
|
||||
if gophercloud.ResponseCodeIs(err, 404) {
|
||||
return nil, ErrNotFound(id)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("openstack: get floating ip %s: %w", id, err)
|
||||
}
|
||||
ip := extractFloatingIP(*f)
|
||||
return &ip, nil
|
||||
}
|
||||
|
||||
func extractFloatingIP(f floatingips.FloatingIP) FloatingIP {
|
||||
projectID := f.ProjectID
|
||||
if projectID == "" {
|
||||
projectID = f.TenantID
|
||||
}
|
||||
return FloatingIP{ID: f.ID, Address: f.FloatingIP, PortID: f.PortID, ProjectID: projectID}
|
||||
}
|
||||
|
||||
// fipListFields is the set of attributes requested from Neutron when listing:
|
||||
@@ -206,7 +252,7 @@ func (o pagedListOpts) ToFloatingIPListQuery() (string, error) {
|
||||
// a proxy that link may point at an internal host.
|
||||
func (c *Client) fetchPage(ctx context.Context, marker string, limit int) ([]FloatingIP, error) {
|
||||
opts := pagedListOpts{
|
||||
ListOpts: floatingips.ListOpts{Limit: limit, Marker: marker},
|
||||
ListOpts: floatingips.ListOpts{Limit: limit, Marker: marker, ProjectID: c.projectID},
|
||||
fields: fipListFields,
|
||||
}
|
||||
var out []FloatingIP
|
||||
@@ -217,11 +263,7 @@ func (c *Client) fetchPage(ctx context.Context, marker string, limit int) ([]Flo
|
||||
}
|
||||
out = make([]FloatingIP, 0, len(list))
|
||||
for _, f := range list {
|
||||
proj := f.TenantID
|
||||
if proj == "" {
|
||||
proj = f.ProjectID // Neutron may return only project_id when `fields` is used
|
||||
}
|
||||
out = append(out, FloatingIP{ID: f.ID, Address: f.FloatingIP, PortID: f.PortID, ProjectID: proj})
|
||||
out = append(out, extractFloatingIP(f))
|
||||
}
|
||||
return false, nil // one page per request
|
||||
})
|
||||
@@ -241,7 +283,7 @@ func (c *Client) ListFreeFloatingIPs(ctx context.Context, pageSize int, onPage f
|
||||
|
||||
// ListFloatingIPsByPort asks Neutron for the floating IPs attached to portID.
|
||||
func (c *Client) ListFloatingIPsByPort(ctx context.Context, portID string) ([]FloatingIP, error) {
|
||||
pages, err := floatingips.List(c.networking, floatingips.ListOpts{PortID: portID}).AllPages(ctx)
|
||||
pages, err := floatingips.List(c.networking, floatingips.ListOpts{PortID: portID, ProjectID: c.projectID}).AllPages(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("openstack: list floating ips of port %s: %w", portID, err)
|
||||
}
|
||||
@@ -250,12 +292,11 @@ func (c *Client) ListFloatingIPsByPort(ctx context.Context, portID string) ([]Fl
|
||||
return nil, fmt.Errorf("openstack: extract floating ips: %w", err)
|
||||
}
|
||||
out := make([]FloatingIP, 0, len(list))
|
||||
for _, f := range list {
|
||||
proj := f.TenantID
|
||||
if proj == "" {
|
||||
proj = f.ProjectID
|
||||
for _, raw := range list {
|
||||
f := extractFloatingIP(raw)
|
||||
if f.PortID == portID && (c.projectID == "" || f.ProjectID == c.projectID) {
|
||||
out = append(out, f)
|
||||
}
|
||||
out = append(out, FloatingIP{ID: f.ID, Address: f.FloatingIP, PortID: f.PortID, ProjectID: proj})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -283,6 +324,9 @@ func (c *Client) DisassociateFloatingIP(ctx context.Context, fipID string) error
|
||||
_, err := floatingips.Update(ctx, c.networking, fipID, floatingips.UpdateOpts{
|
||||
PortID: &empty,
|
||||
}).Extract()
|
||||
if gophercloud.ResponseCodeIs(err, 404) {
|
||||
return ErrNotFound(fipID)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("openstack: disassociate floating ip %s: %w", fipID, err)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
// mock via config alone.
|
||||
package openstack
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type FloatingIP struct {
|
||||
ID string
|
||||
@@ -24,6 +27,9 @@ type FloatingIPClient interface {
|
||||
// is registered in the service project.
|
||||
GetFloatingIPByAddress(ctx context.Context, address string) (*FloatingIP, error)
|
||||
|
||||
// GetFloatingIP reads a known Neutron resource by ID.
|
||||
GetFloatingIP(ctx context.Context, id string) (*FloatingIP, error)
|
||||
|
||||
// ListFloatingIPs returns every floating IP registered in the service
|
||||
// project, both associated and free — filtering to the free pool (empty
|
||||
// PortID) is the caller's job. Used to discover addresses to feed into
|
||||
@@ -64,8 +70,8 @@ func ErrNotFound(address string) error { return ¬FoundError{address} }
|
||||
// IsNotFound reports whether err indicates the address has no matching
|
||||
// Neutron floating-ip resource.
|
||||
func IsNotFound(err error) bool {
|
||||
_, ok := err.(*notFoundError)
|
||||
return ok
|
||||
var notFound *notFoundError
|
||||
return errors.As(err, ¬Found)
|
||||
}
|
||||
|
||||
// listAll collects every page of c.ListFreeFloatingIPs into one slice; it is
|
||||
|
||||
@@ -81,6 +81,17 @@ func (m *MockClient) GetFloatingIPByAddress(ctx context.Context, address string)
|
||||
return &f, nil
|
||||
}
|
||||
|
||||
func (m *MockClient) GetFloatingIP(ctx context.Context, id string) (*FloatingIP, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f, ok := m.fips[id]
|
||||
if !ok {
|
||||
return nil, ErrNotFound(id)
|
||||
}
|
||||
copy := *f
|
||||
return ©, nil
|
||||
}
|
||||
|
||||
// SeedMany registers n free floating IPs with IDs "<prefix>-00000"… and
|
||||
// ascending addresses starting at 198.18.0.1 (TEST-NET benchmarking range),
|
||||
// returning the addresses in seed order.
|
||||
|
||||
Reference in new issue
Block a user