1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
from st2client.models.action_alias import ActionAlias |
17
|
|
|
from st2client.models.aliasexecution import ActionAliasExecution |
18
|
|
|
from st2client.commands import resource |
19
|
|
|
from st2client.formatters import table |
20
|
|
|
|
21
|
|
|
__all__ = [ |
22
|
|
|
'ActionAliasBranch' |
23
|
|
|
] |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class ActionAliasBranch(resource.ResourceBranch): |
27
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
28
|
|
|
super(ActionAliasBranch, self).__init__( |
29
|
|
|
ActionAlias, description, app, subparsers, |
30
|
|
|
parent_parser=parent_parser, read_only=False, |
31
|
|
|
commands={ |
32
|
|
|
'list': ActionAliasListCommand, |
33
|
|
|
'get': ActionAliasGetCommand |
34
|
|
|
}) |
35
|
|
|
|
36
|
|
|
self.commands['match'] = ActionAliasMatchCommand( |
37
|
|
|
self.resource, self.app, self.subparsers, |
38
|
|
|
add_help=False) |
39
|
|
|
self.commands['execute'] = ActionAliasExecuteCommand( |
40
|
|
|
self.resource, self.app, self.subparsers, |
41
|
|
|
add_help=False) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class ActionAliasListCommand(resource.ContentPackResourceListCommand): |
45
|
|
|
display_attributes = ['ref', 'pack', 'description', 'enabled'] |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
class ActionAliasGetCommand(resource.ContentPackResourceGetCommand): |
49
|
|
|
display_attributes = ['all'] |
50
|
|
|
attribute_display_order = ['id', 'ref', 'pack', 'name', 'description', |
51
|
|
|
'enabled', 'action_ref', 'formats'] |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
class ActionAliasMatchCommand(resource.ResourceCommand): |
55
|
|
|
display_attributes = ['id', 'name', 'description'] |
56
|
|
|
|
57
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
58
|
|
|
super(ActionAliasMatchCommand, self).__init__( |
59
|
|
|
resource, 'match', |
60
|
|
|
'Get the list of %s that match the command text.' % |
61
|
|
|
resource.get_plural_display_name().lower(), |
62
|
|
|
*args, **kwargs) |
63
|
|
|
|
64
|
|
|
self.parser.add_argument('match_text', |
65
|
|
|
metavar='command', |
66
|
|
|
help=help) |
67
|
|
|
|
68
|
|
|
self.parser.add_argument('-a', '--attr', nargs='+', |
69
|
|
|
default=self.display_attributes, |
70
|
|
|
help=('List of attributes to include in the ' |
71
|
|
|
'output. "all" will return all ' |
72
|
|
|
'attributes.')) |
73
|
|
|
self.parser.add_argument('-w', '--width', nargs='+', type=int, |
74
|
|
|
default=None, |
75
|
|
|
help=('Set the width of columns in output.')) |
76
|
|
|
|
77
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
78
|
|
|
def run(self, args, **kwargs): |
79
|
|
|
matches = self.manager.match(args.match_text, **kwargs) |
80
|
|
|
return [match['actionalias'] for match in matches] # show only alias objects |
81
|
|
|
|
82
|
|
|
def run_and_print(self, args, **kwargs): |
83
|
|
|
instances = self.run(args, **kwargs) |
84
|
|
|
self.print_output(instances, table.MultiColumnTable, |
85
|
|
|
attributes=args.attr, widths=args.width, |
86
|
|
|
json=args.json, yaml=args.yaml) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
class ActionAliasExecuteCommand(resource.ResourceCommand): |
90
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
91
|
|
|
super(ActionAliasExecuteCommand, self).__init__( |
92
|
|
|
resource, 'execute', |
93
|
|
|
'Execute the command text by finding a matching ActionAlias.', |
94
|
|
|
*args, **kwargs) |
95
|
|
|
|
96
|
|
|
self.parser.add_argument('command_text', |
97
|
|
|
metavar='command', |
98
|
|
|
help=help) |
99
|
|
|
self.parser.add_argument('-h', '--help', |
100
|
|
|
action='store_true', dest='help', |
101
|
|
|
help='Print usage for the given action.') |
102
|
|
|
self.parser.add_argument('--trace-tag', '--trace_tag', |
103
|
|
|
help='A trace tag string to track execution later.', |
104
|
|
|
dest='trace_tag', required=False) |
105
|
|
|
self.parser.add_argument('--trace-id', |
106
|
|
|
help='Existing trace id for this execution.', |
107
|
|
|
dest='trace_id', required=False) |
108
|
|
|
self.parser.add_argument('-a', '--async', |
109
|
|
|
action='store_true', dest='async', |
110
|
|
|
help='Do not wait for action to finish.') |
111
|
|
|
self.parser.add_argument('-u', '--user', type=str, default=None, |
112
|
|
|
help='User under which to run the action (admins only).') |
113
|
|
|
|
114
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
115
|
|
|
def run(self, args, **kwargs): |
116
|
|
|
matches = self.manager.match(args.match_text, **kwargs) |
117
|
|
|
|
118
|
|
|
match = matches[0] |
119
|
|
|
action_alias = match['actionalias'] |
120
|
|
|
|
121
|
|
|
execution = ActionAliasExecution() |
122
|
|
|
execution.name = action_alias['name'] |
123
|
|
|
execution.format = match['representation'] |
124
|
|
|
execution.command = args.match_text |
125
|
|
|
execution.source_channel = 'cli' # ? |
126
|
|
|
execution.notification_channel = None |
127
|
|
|
execution.notification_route = None |
128
|
|
|
execution.user = args.user |
129
|
|
|
|
130
|
|
|
action_exec_mgr = self.app.client.managers['ActionAliasExecution'] |
131
|
|
|
|
132
|
|
|
execution = action_exec_mgr.create(execution, **kwargs) |
133
|
|
|
|
134
|
|
|
return execution |
135
|
|
|
|
136
|
|
|
def run_and_print(self, args, **kwargs): |
137
|
|
|
instances = self.run(args, **kwargs) |
138
|
|
|
self.print_output(instances, table.MultiColumnTable, |
139
|
|
|
attributes=args.attr, widths=args.width, |
140
|
|
|
json=args.json, yaml=args.yaml) |
141
|
|
|
|
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: