migrated to cobra structure

adding abstractions for database to avoid passing around the connection
This commit is contained in:
Matthew Stobbs 2024-09-20 15:21:41 -06:00
parent 312399093f
commit 1d0a5811ce
7 changed files with 142 additions and 67 deletions

0
LICENSE Normal file
View File

109
cmd/root.go Normal file
View File

@ -0,0 +1,109 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"context"
"log/slog"
"os"
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
"git.staur.ca/stobbsm/kea-manage/lib/database/postgres"
"github.com/jackc/pgx/v5"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "kea-manage",
Short: "Manage the isc-kea dhcp server",
Long: `Manage the isc-kea dhcp server backend in database.`,
Run: runMain,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kea-manage.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// 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 runMain(cmd *cobra.Command, args []string) {
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,
),
}
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 {
slog.Error(err.Error())
continue
}
slog.Info("inserted reservation", *v)
}
}

View File

@ -68,10 +68,6 @@ func (r *ReservationV4) check(conn *pgx.Conn) error {
} }
func (r *ReservationV4) Insert(conn *pgx.Conn) error { func (r *ReservationV4) Insert(conn *pgx.Conn) error {
// if err := r.check(conn); err != nil {
// return err
// }
tx, err := conn.Begin(context.Background()) tx, err := conn.Begin(context.Background())
if err != nil { if err != nil {
return err return err

View File

@ -1,3 +1,10 @@
database:
type: pgx
host: 10.1.0.6
port: 5432
user: kea
password: xGq42ZMeMUIWRK
database: kea
reservationV4: reservationV4:
- type: macaddr - type: macaddr
identifer: "90:e2:ba:19:25:70" identifer: "90:e2:ba:19:25:70"

1
lib/database/database.go Normal file
View File

@ -0,0 +1 @@
package database

View File

@ -0,0 +1,23 @@
package postgres
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
)
type Postgres struct {
connString string
}
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),
}
return p
}
func (p *Postgres) Connection(ctx context.Context) (*pgx.Conn, error) {
return pgx.Connect(ctx, p.connString)
}

65
main.go
View File

@ -1,78 +1,17 @@
package main package main
import ( import (
"context"
"log/slog"
"os" "os"
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation" "git.staur.ca/stobbsm/kea-manage/cmd"
"github.com/jackc/pgx/v5"
"github.com/spf13/viper" "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() { 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.SetConfigName("kea-manage")
viper.AddConfigPath("/etc") viper.AddConfigPath("/etc")
viper.AddConfigPath(os.Getenv("HOME") + "/.config/kea-manage") viper.AddConfigPath(os.Getenv("HOME") + "/.config/kea-manage")
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL")) cmd.Execute()
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)
}
} }