deb-mistral/mistral/api/controllers/v2/cron_trigger.py
Xavier Hardy 843df14e0f Add filters to all collections listing functions (tags included)
Change-Id: I9c4880a41af0a79d8285a4ad23af453ba81c28bc
Partial-Bug: #1585646
2016-07-06 10:18:55 +02:00

211 lines
8.3 KiB
Python

# Copyright 2014 - Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from oslo_log import log as logging
from pecan import rest
from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan
from mistral.api import access_control as acl
from mistral.api.controllers import resource
from mistral.api.controllers.v2 import types
from mistral import context
from mistral.db.v2 import api as db_api
from mistral.services import triggers
from mistral.utils import rest_utils
LOG = logging.getLogger(__name__)
SCOPE_TYPES = wtypes.Enum(str, 'private', 'public')
class CronTrigger(resource.Resource):
"""CronTrigger resource."""
id = wtypes.text
name = wtypes.text
workflow_name = wtypes.text
workflow_id = wtypes.text
workflow_input = types.jsontype
workflow_params = types.jsontype
scope = SCOPE_TYPES
pattern = wtypes.text
remaining_executions = wtypes.IntegerType(minimum=1)
first_execution_time = wtypes.text
next_execution_time = wtypes.text
created_at = wtypes.text
updated_at = wtypes.text
@classmethod
def sample(cls):
return cls(id='123e4567-e89b-12d3-a456-426655440000',
name='my_trigger',
workflow_name='my_wf',
workflow_id='123e4567-e89b-12d3-a456-426655441111',
workflow_input={},
workflow_params={},
scope='private',
pattern='* * * * *',
remaining_executions=42,
created_at='1970-01-01T00:00:00.000000',
updated_at='1970-01-01T00:00:00.000000')
class CronTriggers(resource.ResourceList):
"""A collection of cron triggers."""
cron_triggers = [CronTrigger]
def __init__(self, **kwargs):
self._type = 'cron_triggers'
super(CronTriggers, self).__init__(**kwargs)
@classmethod
def sample(cls):
return cls(cron_triggers=[CronTrigger.sample()])
class CronTriggersController(rest.RestController):
@rest_utils.wrap_wsme_controller_exception
@wsme_pecan.wsexpose(CronTrigger, wtypes.text)
def get(self, name):
"""Returns the named cron_trigger."""
acl.enforce('cron_triggers:get', context.ctx())
LOG.info('Fetch cron trigger [name=%s]' % name)
db_model = db_api.get_cron_trigger(name)
return CronTrigger.from_dict(db_model.to_dict())
@rest_utils.wrap_wsme_controller_exception
@wsme_pecan.wsexpose(CronTrigger, body=CronTrigger, status_code=201)
def post(self, cron_trigger):
"""Creates a new cron trigger."""
acl.enforce('cron_triggers:create', context.ctx())
LOG.info('Create cron trigger: %s' % cron_trigger)
values = cron_trigger.to_dict()
db_model = triggers.create_cron_trigger(
values['name'],
values.get('workflow_name'),
values.get('workflow_input'),
values.get('workflow_params'),
values.get('pattern'),
values.get('first_execution_time'),
values.get('remaining_executions'),
workflow_id=values.get('workflow_id')
)
return CronTrigger.from_dict(db_model.to_dict())
@rest_utils.wrap_wsme_controller_exception
@wsme_pecan.wsexpose(None, wtypes.text, status_code=204)
def delete(self, name):
"""Delete cron trigger."""
acl.enforce('cron_triggers:delete', context.ctx())
LOG.info("Delete cron trigger [name=%s]" % name)
db_api.delete_cron_trigger(name)
@wsme_pecan.wsexpose(CronTriggers, types.uuid, int, types.uniquelist,
types.list, types.uniquelist, wtypes.text,
wtypes.text, types.uuid, types.jsontype,
types.jsontype, SCOPE_TYPES, wtypes.text,
wtypes.IntegerType(minimum=1), wtypes.text,
wtypes.text, wtypes.text, wtypes.text)
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):
"""Return all cron triggers.
:param marker: Optional. Pagination marker for large data sets.
:param limit: Optional. Maximum number of resources to return in a
single result. Default value is None for backward
compatibility.
:param sort_keys: Optional. Columns to sort results by.
Default: created_at, which is backward compatible.
:param sort_dirs: Optional. Directions to sort corresponding to
sort_keys, "asc" or "desc" can be chosen.
Default: desc. The length of sort_dirs can be equal
or less than that of sort_keys.
:param fields: Optional. A specified list of fields of the resource to
be returned. 'id' will be included automatically in
fields if it's provided, since it will be used when
constructing 'next' link.
:param name: Optional. Keep only resources with a specific name.
:param workflow_name: Optional. Keep only resources with a specific
workflow name.
:param workflow_id: Optional. Keep only resources with a specific
workflow ID.
:param workflow_input: Optional. Keep only resources with a specific
workflow input.
:param workflow_params: Optional. Keep only resources with specific
workflow parameters.
:param scope: Optional. Keep only resources with a specific scope.
:param pattern: Optional. Keep only resources with a specific pattern.
:param remaining_executions: Optional. Keep only resources with a
specific number of remaining executions.
:param first_execution_time: Optional. Keep only resources with a
specific time and date of first execution.
:param next_execution_time: Optional. Keep only resources with a
specific time and date of next execution.
:param created_at: Optional. Keep only resources created at a specific
time and date.
:param updated_at: Optional. Keep only resources with specific latest
update time and date.
"""
acl.enforce('cron_triggers:list', context.ctx())
filters = rest_utils.filters_to_dict(
created_at=created_at,
name=name,
updated_at=updated_at,
workflow_name=workflow_name,
workflow_id=workflow_id,
workflow_input=workflow_input,
workflow_params=workflow_params,
scope=scope,
pattern=pattern,
remaining_executions=remaining_executions,
first_execution_time=first_execution_time,
next_execution_time=next_execution_time
)
LOG.info("Fetch cron triggers. marker=%s, limit=%s, sort_keys=%s, "
"sort_dirs=%s, filters=%s", marker, limit, sort_keys,
sort_dirs, filters)
return rest_utils.get_all(
CronTriggers,
CronTrigger,
db_api.get_cron_triggers,
db_api.get_cron_trigger,
resource_function=None,
marker=marker,
limit=limit,
sort_keys=sort_keys,
sort_dirs=sort_dirs,
fields=fields,
**filters
)