Bladeren bron

Simplified wait_for and wait_till_ready methods. Removed example.py

nuwan_ag 10 jaren geleden
bovenliggende
commit
44b2ec1a36
3 gewijzigde bestanden met toevoegingen van 72 en 66 verwijderingen
  1. 27 49
      cloudbridge/cloud/base.py
  2. 45 2
      cloudbridge/cloud/interfaces/resources.py
  3. 0 15
      example.py

+ 27 - 49
cloudbridge/cloud/base.py

@@ -122,22 +122,11 @@ class BaseCloudProvider(CloudProvider):
 class BaseObjectLifeCycleMixin(ObjectLifeCycleMixin):
     """
     A base implementation of an ObjectLifeCycleMixin.
-    This base implementation has an implementation of wait_till_ready
+    This base implementation has an implementation of wait_for
     which refreshes the object's state till the desired ready states
-    are reached. Subclasses must implement two new properties - ready_states
-    and terminal_states, which return a list of states to wait for.
+    are reached. Subclasses must still implement the wait_till_ready
+    method, since the desired ready states are object specific.
     """
-    @property
-    def ready_states(self):
-        raise NotImplementedError(
-            "ready_states not implemented by this object. Subclasses must"
-            " implement this method and return a valid set of ready states")
-
-    @property
-    def terminal_states(self):
-        raise NotImplementedError(
-            "terminal_states not implemented by this object. Subclasses must"
-            " implement this method and return a valid set of terminal states")
 
     def wait_for(self, target_states, terminal_states=None, timeout=600,
                  interval=5):
@@ -170,13 +159,6 @@ class BaseObjectLifeCycleMixin(ObjectLifeCycleMixin):
                         " still in state: {1}".format(self, self.state))
             self.refresh()
 
-    def wait_till_ready(self, timeout=600, interval=5):
-        self.wait_for(
-            self.ready_states,
-            self.terminal_states,
-            timeout,
-            interval)
-
 
 class BaseResultList(ResultList):
 
@@ -259,13 +241,12 @@ class BaseInstance(BaseObjectLifeCycleMixin, Instance):
                 self.private_ips == other.private_ips and
                 self.image_id == other.image_id)
 
-    @property
-    def ready_states(self):
-        return [InstanceState.RUNNING]
-
-    @property
-    def terminal_states(self):
-        return [InstanceState.TERMINATED, InstanceState.ERROR]
+    def wait_till_ready(self, timeout=600, interval=5):
+        self.wait_for(
+            [InstanceState.RUNNING],
+            terminal_states=[InstanceState.TERMINATED, InstanceState.ERROR],
+            timeout=timeout,
+            interval=interval)
 
     def __repr__(self):
         return "<CB-{0}: {1} ({2})>".format(self.__class__.__name__,
@@ -357,13 +338,12 @@ class BaseMachineImage(BaseObjectLifeCycleMixin, MachineImage):
                 self.name == other.name and
                 self.description == other.description)
 
-    @property
-    def ready_states(self):
-        return [MachineImageState.AVAILABLE]
-
-    @property
-    def terminal_states(self):
-        return [MachineImageState.ERROR]
+    def wait_till_ready(self, timeout=600, interval=5):
+        self.wait_for(
+            [MachineImageState.AVAILABLE],
+            terminal_states=[MachineImageState.ERROR],
+            timeout=timeout,
+            interval=interval)
 
     def __repr__(self):
         return "<CB-{0}: {1} ({2})>".format(self.__class__.__name__,
@@ -383,13 +363,12 @@ class BaseVolume(BaseObjectLifeCycleMixin, Volume):
                 self.state == other.state and
                 self.name == other.name)
 
-    @property
-    def ready_states(self):
-        return [VolumeState.AVAILABLE]
-
-    @property
-    def terminal_states(self):
-        return [VolumeState.ERROR, VolumeState.DELETED]
+    def wait_till_ready(self, timeout=600, interval=5):
+        self.wait_for(
+            [VolumeState.AVAILABLE],
+            terminal_states=[VolumeState.ERROR, VolumeState.DELETED],
+            timeout=timeout,
+            interval=interval)
 
     def __repr__(self):
         return "<CB-{0}: {1} ({2})>".format(self.__class__.__name__,
@@ -409,13 +388,12 @@ class BaseSnapshot(BaseObjectLifeCycleMixin, Snapshot):
                 self.state == other.state and
                 self.name == other.name)
 
-    @property
-    def ready_states(self):
-        return [SnapshotState.AVAILABLE]
-
-    @property
-    def terminal_states(self):
-        return [SnapshotState.ERROR]
+    def wait_till_ready(self, timeout=600, interval=5):
+        self.wait_for(
+            [SnapshotState.AVAILABLE],
+            terminal_states=[SnapshotState.ERROR],
+            timeout=timeout,
+            interval=interval)
 
     def __repr__(self):
         return "<CB-{0}: {1} ({2})>".format(self.__class__.__name__,

+ 45 - 2
cloudbridge/cloud/interfaces/resources.py

@@ -104,11 +104,54 @@ class ObjectLifeCycleMixin(object):
         """
         pass
 
+    @abstractmethod
+    def wait_for(self, target_states, terminal_states=None, timeout=600,
+                 interval=5):
+        """
+        Wait for a specified timeout for an object to reach a set of desired
+        target states. If the object does not reach the desired state within
+        the specified timeout, a ``WaitStateException`` will be raised.
+        The optional terminal_states property can be used to specify an
+        additional set of states which, should the object reach one,
+        the object thereafter will not transition into the desired target
+        state. Should this happen, a ``WaitStateException`` will be raised.
+
+        Example::
+            instance.wait_for(
+                [InstanceState.TERMINATED, InstanceState.UNKNOWN],
+                terminal_states=[InstanceState.ERROR],
+                interval=self.get_test_wait_interval())
+
+        :type target_states: ``list`` of states
+        :param target_states: The list of target states to wait for.
+
+        :type terminal_states: ``list`` of states
+        :param terminal_states: A list of terminal states after which the
+        object will not transition into a target state. A WaitStateException
+        will be raised if the object transition into a terminal state.
+
+        :type timeout: int
+        :param timeout: The maximum length of time (in seconds) to wait for the
+                        object to become ready.
+
+        :type interval: int
+        :param interval: How frequently to poll the object's ready state (in
+                         seconds).
+
+        :rtype: ``True``
+        :return: Returns ``True`` if successful. A ``WaitStateException``
+                 exception may be thrown by the underlying service if the
+                 object cannot  get into a ready state (e.g. if the object
+                 is in an error state).
+        """
+        pass
+
     @abstractmethod
     def wait_till_ready(self, timeout, interval):
         """
-        Wait till the current object is in a ready state, which is any
-        state where the end-user can successfully interact with the object.
+        A convenience method to wait till the current object reaches a state
+        which is ready for use, which is any state where the end-user can
+        successfully interact with the object.
         Will throw a ``WaitStateException`` if the object is not ready within
         the specified timeout.
 

+ 0 - 15
example.py

@@ -1,15 +0,0 @@
-from test import helpers
-
-from cloudbridge.cloud.factory import CloudProviderFactory
-from cloudbridge.cloud.factory import ProviderList
-
-
-config = {}
-provider = CloudProviderFactory().create_provider(
-    ProviderList.OPENSTACK,
-    config)
-# print provider.security.list_key_pairs()
-# print provider.compute.instance_types.list()
-# print next(provider.compute.instance_types.find_by_name("m1.small"))
-instance = helpers.get_test_instance(provider)
-# print provider.security.list_security_groups()