blob: 0e35b0a59d5a6e0e7470c12486f8b179eccfef7b [file] [log] [blame]
// 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 machineprototype
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"
)
// DeleteMachineLSEPrototypeCmd delete MachineLSEPrototype by given name.
var DeleteMachineLSEPrototypeCmd = &subcommands.Command{
UsageLine: "machine-prototype",
ShortDesc: "Delete prototype for a host",
LongDesc: `Delete prototype for a host.
Example:
shivas delete-machine-prototype {Machine Prototype Name}
Deletes the given machine prototype.`,
CommandRun: func() subcommands.CommandRun {
c := &deleteMachineLSEPrototype{}
c.authFlags.Register(&c.Flags, site.DefaultAuthOptions)
c.envFlags.Register(&c.Flags)
return c
},
}
type deleteMachineLSEPrototype struct {
subcommands.CommandRunBase
authFlags authcli.Flags
envFlags site.EnvFlags
}
func (c *deleteMachineLSEPrototype) 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 *deleteMachineLSEPrototype) 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()
fmt.Printf("Using UnifiedFleet service %s\n", e.UnifiedFleetService)
ic := ufsAPI.NewFleetPRPCClient(&prpc.Client{
C: hc,
Host: e.UnifiedFleetService,
Options: site.DefaultPRPCOptions,
})
if err := utils.PrintExistingMachinePrototype(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 the machine prototype: %s", args[0])) {
return nil
}
_, err = ic.DeleteMachineLSEPrototype(ctx, &ufsAPI.DeleteMachineLSEPrototypeRequest{
Name: ufsUtil.AddPrefix(ufsUtil.MachineLSEPrototypeCollection, args[0]),
})
if err == nil {
fmt.Fprintln(a.GetOut(), args[0], "deleted successfully.")
return nil
}
return err
}
func (c *deleteMachineLSEPrototype) validateArgs() error {
if c.Flags.NArg() == 0 {
return cmdlib.NewUsageError(c.Flags, "Please provide the machine prototype name to be deleted.")
}
return nil
}