From 3c8ac1d21135326fa3569cd5031a2987ec5de3a6 Mon Sep 17 00:00:00 2001 From: lauralani Date: Sat, 25 Mar 2023 16:51:07 +0100 Subject: [PATCH] bump to 0.2.0, add IPv6 Support --- cmd/constants.go | 2 +- cmd/ip-add.go | 14 +++++++++----- cmd/ip-delete.go | 6 ++++++ cmd/ip-edit.go | 2 +- cmd/ip-show.go | 9 ++++++--- cmd/powerdns.go | 6 +++--- cmd/root.go | 2 +- cmd/storage.go | 2 +- cmd/subnet-add.go | 9 --------- cmd/subnet-show.go | 43 ++++++++++++++++++++++++++++++++----------- 10 files changed, 60 insertions(+), 35 deletions(-) diff --git a/cmd/constants.go b/cmd/constants.go index 2b8840a..b2e6e89 100644 --- a/cmd/constants.go +++ b/cmd/constants.go @@ -5,5 +5,5 @@ Copyright © 2023 Laura Kalb package cmd const ( - ipam_version = "DEVEL" + ipamVersion = "0.2.0" ) diff --git a/cmd/ip-add.go b/cmd/ip-add.go index 38f8c54..7c80821 100644 --- a/cmd/ip-add.go +++ b/cmd/ip-add.go @@ -41,10 +41,10 @@ var ipaddCmd = &cobra.Command{ // Exit if parsed value is an IPv6 Address // TODO: Implement IPv6 support - if !ip.Is4() { - fmt.Printf("[ERROR] IPv6 is not yet supported!\n") - os.Exit(1) - } + //if !ip.Is4() { + // fmt.Printf("[ERROR] IPv6 is not yet supported!\n") + // os.Exit(1) + //} subnet, subnetexists := FindBestSubnet(ip) @@ -60,7 +60,11 @@ var ipaddCmd = &cobra.Command{ } currentuser, _ := user.Current() - subnet.Addresses = append(subnet.Addresses, Address{ip, hostname, time.Now(), currentuser.Username}) + timestamp := time.Now() + + subnet.Addresses = append(subnet.Addresses, Address{ip, hostname, timestamp, currentuser.Username}) + subnet.ChangedBy = currentuser.Username + subnet.ChangedAt = timestamp writeerr := subnet.WriteSubnet() if writeerr != nil { diff --git a/cmd/ip-delete.go b/cmd/ip-delete.go index 6eaea9d..f4818d7 100644 --- a/cmd/ip-delete.go +++ b/cmd/ip-delete.go @@ -7,6 +7,8 @@ import ( "fmt" "net/netip" "os" + "os/user" + "time" "github.com/spf13/cobra" ) @@ -41,6 +43,10 @@ var ipdeleteCmd = &cobra.Command{ os.Exit(1) } + currentuser, _ := user.Current() + subnet.ChangedAt = time.Now() + subnet.ChangedBy = currentuser.Username + writeerr := subnet.WriteSubnet() if writeerr != nil { fmt.Println("[ERROR]", writeerr) diff --git a/cmd/ip-edit.go b/cmd/ip-edit.go index 00c5204..8d7e52d 100644 --- a/cmd/ip-edit.go +++ b/cmd/ip-edit.go @@ -14,7 +14,7 @@ var ipeditCmd = &cobra.Command{ Short: "Edit an IP address", Long: `Edit an IP address`, Aliases: []string{"e"}, - Args: cobra.ExactArgs(1), + //Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { fmt.Println("not implemented yet; please delete and readd") }, diff --git a/cmd/ip-show.go b/cmd/ip-show.go index d6c7a1b..2026a65 100644 --- a/cmd/ip-show.go +++ b/cmd/ip-show.go @@ -7,6 +7,7 @@ import ( "fmt" "net/netip" "os" + "time" "github.com/spf13/cobra" ) @@ -39,9 +40,11 @@ var ipshowCmd = &cobra.Command{ os.Exit(1) } - fmt.Printf("IP: %v\n", ip.String()) - fmt.Printf("FQDN: %v\n", addr.FQDN) - fmt.Printf("Subnet: %v (%v)\n", subnet.Subnet.String(), subnet.Name) + fmt.Printf("IP: %v\n", ip.String()) + fmt.Printf("FQDN: %v\n", addr.FQDN) + fmt.Printf("Subnet: %v (%v, vlan %v)\n", subnet.Subnet.String(), subnet.Name, subnet.Vlan) + fmt.Printf("Modified at: %v\n", subnet.ChangedAt.Format(time.RFC1123)) + fmt.Printf("Modified by: %v\n", subnet.ChangedBy) }, } diff --git a/cmd/powerdns.go b/cmd/powerdns.go index c6b259e..789d44a 100644 --- a/cmd/powerdns.go +++ b/cmd/powerdns.go @@ -33,7 +33,7 @@ type Patch struct { Rrsets []DNSRecordSet `json:"rrsets"` } -// Checks if a given Record already exists in the DNSRecordSet list. +// GetRecord checks if a given Record already exists in the DNSRecordSet list. // // Returns the DNSRecordSet and true if record exists, empty // DNSRecordSet and false if not. @@ -56,7 +56,7 @@ func (z DNSZone) GetRecord(fqdn string, rtype string, rcontent string) (DNSRecor return DNSRecordSet{}, false } -// Sends a PATCH API request for DNSZone z. Returns error or nil +// SendPATCH sends a PATCH API request for DNSZone z. Returns error or nil // // Example args for "test.example.com IN A 127.0.0.1" // @@ -259,7 +259,7 @@ func GetBestDNSZone(fqdn string) (DNSZone, error) { return zone, nil } -// AddDNSfqdn tries to create forward and reverse lookup records +// AddDNSFqdn tries to create forward and reverse lookup records // for given fqdn with netip.Addr addr, IF PowerDNS integration // is enabled. // diff --git a/cmd/root.go b/cmd/root.go index 2c1b1c7..9b1592c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,7 +18,7 @@ var rootCmd = &cobra.Command{ Long: `A cli based ipam. You can manage subnets, single ip addresses within those, and the corresponding A records. PowerDNS and IPV6-Support will follow`, - Version: ipam_version, + Version: ipamVersion, } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cmd/storage.go b/cmd/storage.go index 6873c83..816c594 100644 --- a/cmd/storage.go +++ b/cmd/storage.go @@ -24,7 +24,7 @@ import ( func FindBestSubnet(ip netip.Addr) (Subnet, bool) { subnets := ListSubnets() var smallestprefix int = 0 - bestmatch, _ := netip.ParsePrefix("0.0.0.0/32") + bestmatch, _ := netip.ParsePrefix("::/128") var isipv4 bool = ip.Is4() var subnet Subnet diff --git a/cmd/subnet-add.go b/cmd/subnet-add.go index 51e1ba9..6bc8fa5 100644 --- a/cmd/subnet-add.go +++ b/cmd/subnet-add.go @@ -84,13 +84,4 @@ var subnetaddCmd = &cobra.Command{ func init() { subnetCmd.AddCommand(subnetaddCmd) - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // addCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/subnet-show.go b/cmd/subnet-show.go index 8796cda..8a9a69c 100644 --- a/cmd/subnet-show.go +++ b/cmd/subnet-show.go @@ -40,20 +40,41 @@ aswell as a list of containing IP addresses`, os.Exit(1) } fmt.Printf("\n") - fmt.Printf("Name: %v\n", subnet.Name) - fmt.Printf("Vlan: %v\n", subnet.Vlan) - fmt.Printf("Prefix: %v\n", subnet.Subnet) - fmt.Printf("Modified at: %v\n", subnet.ChangedAt.Format(time.RFC1123)) - fmt.Printf("Modified by: %v\n\n", subnet.ChangedBy) + fmt.Printf("Name: %v\n", subnet.Name) + fmt.Printf("Vlan: %v\n", subnet.Vlan) + fmt.Printf("Prefix: %v\n", subnet.Subnet) + fmt.Printf("Modified at: %v\n", subnet.ChangedAt.Format(time.RFC1123)) + fmt.Printf("Modified by: %v\n\n", subnet.ChangedBy) - fmt.Printf("%v:\n", subnet.Subnet) - for _, element := range subnet.Addresses { - if element.FQDN == "" { - fmt.Printf("\t%v\n", element.IP.String()) - } else { - fmt.Printf("\t%v: %v\n", element.IP.String(), element.FQDN) + //fmt.Printf("%v:\n", subnet.Subnet) + + addrlist := SortAddresses(subnet.Addresses) + + if subnet.Subnet.Addr().Is4() { + + fmt.Printf("%-15s Hostname\n", "IP Address") + for _, element := range addrlist { + if element.FQDN == "" { + fmt.Printf("%v\n", element.IP.String()) + } else { + fmt.Printf("%-15s %v\n", element.IP.String(), element.FQDN) + } + } + } else { + + fmt.Printf("%-39s Hostname\n", "IP Address") + + for _, element := range addrlist { + if element.FQDN == "" { + fmt.Printf("%v\n", element.IP.String()) + } else { + fmt.Printf("%-39s %v\n", element.IP.String(), element.FQDN) + } } } + + + }, }