mirror of
https://codeberg.org/lauralani/ipam.git
synced 2024-11-23 20:20:39 +01:00
bump to 0.2.0, add IPv6 Support
This commit is contained in:
parent
1afe714822
commit
3c8ac1d211
10 changed files with 60 additions and 35 deletions
|
@ -5,5 +5,5 @@ Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ipam_version = "DEVEL"
|
ipamVersion = "0.2.0"
|
||||||
)
|
)
|
||||||
|
|
|
@ -41,10 +41,10 @@ var ipaddCmd = &cobra.Command{
|
||||||
|
|
||||||
// Exit if parsed value is an IPv6 Address
|
// Exit if parsed value is an IPv6 Address
|
||||||
// TODO: Implement IPv6 support
|
// TODO: Implement IPv6 support
|
||||||
if !ip.Is4() {
|
//if !ip.Is4() {
|
||||||
fmt.Printf("[ERROR] IPv6 is not yet supported!\n")
|
// fmt.Printf("[ERROR] IPv6 is not yet supported!\n")
|
||||||
os.Exit(1)
|
// os.Exit(1)
|
||||||
}
|
//}
|
||||||
|
|
||||||
subnet, subnetexists := FindBestSubnet(ip)
|
subnet, subnetexists := FindBestSubnet(ip)
|
||||||
|
|
||||||
|
@ -60,7 +60,11 @@ var ipaddCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
currentuser, _ := user.Current()
|
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()
|
writeerr := subnet.WriteSubnet()
|
||||||
if writeerr != nil {
|
if writeerr != nil {
|
||||||
|
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -41,6 +43,10 @@ var ipdeleteCmd = &cobra.Command{
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentuser, _ := user.Current()
|
||||||
|
subnet.ChangedAt = time.Now()
|
||||||
|
subnet.ChangedBy = currentuser.Username
|
||||||
|
|
||||||
writeerr := subnet.WriteSubnet()
|
writeerr := subnet.WriteSubnet()
|
||||||
if writeerr != nil {
|
if writeerr != nil {
|
||||||
fmt.Println("[ERROR]", writeerr)
|
fmt.Println("[ERROR]", writeerr)
|
||||||
|
|
|
@ -14,7 +14,7 @@ var ipeditCmd = &cobra.Command{
|
||||||
Short: "Edit an IP address",
|
Short: "Edit an IP address",
|
||||||
Long: `Edit an IP address`,
|
Long: `Edit an IP address`,
|
||||||
Aliases: []string{"e"},
|
Aliases: []string{"e"},
|
||||||
Args: cobra.ExactArgs(1),
|
//Args: cobra.ExactArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("not implemented yet; please delete and readd")
|
fmt.Println("not implemented yet; please delete and readd")
|
||||||
},
|
},
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -39,9 +40,11 @@ var ipshowCmd = &cobra.Command{
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("IP: %v\n", ip.String())
|
fmt.Printf("IP: %v\n", ip.String())
|
||||||
fmt.Printf("FQDN: %v\n", addr.FQDN)
|
fmt.Printf("FQDN: %v\n", addr.FQDN)
|
||||||
fmt.Printf("Subnet: %v (%v)\n", subnet.Subnet.String(), subnet.Name)
|
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)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ type Patch struct {
|
||||||
Rrsets []DNSRecordSet `json:"rrsets"`
|
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
|
// Returns the DNSRecordSet and true if record exists, empty
|
||||||
// DNSRecordSet and false if not.
|
// DNSRecordSet and false if not.
|
||||||
|
@ -56,7 +56,7 @@ func (z DNSZone) GetRecord(fqdn string, rtype string, rcontent string) (DNSRecor
|
||||||
return DNSRecordSet{}, false
|
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"
|
// 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
|
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
|
// for given fqdn with netip.Addr addr, IF PowerDNS integration
|
||||||
// is enabled.
|
// is enabled.
|
||||||
//
|
//
|
||||||
|
|
|
@ -18,7 +18,7 @@ var rootCmd = &cobra.Command{
|
||||||
Long: `A cli based ipam.
|
Long: `A cli based ipam.
|
||||||
You can manage subnets, single ip addresses within those, and the corresponding A records.
|
You can manage subnets, single ip addresses within those, and the corresponding A records.
|
||||||
PowerDNS and IPV6-Support will follow`,
|
PowerDNS and IPV6-Support will follow`,
|
||||||
Version: ipam_version,
|
Version: ipamVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
|
|
|
@ -24,7 +24,7 @@ import (
|
||||||
func FindBestSubnet(ip netip.Addr) (Subnet, bool) {
|
func FindBestSubnet(ip netip.Addr) (Subnet, bool) {
|
||||||
subnets := ListSubnets()
|
subnets := ListSubnets()
|
||||||
var smallestprefix int = 0
|
var smallestprefix int = 0
|
||||||
bestmatch, _ := netip.ParsePrefix("0.0.0.0/32")
|
bestmatch, _ := netip.ParsePrefix("::/128")
|
||||||
var isipv4 bool = ip.Is4()
|
var isipv4 bool = ip.Is4()
|
||||||
var subnet Subnet
|
var subnet Subnet
|
||||||
|
|
||||||
|
|
|
@ -84,13 +84,4 @@ var subnetaddCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
subnetCmd.AddCommand(subnetaddCmd)
|
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")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,20 +40,41 @@ aswell as a list of containing IP addresses`,
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
fmt.Printf("Name: %v\n", subnet.Name)
|
fmt.Printf("Name: %v\n", subnet.Name)
|
||||||
fmt.Printf("Vlan: %v\n", subnet.Vlan)
|
fmt.Printf("Vlan: %v\n", subnet.Vlan)
|
||||||
fmt.Printf("Prefix: %v\n", subnet.Subnet)
|
fmt.Printf("Prefix: %v\n", subnet.Subnet)
|
||||||
fmt.Printf("Modified at: %v\n", subnet.ChangedAt.Format(time.RFC1123))
|
fmt.Printf("Modified at: %v\n", subnet.ChangedAt.Format(time.RFC1123))
|
||||||
fmt.Printf("Modified by: %v\n\n", subnet.ChangedBy)
|
fmt.Printf("Modified by: %v\n\n", subnet.ChangedBy)
|
||||||
|
|
||||||
fmt.Printf("%v:\n", subnet.Subnet)
|
//fmt.Printf("%v:\n", subnet.Subnet)
|
||||||
for _, element := range subnet.Addresses {
|
|
||||||
if element.FQDN == "" {
|
addrlist := SortAddresses(subnet.Addresses)
|
||||||
fmt.Printf("\t%v\n", element.IP.String())
|
|
||||||
} else {
|
if subnet.Subnet.Addr().Is4() {
|
||||||
fmt.Printf("\t%v: %v\n", element.IP.String(), element.FQDN)
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue