Files
cloud-ip-validator/internal/orchestrator/floating_ips.go
T

65 lines
2.3 KiB
Go

package orchestrator
import (
"context"
"fmt"
"time"
"cloudipvalidator/internal/db"
"cloudipvalidator/internal/openstack"
)
func (o *Orchestrator) matchesFloatingIP(fip *openstack.FloatingIP, address string) bool {
return fip != nil && fip.ID != "" && fip.Address == address &&
(o.FIPScope.ProjectID == "" || fip.ProjectID == o.FIPScope.ProjectID)
}
func (o *Orchestrator) readFloatingIP(ctx context.Context, operationID, address, id string) (*openstack.FloatingIP, error) {
start := time.Now()
fip, err := o.OS.GetFloatingIP(ctx, id)
if err == nil && (!o.matchesFloatingIP(fip, address) || fip.ID != id) {
fip = nil
err = fmt.Errorf("resource %s does not match the expected address and project: %w", id, openstack.ErrNotFound(address))
}
o.logFloatingIPStage(operationID, address, id, "get_by_uuid", start, err)
return fip, err
}
// resolveFloatingIP is for new attachments. Existing attachments and
// reservations use readFloatingIP with their own recorded UUID.
func (o *Orchestrator) resolveFloatingIP(ctx context.Context, operationID, address string) (*openstack.FloatingIP, error) {
id, err := o.DB.FloatingIPCandidate(ctx, o.FIPScope, address)
if err != nil {
return nil, fmt.Errorf("read floating ip mapping: %w", err)
}
var fip *openstack.FloatingIP
start := time.Now()
if id != "" {
fip, err = o.readFloatingIP(ctx, operationID, address, id)
if err != nil && !openstack.IsNotFound(err) {
return nil, err
}
}
if fip == nil {
start = time.Now()
fip, err = o.OS.GetFloatingIPByAddress(ctx, address)
if err == nil && !o.matchesFloatingIP(fip, address) {
err = openstack.ErrNotFound(address)
}
o.logFloatingIPStage(operationID, address, "", "find_by_address", start, err)
if err != nil {
return nil, err
}
}
mapping := db.FloatingIPMapping{Address: address, FIPID: fip.ID}
if err := o.DB.ConfirmFloatingIPs(ctx, o.FIPScope, []db.FloatingIPMapping{mapping}, start); err != nil {
return nil, fmt.Errorf("record floating ip mapping: %w", err)
}
return fip, nil
}
func (o *Orchestrator) logFloatingIPStage(operationID, address, id, stage string, start time.Time, err error) {
o.Log.Info("floating ip stage", "operation_id", operationID, "ip", address, "fip_id", id,
"stage", stage, "duration_ms", time.Since(start).Milliseconds(), "err", err)
}