Explorar el Código

get methods should return None if the underlying objects do not exist.

nuwan_ag hace 10 años
padre
commit
277c1a1c01

+ 10 - 5
cloudbridge/providers/interfaces/services.py

@@ -28,7 +28,8 @@ class ComputeService(ProviderService):
 
     def get_instance(self, instance_id):
         """
-        Returns an instance given its id.
+        Returns an instance given its id. Returns None
+        if the object does not exist.
 
         :rtype: ``object`` of :class:`.Instance`
         :return:  an Instance object
@@ -136,7 +137,8 @@ class VolumeService(ProviderService):
 
     def get_volume(self, volume_id):
         """
-        Returns a volume given its id.
+        Returns a volume given its id. Returns None if the volume
+        does not exist.
 
         :rtype: ``object`` of :class:`.Volume`
         :return: a Volume object
@@ -197,7 +199,8 @@ class SnapshotService(ProviderService):
 
     def get_snapshot(self, volume_id):
         """
-        Returns a snapshot given its id.
+        Returns a snapshot given its id. Returns None if the snapshot
+        does not exist.
 
         :rtype: ``object`` of :class:`.Snapshot`
         :return: a Snapshot object
@@ -285,7 +288,8 @@ class ImageService(ProviderService):
 
     def get_image(self, image_id):
         """
-        Returns an Image given its id
+        Returns an Image given its id. Returns None if the Image does not
+        exist.
 
         :rtype: ``object`` of :class:`.Image`
         :return:  an Image instance
@@ -322,7 +326,8 @@ class ObjectStoreService(ProviderService):
 
     def get_container(self, container_id):
         """
-        Returns a container given its id
+        Returns a container given its id. Returns None if the container
+        does not exist.
 
         :rtype: ``object`` of :class:`.Container`
         :return:  a Container instance

+ 18 - 18
cloudbridge/providers/openstack/resources.py

@@ -1,9 +1,6 @@
 """
 DataTypes used by this provider
 """
-
-from cinderclient.exceptions import NotFound
-
 from cloudbridge.providers.base import BaseInstance
 from cloudbridge.providers.base import BaseKeyPair
 from cloudbridge.providers.base import BaseMachineImage
@@ -84,10 +81,10 @@ class OpenStackMachineImage(BaseMachineImage):
         Refreshes the state of this instance by re-querying the cloud provider
         for its latest state.
         """
-        try:
-            image = self.provider.images.get_image(self.image_id)
+        image = self.provider.images.get_image(self.image_id)
+        if image:
             self._os_image = image._os_image
-        except NotFound:
+        else:
             # The image no longer exists and cannot be refreshed.
             # set the status to unknown
             self._os_image.status = 'unknown'
@@ -282,10 +279,11 @@ class OpenStackInstance(BaseInstance):
         Refreshes the state of this instance by re-querying the cloud provider
         for its latest state.
         """
-        try:
-            self._os_instance = self.provider.compute.get_instance(
-                self.instance_id)._os_instance
-        except NotFound:
+        instance = self.provider.compute.get_instance(
+            self.instance_id)
+        if instance:
+            self._os_instance = instance._os_instance
+        else:
             # The instance no longer exists and cannot be refreshed.
             # set the status to unknown
             self._os_instance.status = 'unknown'
@@ -386,10 +384,11 @@ class OpenStackVolume(BaseVolume):
         Refreshes the state of this volume by re-querying the cloud provider
         for its latest state.
         """
-        try:
-            self._volume = self.provider.block_store.volumes.get_volume(
-                self.volume_id)._volume
-        except NotFound:
+        vol = self.provider.block_store.volumes.get_volume(
+            self.volume_id)
+        if vol:
+            self._volume = vol._volume
+        else:
             # The volume no longer exists and cannot be refreshed.
             # set the status to unknown
             self._volume.status = 'unknown'
@@ -442,10 +441,11 @@ class OpenStackSnapshot(BaseSnapshot):
         Refreshes the state of this snapshot by re-querying the cloud provider
         for its latest state.
         """
-        try:
-            self._snapshot = self.provider.block_store.snapshots.get_snapshot(
-                self.snapshot_id)._snapshot
-        except NotFound:
+        snap = self.provider.block_store.snapshots.get_snapshot(
+            self.snapshot_id)
+        if snap:
+            self._snapshot = snap._snapshot
+        else:
             # The snapshot no longer exists and cannot be refreshed.
             # set the status to unknown
             self._snapshot.status = 'unknown'

+ 22 - 10
cloudbridge/providers/openstack/services.py

@@ -1,6 +1,8 @@
 """
 Services implemented by this provider
 """
+from cinderclient.exceptions import NotFound as CinderNotFound
+from novaclient.exceptions import NotFound as NovaNotFound
 
 from cloudbridge.providers.base import BaseKeyPair
 from cloudbridge.providers.base import BaseSecurityGroup
@@ -60,10 +62,10 @@ class OpenStackImageService(ImageService):
         """
         Returns an Image given its id
         """
-        image = self.provider.nova.images.get(image_id)
-        if image:
-            return OpenStackMachineImage(self.provider, image)
-        else:
+        try:
+            return OpenStackMachineImage(
+                self.provider, self.provider.nova.images.get(image_id))
+        except NovaNotFound:
             return None
 
     def find_image(self, name):
@@ -123,8 +125,11 @@ class OpenStackVolumeService(VolumeService):
         """
         Returns a volume given its id.
         """
-        vol = self.provider.cinder.volumes.get(volume_id)
-        return OpenStackVolume(self.provider, vol) if vol else None
+        try:
+            return OpenStackVolume(
+                self.provider, self.provider.cinder.volumes.get(volume_id))
+        except CinderNotFound:
+            return None
 
     def find_volume(self, name):
         """
@@ -163,8 +168,12 @@ class OpenStackSnapshotService(SnapshotService):
         """
         Returns a snapshot given its id.
         """
-        snap = self.provider.cinder.volume_snapshots.get(snapshot_id)
-        return OpenStackSnapshot(self.provider, snap) if snap else None
+        try:
+            return OpenStackSnapshot(
+                self.provider,
+                self.provider.cinder.volume_snapshots.get(snapshot_id))
+        except CinderNotFound:
+            return None
 
     def find_snapshot(self, name):
         """
@@ -255,5 +264,8 @@ class OpenStackComputeService(ComputeService):
         """
         Returns an instance given its id.
         """
-        os_instance = self.provider.nova.servers.get(instance_id)
-        return OpenStackInstance(self.provider, os_instance)
+        try:
+            os_instance = self.provider.nova.servers.get(instance_id)
+            return OpenStackInstance(self.provider, os_instance)
+        except NovaNotFound:
+            return None