2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-05 13:18:30 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
2021-02-03 21:29:44 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
2020-10-05 13:18:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure ScalingPolicyInfoCommand satisfies the cli.Command interface.
|
|
|
|
var _ cli.Command = &ScalingPolicyInfoCommand{}
|
|
|
|
|
|
|
|
// ScalingPolicyListCommand implements cli.Command.
|
|
|
|
type ScalingPolicyInfoCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help satisfies the cli.Command Help function.
|
|
|
|
func (s *ScalingPolicyInfoCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad scaling policy info [options] <policy_id>
|
|
|
|
|
|
|
|
Info is used to read the specified scaling policy.
|
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this command requires a token with the 'read-job' and
|
|
|
|
'list-jobs' capabilities for the policy's namespace.
|
|
|
|
|
2020-10-05 13:18:30 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault) + `
|
2020-10-05 13:18:30 +00:00
|
|
|
|
|
|
|
Policy Info Options:
|
|
|
|
|
2021-02-04 15:08:13 +00:00
|
|
|
-verbose
|
|
|
|
Display full information.
|
|
|
|
|
2020-10-05 13:18:30 +00:00
|
|
|
-json
|
|
|
|
Output the scaling policy in its JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display the scaling policy using a Go template.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synopsis satisfies the cli.Command Synopsis function.
|
|
|
|
func (s *ScalingPolicyInfoCommand) Synopsis() string {
|
|
|
|
return "Display an individual Nomad scaling policy"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScalingPolicyInfoCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(s.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
2021-02-04 15:08:13 +00:00
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
2020-10-05 13:18:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:44:21 +00:00
|
|
|
func (s *ScalingPolicyInfoCommand) AutocompleteArgs() complete.Predictor {
|
2021-02-03 21:29:44 +00:00
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
2021-12-20 10:44:21 +00:00
|
|
|
client, err := s.Meta.Client()
|
2021-02-03 21:29:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.ScalingPolicies, nil)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.ScalingPolicies]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:18:30 +00:00
|
|
|
// Name returns the name of this command.
|
|
|
|
func (s *ScalingPolicyInfoCommand) Name() string { return "scaling policy info" }
|
|
|
|
|
|
|
|
// Run satisfies the cli.Command Run function.
|
|
|
|
func (s *ScalingPolicyInfoCommand) Run(args []string) int {
|
2021-02-04 15:08:13 +00:00
|
|
|
var json, verbose bool
|
2020-10-05 13:18:30 +00:00
|
|
|
var tmpl string
|
|
|
|
|
|
|
|
flags := s.Meta.FlagSet(s.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { s.Ui.Output(s.Help()) }
|
2021-02-04 15:08:13 +00:00
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
2020-10-05 13:18:30 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:08:13 +00:00
|
|
|
// Truncate the id unless full length is requested
|
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:29:44 +00:00
|
|
|
// Get the HTTP client.
|
|
|
|
client, err := s.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
2020-10-05 13:18:30 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:29:44 +00:00
|
|
|
args = flags.Args()
|
|
|
|
|
|
|
|
// Formatted list mode if no policy ID
|
|
|
|
if len(args) == 0 && (json || len(tmpl) > 0) {
|
|
|
|
policies, _, err := client.Scaling().ListPolicies(nil)
|
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(fmt.Sprintf("Error listing scaling policies: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
out, err := Format(json, tmpl, policies)
|
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
s.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
s.Ui.Error("This command takes one of the following argument conditions:")
|
|
|
|
s.Ui.Error(" * A single <policy_id>")
|
|
|
|
s.Ui.Error(" * No arguments, with output format specified")
|
|
|
|
s.Ui.Error(commandErrorText(s))
|
|
|
|
return 1
|
|
|
|
}
|
2020-10-05 13:18:30 +00:00
|
|
|
policyID := args[0]
|
2021-02-03 21:29:44 +00:00
|
|
|
if len(policyID) == 1 {
|
|
|
|
s.Ui.Error("Identifier must contain at least two characters.")
|
|
|
|
return 1
|
|
|
|
}
|
2020-10-05 13:18:30 +00:00
|
|
|
|
2021-02-03 21:29:44 +00:00
|
|
|
policyID = sanitizeUUIDPrefix(policyID)
|
|
|
|
policies, _, err := client.Scaling().ListPolicies(&api.QueryOptions{
|
|
|
|
Prefix: policyID,
|
|
|
|
})
|
2020-10-05 13:18:30 +00:00
|
|
|
if err != nil {
|
2021-02-03 21:29:44 +00:00
|
|
|
s.Ui.Error(fmt.Sprintf("Error querying scaling policy: %v", err))
|
2020-10-05 13:18:30 +00:00
|
|
|
return 1
|
|
|
|
}
|
2021-02-03 21:29:44 +00:00
|
|
|
if len(policies) == 0 {
|
|
|
|
s.Ui.Error(fmt.Sprintf("No scaling policies with prefix or id %q found", policyID))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(policies) > 1 {
|
2021-02-04 15:08:13 +00:00
|
|
|
out := formatScalingPolicies(policies, length)
|
|
|
|
s.Ui.Error(fmt.Sprintf("Prefix matched multiple scaling policies\n\n%s", out))
|
2021-02-03 21:29:44 +00:00
|
|
|
return 0
|
|
|
|
}
|
2020-10-05 13:18:30 +00:00
|
|
|
|
2021-02-03 21:29:44 +00:00
|
|
|
policy, _, err := client.Scaling().GetPolicy(policies[0].ID, nil)
|
2020-10-05 13:18:30 +00:00
|
|
|
if err != nil {
|
2021-02-03 21:29:44 +00:00
|
|
|
s.Ui.Error(fmt.Sprintf("Error querying scaling policy: %s", err))
|
2020-10-05 13:18:30 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, policy)
|
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2021-02-03 21:29:44 +00:00
|
|
|
|
2020-10-05 13:18:30 +00:00
|
|
|
s.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the policy document which is a freeform map[string]interface{}
|
|
|
|
// and therefore can only be made pretty to a certain extent. Do this
|
|
|
|
// before the rest of the formatting so any errors are clearly passed back
|
|
|
|
// to the CLI.
|
2021-02-03 21:29:44 +00:00
|
|
|
out := "<empty>"
|
|
|
|
if len(policy.Policy) > 0 {
|
|
|
|
out, err = Format(true, "", policy.Policy)
|
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info := []string{
|
2021-02-04 15:08:13 +00:00
|
|
|
fmt.Sprintf("ID|%s", limit(policy.ID, length)),
|
2020-10-05 13:18:30 +00:00
|
|
|
fmt.Sprintf("Enabled|%v", *policy.Enabled),
|
|
|
|
fmt.Sprintf("Target|%s", formatScalingPolicyTarget(policy.Target)),
|
|
|
|
fmt.Sprintf("Min|%v", *policy.Min),
|
|
|
|
fmt.Sprintf("Max|%v", *policy.Max),
|
|
|
|
}
|
|
|
|
s.Ui.Output(formatKV(info))
|
|
|
|
s.Ui.Output("\nPolicy:")
|
|
|
|
s.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|