Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nomad
Commits
bfec8a7e
Commit
bfec8a7e
authored
4 years ago
by
Chris Baker
Browse files
Options
Download
Email Patches
Plain Diff
updated "scaling policy info" with prefix search and auto-complete
parent
fbdbd773
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
command/scaling_policy_info.go
+75
-16
command/scaling_policy_info.go
command/scaling_policy_info_test.go
+4
-4
command/scaling_policy_info_test.go
command/scaling_policy_list.go
+15
-12
command/scaling_policy_list.go
with
94 additions
and
32 deletions
+94
-32
command/scaling_policy_info.go
+
75
-
16
View file @
bfec8a7e
...
...
@@ -6,6 +6,9 @@ import (
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
)
// Ensure ScalingPolicyInfoCommand satisfies the cli.Command interface.
...
...
@@ -54,6 +57,21 @@ func (s *ScalingPolicyInfoCommand) AutocompleteFlags() complete.Flags {
})
}
func
(
c
*
ScalingPolicyInfoCommand
)
AutocompleteArgs
()
complete
.
Predictor
{
return
complete
.
PredictFunc
(
func
(
a
complete
.
Args
)
[]
string
{
client
,
err
:=
c
.
Meta
.
Client
()
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
]
})
}
// Name returns the name of this command.
func
(
s
*
ScalingPolicyInfoCommand
)
Name
()
string
{
return
"scaling policy info"
}
...
...
@@ -70,37 +88,75 @@ func (s *ScalingPolicyInfoCommand) Run(args []string) int {
return
1
}
if
args
=
flags
.
Args
();
len
(
args
)
!=
1
{
s
.
Ui
.
Error
(
"This command takes one argument: <policy_id>"
)
s
.
Ui
.
Error
(
commandErrorText
(
s
))
// Get the HTTP client.
client
,
err
:=
s
.
Meta
.
Client
()
if
err
!=
nil
{
s
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error initializing client: %s"
,
err
))
return
1
}
// Get the policy ID.
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
}
policyID
:=
args
[
0
]
if
len
(
policyID
)
==
1
{
s
.
Ui
.
Error
(
"Identifier must contain at least two characters."
)
return
1
}
// Get the HTTP client.
client
,
err
:=
s
.
Meta
.
Client
()
policyID
=
sanitizeUUIDPrefix
(
policyID
)
policies
,
_
,
err
:=
client
.
Scaling
()
.
ListPolicies
(
&
api
.
QueryOptions
{
Prefix
:
policyID
,
})
if
err
!=
nil
{
s
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error
initi
ali
zi
ng
client
: %
s
"
,
err
))
s
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error
querying sc
aling
policy
: %
v
"
,
err
))
return
1
}
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
{
out
:=
formatScalingPolicies
(
policies
)
s
.
Ui
.
Output
(
fmt
.
Sprintf
(
"Prefix matched multiple scaling policies
\n\n
%s"
,
out
))
return
0
}
policy
,
_
,
err
:=
client
.
Scaling
()
.
GetPolicy
(
polic
y
ID
,
nil
)
policy
,
_
,
err
:=
client
.
Scaling
()
.
GetPolicy
(
polic
ies
[
0
]
.
ID
,
nil
)
if
err
!=
nil
{
s
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error
list
ing scaling polic
ies
: %s"
,
err
))
s
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error
query
ing scaling polic
y
: %s"
,
err
))
return
1
}
// If the user has specified to output the policy as JSON or using a
// template then perform this action for the entire object and exit the
// command.
if
json
||
len
(
tmpl
)
>
0
{
out
,
err
:=
Format
(
json
,
tmpl
,
policy
)
if
err
!=
nil
{
s
.
Ui
.
Error
(
err
.
Error
())
return
1
}
s
.
Ui
.
Output
(
out
)
return
0
}
...
...
@@ -109,10 +165,13 @@ func (s *ScalingPolicyInfoCommand) Run(args []string) int {
// 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.
out
,
err
:=
Format
(
true
,
""
,
policy
.
Policy
)
if
err
!=
nil
{
s
.
Ui
.
Error
(
err
.
Error
())
return
1
out
:=
"<empty>"
if
len
(
policy
.
Policy
)
>
0
{
out
,
err
=
Format
(
true
,
""
,
policy
.
Policy
)
if
err
!=
nil
{
s
.
Ui
.
Error
(
err
.
Error
())
return
1
}
}
info
:=
[]
string
{
...
...
This diff is collapsed.
Click to expand it.
command/scaling_policy_info_test.go
+
4
-
4
View file @
bfec8a7e
...
...
@@ -35,10 +35,10 @@ 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
{
if
code
:=
cmd
.
Run
([]
string
{
"-address="
+
url
,
"first"
,
"second"
});
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
argument: <policy_id>
"
)
{
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
)
}
...
...
@@ -46,8 +46,8 @@ func TestScalingPolicyInfoCommand_Run(t *testing.T) {
if
code
:=
cmd
.
Run
([]
string
{
"-address="
+
url
,
"scaling_policy_info"
});
code
!=
1
{
t
.
Fatalf
(
"expected cmd run exit code 1, got: %d"
,
code
)
}
if
out
:=
ui
.
ErrorWriter
.
String
();
!
strings
.
Contains
(
out
,
"404 (policy not
found
)"
)
{
t
.
Fatalf
(
"expected
404 not
found within output: %v"
,
out
)
if
out
:=
ui
.
ErrorWriter
.
String
();
!
strings
.
Contains
(
out
,
`No scaling policies with prefix or id "scaling_policy_inf"
found
`
)
{
t
.
Fatalf
(
"expected
'no policies
found
'
within output: %v"
,
out
)
}
// Generate a test job.
...
...
This diff is collapsed.
Click to expand it.
command/scaling_policy_list.go
+
15
-
12
View file @
bfec8a7e
...
...
@@ -87,6 +87,7 @@ func (s *ScalingPolicyListCommand) Run(args []string) int {
if
args
=
flags
.
Args
();
len
(
args
)
>
0
{
s
.
Ui
.
Error
(
"This command takes no arguments"
)
s
.
Ui
.
Error
(
commandErrorText
(
s
))
return
1
}
// Get the HTTP client.
...
...
@@ -111,11 +112,6 @@ func (s *ScalingPolicyListCommand) Run(args []string) int {
return
1
}
if
len
(
policies
)
==
0
{
s
.
Ui
.
Output
(
"No policies found"
)
return
0
}
if
json
||
len
(
tmpl
)
>
0
{
out
,
err
:=
Format
(
json
,
tmpl
,
policies
)
if
err
!=
nil
{
...
...
@@ -126,23 +122,30 @@ func (s *ScalingPolicyListCommand) Run(args []string) int {
return
0
}
output
:=
formatScalingPolicies
(
policies
)
s
.
Ui
.
Output
(
output
)
return
0
}
func
formatScalingPolicies
(
stubs
[]
*
api
.
ScalingPolicyListStub
)
string
{
if
len
(
stubs
)
==
0
{
return
"No policies found"
}
// Create the output table header.
output
:=
[]
string
{
"ID|Enabled|Type|Target"
}
policies
:=
[]
string
{
"ID|Enabled|Type|Target"
}
// Sort the list of policies based on their target.
sortedPolicies
:=
scalingPolicyStubList
{
policies
:
policie
s
}
sortedPolicies
:=
scalingPolicyStubList
{
policies
:
stub
s
}
sort
.
Sort
(
sortedPolicies
)
// Iterate the policies and add to the output.
for
_
,
policy
:=
range
sortedPolicies
.
policies
{
output
=
append
(
output
,
fmt
.
Sprintf
(
policies
=
append
(
policies
,
fmt
.
Sprintf
(
"%s|%v|%s|%s"
,
policy
.
ID
,
policy
.
Enabled
,
policy
.
Type
,
formatScalingPolicyTarget
(
policy
.
Target
)))
}
// Output.
s
.
Ui
.
Output
(
formatList
(
output
))
return
0
return
formatList
(
policies
)
}
// scalingPolicyStubList is a wrapper around []*api.ScalingPolicyListStub that
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help