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