diff options
Diffstat (limited to 'saharaclient/api')
-rw-r--r-- | saharaclient/api/base.py | 41 | ||||
-rw-r--r-- | saharaclient/api/cluster_templates.py | 7 | ||||
-rw-r--r-- | saharaclient/api/clusters.py | 7 | ||||
-rw-r--r-- | saharaclient/api/data_sources.py | 7 | ||||
-rw-r--r-- | saharaclient/api/job_binaries.py | 7 | ||||
-rw-r--r-- | saharaclient/api/job_binary_internals.py | 7 | ||||
-rw-r--r-- | saharaclient/api/job_executions.py | 7 | ||||
-rw-r--r-- | saharaclient/api/jobs.py | 7 | ||||
-rw-r--r-- | saharaclient/api/node_group_templates.py | 8 |
9 files changed, 70 insertions, 28 deletions
diff --git a/saharaclient/api/base.py b/saharaclient/api/base.py index a5323cd..aae3211 100644 --- a/saharaclient/api/base.py +++ b/saharaclient/api/base.py @@ -178,6 +178,26 @@ class ResourceManager(object): else: self._raise_api_exception(resp) + def _page(self, url, response_key, limit=None): + resp = self.api.get(url) + if resp.status_code == 200: + result = get_json(resp) + data = result[response_key] + meta = result.get('markers') + + next, prev = None, None + + if meta: + prev = meta.get('prev') + next = meta.get('next') + + l = [self.resource_class(self, res) + for res in data] + + return Page(l, prev, next, limit) + else: + self._raise_api_exception(resp) + def _get(self, url, response_key=None): resp = self.api.get(url) @@ -231,10 +251,25 @@ class APIException(Exception): self.error_message = error_message -def get_query_string(search_opts): - if search_opts: - qparams = sorted(search_opts.items(), key=lambda x: x[0]) +def get_query_string(search_opts, limit=None, marker=None): + opts = {} + if marker is not None: + opts['marker'] = marker + if limit is not None: + opts['limit'] = limit + if search_opts is not None: + opts.update(search_opts) + if opts: + qparams = sorted(opts.items(), key=lambda x: x[0]) query_string = "?%s" % parse.urlencode(qparams, doseq=True) else: query_string = "" return query_string + + +class Page(list): + def __init__(self, l, prev, next, limit): + super(Page, self).__init__(l) + self.prev = prev + self.next = next + self.limit = limit diff --git a/saharaclient/api/cluster_templates.py b/saharaclient/api/cluster_templates.py index 04c2585..395b3e0 100644 --- a/saharaclient/api/cluster_templates.py +++ b/saharaclient/api/cluster_templates.py @@ -77,10 +77,11 @@ class ClusterTemplateManager(base.ResourceManager): return self._update('/cluster-templates/%s' % cluster_template_id, data, 'cluster_template') - def list(self, search_opts=None): + def list(self, search_opts=None, marker=None, limit=None): """Get list of Cluster Templates.""" - query = base.get_query_string(search_opts) - return self._list('/cluster-templates%s' % query, 'cluster_templates') + query = base.get_query_string(search_opts, marker=marker, limit=limit) + url = "/cluster-templates%s" % query + return self._page(url, 'cluster_templates', limit) def get(self, cluster_template_id): """Get information about a Cluster Template.""" diff --git a/saharaclient/api/clusters.py b/saharaclient/api/clusters.py index af1a2a1..9ceab3f 100644 --- a/saharaclient/api/clusters.py +++ b/saharaclient/api/clusters.py @@ -100,10 +100,11 @@ class ClusterManager(base.ResourceManager): """ return self._update('/clusters/%s' % cluster_id, scale_object) - def list(self, search_opts=None): + def list(self, search_opts=None, limit=None, marker=None): """Get a list of Clusters.""" - query = base.get_query_string(search_opts) - return self._list('/clusters%s' % query, 'clusters') + query = base.get_query_string(search_opts, limit=limit, marker=marker) + url = "/clusters%s" % query + return self._page(url, 'clusters', limit) def get(self, cluster_id, show_progress=False): """Get information about a Cluster.""" diff --git a/saharaclient/api/data_sources.py b/saharaclient/api/data_sources.py index 4f2af19..22c06c9 100644 --- a/saharaclient/api/data_sources.py +++ b/saharaclient/api/data_sources.py @@ -44,10 +44,11 @@ class DataSourceManager(base.ResourceManager): return self._create('/data-sources', data, 'data_source') - def list(self, search_opts=None): + def list(self, search_opts=None, limit=None, marker=None): """Get a list of Data Sources.""" - query = base.get_query_string(search_opts) - return self._list('/data-sources%s' % query, 'data_sources') + query = base.get_query_string(search_opts, limit=limit, marker=marker) + url = "/data-sources%s" % query + return self._page(url, 'data_sources', limit) def get(self, data_source_id): """Get information about a Data Source.""" diff --git a/saharaclient/api/job_binaries.py b/saharaclient/api/job_binaries.py index b5ae3ef..8fc540e 100644 --- a/saharaclient/api/job_binaries.py +++ b/saharaclient/api/job_binaries.py @@ -36,10 +36,11 @@ class JobBinariesManager(base.ResourceManager): return self._create('/job-binaries', data, 'job_binary') - def list(self, search_opts=None): + def list(self, search_opts=None, limit=None, marker=None): """Get a list of Job Binaries.""" - query = base.get_query_string(search_opts) - return self._list('/job-binaries%s' % query, 'binaries') + query = base.get_query_string(search_opts, limit=limit, marker=marker) + url = "/job-binaries%s" % query + return self._page(url, 'binaries', limit) def get(self, job_binary_id): """Get information about a Job Binary.""" diff --git a/saharaclient/api/job_binary_internals.py b/saharaclient/api/job_binary_internals.py index 27f66ce..f19dc15 100644 --- a/saharaclient/api/job_binary_internals.py +++ b/saharaclient/api/job_binary_internals.py @@ -35,10 +35,11 @@ class JobBinaryInternalsManager(base.ResourceManager): urlparse.quote(name.encode('utf-8')), data, 'job_binary_internal', dump_json=False) - def list(self, search_opts=None): + def list(self, search_opts=None, limit=None, marker=None): """Get a list of Job Binary Internals.""" - query = base.get_query_string(search_opts) - return self._list('/job-binary-internals%s' % query, 'binaries') + query = base.get_query_string(search_opts, limit=limit, marker=marker) + url = "/job-binary-internals%s" % query + return self._page(url, 'binaries', limit) def get(self, job_binary_id): """Get information about a Job Binary Internal.""" diff --git a/saharaclient/api/job_executions.py b/saharaclient/api/job_executions.py index 7a0ca9a..e696fa1 100644 --- a/saharaclient/api/job_executions.py +++ b/saharaclient/api/job_executions.py @@ -24,10 +24,11 @@ class JobExecutionsManager(base.ResourceManager): resource_class = JobExecution NotUpdated = base.NotUpdated() - def list(self, search_opts=None): + def list(self, search_opts=None, marker=None, limit=None): """Get a list of Job Executions.""" - query = base.get_query_string(search_opts) - return self._list('/job-executions%s' % query, 'job_executions') + query = base.get_query_string(search_opts, limit=limit, marker=marker) + url = "/job-executions%s" % query + return self._page(url, 'job_executions', limit) def get(self, obj_id): """Get information about a Job Execution.""" diff --git a/saharaclient/api/jobs.py b/saharaclient/api/jobs.py index 374fc78..7f92aff 100644 --- a/saharaclient/api/jobs.py +++ b/saharaclient/api/jobs.py @@ -38,10 +38,11 @@ class JobsManager(base.ResourceManager): return self._create('/jobs', data, 'job') - def list(self, search_opts=None): + def list(self, search_opts=None, limit=None, marker=None): """Get a list of Jobs.""" - query = base.get_query_string(search_opts) - return self._list('/jobs%s' % query, 'jobs') + query = base.get_query_string(search_opts, limit=limit, marker=marker) + url = "/jobs%s" % query + return self._page(url, 'jobs', limit) def get(self, job_id): """Get information about a Job""" diff --git a/saharaclient/api/node_group_templates.py b/saharaclient/api/node_group_templates.py index f023909..aca358c 100644 --- a/saharaclient/api/node_group_templates.py +++ b/saharaclient/api/node_group_templates.py @@ -111,11 +111,11 @@ class NodeGroupTemplateManager(base.ResourceManager): return self._update('/node-group-templates/%s' % ng_template_id, data, 'node_group_template') - def list(self, search_opts=None): + def list(self, search_opts=None, marker=None, limit=None): """Get a list of Node Group Templates.""" - query = base.get_query_string(search_opts) - return self._list('/node-group-templates%s' % query, - 'node_group_templates') + query = base.get_query_string(search_opts, limit=limit, marker=marker) + url = "/node-group-templates%s" % query + return self._page(url, 'node_group_templates', limit) def get(self, ng_template_id): """Get information about a Node Group Template.""" |