2020-10-05 13:18:30 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure ScalingPolicyListCommand satisfies the cli.Command interface.
|
|
|
|
var _ cli.Command = &ScalingPolicyListCommand{}
|
|
|
|
|
|
|
|
// ScalingPolicyListCommand implements cli.Command.
|
|
|
|
type ScalingPolicyListCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help satisfies the cli.Command Help function.
|
|
|
|
func (s *ScalingPolicyListCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad scaling policy list [options]
|
|
|
|
|
|
|
|
List is used to list the currently configured scaling policies.
|
|
|
|
|
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 namespace of all policies. Any namespaces
|
|
|
|
that the token does not have access to will have its policies filtered from
|
|
|
|
the results.
|
|
|
|
|
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:
|
|
|
|
|
2020-11-04 12:35:29 +00:00
|
|
|
-job
|
|
|
|
Specifies the job ID to filter the scaling policies list by.
|
|
|
|
|
2020-09-09 22:30:40 +00:00
|
|
|
-type
|
|
|
|
Filter scaling policies by type.
|
|
|
|
|
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 *ScalingPolicyListCommand) Synopsis() string {
|
|
|
|
return "Display all Nomad scaling policies"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScalingPolicyListCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(s.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
2021-02-04 15:08:13 +00:00
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
"-job": complete.PredictNothing,
|
|
|
|
"-type": complete.PredictNothing,
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
2020-10-05 13:18:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of this command.
|
|
|
|
func (s *ScalingPolicyListCommand) Name() string { return "scaling policy list" }
|
|
|
|
|
|
|
|
// Run satisfies the cli.Command Run function.
|
|
|
|
func (s *ScalingPolicyListCommand) Run(args []string) int {
|
2021-02-04 15:08:13 +00:00
|
|
|
var json, verbose bool
|
2020-09-09 22:30:40 +00:00
|
|
|
var tmpl, policyType, job string
|
2020-10-05 13:18:30 +00:00
|
|
|
|
|
|
|
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", "", "")
|
2020-09-09 22:30:40 +00:00
|
|
|
flags.StringVar(&policyType, "type", "", "")
|
|
|
|
flags.StringVar(&job, "job", "", "")
|
2020-10-05 13:18:30 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if args = flags.Args(); len(args) > 0 {
|
|
|
|
s.Ui.Error("This command takes no arguments")
|
|
|
|
s.Ui.Error(commandErrorText(s))
|
2021-02-03 21:29:44 +00:00
|
|
|
return 1
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 15:08:13 +00:00
|
|
|
// Truncate the id unless full length is requested
|
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:18:30 +00:00
|
|
|
// Get the HTTP client.
|
|
|
|
client, err := s.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:30:40 +00:00
|
|
|
q := &api.QueryOptions{
|
|
|
|
Params: map[string]string{},
|
|
|
|
}
|
|
|
|
if policyType != "" {
|
|
|
|
q.Params["type"] = policyType
|
|
|
|
}
|
|
|
|
if job != "" {
|
|
|
|
q.Params["job"] = job
|
|
|
|
}
|
|
|
|
policies, _, err := client.Scaling().ListPolicies(q)
|
2020-10-05 13:18:30 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(fmt.Sprintf("Error listing scaling policies: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, policies)
|
|
|
|
if err != nil {
|
|
|
|
s.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
s.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:08:13 +00:00
|
|
|
output := formatScalingPolicies(policies, length)
|
2021-02-03 21:29:44 +00:00
|
|
|
s.Ui.Output(output)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:08:13 +00:00
|
|
|
func formatScalingPolicies(stubs []*api.ScalingPolicyListStub, uuidLength int) string {
|
2021-02-03 21:29:44 +00:00
|
|
|
if len(stubs) == 0 {
|
|
|
|
return "No policies found"
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:18:30 +00:00
|
|
|
// Create the output table header.
|
2021-02-03 21:29:44 +00:00
|
|
|
policies := []string{"ID|Enabled|Type|Target"}
|
2020-10-05 13:18:30 +00:00
|
|
|
|
|
|
|
// Sort the list of policies based on their target.
|
2021-02-03 21:29:44 +00:00
|
|
|
sortedPolicies := scalingPolicyStubList{policies: stubs}
|
2020-10-05 13:18:30 +00:00
|
|
|
sort.Sort(sortedPolicies)
|
|
|
|
|
|
|
|
// Iterate the policies and add to the output.
|
|
|
|
for _, policy := range sortedPolicies.policies {
|
2021-02-03 21:29:44 +00:00
|
|
|
policies = append(policies, fmt.Sprintf(
|
2020-09-09 22:30:40 +00:00
|
|
|
"%s|%v|%s|%s",
|
2021-02-04 15:08:13 +00:00
|
|
|
limit(policy.ID, uuidLength),
|
|
|
|
policy.Enabled,
|
|
|
|
policy.Type,
|
|
|
|
formatScalingPolicyTarget(policy.Target)))
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|
2021-02-03 21:29:44 +00:00
|
|
|
return formatList(policies)
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scalingPolicyStubList is a wrapper around []*api.ScalingPolicyListStub that
|
|
|
|
// list us sort the policies alphabetically based on their target.
|
|
|
|
type scalingPolicyStubList struct {
|
|
|
|
policies []*api.ScalingPolicyListStub
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len satisfies the Len function of the sort.Interface interface.
|
|
|
|
func (s scalingPolicyStubList) Len() int { return len(s.policies) }
|
|
|
|
|
|
|
|
// Swap satisfies the Swap function of the sort.Interface interface.
|
|
|
|
func (s scalingPolicyStubList) Swap(i, j int) {
|
|
|
|
s.policies[i], s.policies[j] = s.policies[j], s.policies[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less satisfies the Less function of the sort.Interface interface.
|
|
|
|
func (s scalingPolicyStubList) Less(i, j int) bool {
|
|
|
|
|
|
|
|
iTarget := formatScalingPolicyTarget(s.policies[i].Target)
|
|
|
|
jTarget := formatScalingPolicyTarget(s.policies[j].Target)
|
|
|
|
|
|
|
|
stringList := []string{iTarget, jTarget}
|
|
|
|
sort.Strings(stringList)
|
|
|
|
|
2020-12-09 19:05:18 +00:00
|
|
|
return stringList[0] == iTarget
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|