From 79c225e99e9401c9e33e31397d4ddd55a0e0b8ac Mon Sep 17 00:00:00 2001 From: Jonas Depoix Date: Thu, 22 Jun 2017 11:29:38 +0200 Subject: [PATCH] remote host is updated after system switch --- .../migration_commander/cloud_commanding.py | 19 ++++++++ .../tests/test_cloud_commanding.py | 45 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/goto_cloud/migration_commander/cloud_commanding.py b/goto_cloud/migration_commander/cloud_commanding.py index c317e76..c79a7c8 100644 --- a/goto_cloud/migration_commander/cloud_commanding.py +++ b/goto_cloud/migration_commander/cloud_commanding.py @@ -114,9 +114,28 @@ def _execute(self): ) if bootstrap_interface: self._cloud_manager.delete_nic(self._target.remote_host.cloud_metadata['id'], bootstrap_interface['id']) + self._update_remote_host() else: self._add_error('bootstrapping network interface could not be found') + def _update_remote_host(self): + source_remote_host = self._source.remote_host + old_remote_host = self._target.remote_host + + self._target.remote_host = RemoteHost.objects.create( + address=self._target.blueprint['network_interfaces'][0]['ip'], + cloud_metadata=old_remote_host.cloud_metadata, + username=source_remote_host.username, + os=source_remote_host.os, + version=source_remote_host.version, + port=source_remote_host.port, + password=source_remote_host.password, + private_key=source_remote_host.private_key, + ) + + self._target.save() + old_remote_host.delete() + class ConfigureBootDeviceCommand(CloudCommand): def _execute(self): diff --git a/goto_cloud/migration_commander/tests/test_cloud_commanding.py b/goto_cloud/migration_commander/tests/test_cloud_commanding.py index d0f9db8..476a051 100644 --- a/goto_cloud/migration_commander/tests/test_cloud_commanding.py +++ b/goto_cloud/migration_commander/tests/test_cloud_commanding.py @@ -219,6 +219,51 @@ def test_execute(self, mocked_delete_nic): mocked_delete_nic.assert_called_with(self.CLOUD_DATA['id'], self.BOOTSTRAP_INTERFACE_ID) + @patch('cloud_management.public.CloudManager.delete_nic', lambda *args, **kwargs: None) + def test_execute__remote_host_updated(self): + self._init_test_data() + cloud_metadata = self.source.target.remote_host.cloud_metadata + old_remote_host_id = self.source.target.remote_host.id + + DeleteBootstrapNetworkInterfaceCommand(self.source).execute() + + self.source.refresh_from_db() + + self.assertEqual( + self.source.target.remote_host.address, + self.source.target.blueprint['network_interfaces'][0]['ip'] + ) + self.assertEqual( + self.source.target.remote_host.cloud_metadata, + cloud_metadata + ) + self.assertEqual( + self.source.target.remote_host.username, + self.source.remote_host.username + ) + self.assertEqual( + self.source.target.remote_host.os, + self.source.remote_host.os + ) + self.assertEqual( + self.source.target.remote_host.version, + self.source.remote_host.version + ) + self.assertEqual( + self.source.target.remote_host.port, + self.source.remote_host.port + ) + self.assertEqual( + self.source.target.remote_host.password, + self.source.remote_host.password + ) + self.assertEqual( + self.source.target.remote_host.private_key, + self.source.remote_host.private_key + ) + self.assertFalse(RemoteHost.objects.filter(id=old_remote_host_id).exists()) + + def test_execute__no_bootstrapping_interface_found(self): self._init_test_data() self.source.target.remote_host.refresh_from_db()