blob: e3c51a5cfbc2b60c94cdbe97b8bc9b060459f171 [file] [log] [blame]
Tim Windelschmidt886c3862023-05-23 16:47:41 +02001package main
2
3import (
4 "context"
5 "time"
6
7 "github.com/spf13/cobra"
8 "k8s.io/klog/v2"
9
10 "source.monogon.dev/cloud/shepherd/equinix/wrapngo"
11 clicontext "source.monogon.dev/metropolis/cli/pkg/context"
12)
13
14var deleteCmd = &cobra.Command{
15 Use: "delete [target]",
16 Short: "Delete all devices from one project",
17 Args: cobra.ExactArgs(1),
18 Run: doDelete,
19}
20
21func init() {
22 rootCmd.AddCommand(deleteCmd)
23}
24
25func doDelete(cmd *cobra.Command, args []string) {
26 ctx := clicontext.WithInterrupt(context.Background())
27 api := wrapngo.New(&c)
28
29 klog.Infof("Listing devices for %q", args[0])
30
31 devices, err := api.ListDevices(ctx, args[0])
32 if err != nil {
33 klog.Exitf("failed listing devices: %v", err)
34 }
35
36 if len(devices) == 0 {
37 klog.Infof("No devices found in %s", args[0])
38 return
39 }
40
41 klog.Infof("Deleting %d Devices in %s. THIS WILL DELETE SERVERS! You have five seconds to cancel!", len(devices), args[0])
42 time.Sleep(5 * time.Second)
43
44 for _, d := range devices {
45 klog.Infof("deleting %s (%s)...", d.ID, d.Hostname)
46 if err := api.DeleteDevice(ctx, d.ID); err != nil {
47 klog.Infof("failed deleting device %s (%s): %v", d.ID, d.Hostname, err)
48 continue
49 }
50 }
51}