Skip to content

Commit

Permalink
Merge 6aea182 into backport/operator-usage-improvements/uniquely-unit…
Browse files Browse the repository at this point in the history
…ed-hermit
  • Loading branch information
hc-github-team-consul-core authored Feb 24, 2023
2 parents 555aa45 + 6aea182 commit f1bef50
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
19 changes: 13 additions & 6 deletions command/operator/usage/instances/usage_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ type cmd struct {

func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.onlyBillable, "billable", false, "Display only billable service info.")
c.flags.BoolVar(&c.onlyConnect, "connect", false, "Display only Connect service info.")
c.flags.BoolVar(&c.onlyBillable, "billable", false, "Display only billable service info. "+
"Cannot be used with -connect.")
c.flags.BoolVar(&c.onlyConnect, "connect", false, "Display only Connect service info."+
"Cannot be used with -billable.")
c.flags.BoolVar(&c.allDatacenters, "all-datacenters", false, "Display service counts from "+
"all datacenters.")

Expand All @@ -54,6 +56,11 @@ func (c *cmd) Run(args []string) int {
return 1
}

if c.onlyBillable && c.onlyConnect {
c.UI.Error("Cannot specify both -billable and -connect flags")
return 1
}

// Create and test the HTTP client
client, err := c.http.APIClient()
if err != nil {
Expand Down Expand Up @@ -219,22 +226,22 @@ func (c *cmd) Help() string {
const (
synopsis = "Display service instance usage information"
help = `
Usage: consul usage instances [options]
Usage: consul operator usage instances [options]
Retrieves usage information about the number of services registered in a given
datacenter. By default, the datacenter of the local agent is queried.
To retrieve the service usage data:
$ consul usage instances
$ consul operator usage instances
To show only billable service instance counts:
$ consul usage instances -billable
$ consul operator usage instances -billable
To show only connect service instance counts:
$ consul usage instances -connect
$ consul operator usage instances -connect
For a full list of options and examples, please see the Consul documentation.
`
Expand Down
44 changes: 35 additions & 9 deletions command/operator/usage/instances/usage_instances_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package instances

import (
"errors"
"testing"

"github.com/hashicorp/consul/agent"
Expand Down Expand Up @@ -36,15 +37,40 @@ func TestUsageInstancesCommand(t *testing.T) {
t.Fatal(err)
}

ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
cases := []struct {
name string
extraArgs []string
output string
err error
}{
{
name: "basic output",
output: "Billable Service Instances Total: 2",
},
{
name: "billable and connect flags together are invalid",
extraArgs: []string{"-billable", "-connect"},
err: errors.New("Cannot specify both -billable and -connect"),
},
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad exit code %d: %s", code, ui.ErrorWriter.String())

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
}
args = append(args, tc.extraArgs...)

code := c.Run(args)
if tc.err != nil {
require.Equal(t, 1, code)
require.Contains(t, ui.ErrorWriter.String(), tc.err.Error())
} else {
require.Equal(t, 0, code)
require.Contains(t, ui.OutputWriter.String(), tc.output)
}
})
}
output := ui.OutputWriter.String()
require.Contains(t, output, "Billable Service Instances Total: 2")
}

0 comments on commit f1bef50

Please sign in to comment.