From a78cfa0270e160f785a592403d23d775bf93d8bb Mon Sep 17 00:00:00 2001 From: chenying Date: Wed, 25 Oct 2017 11:04:01 +0800 Subject: [PATCH] Add verify action for the server backup protection plugin Change-Id: I0c45753edd6c0a9ba7bf65d1a58c5c5765cd9bb0 Implements: blueprint support-verify-the-checkpoint-api --- .../server/nova_protection_plugin.py | 37 +++++++++++++++++++ .../server/server_plugin_schemas.py | 6 +++ .../protection/test_nova_protection_plugin.py | 17 +++++++++ 3 files changed, 60 insertions(+) diff --git a/karbor/services/protection/protection_plugins/server/nova_protection_plugin.py b/karbor/services/protection/protection_plugins/server/nova_protection_plugin.py index fe61812f..55933e26 100644 --- a/karbor/services/protection/protection_plugins/server/nova_protection_plugin.py +++ b/karbor/services/protection/protection_plugins/server/nova_protection_plugin.py @@ -181,6 +181,36 @@ class DeleteOperation(protection_plugin.Operation): resource_type=constants.SERVER_RESOURCE_TYPE) +class VerifyOperation(protection_plugin.Operation): + def __init__(self): + super(VerifyOperation, self).__init__() + + def on_main(self, checkpoint, resource, context, parameters, **kwargs): + original_server_id = resource.id + bank_section = checkpoint.get_resource_bank_section( + original_server_id) + LOG.info('Verifying the server backup, server_id: %s', + original_server_id) + + update_method = partial( + utils.update_resource_verify_result, + kwargs.get('verify'), resource.type, original_server_id) + + backup_status = bank_section.get_object("status") + + if backup_status == constants.RESOURCE_STATUS_AVAILABLE: + update_method(constants.RESOURCE_STATUS_AVAILABLE) + else: + reason = ('The status of server backup status is %s.' + % backup_status) + update_method(backup_status, reason) + raise exception.VerifyResourceFailed( + name="Server backup", + reason=reason, + resource_id=original_server_id, + resource_type=resource.type) + + class RestoreOperation(protection_plugin.Operation): def __init__(self, poll_interval): super(RestoreOperation, self).__init__() @@ -421,6 +451,10 @@ class NovaProtectionPlugin(protection_plugin.ProtectionPlugin): def get_restore_schema(cls, resource_type): return server_plugin_schemas.RESTORE_SCHEMA + @classmethod + def get_verify_schema(cls, resources_type): + return server_plugin_schemas.VERIFY_SCHEMA + @classmethod def get_saved_info_schema(cls, resource_type): return server_plugin_schemas.SAVED_INFO_SCHEMA @@ -435,5 +469,8 @@ class NovaProtectionPlugin(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() diff --git a/karbor/services/protection/protection_plugins/server/server_plugin_schemas.py b/karbor/services/protection/protection_plugins/server/server_plugin_schemas.py index be8682a9..7410f6b2 100644 --- a/karbor/services/protection/protection_plugins/server/server_plugin_schemas.py +++ b/karbor/services/protection/protection_plugins/server/server_plugin_schemas.py @@ -30,6 +30,12 @@ RESTORE_SCHEMA = { "required": ["restore_name"] } +VERIFY_SCHEMA = { + "title": "Server Protection Verify", + "type": "object", + "properties": {} +} + # TODO(luobin) SAVED_INFO_SCHEMA = { "title": "Server Protection Saved Info", diff --git a/karbor/tests/unit/protection/test_nova_protection_plugin.py b/karbor/tests/unit/protection/test_nova_protection_plugin.py index 0cc602d3..5ffba26b 100644 --- a/karbor/tests/unit/protection/test_nova_protection_plugin.py +++ b/karbor/tests/unit/protection/test_nova_protection_plugin.py @@ -454,6 +454,23 @@ class NovaProtectionPluginTest(base.TestCase): call_hooks(delete_operation, self.checkpoint, resource, self.cntxt, {}) + @mock.patch('karbor.services.protection.protection_plugins.utils.' + 'update_resource_verify_result') + def test_verify_backup(self, mock_update_verify): + resource = Resource(id="123", + type=constants.SERVER_RESOURCE_TYPE, + name='fake') + + fake_bank._plugin._objects[ + "/resource_data/checkpoint_id/123/status" + ] = "available" + + 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.SERVER_RESOURCE_TYPE], types)