From f352fd979046449c53105e2f17564a83289f0435 Mon Sep 17 00:00:00 2001 From: Brian Curtin Date: Wed, 11 Feb 2015 18:47:27 -0600 Subject: [PATCH] Add the Snapshot resource for the Volume service Change-Id: Ibb94ba07b55a0b78ebaf2046a88090554e6731f2 --- doc/source/users/resources/volume/index.rst | 1 + .../users/resources/volume/v2/snapshot.rst | 21 +++++ openstack/tests/volume/v2/test_snapshot.py | 81 +++++++++++++++++++ openstack/volume/v2/snapshot.py | 61 ++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 doc/source/users/resources/volume/v2/snapshot.rst create mode 100644 openstack/tests/volume/v2/test_snapshot.py create mode 100644 openstack/volume/v2/snapshot.py diff --git a/doc/source/users/resources/volume/index.rst b/doc/source/users/resources/volume/index.rst index ebf78763..a7050fc1 100644 --- a/doc/source/users/resources/volume/index.rst +++ b/doc/source/users/resources/volume/index.rst @@ -4,4 +4,5 @@ Volume Resources .. toctree:: :maxdepth: 1 + v2/snapshot v2/type diff --git a/doc/source/users/resources/volume/v2/snapshot.rst b/doc/source/users/resources/volume/v2/snapshot.rst new file mode 100644 index 00000000..972205b2 --- /dev/null +++ b/doc/source/users/resources/volume/v2/snapshot.rst @@ -0,0 +1,21 @@ +openstack.volume.v2.snapshot +============================ + +.. automodule:: openstack.volume.v2.snapshot + +The Snapshot Class +------------------ + +The ``Snapshot`` class inherits from :class:`~openstack.resource.Resource`. + +.. autoclass:: openstack.volume.v2.snapshot.Snapshot + :members: + +The SnapshotDetail Class +------------------------ + +The ``SnapshotDetail`` class inherits from +:class:`~openstack.volume.v2.snapshot.Snapshot`. + +.. autoclass:: openstack.volume.v2.snapshot.SnapshotDetail + :members: diff --git a/openstack/tests/volume/v2/test_snapshot.py b/openstack/tests/volume/v2/test_snapshot.py new file mode 100644 index 00000000..dc08aa6f --- /dev/null +++ b/openstack/tests/volume/v2/test_snapshot.py @@ -0,0 +1,81 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import testtools + +from openstack.volume.v2 import snapshot + +FAKE_ID = "ffa9bc5e-1172-4021-acaf-cdcd78a9584d" + +SNAPSHOT = { + "status": "creating", + "description": "Daily backup", + "created_at": "2013-02-25T03:56:53.081642", + "metadata": {}, + "volume_id": "5aa119a8-d25b-45a7-8d1b-88e127885635", + "size": 1, + "id": FAKE_ID, + "name": "snap-001" +} + +DETAILS = { + "os-extended-snapshot-attributes:progress": "100%", + "os-extended-snapshot-attributes:project_id": + "0c2eba2c5af04d3f9e9d0d410b371fde" +} + +DETAILED_SNAPSHOT = SNAPSHOT.copy() +DETAILED_SNAPSHOT.update(**DETAILS) + + +class TestSnapshot(testtools.TestCase): + + def test_basic(self): + sot = snapshot.Snapshot(SNAPSHOT) + self.assertEqual("snapshot", sot.resource_key) + self.assertEqual("snapshots", sot.resources_key) + self.assertEqual("id", sot.id_attribute) + self.assertEqual("/snapshots", sot.base_path) + self.assertEqual("volume", sot.service.service_type) + self.assertTrue(sot.allow_update) + self.assertTrue(sot.allow_create) + self.assertTrue(sot.allow_retrieve) + self.assertTrue(sot.allow_delete) + self.assertFalse(sot.allow_list) + + def test_create_basic(self): + sot = snapshot.Snapshot(SNAPSHOT) + self.assertEqual(SNAPSHOT["id"], sot.id) + self.assertEqual(SNAPSHOT["status"], sot.status) + self.assertEqual(SNAPSHOT["created_at"], sot.created) + self.assertEqual(SNAPSHOT["metadata"], sot.metadata) + self.assertEqual(SNAPSHOT["volume_id"], sot.volume) + self.assertEqual(SNAPSHOT["size"], sot.size) + self.assertEqual(SNAPSHOT["name"], sot.name) + + +class TestSnapshotDetail(testtools.TestCase): + + def test_basic(self): + sot = snapshot.SnapshotDetail(DETAILED_SNAPSHOT) + self.assertTrue(isinstance(sot, snapshot.Snapshot)) + self.assertEqual("/snapshots/detail", sot.base_path) + + def test_create_detailed(self): + sot = snapshot.SnapshotDetail(DETAILED_SNAPSHOT) + + self.assertEqual( + DETAILED_SNAPSHOT["os-extended-snapshot-attributes:progress"], + sot.progress) + self.assertEqual( + DETAILED_SNAPSHOT["os-extended-snapshot-attributes:project_id"], + sot.project_id) diff --git a/openstack/volume/v2/snapshot.py b/openstack/volume/v2/snapshot.py new file mode 100644 index 00000000..120020f1 --- /dev/null +++ b/openstack/volume/v2/snapshot.py @@ -0,0 +1,61 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from openstack import resource +from openstack.volume import volume_service + + +class Snapshot(resource.Resource): + resource_key = "snapshot" + resources_key = "snapshots" + base_path = "/snapshots" + service = volume_service.VolumeService() + + # capabilities + allow_retrieve = True + allow_create = True + allow_delete = True + allow_update = True + put_update = True + + # Properties + #: A UUID representing this snapshot. + id = resource.prop("id") + #: Name of the snapshot. Default is None. + name = resource.prop("name") + + #: The current status of this snapshot. Potential values are creating, + #: available, deleting, error, and error_deleting. + status = resource.prop("status") + #: Description of snapshot. Default is None. + description = resource.prop("description") + #: The timestamp of this snapshot creation. + created = resource.prop("created_at") + #: Metadata associated with this snapshot. + metadata = resource.prop("metadata", type=dict) + #: The ID of the volume this snapshot was taken of. + volume = resource.prop("volume_id") + #: The size of the volume, in GBs. + size = resource.prop("size", type=int) + #: Indicate whether to snapshot, even if the volume is attached. + #: Default is False. + force = resource.prop("force", type=bool) + + +class SnapshotDetail(Snapshot): + + base_path = "/snapshots/detail" + + #: The percentage of completeness the snapshot is currently at. + progress = resource.prop("os-extended-snapshot-attributes:progress") + #: The tenant ID this snapshot is associatd with. + project_id = resource.prop("os-extended-snapshot-attributes:project_id")