2024-09-20 21:21:41 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-09-22 02:02:02 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
2024-09-24 20:43:54 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/lib/database"
|
2024-12-02 19:20:39 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/lib/types"
|
2024-09-20 21:21:41 +00:00
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Postgres struct {
|
|
|
|
connString string
|
2024-09-22 02:02:02 +00:00
|
|
|
conn *pgx.Conn
|
2024-09-20 21:21:41 +00:00
|
|
|
}
|
|
|
|
|
2024-09-22 02:02:02 +00:00
|
|
|
// Open receives the host, port, username, password and database name of a postgresql
|
|
|
|
// database, and manages connections to it.
|
2024-09-24 20:43:54 +00:00
|
|
|
func Open(host, port, user, password, name string) *Postgres {
|
2024-09-20 21:21:41 +00:00
|
|
|
var p = &Postgres{
|
2024-09-24 20:43:54 +00:00
|
|
|
connString: fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, password, host, port, name),
|
2024-09-20 21:21:41 +00:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2024-09-22 02:02:02 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-09-24 20:43:54 +00:00
|
|
|
// InsertResV4 adds IPv4 reservations to the kea database
|
2024-09-22 02:02:02 +00:00
|
|
|
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())
|
|
|
|
|
2024-12-02 19:20:39 +00:00
|
|
|
ct, err := tx.Exec(context.Background(), insertIfNotExists, r.Identifier, types.HwAddress.Int(), r.SubnetID, r.Ipv4, r.Hostname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(context.Background()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ct.RowsAffected() == 0 {
|
|
|
|
return database.Exists
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Postgres) UpdateResV4(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())
|
|
|
|
|
|
|
|
ct, err := tx.Exec(context.Background(), update, r.Identifier, types.HwAddress.Int(), r.SubnetID, r.Ipv4, r.Hostname)
|
2024-09-24 20:43:54 +00:00
|
|
|
if err != nil {
|
2024-09-22 02:02:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(context.Background()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-24 20:43:54 +00:00
|
|
|
|
|
|
|
if ct.RowsAffected() == 0 {
|
|
|
|
return database.Exists
|
|
|
|
}
|
|
|
|
|
2024-09-22 02:02:02 +00:00
|
|
|
return nil
|
2024-09-20 21:21:41 +00:00
|
|
|
}
|