Repository: libcloud Updated Branches: refs/heads/trunk 33b96f81a -> 728bd8873
Add standardized snapshot states to VolumeSnapshot LIBCLOUD-759 closes: #602 Signed-off-by: Allard Hoeve <all...@byte.nl> Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/728bd887 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/728bd887 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/728bd887 Branch: refs/heads/trunk Commit: 728bd887336ec324c0697da5ba5349a1f7024585 Parents: 33b96f8 Author: Allard Hoeve <all...@byte.nl> Authored: Thu Oct 15 11:48:38 2015 +0200 Committer: Allard Hoeve <all...@byte.nl> Committed: Tue Nov 3 11:45:04 2015 +0100 ---------------------------------------------------------------------- CHANGES.rst | 4 +++ libcloud/compute/base.py | 12 ++++++--- libcloud/compute/drivers/ec2.py | 22 ++++++++++++++--- libcloud/compute/drivers/openstack.py | 20 +++++++++++++-- libcloud/compute/drivers/rackspace.py | 11 +++++++-- libcloud/compute/types.py | 26 ++++++++++++++------ .../compute/fixtures/ec2/describe_snapshots.xml | 2 +- libcloud/test/compute/test_ec2.py | 6 ++++- libcloud/test/compute/test_openstack.py | 4 ++- 9 files changed, 87 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/CHANGES.rst ---------------------------------------------------------------------- diff --git a/CHANGES.rst b/CHANGES.rst index 08a0d2e..d0512f5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -127,6 +127,10 @@ Compute (LIBCLOUD-759, GITHUB-603) [David Wilson] +- Standardize VolumeSnapshot states into the ``state`` attribute. + (LIBCLOUD-758, GITHUB-602) + [Allard Hoeve] + Storage ~~~~~~~ http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/compute/base.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/base.py b/libcloud/compute/base.py index c1b5bda..bac4787 100644 --- a/libcloud/compute/base.py +++ b/libcloud/compute/base.py @@ -554,7 +554,8 @@ class VolumeSnapshot(object): """ A base VolumeSnapshot class to derive from. """ - def __init__(self, id, driver, size=None, extra=None, created=None): + def __init__(self, id, driver, size=None, extra=None, created=None, + state=None): """ VolumeSnapshot constructor. @@ -574,12 +575,17 @@ class VolumeSnapshot(object): :param created: A datetime object that represents when the snapshot was created :type created: ``datetime.datetime`` + + :param state: A string representing the state the snapshot is + in. See `libcloud.compute.types.StorageVolumeState`. + :type state: ``str`` """ self.id = id self.driver = driver self.size = size self.extra = extra or {} self.created = created + self.state = state def destroy(self): """ @@ -590,8 +596,8 @@ class VolumeSnapshot(object): return self.driver.destroy_volume_snapshot(snapshot=self) def __repr__(self): - return ('<VolumeSnapshot id=%s size=%s driver=%s>' % - (self.id, self.size, self.driver.name)) + return ('<VolumeSnapshot id=%s size=%s driver=%s state=%s>' % + (self.id, self.size, self.driver.name, self.state)) class KeyPair(object): http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/compute/drivers/ec2.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py index 74c44b8..77d9686 100644 --- a/libcloud/compute/drivers/ec2.py +++ b/libcloud/compute/drivers/ec2.py @@ -43,7 +43,7 @@ from libcloud.compute.base import Node, NodeDriver, NodeLocation, NodeSize from libcloud.compute.base import NodeImage, StorageVolume, VolumeSnapshot from libcloud.compute.base import KeyPair from libcloud.compute.types import NodeState, KeyPairDoesNotExistError, \ - StorageVolumeState + StorageVolumeState, VolumeSnapshotState __all__ = [ 'API_VERSION', @@ -2107,6 +2107,12 @@ class BaseEC2NodeDriver(NodeDriver): 'error_deleting': StorageVolumeState.ERROR } + SNAPSHOT_STATE_MAP = { + 'pending': VolumeSnapshotState.CREATING, + 'completed': VolumeSnapshotState.AVAILABLE, + 'error': VolumeSnapshotState.ERROR, + } + def list_nodes(self, ex_node_ids=None, ex_filters=None): """ List all nodes @@ -4917,8 +4923,18 @@ class BaseEC2NodeDriver(NodeDriver): extra['tags'] = tags extra['name'] = name - return VolumeSnapshot(snapId, size=int(size), - driver=self, extra=extra, created=created) + # state + state = self.SNAPSHOT_STATE_MAP.get( + extra["state"], + VolumeSnapshotState.UNKNOWN + ) + + return VolumeSnapshot(snapId, + size=int(size), + driver=self, + extra=extra, + created=created, + state=state) def _to_key_pairs(self, elems): key_pairs = [self._to_key_pair(elem=elem) for elem in elems] http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/compute/drivers/openstack.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/openstack.py b/libcloud/compute/drivers/openstack.py index 4205d60..a8e4dbb 100644 --- a/libcloud/compute/drivers/openstack.py +++ b/libcloud/compute/drivers/openstack.py @@ -45,7 +45,8 @@ from libcloud.compute.base import NodeSize, NodeImage from libcloud.compute.base import (NodeDriver, Node, NodeLocation, StorageVolume, VolumeSnapshot) from libcloud.compute.base import KeyPair -from libcloud.compute.types import NodeState, StorageVolumeState, Provider +from libcloud.compute.types import NodeState, StorageVolumeState, Provider, \ + VolumeSnapshotState from libcloud.pricing import get_size_price from libcloud.utils.xml import findall @@ -119,6 +120,16 @@ class OpenStackNodeDriver(NodeDriver, OpenStackDriverMixin): 'error_extending': StorageVolumeState.ERROR, } + # http://developer.openstack.org/api-ref-blockstorage-v2.html#ext-backups-v2 + SNAPSHOT_STATE_MAP = { + 'creating': VolumeSnapshotState.CREATING, + 'available': VolumeSnapshotState.AVAILABLE, + 'deleting': VolumeSnapshotState.DELETING, + 'error': VolumeSnapshotState.ERROR, + 'restoring': VolumeSnapshotState.RESTORING, + 'error_restoring': VolumeSnapshotState.ERROR + } + def __new__(cls, key, secret=None, secure=True, host=None, port=None, api_version=DEFAULT_API_VERSION, **kwargs): if cls is OpenStackNodeDriver: @@ -2165,6 +2176,11 @@ class OpenStack_1_1_NodeDriver(OpenStackNodeDriver): 'description': description, 'status': status} + state = self.SNAPSHOT_STATE_MAP.get( + status, + VolumeSnapshotState.UNKNOWN + ) + try: created_dt = parse_date(created_at) except ValueError: @@ -2172,7 +2188,7 @@ class OpenStack_1_1_NodeDriver(OpenStackNodeDriver): snapshot = VolumeSnapshot(id=data['id'], driver=self, size=data['size'], extra=extra, - created=created_dt) + created=created_dt, state=state) return snapshot def _to_size(self, api_flavor, price=None, bandwidth=None): http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/compute/drivers/rackspace.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/rackspace.py b/libcloud/compute/drivers/rackspace.py index 8085220..4fa098a 100644 --- a/libcloud/compute/drivers/rackspace.py +++ b/libcloud/compute/drivers/rackspace.py @@ -14,7 +14,7 @@ """ Rackspace driver """ -from libcloud.compute.types import Provider, LibcloudError +from libcloud.compute.types import Provider, LibcloudError, VolumeSnapshotState from libcloud.compute.base import NodeLocation, VolumeSnapshot from libcloud.compute.drivers.openstack import OpenStack_1_0_Connection,\ OpenStack_1_0_NodeDriver, OpenStack_1_0_Response @@ -216,6 +216,11 @@ class RackspaceNodeDriver(OpenStack_1_1_NodeDriver): 'description': api_node['displayDescription'], 'status': api_node['status']} + state = self.SNAPSHOT_STATE_MAP.get( + api_node['status'], + VolumeSnapshotState.UNKNOWN + ) + try: created_td = parse_date(api_node['createdAt']) except ValueError: @@ -223,7 +228,9 @@ class RackspaceNodeDriver(OpenStack_1_1_NodeDriver): snapshot = VolumeSnapshot(id=api_node['id'], driver=self, size=api_node['size'], - extra=extra, created=created_td) + extra=extra, + created=created_td, + state=state) return snapshot def _ex_connection_class_kwargs(self): http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/compute/types.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/types.py b/libcloud/compute/types.py index ddb9365..022df90 100644 --- a/libcloud/compute/types.py +++ b/libcloud/compute/types.py @@ -236,15 +236,27 @@ class StorageVolumeState(object): """ Standard states of a StorageVolume """ + AVAILABLE = "available" + ERROR = "error" + INUSE = "in_use" + CREATING = "creating" + DELETING = "deleting" + DELETED = "deleted" + BACKUP = "backup" + ATTACHING = "attaching" + UNKNOWN = "unknown" + + +class VolumeSnapshotState(object): + """ + Standard states of VolumeSnapshots + """ AVAILABLE = 0 ERROR = 1 - INUSE = 2 - CREATING = 3 - DELETING = 4 - DELETED = 5 - BACKUP = 6 - ATTACHING = 7 - UNKNOWN = 8 + CREATING = 2 + DELETING = 3 + RESTORING = 4 + UNKNOWN = 5 class Architecture(object): http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/test/compute/fixtures/ec2/describe_snapshots.xml ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/ec2/describe_snapshots.xml b/libcloud/test/compute/fixtures/ec2/describe_snapshots.xml index 2faf38f..95a8b4b 100644 --- a/libcloud/test/compute/fixtures/ec2/describe_snapshots.xml +++ b/libcloud/test/compute/fixtures/ec2/describe_snapshots.xml @@ -22,7 +22,7 @@ <item> <snapshotId>snap-18349159</snapshotId> <volumeId>vol-b5a2c1v9</volumeId> - <status>pending</status> + <status>completed</status> <startTime>2013-09-15T16:00:30.000Z</startTime> <progress>30%</progress> <ownerId>1938218230</ownerId> http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/test/compute/test_ec2.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_ec2.py b/libcloud/test/compute/test_ec2.py index 3f74cec..6b751b6 100644 --- a/libcloud/test/compute/test_ec2.py +++ b/libcloud/test/compute/test_ec2.py @@ -39,7 +39,8 @@ from libcloud.compute.drivers.ec2 import ExEC2AvailabilityZone from libcloud.compute.drivers.ec2 import EC2NetworkSubnet from libcloud.compute.base import Node, NodeImage, NodeSize, NodeLocation from libcloud.compute.base import StorageVolume, VolumeSnapshot -from libcloud.compute.types import KeyPairDoesNotExistError, StorageVolumeState +from libcloud.compute.types import KeyPairDoesNotExistError, StorageVolumeState, \ + VolumeSnapshotState from libcloud.test import MockHttpTestCase, LibcloudTestCase from libcloud.test.compute import TestCaseMixin @@ -866,6 +867,7 @@ class EC2Tests(LibcloudTestCase, TestCaseMixin): self.assertEqual('Test snapshot', snap.extra['name']) self.assertEqual(vol.id, snap.extra['volume_id']) self.assertEqual('pending', snap.extra['state']) + self.assertEqual(VolumeSnapshotState.CREATING, snap.state) # 2013-08-15T16:22:30.000Z self.assertEqual(datetime(2013, 8, 15, 16, 22, 30, tzinfo=UTC), snap.created) @@ -875,11 +877,13 @@ class EC2Tests(LibcloudTestCase, TestCaseMixin): self.assertEqual(len(snaps), 3) self.assertEqual('snap-428abd35', snaps[0].id) + self.assertEqual(VolumeSnapshotState.CREATING, snaps[0].state) self.assertEqual('vol-e020df80', snaps[0].extra['volume_id']) self.assertEqual(30, snaps[0].size) self.assertEqual('Daily Backup', snaps[0].extra['description']) self.assertEqual('snap-18349159', snaps[1].id) + self.assertEqual(VolumeSnapshotState.AVAILABLE, snaps[1].state) self.assertEqual('vol-b5a2c1v9', snaps[1].extra['volume_id']) self.assertEqual(15, snaps[1].size) self.assertEqual('Weekly backup', snaps[1].extra['description']) http://git-wip-us.apache.org/repos/asf/libcloud/blob/728bd887/libcloud/test/compute/test_openstack.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_openstack.py b/libcloud/test/compute/test_openstack.py index 677b1be..7a63b70 100644 --- a/libcloud/test/compute/test_openstack.py +++ b/libcloud/test/compute/test_openstack.py @@ -34,7 +34,8 @@ from libcloud.utils.py3 import u from libcloud.common.types import InvalidCredsError, MalformedResponseError, \ LibcloudError -from libcloud.compute.types import Provider, KeyPairDoesNotExistError, StorageVolumeState +from libcloud.compute.types import Provider, KeyPairDoesNotExistError, StorageVolumeState, \ + VolumeSnapshotState from libcloud.compute.providers import get_driver from libcloud.compute.drivers.openstack import ( OpenStack_1_0_NodeDriver, OpenStack_1_0_Response, @@ -1431,6 +1432,7 @@ class OpenStack_1_1_Tests(unittest.TestCase, TestCaseMixin): self.assertEqual(snapshots[0].created, datetime.datetime(2012, 2, 29, 3, 50, 7, tzinfo=UTC)) self.assertEqual(snapshots[0].extra['created'], "2012-02-29T03:50:07Z") self.assertEqual(snapshots[0].extra['name'], 'snap-001') + self.assertEqual(snapshots[0].state, VolumeSnapshotState.AVAILABLE) # invalid date is parsed as None assert snapshots[2].created is None