| // Copyright 2020 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package kvm |
| |
| import ( |
| "fmt" |
| "os" |
| |
| "github.com/maruel/subcommands" |
| "go.chromium.org/luci/auth/client/authcli" |
| "go.chromium.org/luci/common/cli" |
| "go.chromium.org/luci/grpc/prpc" |
| |
| "infra/cmd/shivas/site" |
| "infra/cmd/shivas/utils" |
| "infra/cmdsupport/cmdlib" |
| ufsAPI "infra/unifiedfleet/api/v1/rpc" |
| ufsUtil "infra/unifiedfleet/app/util" |
| ) |
| |
| // DeleteKVMCmd delete KVM by given name. |
| var DeleteKVMCmd = &subcommands.Command{ |
| UsageLine: "kvm {KVM Name}", |
| ShortDesc: "Delete a kvm on a rack", |
| LongDesc: `Delete a kvm on a rack. |
| |
| Example: |
| shivas delete kvm {KVM Name} |
| Deletes the given kvm.`, |
| CommandRun: func() subcommands.CommandRun { |
| c := &deleteKVM{} |
| c.authFlags.Register(&c.Flags, site.DefaultAuthOptions) |
| c.envFlags.Register(&c.Flags) |
| return c |
| }, |
| } |
| |
| type deleteKVM struct { |
| subcommands.CommandRunBase |
| authFlags authcli.Flags |
| envFlags site.EnvFlags |
| } |
| |
| func (c *deleteKVM) Run(a subcommands.Application, args []string, env subcommands.Env) int { |
| if err := c.innerRun(a, args, env); err != nil { |
| cmdlib.PrintError(a, err) |
| return 1 |
| } |
| return 0 |
| } |
| |
| func (c *deleteKVM) innerRun(a subcommands.Application, args []string, env subcommands.Env) error { |
| if err := c.validateArgs(); err != nil { |
| return err |
| } |
| ctx := cli.GetContext(a, c, env) |
| ns, err := c.envFlags.Namespace() |
| if err != nil { |
| return err |
| } |
| ctx = utils.SetupContext(ctx, ns) |
| hc, err := cmdlib.NewHTTPClient(ctx, &c.authFlags) |
| if err != nil { |
| return err |
| } |
| e := c.envFlags.Env() |
| ic := ufsAPI.NewFleetPRPCClient(&prpc.Client{ |
| C: hc, |
| Host: e.UnifiedFleetService, |
| Options: site.DefaultPRPCOptions, |
| }) |
| if err := utils.PrintExistingKVM(ctx, ic, args[0]); err != nil { |
| return err |
| } |
| prompt := utils.CLIPrompt(a.GetOut(), os.Stdin, false) |
| if prompt != nil && !prompt(fmt.Sprintf("Are you sure you want to delete KVM: %s", args[0])) { |
| return nil |
| } |
| _, err = ic.DeleteKVM(ctx, &ufsAPI.DeleteKVMRequest{ |
| Name: ufsUtil.AddPrefix(ufsUtil.KVMCollection, args[0]), |
| }) |
| if err == nil { |
| fmt.Fprintln(a.GetOut(), args[0], "is deleted successfully.") |
| return nil |
| } |
| return err |
| } |
| |
| func (c *deleteKVM) validateArgs() error { |
| if c.Flags.NArg() == 0 { |
| return cmdlib.NewUsageError(c.Flags, "Please provide the kvm name to be deleted.") |
| } |
| return nil |
| } |