[Python-modules-commits] [python-cs] 01/03: Import python-cs_0.8.0.orig.tar.gz

Vincent Bernat bernat at moszumanska.debian.org
Tue Mar 1 10:41:17 UTC 2016


This is an automated email from the git hooks/post-receive script.

bernat pushed a commit to branch master
in repository python-cs.

commit 6059f5abe64d5730c55f28992763a162342e6ed6
Author: Vincent Bernat <bernat at debian.org>
Date:   Tue Mar 1 11:21:43 2016 +0100

    Import python-cs_0.8.0.orig.tar.gz
---
 .travis.yml |  3 ++-
 README.rst  | 18 ++++++++++++++
 cs.py       | 79 ++++++++++++++++++++++++++++++++++++++-----------------------
 setup.py    |  2 +-
 tox.ini     |  1 +
 5 files changed, 72 insertions(+), 31 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index cb39276..7287e75 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,5 @@
 language: python
-python: 3.4
+python: 3.5
 sudo: false
 
 env:
@@ -7,6 +7,7 @@ env:
   - TOXENV=py27
   - TOXENV=py33
   - TOXENV=py34
+  - TOXENV=py35
   - TOXENV=lint
 
 install:
diff --git a/README.rst b/README.rst
index a183570..a733f6d 100644
--- a/README.rst
+++ b/README.rst
@@ -62,6 +62,9 @@ Then::
 The command-line client polls when async results are returned. To disable
 polling, use the ``--async`` flag.
 
+Configuration
+-------------
+
 Configuration is read from several locations, in the following order:
 
 * The ``CLOUDSTACK_ENDPOINT``, ``CLOUDSTACK_KEY``, ``CLOUDSTACK_SECRET`` and
@@ -105,3 +108,18 @@ the credentials or endpoint to use with a command-line flag::
 Usage::
 
     $ cs listVirtualMachines --region=exoscale
+
+Optionally ``CLOUDSTACK_REGION`` can be used to overwrite the default region ``cloudstack``.
+
+Pagination
+----------
+
+CloudStack paginates requests. ``cs`` is able to abstract away the pagination
+logic to allow fetching large result sets in one go. This is done with the
+``fetch_list`` parameter::
+
+    $ cs listVirtualMachines fetch_list=true
+
+Or in Python::
+
+    cs.listVirtualMachines(fetch_list=True)
diff --git a/cs.py b/cs.py
index 43e4bb1..9d297a2 100644
--- a/cs.py
+++ b/cs.py
@@ -104,42 +104,61 @@ class CloudStack(object):
             return self._request(command, **kwargs)
         return handler
 
-    def _request(self, command, json=True, opcode_name='command', **kwargs):
+    def _request(self, command, json=True, opcode_name='command',
+                 fetch_list=False, **kwargs):
         kwargs.update({
             'apiKey': self.key,
             opcode_name: command,
         })
         if json:
             kwargs['response'] = 'json'
-        if 'page' in kwargs:
+        if 'page' in kwargs or fetch_list:
             kwargs.setdefault('pagesize', 500)
 
-        kwargs = transform(kwargs)
-        kwargs['signature'] = self._sign(kwargs)
+        kwarg = 'params' if self.method == 'get' else 'data'
 
-        kw = {'timeout': self.timeout}
-        if self.method == 'get':
-            kw['params'] = kwargs
-        else:
-            kw['data'] = kwargs
-        response = getattr(requests, self.method)(self.endpoint, **kw)
-
-        try:
-            data = response.json()
-        except ValueError as e:
-            msg = "Make sure endpoint URL '%s' is correct." % self.endpoint
-            raise CloudStackException(
-                "HTTP {0} response from CloudStack".format(
-                    response.status_code), response, "%s. " % str(e) + msg
-                )
-
-        [key] = data.keys()
-        data = data[key]
-        if response.status_code != 200:
-            raise CloudStackException(
-                "HTTP {0} response from CloudStack".format(
-                    response.status_code), response, data)
-        return data
+        done = False
+        final_data = []
+        page = 1
+        while not done:
+            if fetch_list:
+                kwargs['page'] = page
+
+            kwargs = transform(kwargs)
+            kwargs.pop('signature', None)
+            kwargs['signature'] = self._sign(kwargs)
+
+            response = getattr(requests, self.method)(self.endpoint,
+                                                      timeout=self.timeout,
+                                                      **{kwarg: kwargs})
+
+            try:
+                data = response.json()
+            except ValueError as e:
+                msg = "Make sure endpoint URL '%s' is correct." % self.endpoint
+                raise CloudStackException(
+                    "HTTP {0} response from CloudStack".format(
+                        response.status_code), response, "%s. " % str(e) + msg
+                    )
+
+            [key] = data.keys()
+            data = data[key]
+            if response.status_code != 200:
+                raise CloudStackException(
+                    "HTTP {0} response from CloudStack".format(
+                        response.status_code), response, data)
+            if fetch_list:
+                try:
+                    [key] = [k for k in data.keys() if k != 'count']
+                except ValueError:
+                    done = True
+                else:
+                    final_data.extend(data[key])
+                    page += 1
+            else:
+                final_data = data
+                done = True
+        return final_data
 
     def _sign(self, data):
         """
@@ -220,7 +239,9 @@ def main():
         key, value = option.split('=', 1)
         kwargs[key].add(value.strip(" \"'"))
 
-    region = args.get('region', 'cloudstack')
+    region = args.get(
+        'region', os.environ.get('CLOUDSTACK_REGION', 'cloudstack')
+    )
 
     try:
         config = read_config(ini_group=region)
@@ -241,7 +262,7 @@ def main():
         while True:
             try:
                 res = cs.queryAsyncJobResult(**response)
-                if res['jobprocstatus'] == 0:
+                if res['jobstatus'] != 0:
                     response = res
                     break
                 time.sleep(3)
diff --git a/setup.py b/setup.py
index d83a90a..8dddaf0 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ with open('README.rst', 'r') as f:
 
 setup(
     name='cs',
-    version='0.6.10',
+    version='0.8.0',
     url='https://github.com/exoscale/cs',
     license='BSD',
     author=u'Bruno Renié',
diff --git a/tox.ini b/tox.ini
index 3f131ac..ec8cbfa 100644
--- a/tox.ini
+++ b/tox.ini
@@ -4,6 +4,7 @@ envlist =
 	py27,
 	py33,
 	py34,
+	py35,
 	lint
 
 [testenv]

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-cs.git



More information about the Python-modules-commits mailing list