2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type QuotaInspectCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
type inspectedQuota struct {
|
|
|
|
Spec *api.QuotaSpec
|
|
|
|
Usages map[string]*api.QuotaUsage
|
|
|
|
Failures map[string]string `json:"UsageLookupErrors"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *QuotaInspectCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad quota inspect [options] <quota>
|
|
|
|
|
|
|
|
Inspect is used to view raw information about a particular quota.
|
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this command requires a token with the 'quota:read'
|
|
|
|
capability and access to any namespaces that the quota is applied to.
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
Inspect Options:
|
|
|
|
|
2023-03-20 09:40:51 +00:00
|
|
|
-json
|
|
|
|
Output the latest quota information in a JSON format.
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
-t
|
2023-03-20 09:40:51 +00:00
|
|
|
Format and display quota information using a Go template.
|
2017-10-13 21:36:02 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *QuotaInspectCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
2023-03-20 09:40:51 +00:00
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
"-json": complete.PredictNothing,
|
2017-10-13 21:36:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *QuotaInspectCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return QuotaPredictor(c.Meta.Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *QuotaInspectCommand) Synopsis() string {
|
|
|
|
return "Inspect a quota specification"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *QuotaInspectCommand) Name() string { return "quota inspect" }
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
func (c *QuotaInspectCommand) Run(args []string) int {
|
2023-03-20 09:40:51 +00:00
|
|
|
var json bool
|
2017-10-13 21:36:02 +00:00
|
|
|
var tmpl string
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2017-10-13 21:36:02 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2023-03-20 09:40:51 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
2017-10-13 21:36:02 +00:00
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-02-07 15:34:05 +00:00
|
|
|
// Check that we got one argument
|
2017-10-13 21:36:02 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); l != 1 {
|
2018-04-18 17:55:51 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <quota>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2017-10-13 21:36:02 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
name := args[0]
|
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a prefix lookup
|
|
|
|
quotas := client.Quotas()
|
|
|
|
spec, possible, err := getQuota(quotas, name)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving quota: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(possible) != 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple quotas\n\n%s", formatQuotaSpecs(possible)))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-03-20 09:40:51 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, spec)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
// Get the quota usages
|
|
|
|
usages, failures := quotaUsages(spec, quotas)
|
|
|
|
|
|
|
|
failuresConverted := make(map[string]string, len(failures))
|
|
|
|
for r, e := range failures {
|
|
|
|
failuresConverted[r] = e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &inspectedQuota{
|
|
|
|
Spec: spec,
|
|
|
|
Usages: usages,
|
|
|
|
Failures: failuresConverted,
|
|
|
|
}
|
|
|
|
|
2023-03-20 09:40:51 +00:00
|
|
|
ftr := JSONFormat{}
|
|
|
|
out, err := ftr.TransformData(data)
|
2017-10-13 21:36:02 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|