From d67eb913fb125d1e98b7b2fbb9055eaf751b904c Mon Sep 17 00:00:00 2001 From: chenying Date: Tue, 24 Oct 2017 19:41:52 +0800 Subject: [PATCH] Add verify action for the share snapshot protection plugin Change-Id: I3da9b9f5b53ea7e1ab6ea518fa4d9582f4eeb095 Implements: blueprint support-verify-the-checkpoint-api --- .../share/share_snapshot_plugin.py | 49 +++++++++++++++++++ .../share/share_snapshot_plugin_schemas.py | 6 +++ .../test_manila_protection_plugin.py | 24 +++++++++ 3 files changed, 79 insertions(+) diff --git a/karbor/services/protection/protection_plugins/share/share_snapshot_plugin.py b/karbor/services/protection/protection_plugins/share/share_snapshot_plugin.py index b77ba1d3..5aa5ddac 100644 --- a/karbor/services/protection/protection_plugins/share/share_snapshot_plugin.py +++ b/karbor/services/protection/protection_plugins/share/share_snapshot_plugin.py @@ -218,6 +218,48 @@ class RestoreOperation(protection_plugin.Operation): original_share_id) +class VerifyOperation(protection_plugin.Operation): + def __init__(self): + super(VerifyOperation, self).__init__() + + def on_main(self, checkpoint, resource, context, parameters, **kwargs): + original_share_id = resource.id + bank_section = checkpoint.get_resource_bank_section(original_share_id) + manila_client = ClientFactory.create_client('manila', context) + resource_metadata = bank_section.get_object('metadata') + LOG.info('Verifying the share snapshot, share_id: %s', + original_share_id) + + update_method = partial( + utils.update_resource_verify_result, + kwargs.get('verify'), resource.type, original_share_id) + + snapshot_id = resource_metadata['snapshot_id'] + try: + share_snapshot = manila_client.share_snapshots.get(snapshot_id) + snapshot_status = share_snapshot.status + except Exception as ex: + LOG.error('Getting share snapshot (snapshot_id: %(snapshot_id)s):' + '%(reason)s fails', + {'snapshot_id': snapshot_id, 'reason': ex}) + reason = 'Getting share snapshot fails.' + update_method(constants.RESOURCE_STATUS_ERROR, reason) + raise + + if snapshot_status == 'available': + update_method(constants.RESOURCE_STATUS_AVAILABLE) + else: + reason = ('The status of share snapshot status is %s.' + % snapshot_status) + update_method(snapshot_status, reason) + raise exception.VerifyResourceFailed( + name="Share snapshot", + reason=reason, + resource_id=original_share_id, + resource_type=resource.type + ) + + class DeleteOperation(protection_plugin.Operation): def __init__(self, poll_interval): super(DeleteOperation, self).__init__() @@ -287,6 +329,10 @@ class ManilaSnapshotProtectionPlugin(protection_plugin.ProtectionPlugin): def get_restore_schema(cls, resources_type): return share_schemas.RESTORE_SCHEMA + @classmethod + def get_verify_schema(cls, resources_type): + return share_schemas.VERIFY_SCHEMA + @classmethod def get_saved_info_schema(cls, resources_type): return share_schemas.SAVED_INFO_SCHEMA @@ -301,5 +347,8 @@ class ManilaSnapshotProtectionPlugin(protection_plugin.ProtectionPlugin): def get_restore_operation(self, resource): return RestoreOperation(self._poll_interval) + def get_verify_operation(self, resource): + return VerifyOperation() + def get_delete_operation(self, resource): return DeleteOperation(self._poll_interval) diff --git a/karbor/services/protection/protection_plugins/share/share_snapshot_plugin_schemas.py b/karbor/services/protection/protection_plugins/share/share_snapshot_plugin_schemas.py index 93f08d42..45138229 100644 --- a/karbor/services/protection/protection_plugins/share/share_snapshot_plugin_schemas.py +++ b/karbor/services/protection/protection_plugins/share/share_snapshot_plugin_schemas.py @@ -50,6 +50,12 @@ RESTORE_SCHEMA = { "required": ["restore_name"] } +VERIFY_SCHEMA = { + "title": "Share snapshot Verify", + "type": "object", + "properties": {} +} + SAVED_INFO_SCHEMA = { "title": "Share Protection Saved Info", "type": "object", diff --git a/karbor/tests/unit/protection/test_manila_protection_plugin.py b/karbor/tests/unit/protection/test_manila_protection_plugin.py index c4e79510..968d515b 100644 --- a/karbor/tests/unit/protection/test_manila_protection_plugin.py +++ b/karbor/tests/unit/protection/test_manila_protection_plugin.py @@ -192,6 +192,30 @@ class ManilaProtectionPluginTest(base.TestCase): call_hooks(delete_operation, self.checkpoint, resource, self.cntxt, {}) + @mock.patch('karbor.services.protection.protection_plugins.utils.' + 'update_resource_verify_result') + @mock.patch('karbor.services.protection.clients.manila.create') + def test_verify_snapshot(self, mock_manila_create, mock_update_verify): + resource = Resource(id="123", + type=constants.SHARE_RESOURCE_TYPE, + name='fake') + mock_manila_create.return_value = self.manila_client + self.manila_client.share_snapshots.get = mock.MagicMock() + self.manila_client.share_snapshots.get.return_value = Snapshot( + id="1234", + status="available" + ) + + fake_bank_section.get_object = mock.MagicMock() + fake_bank_section.get_object.return_value = { + "snapshot_id": "1234"} + + verify_operation = self.plugin.get_verify_operation(resource) + call_hooks(verify_operation, self.checkpoint, resource, self.cntxt, + {}) + mock_update_verify.assert_called_with( + None, resource.type, resource.id, 'available') + def test_get_supported_resources_types(self): types = self.plugin.get_supported_resources_types() self.assertEqual([constants.SHARE_RESOURCE_TYPE], types)