ipam/cmd/subnet-delete.go

86 lines
2.6 KiB
Go
Raw Normal View History

2023-03-11 13:49:35 +01:00
/*
Copyright © 2023 Laura Kalb <dev@lauka.net>
*/
package cmd
import (
2023-03-25 15:16:26 +01:00
"fmt"
"net/netip"
"os"
2023-03-11 13:49:35 +01:00
2023-03-25 15:16:26 +01:00
"github.com/spf13/cobra"
2023-03-11 13:49:35 +01:00
)
// deleteCmd represents the delete command
var subnetdeleteCmd = &cobra.Command{
2023-03-25 15:16:26 +01:00
Use: "delete",
Short: "delete subnet",
Long: `Delete a subnet from the ipam.`,
Args: cobra.ExactArgs(1),
Aliases: []string{"d"},
Example: "ipam subnet delete 192.168.0.0/24",
Run: func(cmd *cobra.Command, args []string) {
subnet, parseerr := netip.ParsePrefix(args[0])
if parseerr != nil {
fmt.Println("[ERROR]", parseerr)
os.Exit(1)
}
2023-03-11 13:49:35 +01:00
2023-03-25 15:16:26 +01:00
if !SubnetExists(subnet) {
fmt.Printf("[ERROR] Couldn't find subnet %v\n", subnet.String())
os.Exit(1)
}
2023-03-15 17:23:14 +01:00
2023-03-25 15:16:26 +01:00
subnetobj, suberr := GetSubnet(subnet)
if suberr != nil {
fmt.Println("[ERROR]", suberr)
os.Exit(1)
}
2023-03-23 13:53:46 +01:00
2023-03-25 15:16:26 +01:00
var confirmation string
skipinteractive, _ := cmd.Flags().GetBool("yes")
if skipinteractive {
confirmation = "y"
} else {
fmt.Printf("[WARNING] Do you really want to delete subnet %v?\n", subnet.String())
fmt.Printf("[WARNING] This will also delete all DNS records if PowerDNS integration is enabled!\n")
fmt.Printf("[WARNING] Continue? [y/N] ")
fmt.Scan(&confirmation)
}
2023-03-14 17:21:49 +01:00
2023-03-25 15:16:26 +01:00
if (confirmation == "y") || (confirmation == "Y") {
for _, address := range subnetobj.Addresses {
if address.FQDN != "" {
deleteerr := DeleteDNSFqdn(address.FQDN, address.IP)
if deleteerr != nil {
fmt.Println("[ERROR]", deleteerr)
}
}
}
deleteerr := DeleteSubnet(subnet)
if deleteerr != nil {
fmt.Println("[ERROR]", deleteerr)
os.Exit(1)
} else {
fmt.Printf("deleted subnet %v\n", subnet.String())
}
2023-03-23 13:53:46 +01:00
2023-03-25 15:16:26 +01:00
}
},
2023-03-11 13:49:35 +01:00
}
func init() {
2023-03-25 15:16:26 +01:00
subnetCmd.AddCommand(subnetdeleteCmd)
2023-03-11 13:49:35 +01:00
2023-03-25 15:16:26 +01:00
// Here you will define your flags and configuration settings.
2023-03-11 13:49:35 +01:00
2023-03-25 15:16:26 +01:00
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
2023-03-11 13:49:35 +01:00
2023-03-25 15:16:26 +01:00
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
subnetdeleteCmd.Flags().BoolP("yes", "y", false, "suppress interactive prompts and answer yes.")
2023-03-11 13:49:35 +01:00
}