initial commit
This commit is contained in:
commit
0038b3ae5e
12
go.mod
Normal file
12
go.mod
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module git.staur.ca/stobbsm/kea-manage
|
||||||
|
|
||||||
|
go 1.22.4
|
||||||
|
|
||||||
|
require github.com/jackc/pgx/v5 v5.7.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
|
golang.org/x/crypto v0.27.0 // indirect
|
||||||
|
golang.org/x/text v0.18.0 // indirect
|
||||||
|
)
|
28
go.sum
Normal file
28
go.sum
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||||
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||||
|
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
|
||||||
|
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||||
|
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
|
||||||
|
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
|
||||||
|
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||||
|
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||||
|
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
89
ipv4/reservation/reservation.go
Normal file
89
ipv4/reservation/reservation.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
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,
|
||||||
|
dhcp_identifier_type,
|
||||||
|
dhcp4_subnet_id,
|
||||||
|
ipv4_address,
|
||||||
|
hostname)
|
||||||
|
VALUES (DECODE(REPLACE($1, ':', ''), 'hex'), $2, $3, (SELECT ($4::inet - '0.0.0.0'::inet)), $5);`
|
||||||
|
|
||||||
|
const getHostIDWithMacAddr = `SELECT host_id
|
||||||
|
FROM hosts
|
||||||
|
WHERE dhcp4_subnet_id = $1
|
||||||
|
AND dhcp_identifier_type = $2
|
||||||
|
AND dhcp_identifier = (DECODE(REPLACE($3, ':', ''), 'hex'));`
|
||||||
|
|
||||||
|
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'));`
|
||||||
|
|
||||||
|
type ReservationV4 struct {
|
||||||
|
MacAddr string
|
||||||
|
Type int
|
||||||
|
SubnetID int
|
||||||
|
Ipv4 string
|
||||||
|
Hostname string
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReserveV4MacAddr(mac string, addr string, hostname string, subnet int) *ReservationV4 {
|
||||||
|
r := &ReservationV4{
|
||||||
|
MacAddr: mac,
|
||||||
|
Type: int(types.MacAddress),
|
||||||
|
SubnetID: subnet,
|
||||||
|
Ipv4: addr,
|
||||||
|
Hostname: hostname,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
// if err := r.check(conn); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
8
lib/types/types.go
Normal file
8
lib/types/types.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
type IdentifierType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
HWAddress = 0
|
||||||
|
MacAddress = 0
|
||||||
|
)
|
61
main.go
Normal file
61
main.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Manage kea dhcp host reservations in postgresql directly
|
||||||
|
// postgresql schema used from [https://gitlab.isc.org/isc-projects/kea/-/wikis/docs/editing-host-reservations]
|
||||||
|
|
||||||
|
// This will only work with hw-address identifiers. If I need support for more, I'll add it
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
reslist := []*reservation.ReservationV4{
|
||||||
|
reservation.ReserveV4MacAddr(
|
||||||
|
"90:e2:ba:19:25:70",
|
||||||
|
"10.0.255.2",
|
||||||
|
"mars",
|
||||||
|
100,
|
||||||
|
),
|
||||||
|
reservation.ReserveV4MacAddr(
|
||||||
|
"90:e2:ba:19:25:70",
|
||||||
|
"10.1.255.2",
|
||||||
|
"mars",
|
||||||
|
101,
|
||||||
|
),
|
||||||
|
reservation.ReserveV4MacAddr(
|
||||||
|
"90:e2:ba:19:25:70",
|
||||||
|
"10.2.255.2",
|
||||||
|
"mars",
|
||||||
|
102,
|
||||||
|
),
|
||||||
|
reservation.ReserveV4MacAddr(
|
||||||
|
"90:e2:ba:19:25:70",
|
||||||
|
"10.3.255.2",
|
||||||
|
"mars",
|
||||||
|
103,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
|
||||||
|
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 {
|
||||||
|
slog.Error(err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
slog.Info("inserted reservation", *v)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user