Tim Windelschmidt | 886c386 | 2023-05-23 16:47:41 +0200 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/spf13/cobra" |
| 7 | "k8s.io/klog/v2" |
| 8 | |
| 9 | "source.monogon.dev/cloud/shepherd/equinix/wrapngo" |
| 10 | clicontext "source.monogon.dev/metropolis/cli/pkg/context" |
| 11 | ) |
| 12 | |
| 13 | var moveCmd = &cobra.Command{ |
| 14 | Use: "move [source] [target]", |
| 15 | Short: "Move all reserved hardware from one to another project", |
| 16 | Args: cobra.ExactArgs(2), |
| 17 | Run: doMove, |
| 18 | } |
| 19 | |
| 20 | func init() { |
| 21 | rootCmd.AddCommand(moveCmd) |
| 22 | } |
| 23 | |
| 24 | func doMove(cmd *cobra.Command, args []string) { |
| 25 | ctx := clicontext.WithInterrupt(context.Background()) |
| 26 | api := wrapngo.New(&c) |
| 27 | |
| 28 | klog.Infof("Listing reservations for %q", args[0]) |
| 29 | reservations, err := api.ListReservations(ctx, args[0]) |
| 30 | if err != nil { |
| 31 | klog.Exitf("failed listing reservations: %v", err) |
| 32 | } |
| 33 | |
| 34 | klog.Infof("Got %d reservations. Moving machines", len(reservations)) |
| 35 | for _, r := range reservations { |
| 36 | _, err := api.MoveReservation(ctx, r.ID, args[1]) |
| 37 | if err != nil { |
| 38 | klog.Errorf("failed moving reservation: %v", err) |
| 39 | continue |
| 40 | } |
| 41 | klog.Infof("Moved Device %s", r.ID) |
| 42 | } |
| 43 | } |