github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/operator_root_keyring_remove.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/posener/complete"
     9  )
    10  
    11  // OperatorRootKeyringRemoveCommand is a Command
    12  // implementation that handles removeing variables encryption
    13  // keys from a keyring.
    14  type OperatorRootKeyringRemoveCommand struct {
    15  	Meta
    16  }
    17  
    18  func (c *OperatorRootKeyringRemoveCommand) Help() string {
    19  	helpText := `
    20  Usage: nomad operator root keyring remove [options] <key ID>
    21  
    22    Remove an encryption key from the cluster. This operation may only be
    23    performed on keys that are not the active key.
    24  
    25    If ACLs are enabled, this command requires a management token.
    26  
    27  General Options:
    28  
    29    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
    30  
    31  	return strings.TrimSpace(helpText)
    32  }
    33  
    34  func (c *OperatorRootKeyringRemoveCommand) Synopsis() string {
    35  	return "Removes a root encryption key"
    36  }
    37  
    38  func (c *OperatorRootKeyringRemoveCommand) AutocompleteFlags() complete.Flags {
    39  	return c.Meta.AutocompleteFlags(FlagSetClient)
    40  }
    41  
    42  func (c *OperatorRootKeyringRemoveCommand) AutocompleteArgs() complete.Predictor {
    43  	return complete.PredictAnything
    44  }
    45  
    46  func (c *OperatorRootKeyringRemoveCommand) Name() string {
    47  	return "root keyring remove"
    48  }
    49  
    50  func (c *OperatorRootKeyringRemoveCommand) Run(args []string) int {
    51  	var verbose bool
    52  
    53  	flags := c.Meta.FlagSet("root keyring remove", FlagSetClient)
    54  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    55  	flags.BoolVar(&verbose, "verbose", false, "")
    56  
    57  	if err := flags.Parse(args); err != nil {
    58  		return 1
    59  	}
    60  
    61  	args = flags.Args()
    62  	if len(args) != 1 {
    63  		c.Ui.Error("This command requires one argument: <key ID>")
    64  		c.Ui.Error(commandErrorText(c))
    65  		return 1
    66  	}
    67  	removeKey := args[0]
    68  
    69  	client, err := c.Meta.Client()
    70  	if err != nil {
    71  		c.Ui.Error(fmt.Sprintf("Error creating nomad cli client: %s", err))
    72  		return 1
    73  	}
    74  	_, err = client.Keyring().Delete(&api.KeyringDeleteOptions{
    75  		KeyID: removeKey,
    76  	}, nil)
    77  	if err != nil {
    78  		c.Ui.Error(fmt.Sprintf("error: %s", err))
    79  		return 1
    80  	}
    81  	c.Ui.Output(fmt.Sprintf("Removed encryption key %s", removeKey))
    82  	return 0
    83  }