ipam/cmd/ip-add.go

114 lines
2.8 KiB
Go
Raw Permalink Normal View History

2023-03-10 17:52:20 +01:00
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
*/
package cmd
import (
2023-04-01 16:18:18 +02:00
"fmt"
"net/netip"
"os"
"os/user"
"time"
2023-03-10 17:52:20 +01:00
2023-04-01 16:18:18 +02:00
"github.com/spf13/cobra"
2023-03-10 17:52:20 +01:00
)
var ipaddCmd = &cobra.Command{
2023-04-04 16:43:45 +02:00
Use: "add ipaddress|subnet [hostname]",
2023-04-01 16:18:18 +02:00
Short: "Add new IP address",
2023-04-04 16:43:45 +02:00
Long: `Adds a new IP address OR the next free IP address from a subnet`,
2023-04-01 16:18:18 +02:00
Aliases: []string{"a"},
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
2023-04-04 16:43:45 +02:00
var iparg, hostname string
2023-04-01 16:18:18 +02:00
if len(args) == 1 {
2023-04-04 16:43:45 +02:00
iparg = args[0]
2023-04-01 16:18:18 +02:00
hostname = ""
} else {
2023-04-04 16:43:45 +02:00
iparg = args[0]
2023-04-01 16:18:18 +02:00
hostname = args[1]
}
2023-04-04 16:43:45 +02:00
var bestsubnet Subnet
var ip netip.Addr
2023-04-01 16:18:18 +02:00
2023-04-04 16:43:45 +02:00
argip, ipparseerr := netip.ParseAddr(iparg)
argsubnet, subparseerr := netip.ParsePrefix(iparg)
2023-04-01 16:18:18 +02:00
2023-04-04 16:43:45 +02:00
if ipparseerr != nil && subparseerr != nil {
fmt.Printf("[ERROR] Argument is neither a valid IP address nor a valid Subnet: %v", iparg)
2023-04-01 16:18:18 +02:00
os.Exit(1)
2023-04-04 16:43:45 +02:00
} else if ipparseerr == nil && subparseerr != nil {
// argument was a single IP
var subnetexists bool
bestsubnet, subnetexists = FindBestSubnet(argip)
if !subnetexists {
fmt.Printf("[ERROR] Found no suitable subnet for IP %v\n", iparg)
fmt.Printf("[ERROR] Maybe you need to add it first?\n")
os.Exit(1)
}
if bestsubnet.HasIP(argip) {
fmt.Printf("[ERROR] IP %v already exists in subnet %v\n", argip.String(), bestsubnet.Subnet.String())
os.Exit(1)
}
ip = argip
} else if subparseerr == nil && ipparseerr != nil {
// argument was a subnet
var subneterr error
bestsubnet, subneterr = GetSubnet(argsubnet)
if subneterr != nil {
fmt.Println("[ERROR]", subneterr)
os.Exit(1)
}
ip = bestsubnet.FindFirstFreeIP()
if !ip.IsValid() {
fmt.Printf("[ERROR] Found no free IP in Subnet %v\n", argsubnet.String())
os.Exit(1)
}
2023-04-01 16:18:18 +02:00
}
currentuser, _ := user.Current()
timestamp := time.Now()
2023-04-04 16:43:45 +02:00
bestsubnet.Addresses = append(bestsubnet.Addresses, Address{ip, hostname, timestamp, currentuser.Username})
bestsubnet.ChangedBy = currentuser.Username
bestsubnet.ChangedAt = timestamp
2023-04-01 16:18:18 +02:00
2023-04-04 16:43:45 +02:00
writeerr := bestsubnet.WriteSubnet()
2023-04-01 16:18:18 +02:00
if writeerr != nil {
fmt.Println("[ERROR]", writeerr)
os.Exit(1)
}
if hostname == "" {
2023-04-04 16:43:45 +02:00
fmt.Printf("added ip:\nip: %v\n", ip.String())
2023-04-01 16:18:18 +02:00
} else {
2023-04-04 16:43:45 +02:00
fmt.Printf("added ip:\nip: %v\nhostname: %v\n", ip.String(), hostname)
2023-04-01 16:18:18 +02:00
dnserr := AddDNSFqdn(hostname, ip)
if dnserr != nil {
fmt.Println("[ERROR]", dnserr)
os.Exit(1)
}
}
},
2023-03-10 17:52:20 +01:00
}
func init() {
2023-04-01 16:18:18 +02:00
ipCmd.AddCommand(ipaddCmd)
2023-03-10 17:52:20 +01:00
2023-04-01 16:18:18 +02:00
// Here you will define your flags and configuration settings.
2023-03-10 17:52:20 +01:00
2023-04-01 16:18:18 +02:00
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
2023-03-10 17:52:20 +01:00
2023-04-01 16:18:18 +02:00
// 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")
2023-03-10 17:52:20 +01:00
}