From 003f9fca01a680ccb1cd8c1645ffbd4749775b28 Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Sat, 21 Sep 2024 20:02:02 -0600 Subject: [PATCH] more work on fixing data access --- cmd/root.go | 10 +-------- ipv4/reservation/reservation.go | 35 ------------------------------ lib/database/database.go | 8 +++++++ lib/database/postgres/postgres.go | 36 +++++++++++++++++++++++++++++-- lib/database/postgres/queries.go | 13 +++++++++++ 5 files changed, 56 insertions(+), 46 deletions(-) create mode 100644 lib/database/postgres/queries.go diff --git a/cmd/root.go b/cmd/root.go index 7ad0e61..b2c3ed3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 } diff --git a/ipv4/reservation/reservation.go b/ipv4/reservation/reservation.go index 2ca7972..4737724 100644 --- a/ipv4/reservation/reservation.go +++ b/ipv4/reservation/reservation.go @@ -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 -} diff --git a/lib/database/database.go b/lib/database/database.go index 636bab8..2f18446 100644 --- a/lib/database/database.go +++ b/lib/database/database.go @@ -1 +1,9 @@ package database + +import ( + "git.staur.ca/stobbsm/kea-manage/ipv4/reservation" +) + +type StoreV4 interface { + InsertResV4(*reservation.ReservationV4) error +} diff --git a/lib/database/postgres/postgres.go b/lib/database/postgres/postgres.go index 95f2c7f..d173583 100644 --- a/lib/database/postgres/postgres.go +++ b/lib/database/postgres/postgres.go @@ -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 } diff --git a/lib/database/postgres/queries.go b/lib/database/postgres/queries.go new file mode 100644 index 0000000..51ad283 --- /dev/null +++ b/lib/database/postgres/queries.go @@ -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'));`