take optional parameters for the timeout and polling interval, defaulted on the 
public methods, raise error on timeout to stop infinite loop

Closes #631

Signed-off-by: Tomaz Muraus <to...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/79388f12
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/79388f12
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/79388f12

Branch: refs/heads/trunk
Commit: 79388f12dd2f7b6fd65a372a6e545a4b17c703f8
Parents: 02a0224
Author: Anthony Shaw <anthony.p.s...@gmail.com>
Authored: Fri Nov 13 00:33:26 2015 +1100
Committer: Tomaz Muraus <to...@apache.org>
Committed: Thu Nov 12 23:46:02 2015 +0100

----------------------------------------------------------------------
 libcloud/common/dimensiondata.py               | 20 ++++++++++++++++----
 libcloud/compute/drivers/dimensiondata.py      | 15 +++++++++++++--
 libcloud/loadbalancer/drivers/dimensiondata.py | 15 +++++++++++++--
 3 files changed, 42 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/79388f12/libcloud/common/dimensiondata.py
----------------------------------------------------------------------
diff --git a/libcloud/common/dimensiondata.py b/libcloud/common/dimensiondata.py
index 3e608fd..268b7f1 100644
--- a/libcloud/common/dimensiondata.py
+++ b/libcloud/common/dimensiondata.py
@@ -218,7 +218,7 @@ class DimensionDataConnection(ConnectionUserAndKey):
         return ("%s/%s/%s" % (self.api_path_version_2, self.api_version_2,
                               self._get_orgId()))
 
-    def wait_for_state(self, state, func, *args, **kwargs):
+    def wait_for_state(self, state, func, poll_interval, timeout, *args, 
**kwargs):
         """
         Wait for the function which returns a instance
         with field status to match
@@ -231,14 +231,26 @@ class DimensionDataConnection(ConnectionUserAndKey):
         :param  func: The function to call, e.g. ex_get_vlan
         :type   func: ``function``
 
+        :param  poll_interval: The number of seconds to wait between checks
+        :type   poll_interval: `int`
+
+        :param  timeout: The total number of seconds to wait to reach a state
+        :type   timeout: `int`
+
+        :param  args: The arguments for func
+        :type   args: Positional arguments
+
         :param  kwargs: The arguments for func
         :type   kwargs: Keyword arguments
         """
-        while(True):
+        cnt = 0
+        while cnt < timeout/poll_interval:
             response = func(*args, **kwargs)
             if response.status is state or response.status in state:
-                break
-            sleep(2)
+                return response
+            sleep(poll_interval)
+            cnt+=1
+        raise Error("Request timed out")
 
     def _get_orgId(self):
         """

http://git-wip-us.apache.org/repos/asf/libcloud/blob/79388f12/libcloud/compute/drivers/dimensiondata.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/dimensiondata.py 
b/libcloud/compute/drivers/dimensiondata.py
index 97350b2..3ee8a11 100644
--- a/libcloud/compute/drivers/dimensiondata.py
+++ b/libcloud/compute/drivers/dimensiondata.py
@@ -816,7 +816,8 @@ class DimensionDataNodeDriver(NodeDriver):
                 filter(lambda x: x.id == id, self.list_locations()))[0]
         return location
 
-    def ex_wait_for_state(self, state, func, *args, **kwargs):
+    def ex_wait_for_state(self, state, func, poll_interval=2,
+                          timeout=60, *args, **kwargs):
         """
         Wait for the function which returns a instance
         with field status to match
@@ -829,10 +830,20 @@ class DimensionDataNodeDriver(NodeDriver):
         :param  func: The function to call, e.g. ex_get_vlan
         :type   func: ``function``
 
+        :param  poll_interval: The number of seconds to wait between checks
+        :type   poll_interval: `int`
+
+        :param  timeout: The total number of seconds to wait to reach a state
+        :type   timeout: `int`
+
+        :param  args: The arguments for func
+        :type   args: Positional arguments
+
         :param  kwargs: The arguments for func
         :type   kwargs: Keyword arguments
         """
-        self.connection.wait_for_state(state, func, *args, **kwargs)
+        return self.connection.wait_for_state(state, func, poll_interval,
+                                              timeout, *args, **kwargs)
 
     def _to_nat_rules(self, object, network_domain):
         rules = []

http://git-wip-us.apache.org/repos/asf/libcloud/blob/79388f12/libcloud/loadbalancer/drivers/dimensiondata.py
----------------------------------------------------------------------
diff --git a/libcloud/loadbalancer/drivers/dimensiondata.py 
b/libcloud/loadbalancer/drivers/dimensiondata.py
index 05b260b..38bdf13 100644
--- a/libcloud/loadbalancer/drivers/dimensiondata.py
+++ b/libcloud/loadbalancer/drivers/dimensiondata.py
@@ -823,7 +823,8 @@ class DimensionDataLBDriver(Driver):
         response_code = findtext(result, 'responseCode', TYPES_URN)
         return response_code in ['IN_PROGRESS', 'OK']
 
-    def ex_wait_for_state(self, state, func, *args, **kwargs):
+    def ex_wait_for_state(self, state, func, poll_interval=2,
+                          timeout=60, *args, **kwargs):
         """
         Wait for the function which returns a instance
         with field status to match
@@ -836,10 +837,20 @@ class DimensionDataLBDriver(Driver):
         :param  func: The function to call, e.g. ex_get_vlan
         :type   func: ``function``
 
+        :param  poll_interval: The number of seconds to wait between checks
+        :type   poll_interval: `int`
+
+        :param  timeout: The total number of seconds to wait to reach a state
+        :type   timeout: `int`
+
+        :param  args: The arguments for func
+        :type   args: Positional arguments
+
         :param  kwargs: The arguments for func
         :type   kwargs: Keyword arguments
         """
-        self.connection.wait_for_state(state, func, *args, **kwargs)
+        return self.connection.wait_for_state(state, func, poll_interval,
+                                              timeout, *args, **kwargs)
 
     def _to_nodes(self, object):
         nodes = []

Reply via email to