more work on fixing data access
This commit is contained in:
parent
1d0a5811ce
commit
003f9fca01
10
cmd/root.go
10
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")
|
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 {
|
for _, v := range reslist {
|
||||||
v := v
|
v := v
|
||||||
if err = v.Insert(conn); err != nil {
|
if err := pg.InsertResV4(v); err != nil {
|
||||||
slog.Error(err.Error())
|
slog.Error(err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
package reservation
|
package reservation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"log/slog"
|
|
||||||
|
|
||||||
"git.staur.ca/stobbsm/kea-manage/lib/types"
|
"git.staur.ca/stobbsm/kea-manage/lib/types"
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const insertHostReservation = `INSERT INTO hosts (dhcp_identifier,
|
const insertHostReservation = `INSERT INTO hosts (dhcp_identifier,
|
||||||
@ -53,33 +48,3 @@ func ReserveV4MacAddr(mac string, addr string, hostname string, subnet int) *Res
|
|||||||
|
|
||||||
return r
|
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
|
|
||||||
}
|
|
||||||
|
@ -1 +1,9 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StoreV4 interface {
|
||||||
|
InsertResV4(*reservation.ReservationV4) error
|
||||||
|
}
|
||||||
|
@ -4,13 +4,17 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Postgres struct {
|
type Postgres struct {
|
||||||
connString string
|
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 {
|
func Open(host string, port int, user string, password string, name string) *Postgres {
|
||||||
var p = &Postgres{
|
var p = &Postgres{
|
||||||
connString: fmt.Sprintf("postgres://%s:%s@%s:%d/%s", user, password, host, port, name),
|
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
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Postgres) Connection(ctx context.Context) (*pgx.Conn, error) {
|
func (p *Postgres) connect(ctx context.Context) error {
|
||||||
return pgx.Connect(ctx, p.connString)
|
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
|
||||||
}
|
}
|
||||||
|
13
lib/database/postgres/queries.go
Normal file
13
lib/database/postgres/queries.go
Normal 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'));`
|
Loading…
Reference in New Issue
Block a user