blob: ec320716c92178602907257918ffdca2b252874c [file] [log] [blame]
Tim Windelschmidt886c3862023-05-23 16:47:41 +02001package main
2
3import (
4 "context"
Tim Windelschmidtb765f242024-05-08 01:40:02 +02005 "os"
6 "os/signal"
Tim Windelschmidt886c3862023-05-23 16:47:41 +02007
8 "github.com/spf13/cobra"
9 "k8s.io/klog/v2"
10
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020011 "source.monogon.dev/cloud/equinix/wrapngo"
Tim Windelschmidt886c3862023-05-23 16:47:41 +020012)
13
14var moveCmd = &cobra.Command{
15 Use: "move [source] [target]",
16 Short: "Move all reserved hardware from one to another project",
17 Args: cobra.ExactArgs(2),
18 Run: doMove,
19}
20
21func init() {
22 rootCmd.AddCommand(moveCmd)
23}
24
25func doMove(cmd *cobra.Command, args []string) {
Tim Windelschmidtb765f242024-05-08 01:40:02 +020026 ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
Tim Windelschmidt886c3862023-05-23 16:47:41 +020027 api := wrapngo.New(&c)
28
29 klog.Infof("Listing reservations for %q", args[0])
30 reservations, err := api.ListReservations(ctx, args[0])
31 if err != nil {
32 klog.Exitf("failed listing reservations: %v", err)
33 }
34
35 klog.Infof("Got %d reservations. Moving machines", len(reservations))
36 for _, r := range reservations {
37 _, err := api.MoveReservation(ctx, r.ID, args[1])
38 if err != nil {
39 klog.Errorf("failed moving reservation: %v", err)
40 continue
41 }
42 klog.Infof("Moved Device %s", r.ID)
43 }
44}