| 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 st2common.exceptions.actionalias import ActionAliasAmbiguityException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | from st2common.models.utils.action_alias_utils import extract_parameters_for_action_alias_db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | from st2common.util.actionalias_matching import match_command_to_alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | from st2client import models | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | from st2client.models.action_alias import ActionAlias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | from st2client.models.action import LiveAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | from st2client.commands import resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | from st2client.formatters import table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     'ActionAliasBranch' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class ActionAliasBranch(resource.ResourceBranch): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     def __init__(self, description, app, subparsers, parent_parser=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         super(ActionAliasBranch, self).__init__( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ActionAlias, description, app, subparsers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             parent_parser=parent_parser, read_only=False, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             commands={ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 'list': ActionAliasListCommand, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 'get': ActionAliasGetCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self.commands['match'] = ActionAliasMatchCommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             self.resource, self.app, self.subparsers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             add_help=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         self.commands['execute'] = ActionAliasExecuteCommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             LiveAction, self.app, self.subparsers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             add_help=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | class ActionAliasListCommand(resource.ContentPackResourceListCommand): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     display_attributes = ['ref', 'pack', 'description', 'enabled'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | class ActionAliasGetCommand(resource.ContentPackResourceGetCommand): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     display_attributes = ['all'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     attribute_display_order = ['id', 'ref', 'pack', 'name', 'description', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                                'enabled', 'action_ref', 'formats'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | class ActionAliasMatchCommand(resource.ResourceCommand): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     display_attributes = ['id', 'name', 'description'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     def __init__(self, resource, *args, **kwargs): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         super(ActionAliasMatchCommand, self).__init__( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             resource, 'match', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'Get the list of %s that match the command text.' % | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             resource.get_plural_display_name().lower(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             *args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         self.parser.add_argument('match_text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                                  metavar='command', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                                  help=help) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         self.parser.add_argument('-a', '--attr', nargs='+', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                                  default=self.display_attributes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                                  help=('List of attributes to include in the ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                                        'output. "all" will return all ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                                        'attributes.')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         self.parser.add_argument('-w', '--width', nargs='+', type=int, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                                  default=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                                  help=('Set the width of columns in output.')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     @resource.add_auth_token_to_kwargs_from_cli | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     def run(self, args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         aliases = self.manager.get_all(**kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         matches = match_command_to_alias(args.match_text, aliases) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return [match[0] for match in matches]  # show only alias objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def run_and_print(self, args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         instances = self.run(args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         self.print_output(instances, table.MultiColumnTable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                           attributes=args.attr, widths=args.width, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                           json=args.json, yaml=args.yaml) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | class ActionAliasExecuteCommand(resource.ResourceCommand): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     display_attributes = ['id', 'name', 'description'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     def __init__(self, resource, *args, **kwargs): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         super(ActionAliasExecuteCommand, self).__init__( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             resource, 'execute', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             'Execute the command text by finding a matching ActionAlias.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             *args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         self.parser.add_argument('match_text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                                  metavar='command', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                                  help=help) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         self.parser.add_argument('-a', '--attr', nargs='+', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                                  default=self.display_attributes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                                  help=('List of attributes to include in the ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                                        'output. "all" will return all ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                                        'attributes.')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         self.parser.add_argument('--trace-tag', '--trace_tag', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                                  help='A trace tag string to track execution later.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                                  dest='trace_tag', required=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         self.parser.add_argument('--trace-id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                                  help='Existing trace id for this execution.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                                  dest='trace_id', required=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         self.parser.add_argument('-a', '--async', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                                  action='store_true', dest='async', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                                  help='Do not wait for action to finish.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         self.parser.add_argument('-u', '--user', type=str, default=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                                  help='User under which to run the action (admins only).') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         self.parser.add_argument('-w', '--width', nargs='+', type=int, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                                  default=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                                  help=('Set the width of columns in output.')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     @resource.add_auth_token_to_kwargs_from_cli | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     def run(self, args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         aliases = self.manager.get_all(**kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         matches = match_command_to_alias(args.match_text, aliases) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if len(matches) > 1: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             raise ActionAliasAmbiguityException("Too many matches for provided command", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                                                 matches=matches) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         elif len(matches) == 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             raise ActionAliasAmbiguityException("Could not locate an ActionAlias with a " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                                                 "matching format command", matches=matches) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         match = matches[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         action_alias_db = match[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if not action_alias_db.enabled: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             raise ValueError('Action alias with name "%s" is disabled.' % | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                              (action_alias_db.ref)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         execution_parameters = extract_parameters_for_action_alias_db( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             action_alias_db=action_alias_db, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             format_str=matches[2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             param_stream=args.match_text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         execution = models.LiveAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         execution.action = action_alias_db.action_ref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         execution.parameters = execution_parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         execution.user = args.user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         if not args.trace_id and args.trace_tag: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             execution.context = {'trace_context': {'trace_tag': args.trace_tag}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         if args.trace_id: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             execution.context = {'trace_context': {'id_': args.trace_id}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         action_exec_mgr = self.app.client.managers['LiveAction'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         execution = action_exec_mgr.create(execution, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         execution = self._get_execution_result(execution=execution, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                                                action_exec_mgr=action_exec_mgr, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                                                args=args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         return execution | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |     def run_and_print(self, args, **kwargs): | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |         instances = self.run(args, **kwargs) | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |         self.print_output(instances, table.MultiColumnTable, | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |                           attributes=args.attr, widths=args.width, | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |                           json=args.json, yaml=args.yaml) | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  | 
            
                        
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: