scaling policy -verbose flag, plus testing and other recommendations from review

This commit is contained in:
Chris Baker 2021-02-04 15:08:13 +00:00
parent 452b738db1
commit 84f1902296
3 changed files with 55 additions and 14 deletions

View File

@ -35,6 +35,9 @@ General Options:
Policy Info Options:
-verbose
Display full information.
-json
Output the scaling policy in its JSON format.
@ -52,8 +55,9 @@ func (s *ScalingPolicyInfoCommand) Synopsis() string {
func (s *ScalingPolicyInfoCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(s.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
"-verbose": complete.PredictNothing,
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
})
}
@ -77,17 +81,24 @@ func (s *ScalingPolicyInfoCommand) Name() string { return "scaling policy info"
// Run satisfies the cli.Command Run function.
func (s *ScalingPolicyInfoCommand) Run(args []string) int {
var json bool
var json, verbose bool
var tmpl string
flags := s.Meta.FlagSet(s.Name(), FlagSetClient)
flags.Usage = func() { s.Ui.Output(s.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
if err := flags.Parse(args); err != nil {
return 1
}
// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}
// Get the HTTP client.
client, err := s.Meta.Client()
if err != nil {
@ -139,8 +150,8 @@ func (s *ScalingPolicyInfoCommand) Run(args []string) int {
return 1
}
if len(policies) > 1 {
out := formatScalingPolicies(policies)
s.Ui.Output(fmt.Sprintf("Prefix matched multiple scaling policies\n\n%s", out))
out := formatScalingPolicies(policies, length)
s.Ui.Error(fmt.Sprintf("Prefix matched multiple scaling policies\n\n%s", out))
return 0
}
@ -175,7 +186,7 @@ func (s *ScalingPolicyInfoCommand) Run(args []string) int {
}
info := []string{
fmt.Sprintf("ID|%s", policy.ID),
fmt.Sprintf("ID|%s", limit(policy.ID, length)),
fmt.Sprintf("Enabled|%v", *policy.Enabled),
fmt.Sprintf("Target|%s", formatScalingPolicyTarget(policy.Target)),
fmt.Sprintf("Min|%v", *policy.Min),

View File

@ -35,6 +35,14 @@ func TestScalingPolicyInfoCommand_Run(t *testing.T) {
cmd := &ScalingPolicyInfoCommand{Meta: Meta{Ui: ui}}
// Calling without the policyID should result in an error.
if code := cmd.Run([]string{"-address=" + url}); code != 1 {
t.Fatalf("expected cmd run exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one of the following argument conditions") {
t.Fatalf("expected argument error within output: %v", out)
}
// Calling with more than one argument should result in an error.
if code := cmd.Run([]string{"-address=" + url, "first", "second"}); code != 1 {
t.Fatalf("expected cmd run exit code 1, got: %d", code)
}
@ -85,4 +93,12 @@ func TestScalingPolicyInfoCommand_Run(t *testing.T) {
if out := ui.OutputWriter.String(); !strings.Contains(out, "Policy:") {
t.Fatalf("expected policy ID within output: %v", out)
}
prefix := policies[0].ID[:2]
if code := cmd.Run([]string{"-address=" + url, prefix}); code != 0 {
t.Fatalf("expected cmd run exit code 0, got: %d", code)
}
if out := ui.OutputWriter.String(); !strings.Contains(out, "Policy:") {
t.Fatalf("expected policy ID within output: %v", out)
}
}

View File

@ -42,6 +42,9 @@ Policy Info Options:
-type
Filter scaling policies by type.
-verbose
Display full information.
-json
Output the scaling policy in its JSON format.
@ -59,10 +62,11 @@ func (s *ScalingPolicyListCommand) Synopsis() string {
func (s *ScalingPolicyListCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(s.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-job": complete.PredictNothing,
"-type": complete.PredictNothing,
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
"-verbose": complete.PredictNothing,
"-job": complete.PredictNothing,
"-type": complete.PredictNothing,
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
})
}
@ -71,11 +75,12 @@ func (s *ScalingPolicyListCommand) Name() string { return "scaling policy list"
// Run satisfies the cli.Command Run function.
func (s *ScalingPolicyListCommand) Run(args []string) int {
var json bool
var json, verbose bool
var tmpl, policyType, job string
flags := s.Meta.FlagSet(s.Name(), FlagSetClient)
flags.Usage = func() { s.Ui.Output(s.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
flags.StringVar(&policyType, "type", "", "")
@ -90,6 +95,12 @@ func (s *ScalingPolicyListCommand) Run(args []string) int {
return 1
}
// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}
// Get the HTTP client.
client, err := s.Meta.Client()
if err != nil {
@ -122,12 +133,12 @@ func (s *ScalingPolicyListCommand) Run(args []string) int {
return 0
}
output := formatScalingPolicies(policies)
output := formatScalingPolicies(policies, length)
s.Ui.Output(output)
return 0
}
func formatScalingPolicies(stubs []*api.ScalingPolicyListStub) string {
func formatScalingPolicies(stubs []*api.ScalingPolicyListStub, uuidLength int) string {
if len(stubs) == 0 {
return "No policies found"
}
@ -143,7 +154,10 @@ func formatScalingPolicies(stubs []*api.ScalingPolicyListStub) string {
for _, policy := range sortedPolicies.policies {
policies = append(policies, fmt.Sprintf(
"%s|%v|%s|%s",
policy.ID, policy.Enabled, policy.Type, formatScalingPolicyTarget(policy.Target)))
limit(policy.ID, uuidLength),
policy.Enabled,
policy.Type,
formatScalingPolicyTarget(policy.Target)))
}
return formatList(policies)
}