From 941a8db372f1f28a2fa756ad42239b9a3f4e8e8e Mon Sep 17 00:00:00 2001 From: Chris Yeoh Date: Mon, 11 Aug 2014 17:13:36 +0930 Subject: [PATCH] Convert migrate_server v3 plugin to v2.1 Changes required to have v3 plugin natively support the v2 API. The migrate_live action is reverted back to os-migrateLive Partially implements blueprint v2-on-v3-api Change-Id: Ib3332ecfbf47ed6cad6108399083f0c26b7eebfe --- .../live-migrate-server.json | 4 +- .../compute/plugins/v3/migrate_server.py | 10 +-- .../compute/schemas/v3/migrate_server.py | 14 ++- .../v3 => contrib}/test_migrate_server.py | 88 +++++++++++-------- .../live-migrate-server.json.tpl | 2 +- 5 files changed, 71 insertions(+), 47 deletions(-) rename nova/tests/api/openstack/compute/{plugins/v3 => contrib}/test_migrate_server.py (70%) diff --git a/doc/v3/api_samples/os-migrate-server/live-migrate-server.json b/doc/v3/api_samples/os-migrate-server/live-migrate-server.json index 10369816aae4..251863d7855e 100644 --- a/doc/v3/api_samples/os-migrate-server/live-migrate-server.json +++ b/doc/v3/api_samples/os-migrate-server/live-migrate-server.json @@ -1,7 +1,7 @@ { - "migrate_live": { + "os-migrateLive": { "host": "01c0cadef72d47e28a672a76060d492c", "block_migration": false, "disk_over_commit": false } -} \ No newline at end of file +} diff --git a/nova/api/openstack/compute/plugins/v3/migrate_server.py b/nova/api/openstack/compute/plugins/v3/migrate_server.py index fb6bfd45f92c..4c7070962ebb 100644 --- a/nova/api/openstack/compute/plugins/v3/migrate_server.py +++ b/nova/api/openstack/compute/plugins/v3/migrate_server.py @@ -64,16 +64,16 @@ class MigrateServerController(wsgi.Controller): return webob.Response(status_int=202) @extensions.expected_errors((400, 404, 409)) - @wsgi.action('migrate_live') + @wsgi.action('os-migrateLive') @validation.schema(migrate_server.migrate_live) def _migrate_live(self, req, id, body): """Permit admins to (live) migrate a server to a new host.""" context = req.environ["nova.context"] authorize(context, 'migrate_live') - block_migration = body["migrate_live"]["block_migration"] - disk_over_commit = body["migrate_live"]["disk_over_commit"] - host = body["migrate_live"]["host"] + block_migration = body["os-migrateLive"]["block_migration"] + disk_over_commit = body["os-migrateLive"]["disk_over_commit"] + host = body["os-migrateLive"]["host"] block_migration = strutils.bool_from_string(block_migration, strict=True) @@ -101,7 +101,7 @@ class MigrateServerController(wsgi.Controller): raise exc.HTTPConflict(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, - 'migrate_live') + 'os-migrateLive') return webob.Response(status_int=202) diff --git a/nova/api/openstack/compute/schemas/v3/migrate_server.py b/nova/api/openstack/compute/schemas/v3/migrate_server.py index 71ea97e2e5e1..602435df2d12 100644 --- a/nova/api/openstack/compute/schemas/v3/migrate_server.py +++ b/nova/api/openstack/compute/schemas/v3/migrate_server.py @@ -12,23 +12,31 @@ # License for the specific language governing permissions and limitations # under the License. +import copy + from nova.api.validation import parameter_types +host = copy.deepcopy(parameter_types.hostname) +host['type'] = ['string', 'null'] + migrate_live = { 'type': 'object', 'properties': { - 'migrate_live': { + 'os-migrateLive': { 'type': 'object', 'properties': { 'block_migration': parameter_types.boolean, 'disk_over_commit': parameter_types.boolean, - 'host': parameter_types.hostname, + 'host': host }, 'required': ['block_migration', 'disk_over_commit', 'host'], 'additionalProperties': False, }, }, - 'required': ['migrate_live'], + 'required': ['os-migrateLive'], 'additionalProperties': False, } + +host = copy.deepcopy(parameter_types.hostname) +host['type'] = ['string', 'null'] diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_migrate_server.py b/nova/tests/api/openstack/compute/contrib/test_migrate_server.py similarity index 70% rename from nova/tests/api/openstack/compute/plugins/v3/test_migrate_server.py rename to nova/tests/api/openstack/compute/contrib/test_migrate_server.py index 7c2a173fbfdf..ed0f84d9b741 100644 --- a/nova/tests/api/openstack/compute/plugins/v3/test_migrate_server.py +++ b/nova/tests/api/openstack/compute/contrib/test_migrate_server.py @@ -39,43 +39,54 @@ class MigrateServerTests(admin_only_action_common.CommonTests): def test_migrate(self): method_translations = {'migrate': 'resize', - 'migrate_live': 'live_migrate'} - body_map = {'migrate_live': {'host': 'hostname', - 'block_migration': False, - 'disk_over_commit': False}} - args_map = {'migrate_live': ((False, False, 'hostname'), {})} - self._test_actions(['migrate', 'migrate_live'], body_map=body_map, + 'os-migrateLive': 'live_migrate'} + body_map = {'os-migrateLive': {'host': 'hostname', + 'block_migration': False, + 'disk_over_commit': False}} + args_map = {'os-migrateLive': ((False, False, 'hostname'), {})} + self._test_actions(['migrate', 'os-migrateLive'], body_map=body_map, + method_translations=method_translations, + args_map=args_map) + + def test_migrate_none_hostname(self): + method_translations = {'migrate': 'resize', + 'os-migrateLive': 'live_migrate'} + body_map = {'os-migrateLive': {'host': None, + 'block_migration': False, + 'disk_over_commit': False}} + args_map = {'os-migrateLive': ((False, False, None), {})} + self._test_actions(['migrate', 'os-migrateLive'], body_map=body_map, method_translations=method_translations, args_map=args_map) def test_migrate_with_non_existed_instance(self): - body_map = {'migrate_live': {'host': 'hostname', + body_map = {'os-migrateLive': {'host': 'hostname', 'block_migration': False, 'disk_over_commit': False}} self._test_actions_with_non_existed_instance( - ['migrate', 'migrate_live'], body_map=body_map) + ['migrate', 'os-migrateLive'], body_map=body_map) def test_migrate_raise_conflict_on_invalid_state(self): method_translations = {'migrate': 'resize', - 'migrate_live': 'live_migrate'} - body_map = {'migrate_live': {'host': 'hostname', - 'block_migration': False, - 'disk_over_commit': False}} - args_map = {'migrate_live': ((False, False, 'hostname'), {})} + 'os-migrateLive': 'live_migrate'} + body_map = {'os-migrateLive': {'host': 'hostname', + 'block_migration': False, + 'disk_over_commit': False}} + args_map = {'os-migrateLive': ((False, False, 'hostname'), {})} self._test_actions_raise_conflict_on_invalid_state( - ['migrate', 'migrate_live'], body_map=body_map, args_map=args_map, - method_translations=method_translations) + ['migrate', 'os-migrateLive'], body_map=body_map, + args_map=args_map, method_translations=method_translations) def test_actions_with_locked_instance(self): method_translations = {'migrate': 'resize', - 'migrate_live': 'live_migrate'} - body_map = {'migrate_live': {'host': 'hostname', - 'block_migration': False, - 'disk_over_commit': False}} - args_map = {'migrate_live': ((False, False, 'hostname'), {})} + 'os-migrateLive': 'live_migrate'} + body_map = {'os-migrateLive': {'host': 'hostname', + 'block_migration': False, + 'disk_over_commit': False}} + args_map = {'os-migrateLive': ((False, False, 'hostname'), {})} self._test_actions_with_locked_instance( - ['migrate', 'migrate_live'], body_map=body_map, args_map=args_map, - method_translations=method_translations) + ['migrate', 'os-migrateLive'], body_map=body_map, + args_map=args_map, method_translations=method_translations) def _test_migrate_exception(self, exc_info, expected_result): self.mox.StubOutWithMock(self.compute_api, 'resize') @@ -102,7 +113,7 @@ class MigrateServerTests(admin_only_action_common.CommonTests): self.mox.ReplayAll() res = self._make_request('/servers/%s/action' % instance.uuid, - {'migrate_live': param}) + {'os-migrateLive': param}) self.assertEqual(202, res.status_int) def test_migrate_live_enabled(self): @@ -119,34 +130,39 @@ class MigrateServerTests(admin_only_action_common.CommonTests): def test_migrate_live_without_host(self): res = self._make_request('/servers/FAKE/action', - {'migrate_live': {'block_migration': False, - 'disk_over_commit': False}}) + {'os-migrateLive': + {'block_migration': False, + 'disk_over_commit': False}}) self.assertEqual(400, res.status_int) def test_migrate_live_without_block_migration(self): res = self._make_request('/servers/FAKE/action', - {'migrate_live': {'host': 'hostname', - 'disk_over_commit': False}}) + {'os-migrateLive': + {'host': 'hostname', + 'disk_over_commit': False}}) self.assertEqual(400, res.status_int) def test_migrate_live_without_disk_over_commit(self): res = self._make_request('/servers/FAKE/action', - {'migrate_live': {'host': 'hostname', - 'block_migration': False}}) + {'os-migrateLive': + {'host': 'hostname', + 'block_migration': False}}) self.assertEqual(400, res.status_int) def test_migrate_live_with_invalid_block_migration(self): res = self._make_request('/servers/FAKE/action', - {'migrate_live': {'host': 'hostname', - 'block_migration': "foo", - 'disk_over_commit': False}}) + {'os-migrateLive': + {'host': 'hostname', + 'block_migration': "foo", + 'disk_over_commit': False}}) self.assertEqual(400, res.status_int) def test_migrate_live_with_invalid_disk_over_commit(self): res = self._make_request('/servers/FAKE/action', - {'migrate_live': {'host': 'hostname', - 'block_migration': False, - 'disk_over_commit': "foo"}}) + {'os-migrateLive': + {'host': 'hostname', + 'block_migration': False, + 'disk_over_commit': "foo"}}) self.assertEqual(400, res.status_int) def _test_migrate_live_failed_with_exception(self, fake_exc, @@ -160,7 +176,7 @@ class MigrateServerTests(admin_only_action_common.CommonTests): self.mox.ReplayAll() res = self._make_request('/servers/%s/action' % instance.uuid, - {'migrate_live': + {'os-migrateLive': {'host': 'hostname', 'block_migration': False, 'disk_over_commit': False}}) diff --git a/nova/tests/integrated/v3/api_samples/os-migrate-server/live-migrate-server.json.tpl b/nova/tests/integrated/v3/api_samples/os-migrate-server/live-migrate-server.json.tpl index 804bba4f18e2..4800d4aa1110 100644 --- a/nova/tests/integrated/v3/api_samples/os-migrate-server/live-migrate-server.json.tpl +++ b/nova/tests/integrated/v3/api_samples/os-migrate-server/live-migrate-server.json.tpl @@ -1,5 +1,5 @@ { - "migrate_live": { + "os-migrateLive": { "host": "%(hostname)s", "block_migration": false, "disk_over_commit": false