Joshua Cornutt 9 лет назад
Родитель
Сommit
05dd74cb56

+ 1 - 1
cloudbridge/cloud/interfaces/services.py

@@ -296,7 +296,7 @@ class VolumeService(PageableObjectMixin, CloudService):
         pass
 
     @abstractmethod
-    def create(self, name, size, zone, snapshot=None, description=None):
+    def create(self, name, size, zone, iops=None, snapshot=None, description=None):
         """
         Creates a new volume.
 

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

@@ -27,6 +27,7 @@ from cloudbridge.cloud.interfaces.resources import NetworkState
 from cloudbridge.cloud.interfaces.resources import RouterState
 from cloudbridge.cloud.interfaces.resources import SnapshotState
 from cloudbridge.cloud.interfaces.resources import VolumeState
+from cloudbridge.cloud.interfaces.services import VolumeService
 from datetime import datetime
 import hashlib
 import inspect
@@ -535,7 +536,10 @@ class AWSSnapshot(BaseSnapshot):
 
         .. note:: an instance must have a (case sensitive) tag ``Name``
         """
-        return self._snapshot.tags.get('Name')
+        for tag in self._snapshot.tags or list():
+            if tag.get('Key') == 'Name':
+                return tag.get('Value')
+        return None
 
     @name.setter
     # pylint:disable=arguments-differ
@@ -543,15 +547,19 @@ class AWSSnapshot(BaseSnapshot):
         """
         Set the snapshot name.
         """
-        self._snapshot.add_tag('Name', value)
+        self._snapshot.create_tags(Tags=[{'Key': 'Name', 'Value': value}])
 
     @property
     def description(self):
-        return self._snapshot.tags.get('Description')
+        for tag in self._snapshot.tags or list():
+            if tag.get('Key') == 'Description':
+                return tag.get('Value')
+        return None
 
     @description.setter
     def description(self, value):
-        self._snapshot.add_tag('Description', value)
+        self._snapshot.create_tags(Tags=[{
+            'Key': 'Description', 'Value': value}])
 
     @property
     def size(self):
@@ -568,7 +576,7 @@ class AWSSnapshot(BaseSnapshot):
     @property
     def state(self):
         return AWSSnapshot.SNAPSHOT_STATE_MAP.get(
-            self._snapshot.status, SnapshotState.UNKNOWN)
+            self._snapshot.state, SnapshotState.UNKNOWN)
 
     def refresh(self):
         """
@@ -576,7 +584,7 @@ class AWSSnapshot(BaseSnapshot):
         for its latest state.
         """
         try:
-            self._snapshot.update(validate=True)
+            self._snapshot.reload()
         except (EC2ResponseError, ValueError):
             # The snapshot no longer exists and cannot be refreshed.
             # set the status to unknown
@@ -592,9 +600,12 @@ class AWSSnapshot(BaseSnapshot):
         """
         Create a new Volume from this Snapshot.
         """
-        ec2_vol = self._snapshot.create_volume(placement, size, volume_type,
-                                               iops)
-        cb_vol = AWSVolume(self._provider, ec2_vol)
+        cb_vol = VolumeService(self._provider).create(
+            name=self.name,
+            size=size,
+            zone=placement,
+            iops=iops,
+            snapshot=self._snapshot)
         cb_vol.name = "Created from {0} ({1})".format(self.id, self.name)
         return cb_vol
 

+ 27 - 49
cloudbridge/cloud/providers/aws/services.py

@@ -271,22 +271,20 @@ class AWSVolumeService(BaseVolumeService):
         """Searches for a volume by name"""
         return self.iface.find(name, 'tag:Name', limit=limit, marker=marker)
 
-    def create(self, name, size, zone, snapshot=None, description=None):
+    def create(self, name, size, zone,
+               snapshot=None, iops=None, description=None):
         """Creates a volume"""
         zone_id = zone.id if isinstance(zone, PlacementZone) else zone
         snapshot_id = snapshot.id if isinstance(
             snapshot, AWSSnapshot) and snapshot else snapshot
         res = self.iface.create('create_volume',
                                 Size=size,
+                                Iops=iops,
                                 AvailabilityZone=zone_id,
                                 SnapshotId=snapshot_id)
-        res.create_tags(Tags=[{
-            'Key': 'Name',
-            'Value': name
-        }, {
-            'Key': 'Description',
-            'Value': description
-        }])
+        res.name = name
+        if res.description:
+            res.description = description
         return res
 
     def delete(self, name):
@@ -298,54 +296,34 @@ class AWSSnapshotService(BaseSnapshotService):
 
     def __init__(self, provider):
         super(AWSSnapshotService, self).__init__(provider)
+        self.iface = EC2ServiceFilter(self.provider,
+                                      'snapshots', AWSSnapshot)
 
-    def get(self, snapshot_id):
-        """
-        Returns a snapshot given its id.
-        """
-        try:
-            snaps = self.provider.ec2_conn.get_all_snapshots(
-                snapshot_ids=[snapshot_id])
-        except EC2ResponseError as ec2e:
-            if ec2e.code == 'InvalidSnapshot.NotFound':
-                return None
-            raise ec2e
-        return AWSSnapshot(self.provider, snaps[0]) if snaps else None
-
-    def find(self, name, limit=None, marker=None):
-        """
-        Searches for a snapshot by a given list of attributes.
-        """
-        filtr = {'tag-value': name}
-        snaps = [AWSSnapshot(self.provider, snap) for snap in
-                 self.provider.ec2_conn.get_all_snapshots(filters=filtr)]
-        return ClientPagedResultList(self.provider, snaps,
-                                     limit=limit, marker=marker)
+    def get(self, vid):
+        """Returns a snapshot given its ID"""
+        return self.iface.get(vid, 'snapshot-id')
 
     def list(self, limit=None, marker=None):
-        """
-        List all snapshots.
-        """
-        snaps = [AWSSnapshot(self.provider, snap)
-                 for snap in self.provider.ec2_conn.get_all_snapshots(
-                 owner='self')]
-        return ClientPagedResultList(self.provider, snaps,
-                                     limit=limit, marker=marker)
+        """List all snapshots associated with this account"""
+        return self.iface.list(limit=limit, marker=marker)
+
+    def find(self, name, limit=None, marker=None):
+        """Searches for a snapshot by name"""
+        return self.iface.find(name, 'tag:Name', limit=limit, marker=marker)
 
     def create(self, name, volume, description=None):
-        """
-        Creates a new snapshot of a given volume.
-        """
+        """Creates a snapshot"""
         volume_id = volume.id if isinstance(volume, AWSVolume) else volume
+        res = self.iface.create('create_snapshot',
+                                VolumeId=volume_id)
+        res.name = name
+        if res.description:
+            res.description = description
+        return res
 
-        ec2_snap = self.provider.ec2_conn.create_snapshot(
-            volume_id,
-            description=description)
-        cb_snap = AWSSnapshot(self.provider, ec2_snap)
-        cb_snap.name = name
-        if description:
-            cb_snap.description = description
-        return cb_snap
+    def delete(self, name):
+        """Deletes a snapshot by name"""
+        return self.iface.delete(name, 'tag:Name')
 
 
 class AWSObjectStoreService(BaseObjectStoreService):