From 7d8d4fba823856018f7c0b6086745e494c6a6ea4 Mon Sep 17 00:00:00 2001 From: Nikolay Mahotkin Date: Tue, 1 Aug 2017 18:39:21 +0300 Subject: [PATCH] Allow to list all cron-triggers Closes-Bug: #1707942 Change-Id: Ia69972493a070aac4d413e27b26b02f6c60b19de --- etc/policy.json | 1 + mistral/api/controllers/v2/cron_trigger.py | 13 +++++++++---- mistral/tests/unit/api/v2/test_cron_triggers.py | 13 +++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/etc/policy.json b/etc/policy.json index 6a454536b..673c9aeb5 100644 --- a/etc/policy.json +++ b/etc/policy.json @@ -19,6 +19,7 @@ "cron_triggers:delete": "rule:admin_or_owner", "cron_triggers:get": "rule:admin_or_owner", "cron_triggers:list": "rule:admin_or_owner", + "cron_triggers:list:all_projects": "rule:admin_only", "environments:create": "rule:admin_or_owner", "environments:delete": "rule:admin_or_owner", diff --git a/mistral/api/controllers/v2/cron_trigger.py b/mistral/api/controllers/v2/cron_trigger.py index be4d47519..57fb29f44 100644 --- a/mistral/api/controllers/v2/cron_trigger.py +++ b/mistral/api/controllers/v2/cron_trigger.py @@ -94,13 +94,13 @@ class CronTriggersController(rest.RestController): wtypes.text, wtypes.text, types.uuid, types.jsontype, types.jsontype, resources.SCOPE_TYPES, wtypes.text, wtypes.IntegerType(minimum=1), wtypes.text, - wtypes.text, wtypes.text, wtypes.text) + wtypes.text, wtypes.text, wtypes.text, bool) def get_all(self, marker=None, limit=None, sort_keys='created_at', sort_dirs='asc', fields='', name=None, workflow_name=None, workflow_id=None, workflow_input=None, workflow_params=None, scope=None, pattern=None, remaining_executions=None, first_execution_time=None, next_execution_time=None, - created_at=None, updated_at=None): + created_at=None, updated_at=None, all_projects=False): """Return all cron triggers. :param marker: Optional. Pagination marker for large data sets. @@ -138,9 +138,13 @@ class CronTriggersController(rest.RestController): time and date. :param updated_at: Optional. Keep only resources with specific latest update time and date. + :param all_projects: Optional. Get resources of all projects. """ acl.enforce('cron_triggers:list', context.ctx()) + if all_projects: + acl.enforce('cron_triggers:list:all_projects', context.ctx()) + filters = filter_utils.create_filters_from_request_params( created_at=created_at, name=name, @@ -158,8 +162,8 @@ class CronTriggersController(rest.RestController): LOG.info( "Fetch cron triggers. marker=%s, limit=%s, sort_keys=%s, " - "sort_dirs=%s, filters=%s", - marker, limit, sort_keys, sort_dirs, filters + "sort_dirs=%s, filters=%s, all_projects=%s", + marker, limit, sort_keys, sort_dirs, filters, all_projects ) return rest_utils.get_all( @@ -172,5 +176,6 @@ class CronTriggersController(rest.RestController): sort_keys=sort_keys, sort_dirs=sort_dirs, fields=fields, + all_projects=all_projects, **filters ) diff --git a/mistral/tests/unit/api/v2/test_cron_triggers.py b/mistral/tests/unit/api/v2/test_cron_triggers.py index beb93bd40..dbad26837 100644 --- a/mistral/tests/unit/api/v2/test_cron_triggers.py +++ b/mistral/tests/unit/api/v2/test_cron_triggers.py @@ -21,6 +21,7 @@ from mistral.db.v2.sqlalchemy import models from mistral import exceptions as exc from mistral.services import security from mistral.tests.unit.api import base +from mistral.tests.unit import base as unit_base WF = models.WorkflowDefinition( spec={ @@ -150,6 +151,18 @@ class TestCronTriggerController(base.APITest): self.assertEqual(1, len(resp.json['cron_triggers'])) self.assertDictEqual(TRIGGER, resp.json['cron_triggers'][0]) + @mock.patch.object(db_api, 'get_cron_triggers') + @mock.patch('mistral.context.MistralContext.from_environ') + def test_get_all_projects_admin(self, mock_context, mock_get_triggers): + admin_ctx = unit_base.get_context(admin=True) + mock_context.return_value = admin_ctx + + resp = self.app.get('/v2/cron_triggers?all_projects=true') + + self.assertEqual(200, resp.status_int) + + self.assertTrue(mock_get_triggers.call_args[1].get('insecure', False)) + @mock.patch.object(db_api, "get_cron_triggers", MOCK_EMPTY) def test_get_all_empty(self): resp = self.app.get('/v2/cron_triggers')