github.com/hernad/nomad@v1.6.112/command/quota_inspect.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"
    11  	"github.com/posener/complete"
    12  )
    13  
    14  type QuotaInspectCommand struct {
    15  	Meta
    16  }
    17  
    18  type inspectedQuota struct {
    19  	Spec     *api.QuotaSpec
    20  	Usages   map[string]*api.QuotaUsage
    21  	Failures map[string]string `json:"UsageLookupErrors"`
    22  }
    23  
    24  func (c *QuotaInspectCommand) Help() string {
    25  	helpText := `
    26  Usage: nomad quota inspect [options] <quota>
    27  
    28    Inspect is used to view raw information about a particular quota.
    29  
    30    If ACLs are enabled, this command requires a token with the 'quota:read'
    31    capability and access to any namespaces that the quota is applied to.
    32  
    33  General Options:
    34  
    35    ` + generalOptionsUsage(usageOptsDefault) + `
    36  
    37  Inspect Options:
    38  
    39    -json
    40      Output the latest quota information in a JSON format.
    41  
    42    -t
    43      Format and display quota information using a Go template.
    44  `
    45  
    46  	return strings.TrimSpace(helpText)
    47  }
    48  
    49  func (c *QuotaInspectCommand) AutocompleteFlags() complete.Flags {
    50  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    51  		complete.Flags{
    52  			"-t":    complete.PredictAnything,
    53  			"-json": complete.PredictNothing,
    54  		})
    55  }
    56  
    57  func (c *QuotaInspectCommand) AutocompleteArgs() complete.Predictor {
    58  	return QuotaPredictor(c.Meta.Client)
    59  }
    60  
    61  func (c *QuotaInspectCommand) Synopsis() string {
    62  	return "Inspect a quota specification"
    63  }
    64  
    65  func (c *QuotaInspectCommand) Name() string { return "quota inspect" }
    66  
    67  func (c *QuotaInspectCommand) Run(args []string) int {
    68  	var json bool
    69  	var tmpl string
    70  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    71  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    72  	flags.BoolVar(&json, "json", false, "")
    73  	flags.StringVar(&tmpl, "t", "", "")
    74  
    75  	if err := flags.Parse(args); err != nil {
    76  		return 1
    77  	}
    78  
    79  	// Check that we got one argument
    80  	args = flags.Args()
    81  	if l := len(args); l != 1 {
    82  		c.Ui.Error("This command takes one argument: <quota>")
    83  		c.Ui.Error(commandErrorText(c))
    84  		return 1
    85  	}
    86  
    87  	name := args[0]
    88  
    89  	// Get the HTTP client
    90  	client, err := c.Meta.Client()
    91  	if err != nil {
    92  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    93  		return 1
    94  	}
    95  
    96  	// Do a prefix lookup
    97  	quotas := client.Quotas()
    98  	spec, possible, err := getQuota(quotas, name)
    99  	if err != nil {
   100  		c.Ui.Error(fmt.Sprintf("Error retrieving quota: %s", err))
   101  		return 1
   102  	}
   103  
   104  	if len(possible) != 0 {
   105  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple quotas\n\n%s", formatQuotaSpecs(possible)))
   106  		return 1
   107  	}
   108  
   109  	if json || len(tmpl) > 0 {
   110  		out, err := Format(json, tmpl, spec)
   111  		if err != nil {
   112  			c.Ui.Error(err.Error())
   113  			return 1
   114  		}
   115  
   116  		c.Ui.Output(out)
   117  		return 0
   118  	}
   119  
   120  	// Get the quota usages
   121  	usages, failures := quotaUsages(spec, quotas)
   122  
   123  	failuresConverted := make(map[string]string, len(failures))
   124  	for r, e := range failures {
   125  		failuresConverted[r] = e.Error()
   126  	}
   127  
   128  	data := &inspectedQuota{
   129  		Spec:     spec,
   130  		Usages:   usages,
   131  		Failures: failuresConverted,
   132  	}
   133  
   134  	ftr := JSONFormat{}
   135  	out, err := ftr.TransformData(data)
   136  	if err != nil {
   137  		c.Ui.Error(err.Error())
   138  		return 1
   139  	}
   140  
   141  	c.Ui.Output(out)
   142  	return 0
   143  }