Browse Source

Merge "Add support to rewrite the flavor id in restore"

Zuul 3 months ago
parent
commit
e0258aac27

+ 4
- 0
karbor/services/protection/protection_plugins/server/nova_protection_plugin.py View File

@@ -230,6 +230,10 @@ class RestoreOperation(protection_plugin.Operation):
230 230
 
231 231
             # restore server instance
232 232
             restore_net_id = parameters.get("restore_net_id", None)
233
+            restore_flavor_id = parameters.get("restore_flavor_id", None)
234
+            if restore_flavor_id:
235
+                resource_definition["server_metadata"]['flavor'] = (
236
+                    restore_flavor_id)
233 237
             new_server_id = self._restore_server_instance(
234 238
                 nova_client, new_resources, original_server_id,
235 239
                 parameters.get("restore_name", "karbor-restore-server"),

+ 5
- 0
karbor/services/protection/protection_plugins/server/server_plugin_schemas.py View File

@@ -30,6 +30,11 @@ RESTORE_SCHEMA = {
30 30
             "type": "string",
31 31
             "title": "Restore Server Net Id",
32 32
             "description": "The net id of the restore server"
33
+        },
34
+        "restore_flavor_id": {
35
+            "type": "string",
36
+            "title": "Restore Server Flavor Id",
37
+            "description": "The flavor id of the restore server"
33 38
         }
34 39
     },
35 40
     "required": ["restore_name"]

+ 59
- 3
karbor/tests/unit/protection/test_nova_protection_plugin.py View File

@@ -29,7 +29,7 @@ from karbor.tests import base
29 29
 
30 30
 class Server(object):
31 31
     def __init__(self, id, addresses, availability_zone,
32
-                 flavor, key_name, security_groups):
32
+                 flavor, key_name, security_groups, status):
33 33
         super(Server, self).__init__()
34 34
         self.id = id
35 35
         self.addresses = addresses
@@ -37,6 +37,7 @@ class Server(object):
37 37
         self.flavor = flavor
38 38
         self.key_name = key_name
39 39
         self.security_groups = security_groups
40
+        self.status = status
40 41
 
41 42
 
42 43
 class Volume(object):
@@ -94,7 +95,8 @@ FakeServers = {
94 95
                                    'rel': 'bookmark'}
95 96
                               ]},
96 97
                       key_name=None,
97
-                      security_groups="default"),
98
+                      security_groups="default",
99
+                      status="ACTIVE"),
98 100
     "vm_id_2": Server(id="vm_id_2",
99 101
                       addresses={'fake_net': [
100 102
                           {'OS-EXT-IPS-MAC:mac_addr': 'mac_address_2',
@@ -109,7 +111,8 @@ FakeServers = {
109 111
                                    'rel': 'bookmark'}
110 112
                               ]},
111 113
                       key_name=None,
112
-                      security_groups="default")
114
+                      security_groups="default",
115
+                      status="ACTIVE")
113 116
 }
114 117
 
115 118
 FakeVolumes = {
@@ -454,6 +457,59 @@ class NovaProtectionPluginTest(base.TestCase):
454 457
         call_hooks(delete_operation, self.checkpoint, resource, self.cntxt,
455 458
                    {})
456 459
 
460
+    @mock.patch('karbor.services.protection.protection_plugins.utils.'
461
+                'update_resource_restore_result')
462
+    @mock.patch('karbor.services.protection.clients.neutron.create')
463
+    @mock.patch('karbor.services.protection.clients.glance.create')
464
+    @mock.patch('karbor.services.protection.clients.nova.create')
465
+    @mock.patch('karbor.services.protection.clients.cinder.create')
466
+    def test_restore_backup_with_parameters(self, mock_cinder_client,
467
+                                            mock_nova_client,
468
+                                            mock_glance_client,
469
+                                            mock_neutron_client,
470
+                                            mock_update_result):
471
+        resource = Resource(id='vm_id_1',
472
+                            type=constants.SERVER_RESOURCE_TYPE,
473
+                            name='fake_vm')
474
+        fake_bank._plugin._objects[
475
+            "/resource_data/checkpoint_id/vm_id_1/metadata"] = {
476
+            "server_metadata": {
477
+                "availability_zone": "nova",
478
+                "key_name": None,
479
+                "floating_ips": [],
480
+                "flavor": "fake_flavor_id_1",
481
+                "networks": ["fake_net_id_1"],
482
+                "security_groups": [{"name": "default"}]},
483
+            "boot_metadata": {
484
+                "boot_image_id": "fake_image_id",
485
+                "boot_device_type": "image"},
486
+            "attach_metadata": {},
487
+            "resource_id": "vm_id_1"}
488
+        restore_operation = self.plugin.get_restore_operation(resource)
489
+        mock_cinder_client.return_value = self.cinder_client
490
+        mock_nova_client.return_value = self.nova_client
491
+        mock_glance_client.return_value = self.glance_client
492
+        mock_neutron_client.return_value = self.neutron_client
493
+        parameters = {'restore_net_id': 'fake_net_id_2',
494
+                      'restore_flavor_id': 'fake_flavor_id_2'}
495
+        checkpoint = Checkpoint()
496
+        new_resources = {"new_resources": {"fake_image_id": "fake_image_id"}}
497
+        self.nova_client.servers.create = mock.MagicMock()
498
+        self.nova_client.servers.create.return_value = FakeServers['vm_id_2']
499
+        call_hooks(restore_operation, checkpoint, resource, self.cntxt,
500
+                   parameters, **new_resources)
501
+        properties = {
502
+            "availability_zone": "nova",
503
+            "flavor": "fake_flavor_id_2",
504
+            "name": "karbor-restore-server",
505
+            "image": "fake_image_id",
506
+            "key_name": None,
507
+            "security_groups": ['default'],
508
+            "nics": [{'net-id': 'fake_net_id_2'}],
509
+            "userdata": None
510
+        }
511
+        self.nova_client.servers.create.assert_called_with(**properties)
512
+
457 513
     @mock.patch('karbor.services.protection.protection_plugins.utils.'
458 514
                 'update_resource_verify_result')
459 515
     def test_verify_backup(self,  mock_update_verify):

Loading…
Cancel
Save