You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2012/08/21 13:41:45 UTC

[7/8] git commit: Python client: use correct methods for actions

Python client: use correct methods for actions

The code for calling instance actions (stop, destroy, etc.) didn't
honour the methods advertised by the API (e.g. it used POST instead of
DELETE for the `destroy` action).

This fixes the issue and deduplicates the code.

Signed-off-by: Tomas Sedovic <to...@sedovic.cz>


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/4362ee16
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/4362ee16
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/4362ee16

Branch: refs/heads/master
Commit: 4362ee16df47e9d9ff0bad17a4338810ef0d7004
Parents: 8ad1c7a
Author: Tomas Sedovic <to...@sedovic.cz>
Authored: Wed Aug 8 15:50:33 2012 +0200
Committer: Tomas Sedovic <to...@sedovic.cz>
Committed: Tue Aug 21 12:06:56 2012 +0200

----------------------------------------------------------------------
 clients/python/deltacloud.py |   52 ++++++++++++++----------------------
 1 files changed, 20 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/4362ee16/clients/python/deltacloud.py
----------------------------------------------------------------------
diff --git a/clients/python/deltacloud.py b/clients/python/deltacloud.py
index 55812d0..708c84b 100644
--- a/clients/python/deltacloud.py
+++ b/clients/python/deltacloud.py
@@ -37,7 +37,8 @@ class SimpleRestClient:
                 method=method.upper(),
                 body=params,
                 headers={'accept':'application/xml'})
-        response = self.parse_xml(response)
+        if response:
+            response = self.parse_xml(response)
         return status, response
 
     def GET(self, uri):
@@ -127,44 +128,31 @@ class Instance(Deltacloud):
             self.key_name = key_auth[0].xpathEval('keyname')[0].content
 
     def start(self):
-        action = self.instance.xpathEval("actions/link[@rel='start']")
-        if not action:
-            return False
-        else:
-            if self.deltacloud.client.POST(action[0].xpathEval("@href")[0].content, {})[0]['status'] == '200':
-                return True
-            else:
-                return False
+        return self.do_action('start')
 
     def stop(self):
-        action = self.instance.xpathEval("actions/link[@rel='stop']")
-        if not action:
-            return False
-        else:
-            if self.deltacloud.client.POST(action[0].xpathEval("@href")[0].content, {})[0]['status'] == '200':
-                return True
-            else:
-                return False
+        return self.do_action('stop')
 
     def reboot(self):
-        action = self.instance.xpathEval("actions/link[@rel='reboot']")
-        if not action:
-            return False
-        else:
-            if self.deltacloud.client.POST(action[0].xpathEval("@href")[0].content, {})[0]['status'] == '200':
-                return True
-            else:
-                return False
+        return self.do_action('reboot')
 
     def destroy(self):
-        action = self.instance.xpathEval("actions/link[@rel='destroy']")
-        if not action:
+        return self.do_action('destroy')
+
+    def actions(self):
+        '''Return all the actions allowed on the instance.'''
+        return [link.xpathEval('@rel')[0].content for link in
+                self.instance.xpathEval('actions/link')]
+
+    def do_action(self, action):
+        '''Run the specified action.'''
+        if not action in self.actions():
             return False
-        else:
-            if self.deltacloud.client.POST(action[0].xpathEval("@href")[0].content, {})[0]['status'] == '200':
-                return True
-            else:
-                return False
+        action = self.instance.xpathEval("actions/link[@rel='%s']" % action)[0]
+        url = action.xpathEval('@href')[0].content
+        method = action.xpathEval('@method')[0].content
+        response, body = self.deltacloud.client.do_request(url, method)
+        return response['status'][0] == '2'  # HTTP 2xx codes mean success
 
 
 class Image(Deltacloud):