Skip to content

Commit 26b4fcc

Browse files
cli: add -json and -t flags to quota status command (#16485)
* cli: add json and t flags to quota status command * cli: add entry to changelog * Update command/quota_status.go Co-authored-by: James Rasell <[email protected]> --------- Co-authored-by: James Rasell <[email protected]>
1 parent 151147b commit 26b4fcc

File tree

4 files changed

+99
-13
lines changed

4 files changed

+99
-13
lines changed

.changelog/16485.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
cli: Added `-json` and `-t` flag to `quota status` command
3+
```

command/quota_status.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,26 @@ Usage: nomad quota status [options] <quota>
2626
2727
General Options:
2828
29-
` + generalOptionsUsage(usageOptsDefault)
29+
` + generalOptionsUsage(usageOptsDefault) + `
30+
31+
Status Specific Options:
32+
33+
-json
34+
Output the latest quota status information in a JSON format.
35+
36+
-t
37+
Format and display quota status information using a Go template.
38+
`
3039

3140
return strings.TrimSpace(helpText)
3241
}
3342

3443
func (c *QuotaStatusCommand) AutocompleteFlags() complete.Flags {
35-
return c.Meta.AutocompleteFlags(FlagSetClient)
44+
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
45+
complete.Flags{
46+
"-json": complete.PredictNothing,
47+
"-t": complete.PredictAnything,
48+
})
3649
}
3750

3851
func (c *QuotaStatusCommand) AutocompleteArgs() complete.Predictor {
@@ -46,8 +59,13 @@ func (c *QuotaStatusCommand) Synopsis() string {
4659
func (c *QuotaStatusCommand) Name() string { return "quota status" }
4760

4861
func (c *QuotaStatusCommand) Run(args []string) int {
62+
var json bool
63+
var tmpl string
64+
4965
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
5066
flags.Usage = func() { c.Ui.Output(c.Help()) }
67+
flags.BoolVar(&json, "json", false, "")
68+
flags.StringVar(&tmpl, "t", "", "")
5169

5270
if err := flags.Parse(args); err != nil {
5371
return 1
@@ -83,6 +101,17 @@ func (c *QuotaStatusCommand) Run(args []string) int {
83101
return 1
84102
}
85103

104+
if json || len(tmpl) > 0 {
105+
out, err := Format(json, tmpl, spec)
106+
if err != nil {
107+
c.Ui.Error(err.Error())
108+
return 1
109+
}
110+
111+
c.Ui.Output(out)
112+
return 0
113+
}
114+
86115
// Format the basics
87116
c.Ui.Output(formatQuotaSpecBasics(spec))
88117

command/quota_status_test.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestQuotaStatusCommand_Fails(t *testing.T) {
4141
ui.ErrorWriter.Reset()
4242
}
4343

44-
func TestQuotaStatusCommand_Good(t *testing.T) {
44+
func TestQuotaStatusCommand_Run(t *testing.T) {
4545
ci.Parallel(t)
4646

4747
// Create a server
@@ -54,7 +54,7 @@ func TestQuotaStatusCommand_Good(t *testing.T) {
5454
// Create a quota to delete
5555
qs := testQuotaSpec()
5656
_, err := client.Quotas().Register(qs, nil)
57-
assert.Nil(t, err)
57+
must.NoError(t, err)
5858

5959
// Delete a namespace
6060
if code := cmd.Run([]string{"-address=" + url, qs.Name}); code != 0 {
@@ -63,14 +63,30 @@ func TestQuotaStatusCommand_Good(t *testing.T) {
6363

6464
// Check for basic spec
6565
out := ui.OutputWriter.String()
66-
if !strings.Contains(out, "= test") {
67-
t.Fatalf("expected quota, got: %s", out)
68-
}
66+
must.StrContains(t, out, "= test")
6967

7068
// Check for usage
71-
if !strings.Contains(out, "0 / 100") {
72-
t.Fatalf("expected quota, got: %s", out)
73-
}
69+
must.StrContains(t, out, "0 / 100")
70+
71+
ui.OutputWriter.Reset()
72+
73+
// List json
74+
must.Zero(t, cmd.Run([]string{"-address=" + url, "-json", qs.Name}))
75+
76+
outJson := api.QuotaSpec{}
77+
err = json.Unmarshal(ui.OutputWriter.Bytes(), &outJson)
78+
must.NoError(t, err)
79+
80+
ui.OutputWriter.Reset()
81+
82+
// Go template to format the output
83+
code = cmd.Run([]string{"-address=" + url, "-t", "{{range .}}{{ .Name }}{{end}}", allocID})
84+
must.Zero(t, code)
85+
86+
out = ui.OutputWriter.String()
87+
must.StrContains(t, out, "test-quota")
88+
89+
ui.OutputWriter.Reset()
7490
}
7591

7692
func TestQuotaStatusCommand_AutocompleteArgs(t *testing.T) {
@@ -86,12 +102,12 @@ func TestQuotaStatusCommand_AutocompleteArgs(t *testing.T) {
86102
// Create a quota
87103
qs := testQuotaSpec()
88104
_, err := client.Quotas().Register(qs, nil)
89-
assert.Nil(err)
105+
must.NoError(err)
90106

91107
args := complete.Args{Last: "t"}
92108
predictor := cmd.AutocompleteArgs()
93109

94110
res := predictor.Predict(args)
95-
assert.Equal(1, len(res))
96-
assert.Equal(qs.Name, res[0])
111+
must.One(t, len(res))
112+
must.StrContains(t, qs.Name, res[0])
97113
}

website/content/docs/commands/quota/status.mdx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ capability and access to any namespaces that the quota is applied to.
2727

2828
@include 'general_options.mdx'
2929

30+
## Inspect Options
31+
32+
- `-json`: Output the quota specifications in a JSON format.
33+
34+
- `-t`: Format and display the quota using a Go template.
35+
3036
## Examples
3137

3238
View the status of a quota specification:
@@ -40,4 +46,36 @@ Limits = 1
4046
Quota Limits
4147
Region CPU Usage Memory Usage Network Usage
4248
global 500 / 2500 256 / 2000 30 / 50
49+
4350
```
51+
52+
The `-json` flag can be used to get the quota status in json format:
53+
54+
```shell-session
55+
$ nomad quota inspect -json default-quota
56+
{
57+
"CreateIndex": 8,
58+
"Description": "Limit the shared default namespace",
59+
"Limits": [
60+
{
61+
"Hash": "NLOoV2WBU8ieJIrYXXx8NRb5C2xU61pVVWRDLEIMxlU=",
62+
"Region": "global",
63+
"RegionLimit": {
64+
"CPU": 2500,
65+
"DiskMB": 0,
66+
"MemoryMB": 2000,
67+
"Networks": null
68+
}
69+
}
70+
],
71+
"ModifyIndex": 56,
72+
"Name": "default-quota"
73+
}
74+
```
75+
76+
Or use the `-t` flag to format and display the quota status using a Go template:
77+
78+
```shell-session
79+
$ nomad quota inspect -t {{ .Description }} default-quota
80+
Limit the shared default namespace
81+
```

0 commit comments

Comments
 (0)