Просмотр исходного кода

Added more properties to Snapshots (description, size, volume_id,
create_time)

Nuwan Goonasekera 10 лет назад
Родитель
Сommit
1043714ecc

+ 55 - 0
cloudbridge/cloud/interfaces/resources.py

@@ -1238,6 +1238,61 @@ class Snapshot(ObjectLifeCycleMixin, CloudResource):
         """
         pass
 
+    @abstractproperty
+    def description(self):
+        """
+        Get the snapshot description. Some cloud providers may not support this
+        property, and will return the snapshot name instead.
+
+        :rtype: ``str``
+        :return: Description for this snapshot as returned by the cloud
+        middleware.
+        """
+        pass
+
+    @description.setter
+    @abstractmethod
+    def description(self, value):
+        """
+        Set the snapshot description. Some cloud providers may not support this
+        property, and setting the description may have no effect. (Providers
+        that do not support this property will always return the snapshot name
+        as the description)
+        """
+        pass
+
+    @abstractproperty
+    def size(self):
+        """
+        Get the snapshot size (in GB).
+
+        :rtype: ``int``
+        :return: Size for this snapshot as returned by the cloud middleware.
+        """
+        pass
+
+    @abstractproperty
+    def volume_id(self):
+        """
+        Get the id of the volume that this snapshot is based on.
+        May return None if the source volume no longer exists.
+
+        :rtype: ``int``
+        :return: Id of the volume that this snapshot is based on
+        """
+        pass
+
+    @abstractproperty
+    def create_time(self):
+        """
+        Get the creation data and time for this snapshot.
+
+        :rtype: ``DateTime``
+        :return: Creation time for this snapshot as returned by the cloud
+        middleware.
+        """
+        pass
+
     @abstractmethod
     def create_volume(self, placement, size=None, volume_type=None, iops=None):
         """

+ 20 - 0
cloudbridge/cloud/providers/aws/resources.py

@@ -507,6 +507,26 @@ class AWSSnapshot(BaseSnapshot):
         """
         self._snapshot.add_tag('Name', value)
 
+    @property
+    def description(self):
+        return self._snapshot.tags.get('Description')
+
+    @description.setter
+    def description(self, value):
+        self._snapshot.add_tag('Description', value)
+
+    @property
+    def size(self):
+        return self._snapshot.volume_size
+
+    @property
+    def volume_id(self):
+        return self._snapshot.volume_id
+
+    @property
+    def create_time(self):
+        return self._snapshot.start_time
+
     @property
     def state(self):
         return AWSSnapshot.SNAPSHOT_STATE_MAP.get(

+ 21 - 0
cloudbridge/cloud/providers/openstack/resources.py

@@ -558,6 +558,27 @@ class OpenStackSnapshot(BaseSnapshot):
         self._snapshot.add_tag('Name', value)
         self._snapshot.update()
 
+    @property
+    def description(self):
+        return self._snapshot.description
+
+    @description.setter
+    def description(self, value):
+        self._snapshot.description = value
+        self._snapshot.update()
+
+    @property
+    def size(self):
+        return self._snapshot.size
+
+    @property
+    def volume_id(self):
+        return self._snapshot.volume_id
+
+    @property
+    def create_time(self):
+        return self._snapshot.created_at
+
     @property
     def state(self):
         return OpenStackSnapshot.SNAPSHOT_STATE_MAP.get(

+ 38 - 0
test/test_block_store_service.py

@@ -4,6 +4,7 @@ import six
 
 from cloudbridge.cloud.interfaces import SnapshotState
 from cloudbridge.cloud.interfaces import VolumeState
+from cloudbridge.cloud.interfaces.resources import AttachmentInfo
 from test.helpers import ProviderTestBase
 import test.helpers as helpers
 
@@ -147,6 +148,7 @@ class CloudBlockStoreServiceTestCase(ProviderTestBase):
                     [VolumeState.IN_USE],
                     terminal_states=[VolumeState.ERROR, VolumeState.DELETED])
                 self.assertIsNotNone(test_vol.attachments)
+                self.assertIsInstance(test_vol.attachments, AttachmentInfo)
                 self.assertEqual(test_vol.attachments.volume, test_vol)
                 self.assertEqual(test_vol.attachments.instance_id,
                                  test_instance.id)
@@ -276,3 +278,39 @@ class CloudBlockStoreServiceTestCase(ProviderTestBase):
                     test_snap_too.id in repr(test_snap_too),
                     "repr(obj) should contain the object id so that the object"
                     " can be reconstructed, but does not.")
+
+    def test_snapshot_properties(self):
+        """
+        Test snapshot properties
+        """
+        name = "CBTestSnapProp-{0}".format(uuid.uuid4())
+        test_vol = self.provider.block_store.volumes.create(
+            name,
+            1,
+            helpers.get_provider_test_data(self.provider, "placement"))
+        with helpers.cleanup_action(lambda: test_vol.delete()):
+            test_vol.wait_till_ready()
+            snap_name = "CBSnapProp-{0}".format(name)
+            test_snap = test_vol.create_snapshot(name=snap_name,
+                                                 description=snap_name)
+
+            def cleanup_snap(snap):
+                snap.delete()
+                snap.wait_for(
+                    [SnapshotState.UNKNOWN],
+                    terminal_states=[SnapshotState.ERROR])
+
+            with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
+                test_snap.wait_till_ready()
+                self.assertTrue(isinstance(test_vol.size, six.integer_types))
+                self.assertEqual(
+                    test_snap.size, test_vol.size,
+                    "Snapshot.size must match original volume's size: %s"
+                    " but is: %s" % (test_vol.size, test_snap.size))
+                self.assertTrue(
+                    test_vol.description is None or
+                    isinstance(test_vol.description, six.string_types),
+                    "Snapshot.description must be None or a string. Got: %s"
+                    % test_vol.description)
+                self.assertEqual(test_vol.id, test_snap.volume_id)
+                self.assertIsNotNone(test_vol.create_time)