79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// 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,
|
|
),
|
|
reservation.ReserveV4MacAddr(
|
|
"BC:24:11:E5:C5:AF",
|
|
"10.0.0.4",
|
|
"ns01",
|
|
100,
|
|
),
|
|
reservation.ReserveV4MacAddr(
|
|
"BC:24:11:B1:1C:AD",
|
|
"10.1.0.6",
|
|
"db01",
|
|
101,
|
|
),
|
|
}
|
|
|
|
viper.SetConfigName("kea-manage")
|
|
viper.AddConfigPath("/etc")
|
|
viper.AddConfigPath(os.Getenv("HOME") + "/.config/kea-manage")
|
|
|
|
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)
|
|
}
|
|
}
|