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

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api/contexts"
     8  	"github.com/posener/complete"
     9  )
    10  
    11  type VolumeStatusCommand struct {
    12  	Meta
    13  	length   int
    14  	short    bool
    15  	verbose  bool
    16  	json     bool
    17  	template string
    18  }
    19  
    20  func (c *VolumeStatusCommand) Help() string {
    21  	helpText := `
    22  Usage: nomad volume status [options] <id>
    23  
    24    Display status information about a CSI volume. If no volume id is given, a
    25    list of all volumes will be displayed.
    26  
    27    When ACLs are enabled, this command requires a token with the
    28    'csi-read-volume' and 'csi-list-volumes' capability for the volume's
    29    namespace.
    30  
    31  General Options:
    32  
    33    ` + generalOptionsUsage(usageOptsDefault) + `
    34  
    35  Status Options:
    36  
    37    -type <type>
    38      List only volumes of type <type>.
    39  
    40    -short
    41      Display short output. Used only when a single volume is being
    42      queried, and drops verbose information about allocations.
    43  
    44    -verbose
    45      Display full allocation information.
    46  
    47    -json
    48      Output the allocation in its JSON format.
    49  
    50    -t
    51      Format and display allocation using a Go template.
    52  `
    53  	return strings.TrimSpace(helpText)
    54  }
    55  
    56  func (c *VolumeStatusCommand) Synopsis() string {
    57  	return "Display status information about a volume"
    58  }
    59  
    60  func (c *VolumeStatusCommand) AutocompleteFlags() complete.Flags {
    61  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    62  		complete.Flags{
    63  			"-type":    predictVolumeType,
    64  			"-short":   complete.PredictNothing,
    65  			"-verbose": complete.PredictNothing,
    66  			"-json":    complete.PredictNothing,
    67  			"-t":       complete.PredictAnything,
    68  		})
    69  }
    70  
    71  func (c *VolumeStatusCommand) AutocompleteArgs() complete.Predictor {
    72  	return complete.PredictFunc(func(a complete.Args) []string {
    73  		client, err := c.Meta.Client()
    74  		if err != nil {
    75  			return nil
    76  		}
    77  
    78  		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Volumes, nil)
    79  		if err != nil {
    80  			return []string{}
    81  		}
    82  		return resp.Matches[contexts.Volumes]
    83  	})
    84  }
    85  
    86  func (c *VolumeStatusCommand) Name() string { return "volume status" }
    87  
    88  func (c *VolumeStatusCommand) Run(args []string) int {
    89  	var typeArg string
    90  
    91  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    92  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    93  	flags.StringVar(&typeArg, "type", "", "")
    94  	flags.BoolVar(&c.short, "short", false, "")
    95  	flags.BoolVar(&c.verbose, "verbose", false, "")
    96  	flags.BoolVar(&c.json, "json", false, "")
    97  	flags.StringVar(&c.template, "t", "", "")
    98  
    99  	if err := flags.Parse(args); err != nil {
   100  		c.Ui.Error(fmt.Sprintf("Error parsing arguments %s", err))
   101  		return 1
   102  	}
   103  
   104  	// Check that we either got no arguments or exactly one
   105  	args = flags.Args()
   106  	if len(args) > 1 {
   107  		c.Ui.Error("This command takes either no arguments or one: <id>")
   108  		c.Ui.Error(commandErrorText(c))
   109  		return 1
   110  	}
   111  
   112  	// Truncate alloc and node IDs unless full length is requested
   113  	c.length = shortId
   114  	if c.verbose {
   115  		c.length = fullId
   116  	}
   117  
   118  	// Get the HTTP client
   119  	client, err := c.Meta.Client()
   120  	if err != nil {
   121  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   122  		return 1
   123  	}
   124  
   125  	id := ""
   126  	if len(args) == 1 {
   127  		id = args[0]
   128  	}
   129  
   130  	code := c.csiStatus(client, id)
   131  	if code != 0 {
   132  		return code
   133  	}
   134  
   135  	// Extend this section with other volume implementations
   136  
   137  	return 0
   138  }