From 1d0a5811ce85f5eaedb7e3abd925d17fa8dcc3b4 Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Fri, 20 Sep 2024 15:21:41 -0600 Subject: [PATCH] migrated to cobra structure adding abstractions for database to avoid passing around the connection --- LICENSE | 0 cmd/root.go | 109 ++++++++++++++++++++++++++++++ ipv4/reservation/reservation.go | 4 -- kea-manage.yaml | 7 ++ lib/database/database.go | 1 + lib/database/postgres/postgres.go | 23 +++++++ main.go | 65 +----------------- 7 files changed, 142 insertions(+), 67 deletions(-) create mode 100644 LICENSE create mode 100644 cmd/root.go create mode 100644 lib/database/database.go create mode 100644 lib/database/postgres/postgres.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..7ad0e61 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,109 @@ +/* +Copyright © 2024 NAME HERE +*/ +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) + } +} diff --git a/ipv4/reservation/reservation.go b/ipv4/reservation/reservation.go index 8a06b42..2ca7972 100644 --- a/ipv4/reservation/reservation.go +++ b/ipv4/reservation/reservation.go @@ -68,10 +68,6 @@ func (r *ReservationV4) check(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()) if err != nil { return err diff --git a/kea-manage.yaml b/kea-manage.yaml index ecb8f9f..d92f6a3 100644 --- a/kea-manage.yaml +++ b/kea-manage.yaml @@ -1,3 +1,10 @@ +database: + type: pgx + host: 10.1.0.6 + port: 5432 + user: kea + password: xGq42ZMeMUIWRK + database: kea reservationV4: - type: macaddr identifer: "90:e2:ba:19:25:70" diff --git a/lib/database/database.go b/lib/database/database.go new file mode 100644 index 0000000..636bab8 --- /dev/null +++ b/lib/database/database.go @@ -0,0 +1 @@ +package database diff --git a/lib/database/postgres/postgres.go b/lib/database/postgres/postgres.go new file mode 100644 index 0000000..95f2c7f --- /dev/null +++ b/lib/database/postgres/postgres.go @@ -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) +} diff --git a/main.go b/main.go index d63bc55..86a88db 100644 --- a/main.go +++ b/main.go @@ -1,78 +1,17 @@ package main import ( - "context" - "log/slog" "os" - "git.staur.ca/stobbsm/kea-manage/ipv4/reservation" - "github.com/jackc/pgx/v5" + "git.staur.ca/stobbsm/kea-manage/cmd" "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) - } + cmd.Execute() }