more work on fixing data access

This commit is contained in:
Matthew Stobbs 2024-09-21 20:02:02 -06:00
parent 1d0a5811ce
commit 003f9fca01
5 changed files with 56 additions and 46 deletions

View File

@ -90,17 +90,9 @@ func runMain(cmd *cobra.Command, args []string) {
pg := postgres.Open("10.1.0.6", 5432, "kea", "xGq42ZMeMUIWRK", "kea")
conn, err := pg.Connection(context.Background())
if err != nil {
slog.Error(err.Error())
} else {
slog.Info("connected to database")
}
defer conn.Close(context.Background())
for _, v := range reslist {
v := v
if err = v.Insert(conn); err != nil {
if err := pg.InsertResV4(v); err != nil {
slog.Error(err.Error())
continue
}

View File

@ -1,12 +1,7 @@
package reservation
import (
"context"
"errors"
"log/slog"
"git.staur.ca/stobbsm/kea-manage/lib/types"
"github.com/jackc/pgx/v5"
)
const insertHostReservation = `INSERT INTO hosts (dhcp_identifier,
@ -53,33 +48,3 @@ func ReserveV4MacAddr(mac string, addr string, hostname string, subnet int) *Res
return r
}
func (r *ReservationV4) check(conn *pgx.Conn) error {
rows, err := conn.Query(context.Background(), getHostIDWithMacAddr, r.SubnetID, r.Type, r.MacAddr)
if err != nil {
slog.Error("error during check query")
return err
}
slog.Info("check result size")
if rv, _ := rows.Values(); len(rv) > 0 {
return errors.New("existing record found")
}
return nil
}
func (r *ReservationV4) Insert(conn *pgx.Conn) error {
tx, err := conn.Begin(context.Background())
if err != nil {
return err
}
defer tx.Rollback(context.Background())
if _, err = tx.Exec(context.Background(), insertIfNotExists, r.MacAddr, r.Type, r.SubnetID, r.Ipv4, r.Hostname); err != nil {
return err
}
if err = tx.Commit(context.Background()); err != nil {
return err
}
return nil
}

View File

@ -1 +1,9 @@
package database
import (
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
)
type StoreV4 interface {
InsertResV4(*reservation.ReservationV4) error
}

View File

@ -4,13 +4,17 @@ import (
"context"
"fmt"
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
"github.com/jackc/pgx/v5"
)
type Postgres struct {
connString string
conn *pgx.Conn
}
// Open receives the host, port, username, password and database name of a postgresql
// database, and manages connections to it.
func Open(host string, port int, user string, password string, name string) *Postgres {
var p = &Postgres{
connString: fmt.Sprintf("postgres://%s:%s@%s:%d/%s", user, password, host, port, name),
@ -18,6 +22,34 @@ func Open(host string, port int, user string, password string, name string) *Pos
return p
}
func (p *Postgres) Connection(ctx context.Context) (*pgx.Conn, error) {
return pgx.Connect(ctx, p.connString)
func (p *Postgres) connect(ctx context.Context) error {
var err error
p.conn, err = pgx.Connect(ctx, p.connString)
return err
}
func (p *Postgres) close() {
p.conn.Close(context.Background())
}
func (p *Postgres) InsertResV4(r *reservation.ReservationV4) error {
if err := p.connect(context.Background()); err != nil {
return err
}
defer p.close()
tx, err := p.conn.Begin(context.Background())
if err != nil {
return err
}
defer tx.Rollback(context.Background())
if _, err = tx.Exec(context.Background(), insertIfNotExists, r.MacAddr, r.Type, r.SubnetID, r.Ipv4, r.Hostname); err != nil {
return err
}
if err = tx.Commit(context.Background()); err != nil {
return err
}
return nil
}

View File

@ -0,0 +1,13 @@
package postgres
const insertIfNotExists = `INSERT INTO hosts (dhcp_identifier,
dhcp_identifier_type,
dhcp4_subnet_id,
ipv4_address,
hostname)
SELECT DECODE(REPLACE($1, ':', ''), 'hex'), $2, $3, (SELECT ($4::inet - '0.0.0.0'::inet)), $5
WHERE NOT EXISTS (
SELECT host_id FROM hosts
WHERE dhcp4_subnet_id = $3
AND dhcp_identifier_type = $2
AND dhcp_identifier = DECODE(REPLACE($1, ':', ''), 'hex'));`