5060804359
- Add snat/masquerade, netmap, and 1:1 nat as stored, terraformable resources: migration 0003, model types, store CRUD (id-keyed, generation-bumping), and REST handlers. These are the global-intent/device-resolved NAT tier; compiler rendering of NAT into per-device configs is a tracked follow-up. - Add a testcontainers-backed store integration suite exercising the CRUD lifecycle, generation bumping, source/dest grammar validation, and FK cascade against a real Postgres. It self-skips under 'go test -short' (the CI path) so a container runtime is only needed for the full run.
161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.unkin.net/unkin/tomswallapi/internal/model"
|
|
)
|
|
|
|
// ---- SNAT / masquerade -----------------------------------------------------
|
|
|
|
func (s *Store) ListSNAT(ctx context.Context) ([]model.SNATRule, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, action, source, egress, address, probability, comment FROM snat ORDER BY id`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []model.SNATRule
|
|
for rows.Next() {
|
|
var r model.SNATRule
|
|
if err := rows.Scan(&r.ID, &r.Action, &r.Source, &r.Egress, &r.Address, &r.Probability, &r.Comment); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) GetSNAT(ctx context.Context, id int64) (model.SNATRule, error) {
|
|
var r model.SNATRule
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT id, action, source, egress, address, probability, comment FROM snat WHERE id = $1`, id,
|
|
).Scan(&r.ID, &r.Action, &r.Source, &r.Egress, &r.Address, &r.Probability, &r.Comment)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return r, ErrNotFound
|
|
}
|
|
return r, err
|
|
}
|
|
|
|
func (s *Store) CreateSNAT(ctx context.Context, r model.SNATRule) (int64, error) {
|
|
var id int64
|
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO snat (action, source, egress, address, probability, comment)
|
|
VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`,
|
|
r.Action, r.Source, r.Egress, r.Address, r.Probability, r.Comment,
|
|
).Scan(&id); err != nil {
|
|
return err
|
|
}
|
|
return bump(ctx, tx)
|
|
})
|
|
return id, err
|
|
}
|
|
|
|
func (s *Store) DeleteSNAT(ctx context.Context, id int64) error {
|
|
return s.deleteOne(ctx, `DELETE FROM snat WHERE id = $1`, id)
|
|
}
|
|
|
|
// ---- Netmap ----------------------------------------------------------------
|
|
|
|
func (s *Store) ListNetmap(ctx context.Context) ([]model.NetmapRule, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, type, from_net, to_net, anchor, comment FROM netmap ORDER BY id`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []model.NetmapRule
|
|
for rows.Next() {
|
|
var r model.NetmapRule
|
|
if err := rows.Scan(&r.ID, &r.Type, &r.FromNet, &r.ToNet, &r.Anchor, &r.Comment); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) GetNetmap(ctx context.Context, id int64) (model.NetmapRule, error) {
|
|
var r model.NetmapRule
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT id, type, from_net, to_net, anchor, comment FROM netmap WHERE id = $1`, id,
|
|
).Scan(&r.ID, &r.Type, &r.FromNet, &r.ToNet, &r.Anchor, &r.Comment)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return r, ErrNotFound
|
|
}
|
|
return r, err
|
|
}
|
|
|
|
func (s *Store) CreateNetmap(ctx context.Context, r model.NetmapRule) (int64, error) {
|
|
var id int64
|
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO netmap (type, from_net, to_net, anchor, comment)
|
|
VALUES ($1, $2, $3, $4, $5) RETURNING id`,
|
|
r.Type, r.FromNet, r.ToNet, r.Anchor, r.Comment,
|
|
).Scan(&id); err != nil {
|
|
return err
|
|
}
|
|
return bump(ctx, tx)
|
|
})
|
|
return id, err
|
|
}
|
|
|
|
func (s *Store) DeleteNetmap(ctx context.Context, id int64) error {
|
|
return s.deleteOne(ctx, `DELETE FROM netmap WHERE id = $1`, id)
|
|
}
|
|
|
|
// ---- 1:1 static NAT --------------------------------------------------------
|
|
|
|
func (s *Store) ListNAT(ctx context.Context) ([]model.NATRule, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, device, external, internal, interface, comment FROM nat ORDER BY id`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []model.NATRule
|
|
for rows.Next() {
|
|
var r model.NATRule
|
|
if err := rows.Scan(&r.ID, &r.Device, &r.External, &r.Internal, &r.Interface, &r.Comment); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) GetNAT(ctx context.Context, id int64) (model.NATRule, error) {
|
|
var r model.NATRule
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT id, device, external, internal, interface, comment FROM nat WHERE id = $1`, id,
|
|
).Scan(&r.ID, &r.Device, &r.External, &r.Internal, &r.Interface, &r.Comment)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return r, ErrNotFound
|
|
}
|
|
return r, err
|
|
}
|
|
|
|
func (s *Store) CreateNAT(ctx context.Context, r model.NATRule) (int64, error) {
|
|
var id int64
|
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
|
if err := tx.QueryRow(ctx, `
|
|
INSERT INTO nat (device, external, internal, interface, comment)
|
|
VALUES ($1, $2, $3, $4, $5) RETURNING id`,
|
|
r.Device, r.External, r.Internal, r.Interface, r.Comment,
|
|
).Scan(&id); err != nil {
|
|
return err
|
|
}
|
|
return bump(ctx, tx)
|
|
})
|
|
return id, err
|
|
}
|
|
|
|
func (s *Store) DeleteNAT(ctx context.Context, id int64) error {
|
|
return s.deleteOne(ctx, `DELETE FROM nat WHERE id = $1`, id)
|
|
}
|