blob: edc3618db0e7da043c378e11925af50083d6031a [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 nic
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"
)
// DeleteNicCmd delete Nic by given name.
var DeleteNicCmd = &subcommands.Command{
UsageLine: "nic {Nic name}",
ShortDesc: "Delete a nic on a machine",
LongDesc: `Delete a nic on a machine.
Example:
shivas delete nic {Nic name}
Deletes the given nic.`,
CommandRun: func() subcommands.CommandRun {
c := &deleteNic{}
c.authFlags.Register(&c.Flags, site.DefaultAuthOptions)
c.envFlags.Register(&c.Flags)
return c
},
}
type deleteNic struct {
subcommands.CommandRunBase
authFlags authcli.Flags
envFlags site.EnvFlags
}
func (c *deleteNic) 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 *deleteNic) 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.PrintExistingNic(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 Nic: %s", args[0])) {
return nil
}
_, err = ic.DeleteNic(ctx, &ufsAPI.DeleteNicRequest{
Name: ufsUtil.AddPrefix(ufsUtil.NicCollection, args[0]),
})
if err == nil {
fmt.Fprintln(a.GetOut(), args[0], "is deleted successfully.")
return nil
}
return err
}
func (c *deleteNic) validateArgs() error {
if c.Flags.NArg() == 0 {
return cmdlib.NewUsageError(c.Flags, "Please provide the nic name to be deleted.")
}
return nil
}