Skip to content

Commit cc110f4

Browse files
Add -json flag to quota inspect command (#16478)
* Added and flag to command * cli[style]: small refactor to avoid confussion with tmpl variable * Update inspect.mdx * cli: add changelog entry * Update .changelog/16478.txt Co-authored-by: James Rasell <[email protected]> * Update command/quota_inspect.go Co-authored-by: James Rasell <[email protected]> --------- Co-authored-by: James Rasell <[email protected]>
1 parent 26b4fcc commit cc110f4

File tree

5 files changed

+94
-27
lines changed

5 files changed

+94
-27
lines changed

.changelog/16478.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` flag to `quota inspect` command
3+
```

api/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func testNamespace() *Namespace {
102102

103103
func testQuotaSpec() *QuotaSpec {
104104
return &QuotaSpec{
105-
Name: "test-namespace",
105+
Name: "test-quota",
106106
Description: "Testing namespaces",
107107
Limits: []*QuotaLimit{
108108
{

command/quota_inspect.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ General Options:
3333
3434
Inspect Options:
3535
36+
-json
37+
Output the latest quota information in a JSON format.
38+
3639
-t
37-
Format and display the namespaces using a Go template.
40+
Format and display quota information using a Go template.
3841
`
3942

4043
return strings.TrimSpace(helpText)
@@ -43,7 +46,8 @@ Inspect Options:
4346
func (c *QuotaInspectCommand) AutocompleteFlags() complete.Flags {
4447
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
4548
complete.Flags{
46-
"-t": complete.PredictAnything,
49+
"-t": complete.PredictAnything,
50+
"-json": complete.PredictNothing,
4751
})
4852
}
4953

@@ -58,9 +62,11 @@ func (c *QuotaInspectCommand) Synopsis() string {
5862
func (c *QuotaInspectCommand) Name() string { return "quota inspect" }
5963

6064
func (c *QuotaInspectCommand) Run(args []string) int {
65+
var json bool
6166
var tmpl string
6267
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
6368
flags.Usage = func() { c.Ui.Output(c.Help()) }
69+
flags.BoolVar(&json, "json", false, "")
6470
flags.StringVar(&tmpl, "t", "", "")
6571

6672
if err := flags.Parse(args); err != nil {
@@ -97,6 +103,17 @@ func (c *QuotaInspectCommand) Run(args []string) int {
97103
return 1
98104
}
99105

106+
if json || len(tmpl) > 0 {
107+
out, err := Format(json, tmpl, spec)
108+
if err != nil {
109+
c.Ui.Error(err.Error())
110+
return 1
111+
}
112+
113+
c.Ui.Output(out)
114+
return 0
115+
}
116+
100117
// Get the quota usages
101118
usages, failures := quotaUsages(spec, quotas)
102119

@@ -111,7 +128,8 @@ func (c *QuotaInspectCommand) Run(args []string) int {
111128
Failures: failuresConverted,
112129
}
113130

114-
out, err := Format(len(tmpl) == 0, tmpl, data)
131+
ftr := JSONFormat{}
132+
out, err := ftr.TransformData(data)
115133
if err != nil {
116134
c.Ui.Error(err.Error())
117135
return 1

command/quota_inspect_test.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/hashicorp/nomad/ci"
1111
"github.com/mitchellh/cli"
1212
"github.com/posener/complete"
13-
"github.com/stretchr/testify/assert"
13+
"github.com/shoenig/test/must"
1414
)
1515

1616
func TestQuotaInspectCommand_Implements(t *testing.T) {
@@ -24,24 +24,20 @@ func TestQuotaInspectCommand_Fails(t *testing.T) {
2424
cmd := &QuotaInspectCommand{Meta: Meta{Ui: ui}}
2525

2626
// Fails on misuse
27-
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
28-
t.Fatalf("expected exit code 1, got: %d", code)
29-
}
30-
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
31-
t.Fatalf("expected help output, got: %s", out)
32-
}
27+
code = cmd.Run([]string{"some", "bad", "args"})
28+
must.One(t, code)
29+
30+
must.StrContains(t, ui.ErrorWriter.String(), commandErrorText(cmd))
3331
ui.ErrorWriter.Reset()
3432

35-
if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
36-
t.Fatalf("expected exit code 1, got: %d", code)
37-
}
38-
if out := ui.ErrorWriter.String(); !strings.Contains(out, "retrieving quota") {
39-
t.Fatalf("connection error, got: %s", out)
40-
}
33+
code = cmd.Run([]string{"-address=nope", "foo"})
34+
must.One(t, code)
35+
36+
must.StrContains(t, ui.ErrorWriter.String(), "retrieving quota")
4137
ui.ErrorWriter.Reset()
4238
}
4339

44-
func TestQuotaInspectCommand_Good(t *testing.T) {
40+
func TestQuotaInspectCommand_Run(t *testing.T) {
4541
ci.Parallel(t)
4642

4743
// Create a server
@@ -54,17 +50,34 @@ func TestQuotaInspectCommand_Good(t *testing.T) {
5450
// Create a quota to delete
5551
qs := testQuotaSpec()
5652
_, err := client.Quotas().Register(qs, nil)
57-
assert.Nil(t, err)
53+
must.NoError(t, err)
5854

59-
// Delete a namespace
60-
if code := cmd.Run([]string{"-address=" + url, qs.Name}); code != 0 {
61-
t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
62-
}
55+
// Delete a quota
56+
code := cmd.Run([]string{"-address=" + url, qs.Name})
57+
must.Zero(t, code)
6358

6459
out := ui.OutputWriter.String()
6560
if !strings.Contains(out, "Usages") || !strings.Contains(out, qs.Name) {
6661
t.Fatalf("expected quota, got: %s", out)
6762
}
63+
64+
// List json
65+
must.Zero(t, cmd.Run([]string{"-address=" + url, "-json", allocID}))
66+
67+
outJson := *api.Quotas{}
68+
err = json.Unmarshal(ui.OutputWriter.Bytes(), &outJson)
69+
must.NoError(t, err)
70+
71+
ui.OutputWriter.Reset()
72+
73+
// Go template to format the output
74+
code = cmd.Run([]string{"-address=" + url, "-t", "{{ .Name }}", allocID})
75+
must.Zero(t, code)
76+
77+
out = ui.OutputWriter.String()
78+
must.StrContains(t, out, "test-quota")
79+
80+
ui.OutputWriter.Reset()
6881
}
6982

7083
func TestQuotaInspectCommand_AutocompleteArgs(t *testing.T) {
@@ -80,12 +93,12 @@ func TestQuotaInspectCommand_AutocompleteArgs(t *testing.T) {
8093
// Create a quota
8194
qs := testQuotaSpec()
8295
_, err := client.Quotas().Register(qs, nil)
83-
assert.Nil(err)
96+
must.NoError(t, err)
8497

8598
args := complete.Args{Last: "t"}
8699
predictor := cmd.AutocompleteArgs()
87100

88101
res := predictor.Predict(args)
89-
assert.Equal(1, len(res))
90-
assert.Equal(qs.Name, res[0])
102+
must.One(t, len(res))
103+
must.StrContains(t, qs.Name, res[0])
91104
}

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ capability and access to any namespaces that the quota is applied to.
2929

3030
## Inspect Options
3131

32-
- `-t` : Format and display the quota using a Go template.
32+
- `-json`: Output the quota specifications in a JSON format.
33+
34+
- `-t`: Format and display the quota using a Go template.
3335

3436
## Examples
3537

@@ -88,3 +90,34 @@ $ nomad quota inspect default-quota
8890
}
8991
}
9092
```
93+
94+
The `-json` flag can be used to get the quota specs in json format:
95+
```shell-session
96+
$ nomad quota inspect -json default-quota
97+
98+
{
99+
"CreateIndex": 8,
100+
"Description": "Limit the shared default namespace",
101+
"Limits": [
102+
{
103+
"Hash": "NLOoV2WBU8ieJIrYXXx8NRb5C2xU61pVVWRDLEIMxlU=",
104+
"Region": "global",
105+
"RegionLimit": {
106+
"CPU": 2500,
107+
"DiskMB": 0,
108+
"MemoryMB": 2000,
109+
"Networks": null
110+
}
111+
}
112+
],
113+
"ModifyIndex": 56,
114+
"Name": "default-quota"
115+
}
116+
```
117+
118+
Or use the `-t` flag to format and display the quota specs using a Go template:
119+
```shell-session
120+
$ nomad quota inspect -t {{ .Description }} default-quota
121+
122+
Limit the shared default namespace
123+
```

0 commit comments

Comments
 (0)