def test_merge_action_runner_params_meta(self): required, optional, immutable = action_param_utils.get_params_view( action_db=ActionParamsUtilsTest.action_db, runner_db=ActionParamsUtilsTest.runnertype_db) merged = {} merged.update(required) merged.update(optional) merged.update(immutable) consolidated = action_param_utils.get_params_view( action_db=ActionParamsUtilsTest.action_db, runner_db=ActionParamsUtilsTest.runnertype_db, merged_only=True) # Validate that merged_only view works. self.assertEqual(merged, consolidated) # Validate required params. self.assertEqual(len(required), 1, 'Required should contain only one param.') self.assertTrue('actionstr' in required, 'actionstr param is a required param.') self.assertTrue('actionstr' not in optional and 'actionstr' not in immutable and 'actionstr' in merged) # Validate immutable params. self.assertTrue('runnerimmutable' in immutable, 'runnerimmutable should be in immutable.') self.assertTrue('actionimmutable' in immutable, 'actionimmutable should be in immutable.') # Validate optional params. for opt in optional: self.assertTrue(opt not in required and opt not in immutable and opt in merged, 'Optional parameter %s failed validation.' % opt)
def test_merge_action_runner_params_meta(self): required, optional, immutable = action_param_utils.get_params_view( action_db=ActionParamsUtilsTest.action_db, runner_db=ActionParamsUtilsTest.runnertype_db) merged = {} merged.update(required) merged.update(optional) merged.update(immutable) consolidated = action_param_utils.get_params_view( action_db=ActionParamsUtilsTest.action_db, runner_db=ActionParamsUtilsTest.runnertype_db, merged_only=True) # Validate that merged_only view works. self.assertEqual(merged, consolidated) # Validate required params. self.assertEqual(len(required), 1, 'Required should contain only one param.') self.assertTrue('actionstr' in required, 'actionstr param is a required param.') self.assertTrue('actionstr' not in optional and 'actionstr' not in immutable and 'actionstr' in merged) # Validate immutable params. self.assertTrue('runnerimmutable' in immutable, 'runnerimmutable should be in immutable.') self.assertTrue('actionimmutable' in immutable, 'actionimmutable should be in immutable.') # Validate optional params. for opt in optional: self.assertTrue(opt not in required and opt not in immutable and opt in merged, 'Optional parameter %s failed validation.' % opt)
def _get_one(ref_or_id, requester_user): """ List merged action & runner parameters by action id or ref. Handle: GET /actions/views/parameters/1 """ if ResourceReference.is_resource_reference(ref_or_id): action_db = LookupUtils._get_action_by_ref(ref_or_id) else: action_db = LookupUtils._get_action_by_id(ref_or_id) permission_type = PermissionType.ACTION_VIEW rbac_utils = get_rbac_backend().get_utils_class() rbac_utils.assert_user_has_resource_db_permission( user_db=requester_user, resource_db=action_db, permission_type=permission_type, ) runner_db = LookupUtils._get_runner_by_name( action_db.runner_type["name"]) all_params = action_param_utils.get_params_view(action_db=action_db, runner_db=runner_db, merged_only=True) return {"parameters": all_params}
def test_merge_action_runner_params_meta(self): required, optional, immutable = action_param_utils.get_params_view( action_db=self.action_dbs["action-1"], runner_db=self.runnertype_dbs["test-runner-1"], ) merged = {} merged.update(required) merged.update(optional) merged.update(immutable) consolidated = action_param_utils.get_params_view( action_db=self.action_dbs["action-1"], runner_db=self.runnertype_dbs["test-runner-1"], merged_only=True, ) # Validate that merged_only view works. self.assertEqual(merged, consolidated) # Validate required params. self.assertEqual(len(required), 1, "Required should contain only one param.") self.assertIn("actionstr", required, "actionstr param is a required param.") self.assertNotIn("actionstr", optional, "actionstr should not be in optional parameters") self.assertNotIn("actionstr", immutable, "actionstr should not be in immutable parameters") self.assertIn("actionstr", merged, "actionstr should be in action parameters") # Validate immutable params. self.assertIn("runnerimmutable", immutable, "runnerimmutable should be in immutable.") self.assertIn("actionimmutable", immutable, "actionimmutable should be in immutable.") # Validate optional params. for opt in optional: self.assertIn(opt, merged, "Optional %s should be in action parameters" % opt) self.assertNotIn( opt, required, "Optional %s should not be in required params" % opt) self.assertNotIn( opt, immutable, "Optional %s should not be in immutable params" % opt)
def get_all( self, exclude_attributes=None, include_attributes=None, sort=None, offset=0, limit=None, requester_user=None, **raw_filters, ): """ List all actions. Handles requests: GET /actions/views/overview """ resp = super(OverviewController, self)._get_all( exclude_fields=exclude_attributes, include_fields=include_attributes, sort=sort, offset=offset, limit=limit, raw_filters=raw_filters, requester_user=requester_user, ) runner_type_names = set([]) action_ids = [] result = [] for item in resp.json: action_api = ActionAPI(**item) result.append(action_api) runner_type_names.add(action_api.runner_type) action_ids.append(str(action_api.id)) # Add combined runner and action parameters to the compound result object # NOTE: This approach results in 2 additional queries while previous one resulted in # N * 2 additional queries # 1. Retrieve all the respective runner objects - we only need parameters runner_type_dbs = RunnerType.query( name__in=runner_type_names, only_fields=["name", "runner_parameters"]) runner_type_dbs = dict([(runner_db.name, runner_db) for runner_db in runner_type_dbs]) # 2. Retrieve all the respective action objects - we only need parameters action_dbs = dict([(action_db.id, action_db) for action_db in result]) for action_api in result: action_db = action_dbs.get(action_api.id, None) runner_db = runner_type_dbs.get(action_api.runner_type, None) all_params = action_param_utils.get_params_view( action_db=action_db, runner_db=runner_db, merged_only=True) action_api.parameters = all_params resp.json = result return resp
def test_merge_action_runner_params_meta(self): required, optional, immutable = action_param_utils.get_params_view( action_db=self.action_dbs['action-1'], runner_db=self.runnertype_dbs['test-runner-1']) merged = {} merged.update(required) merged.update(optional) merged.update(immutable) consolidated = action_param_utils.get_params_view( action_db=self.action_dbs['action-1'], runner_db=self.runnertype_dbs['test-runner-1'], merged_only=True) # Validate that merged_only view works. self.assertEqual(merged, consolidated) # Validate required params. self.assertEqual(len(required), 1, 'Required should contain only one param.') self.assertIn('actionstr', required, 'actionstr param is a required param.') self.assertNotIn('actionstr', optional, 'actionstr should not be in optional parameters') self.assertNotIn('actionstr', immutable, 'actionstr should not be in immutable parameters') self.assertIn('actionstr', merged, 'actionstr should be in action parameters') # Validate immutable params. self.assertIn('runnerimmutable', immutable, 'runnerimmutable should be in immutable.') self.assertIn('actionimmutable', immutable, 'actionimmutable should be in immutable.') # Validate optional params. for opt in optional: self.assertIn(opt, merged, 'Optional %s should be in action parameters' % opt) self.assertNotIn( opt, required, 'Optional %s should not be in required params' % opt) self.assertNotIn( opt, immutable, 'Optional %s should not be in immutable params' % opt)
def _get_one(action_id): """ List merged action & runner parameters by action id. Handle: GET /actions/views/parameters/1 """ action_db = LookupUtils._get_action_by_id(action_id) LOG.info('Found action: %s, runner: %s', action_db, action_db.runner_type['name']) runner_db = LookupUtils._get_runner_by_name(action_db.runner_type['name']) all_params = action_param_utils.get_params_view( action_db=action_db, runner_db=runner_db, merged_only=True) return {'parameters': all_params}
def get_all(self, exclude_attributes=None, include_attributes=None, sort=None, offset=0, limit=None, requester_user=None, **raw_filters): """ List all actions. Handles requests: GET /actions/views/overview """ resp = super(OverviewController, self)._get_all(exclude_fields=exclude_attributes, include_fields=include_attributes, sort=sort, offset=offset, limit=limit, raw_filters=raw_filters, requester_user=requester_user) runner_type_names = set([]) action_ids = [] result = [] for item in resp.json: action_api = ActionAPI(**item) result.append(action_api) runner_type_names.add(action_api.runner_type) action_ids.append(str(action_api.id)) # Add combined runner and action parameters to the compound result object # NOTE: This approach results in 2 additional queries while previous one resulted in # N * 2 additional queries # 1. Retrieve all the respective runner objects - we only need parameters runner_type_dbs = RunnerType.query(name__in=runner_type_names, only_fields=['name', 'runner_parameters']) runner_type_dbs = dict([(runner_db.name, runner_db) for runner_db in runner_type_dbs]) # 2. Retrieve all the respective action objects - we only need parameters action_dbs = dict([(action_db.id, action_db) for action_db in result]) for action_api in result: action_db = action_dbs.get(action_api.id, None) runner_db = runner_type_dbs.get(action_api.runner_type, None) all_params = action_param_utils.get_params_view(action_db=action_db, runner_db=runner_db, merged_only=True) action_api.parameters = all_params resp.json = result return resp
def _get_one(action_id): """ List merged action & runner parameters by action id. Handle: GET /actions/views/parameters/1 """ action_db = LookupUtils._get_action_by_id(action_id) LOG.info('Found action: %s, runner: %s', action_db, action_db.runner_type['name']) runner_db = LookupUtils._get_runner_by_name( action_db.runner_type['name']) all_params = action_param_utils.get_params_view(action_db=action_db, runner_db=runner_db, merged_only=True) return {'parameters': all_params}
def _get_one(action_id, requester_user): """ List merged action & runner parameters by action id. Handle: GET /actions/views/parameters/1 """ action_db = LookupUtils._get_action_by_id(action_id) permission_type = PermissionType.ACTION_VIEW rbac_utils.assert_user_has_resource_db_permission(user_db=requester_user, resource_db=action_db, permission_type=permission_type) runner_db = LookupUtils._get_runner_by_name(action_db.runner_type['name']) all_params = action_param_utils.get_params_view( action_db=action_db, runner_db=runner_db, merged_only=True) return {'parameters': all_params}
def _get_one(action_id, requester_user): """ List merged action & runner parameters by action id. Handle: GET /actions/views/parameters/1 """ action_db = LookupUtils._get_action_by_id(action_id) permission_type = PermissionType.ACTION_VIEW rbac_utils.assert_user_has_resource_db_permission( user_db=requester_user, resource_db=action_db, permission_type=permission_type) runner_db = LookupUtils._get_runner_by_name( action_db.runner_type['name']) all_params = action_param_utils.get_params_view(action_db=action_db, runner_db=runner_db, merged_only=True) return {'parameters': all_params}