// Package ipaddresses implements CRUD against the `ip_addresses` // table — addresses bound to operator-declared network interfaces, // with is_vip + vip_priority for cluster-failover semantics. package ipaddresses import ( "context" "errors" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "git.netcell-it.de/projekte/edgeguard-native/internal/models" ) var ErrNotFound = errors.New("ip address not found") type Repo struct { Pool *pgxpool.Pool } func New(pool *pgxpool.Pool) *Repo { return &Repo{Pool: pool} } const baseSelect = ` SELECT id, interface_id, address, prefix, is_vip, vip_priority, description, active, created_at, updated_at FROM ip_addresses ` func (r *Repo) List(ctx context.Context) ([]models.IPAddress, error) { rows, err := r.Pool.Query(ctx, baseSelect+" ORDER BY interface_id ASC, address ASC") if err != nil { return nil, err } defer rows.Close() out := make([]models.IPAddress, 0, 8) for rows.Next() { a, err := scan(rows) if err != nil { return nil, err } out = append(out, *a) } return out, rows.Err() } func (r *Repo) ListForInterface(ctx context.Context, ifaceID int64) ([]models.IPAddress, error) { rows, err := r.Pool.Query(ctx, baseSelect+ " WHERE interface_id = $1 ORDER BY address ASC", ifaceID) if err != nil { return nil, err } defer rows.Close() out := make([]models.IPAddress, 0, 4) for rows.Next() { a, err := scan(rows) if err != nil { return nil, err } out = append(out, *a) } return out, rows.Err() } func (r *Repo) Get(ctx context.Context, id int64) (*models.IPAddress, error) { row := r.Pool.QueryRow(ctx, baseSelect+" WHERE id = $1", id) a, err := scan(row) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } return nil, err } return a, nil } func (r *Repo) Create(ctx context.Context, a models.IPAddress) (*models.IPAddress, error) { row := r.Pool.QueryRow(ctx, ` INSERT INTO ip_addresses (interface_id, address, prefix, is_vip, vip_priority, description, active) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id, interface_id, address, prefix, is_vip, vip_priority, description, active, created_at, updated_at`, a.InterfaceID, a.Address, a.Prefix, a.IsVIP, a.VIPPriority, a.Description, a.Active) return scan(row) } func (r *Repo) Update(ctx context.Context, id int64, a models.IPAddress) (*models.IPAddress, error) { row := r.Pool.QueryRow(ctx, ` UPDATE ip_addresses SET interface_id = $1, address = $2, prefix = $3, is_vip = $4, vip_priority = $5, description = $6, active = $7, updated_at = NOW() WHERE id = $8 RETURNING id, interface_id, address, prefix, is_vip, vip_priority, description, active, created_at, updated_at`, a.InterfaceID, a.Address, a.Prefix, a.IsVIP, a.VIPPriority, a.Description, a.Active, id) out, err := scan(row) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } return nil, err } return out, nil } func (r *Repo) Delete(ctx context.Context, id int64) error { tag, err := r.Pool.Exec(ctx, `DELETE FROM ip_addresses WHERE id = $1`, id) if err != nil { return err } if tag.RowsAffected() == 0 { return ErrNotFound } return nil } func scan(row interface{ Scan(...any) error }) (*models.IPAddress, error) { var a models.IPAddress if err := row.Scan( &a.ID, &a.InterfaceID, &a.Address, &a.Prefix, &a.IsVIP, &a.VIPPriority, &a.Description, &a.Active, &a.CreatedAt, &a.UpdatedAt, ); err != nil { return nil, err } return &a, nil }