github.com/hernad/nomad@v1.6.112/command/volume_status.go (about)

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