Allow to list all cron-triggers

Closes-Bug: #1707942

Change-Id: Ia69972493a070aac4d413e27b26b02f6c60b19de
This commit is contained in:
Nikolay Mahotkin 2017-08-01 18:39:21 +03:00
parent a1a3182bca
commit 7d8d4fba82
3 changed files with 23 additions and 4 deletions

View File

@ -19,6 +19,7 @@
"cron_triggers:delete": "rule:admin_or_owner", "cron_triggers:delete": "rule:admin_or_owner",
"cron_triggers:get": "rule:admin_or_owner", "cron_triggers:get": "rule:admin_or_owner",
"cron_triggers:list": "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:create": "rule:admin_or_owner",
"environments:delete": "rule:admin_or_owner", "environments:delete": "rule:admin_or_owner",

View File

@ -94,13 +94,13 @@ class CronTriggersController(rest.RestController):
wtypes.text, wtypes.text, types.uuid, types.jsontype, wtypes.text, wtypes.text, types.uuid, types.jsontype,
types.jsontype, resources.SCOPE_TYPES, wtypes.text, types.jsontype, resources.SCOPE_TYPES, wtypes.text,
wtypes.IntegerType(minimum=1), 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', def get_all(self, marker=None, limit=None, sort_keys='created_at',
sort_dirs='asc', fields='', name=None, workflow_name=None, sort_dirs='asc', fields='', name=None, workflow_name=None,
workflow_id=None, workflow_input=None, workflow_params=None, workflow_id=None, workflow_input=None, workflow_params=None,
scope=None, pattern=None, remaining_executions=None, scope=None, pattern=None, remaining_executions=None,
first_execution_time=None, next_execution_time=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. """Return all cron triggers.
:param marker: Optional. Pagination marker for large data sets. :param marker: Optional. Pagination marker for large data sets.
@ -138,9 +138,13 @@ class CronTriggersController(rest.RestController):
time and date. time and date.
:param updated_at: Optional. Keep only resources with specific latest :param updated_at: Optional. Keep only resources with specific latest
update time and date. update time and date.
:param all_projects: Optional. Get resources of all projects.
""" """
acl.enforce('cron_triggers:list', context.ctx()) 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( filters = filter_utils.create_filters_from_request_params(
created_at=created_at, created_at=created_at,
name=name, name=name,
@ -158,8 +162,8 @@ class CronTriggersController(rest.RestController):
LOG.info( LOG.info(
"Fetch cron triggers. marker=%s, limit=%s, sort_keys=%s, " "Fetch cron triggers. marker=%s, limit=%s, sort_keys=%s, "
"sort_dirs=%s, filters=%s", "sort_dirs=%s, filters=%s, all_projects=%s",
marker, limit, sort_keys, sort_dirs, filters marker, limit, sort_keys, sort_dirs, filters, all_projects
) )
return rest_utils.get_all( return rest_utils.get_all(
@ -172,5 +176,6 @@ class CronTriggersController(rest.RestController):
sort_keys=sort_keys, sort_keys=sort_keys,
sort_dirs=sort_dirs, sort_dirs=sort_dirs,
fields=fields, fields=fields,
all_projects=all_projects,
**filters **filters
) )

View File

@ -21,6 +21,7 @@ from mistral.db.v2.sqlalchemy import models
from mistral import exceptions as exc from mistral import exceptions as exc
from mistral.services import security from mistral.services import security
from mistral.tests.unit.api import base from mistral.tests.unit.api import base
from mistral.tests.unit import base as unit_base
WF = models.WorkflowDefinition( WF = models.WorkflowDefinition(
spec={ spec={
@ -150,6 +151,18 @@ class TestCronTriggerController(base.APITest):
self.assertEqual(1, len(resp.json['cron_triggers'])) self.assertEqual(1, len(resp.json['cron_triggers']))
self.assertDictEqual(TRIGGER, resp.json['cron_triggers'][0]) 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) @mock.patch.object(db_api, "get_cron_triggers", MOCK_EMPTY)
def test_get_all_empty(self): def test_get_all_empty(self):
resp = self.app.get('/v2/cron_triggers') resp = self.app.get('/v2/cron_triggers')