open-nomad/command/quota_inspect.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

144 lines
3.0 KiB
Go
Raw Normal View History

// 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.
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:
` + generalOptionsUsage(usageOptsDefault) + `
2017-10-13 21:36:02 +00:00
Inspect Options:
-json
Output the latest quota information in a JSON format.
2017-10-13 21:36:02 +00:00
-t
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{
"-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"
}
func (c *QuotaInspectCommand) Name() string { return "quota inspect" }
2017-10-13 21:36:02 +00:00
func (c *QuotaInspectCommand) Run(args []string) int {
var json bool
2017-10-13 21:36:02 +00:00
var tmpl string
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
2017-10-13 21:36:02 +00:00
flags.Usage = func() { c.Ui.Output(c.Help()) }
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
}
// Check that we got one argument
2017-10-13 21:36:02 +00:00
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error("This command takes one argument: <quota>")
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
}
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,
}
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
}