| 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 |  |  | import six | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | import pecan | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | from mongoengine import ValidationError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | from st2common import log as logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | from st2common.models.api.base import jsexpose | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | from st2common.models.api.action import RunnerTypeAPI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | from st2common.persistence.runner import RunnerType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | from st2api.controllers.resource import ResourceController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | from st2common.rbac.types import PermissionType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | from st2common.rbac.decorators import request_user_has_permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | from st2common.rbac.decorators import request_user_has_resource_db_permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | http_client = six.moves.http_client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | LOG = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class RunnerTypesController(ResourceController): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         Implements the RESTful web endpoint that handles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         the lifecycle of an RunnerType in the system. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     model = RunnerTypeAPI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     access = RunnerType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     supported_filters = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         'name': 'name' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     query_options = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         'sort': ['name'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     @request_user_has_permission(permission_type=PermissionType.RUNNER_LIST) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     @jsexpose() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     def get_all(self, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return super(RunnerTypesController, self)._get_all(**kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     @request_user_has_resource_db_permission(permission_type=PermissionType.RUNNER_VIEW) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     @jsexpose(arg_types=[str]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     def get_one(self, name_or_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return super(RunnerTypesController, self)._get_one_by_name_or_id(name_or_id) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     @request_user_has_resource_db_permission(permission_type=PermissionType.RUNNER_MODIFY) | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |     @jsexpose(arg_types=[str], body_cls=RunnerTypeAPI) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |     def put(self, name_or_id, runner_type_api): | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         # Note: We only allow "enabled" attribute of the runner to be changed | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         runner_type_db = self._get_by_name_or_id(name_or_id=name_or_id) | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         old_runner_type_db = runner_type_db | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         LOG.debug('PUT /runnertypes/ lookup with id=%s found object: %s', name_or_id, | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |                   runner_type_db) | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         try: | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             if runner_type_api.id and runner_type_api.id != name_or_id: | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |                 LOG.warning('Discarding mismatched id=%s found in payload and using uri_id=%s.', | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |                             runner_type_api.id, name_or_id) | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |             runner_type_db.enabled = runner_type_api.enabled | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |             runner_type_db = RunnerType.add_or_update(runner_type_db) | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         except (ValidationError, ValueError) as e: | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |             LOG.exception('Validation failed for runner type data=%s', runner_type_api) | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |             pecan.abort(http_client.BAD_REQUEST, str(e)) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |             return | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         extra = {'old_runner_type_db': old_runner_type_db, 'new_runner_type_db': runner_type_db} | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         LOG.audit('Runner Type updated. RunnerType.id=%s.' % (runner_type_db.id), extra=extra) | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         runner_type_api = RunnerTypeAPI.from_model(runner_type_db) | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |         return runner_type_api | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |