From fdda862e7c1b8db95a0375c1bcfba57e62b9a746 Mon Sep 17 00:00:00 2001 From: Renat Akhmerov Date: Fri, 23 Dec 2016 18:13:42 +0700 Subject: [PATCH] Add filters to client Python APIs * A number of Mistral REST API endpoints support parameters that allow to filter colections. For example, we can pass do 'GET /v2/actions?name=in:std.' in a query string and Mistral will return only actions containing 'std.' in their name. This capability was missing in the client APIs, this patch adds corresponding parameters in the APIs for actions, workflows, executions (workflow executions) and tasks (task executions). TODO: - Add required tests Partially implements: blueprint mistral-client-api-filters Change-Id: Ic49f58626cf129f9c9399268c3cd12cc93eee039 --- mistralclient/api/v2/actions.py | 6 +++++- mistralclient/api/v2/executions.py | 5 ++++- mistralclient/api/v2/tasks.py | 30 ++++++++++++++++++++++++++++-- mistralclient/api/v2/workflows.py | 6 +++++- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/mistralclient/api/v2/actions.py b/mistralclient/api/v2/actions.py index c6cea29..3b7a800 100644 --- a/mistralclient/api/v2/actions.py +++ b/mistralclient/api/v2/actions.py @@ -67,7 +67,8 @@ class ActionManager(base.ResourceManager): return [self.resource_class(self, resource_data) for resource_data in base.extract_json(resp, 'actions')] - def list(self, marker='', limit=None, sort_keys='', sort_dirs=''): + def list(self, marker='', limit=None, sort_keys='', sort_dirs='', + **filters): qparams = {} if marker: @@ -82,6 +83,9 @@ class ActionManager(base.ResourceManager): if sort_dirs: qparams['sort_dirs'] = sort_dirs + for name, val in filters.items(): + qparams[name] = val + query_string = ("?%s" % urlparse.urlencode(list(qparams.items())) if qparams else "") diff --git a/mistralclient/api/v2/executions.py b/mistralclient/api/v2/executions.py index 4efe37b..137a4e0 100644 --- a/mistralclient/api/v2/executions.py +++ b/mistralclient/api/v2/executions.py @@ -70,7 +70,7 @@ class ExecutionManager(base.ResourceManager): return self._update('/executions/%s' % id, data) def list(self, task=None, marker='', limit=None, sort_keys='', - sort_dirs=''): + sort_dirs='', **filters): qparams = {} if task: @@ -88,6 +88,9 @@ class ExecutionManager(base.ResourceManager): if sort_dirs: qparams['sort_dirs'] = sort_dirs + for name, val in filters.items(): + qparams[name] = val + query_string = ("?%s" % urlparse.urlencode(list(qparams.items())) if qparams else "") diff --git a/mistralclient/api/v2/tasks.py b/mistralclient/api/v2/tasks.py index 3db9180..7a9eae6 100644 --- a/mistralclient/api/v2/tasks.py +++ b/mistralclient/api/v2/tasks.py @@ -14,9 +14,12 @@ # limitations under the License. import json +import six from mistralclient.api import base +urlparse = six.moves.urllib.parse + class Task(base.Resource): resource_name = 'Task' @@ -25,13 +28,36 @@ class Task(base.Resource): class TaskManager(base.ResourceManager): resource_class = Task - def list(self, workflow_execution_id=None): + def list(self, workflow_execution_id=None, marker='', limit=None, + sort_keys='', sort_dirs='', **filters): url = '/tasks' if workflow_execution_id: url = '/executions/%s/tasks' % workflow_execution_id - return self._list(url, response_key='tasks') + url += '%s' + + qparams = {} + + if marker: + qparams['marker'] = marker + + if limit: + qparams['limit'] = limit + + if sort_keys: + qparams['sort_keys'] = sort_keys + + if sort_dirs: + qparams['sort_dirs'] = sort_dirs + + for name, val in filters.items(): + qparams[name] = val + + query_string = ("?%s" % urlparse.urlencode(list(qparams.items())) + if qparams else "") + + return self._list(url % query_string, response_key='tasks') def get(self, id): self._ensure_not_empty(id=id) diff --git a/mistralclient/api/v2/workflows.py b/mistralclient/api/v2/workflows.py index 474a0b0..e6bbbef 100644 --- a/mistralclient/api/v2/workflows.py +++ b/mistralclient/api/v2/workflows.py @@ -72,7 +72,8 @@ class WorkflowManager(base.ResourceManager): return [self.resource_class(self, resource_data) for resource_data in base.extract_json(resp, 'workflows')] - def list(self, marker='', limit=None, sort_keys='', sort_dirs=''): + def list(self, marker='', limit=None, sort_keys='', sort_dirs='', + **filters): qparams = {} if marker: @@ -87,6 +88,9 @@ class WorkflowManager(base.ResourceManager): if sort_dirs: qparams['sort_dirs'] = sort_dirs + for name, val in filters.items(): + qparams[name] = val + query_string = ("?%s" % urlparse.urlencode(list(qparams.items())) if qparams else "")