From a66aabb4d8bf47262c2dbf6c8235efff9a87375c Mon Sep 17 00:00:00 2001 From: ghanshyam Date: Tue, 17 Jul 2018 11:18:20 +0000 Subject: [PATCH] Merge server create for keypair extension As nova extensions has been deprecated already and goal is to merge all scattered code into main controller side. Currently schema and request/response extended code are there among all extensions. This commit merge the server_create for keypair extensions. Partially implements: blueprint api-extensions-merge-rocky Change-Id: I6b5b597a263ff61e90871121c14dfc141c2fef0f --- nova/api/openstack/compute/keypairs.py | 10 ------- nova/api/openstack/compute/servers.py | 7 +++-- .../api/openstack/compute/test_keypairs.py | 30 ------------------- .../api/openstack/compute/test_serversV21.py | 20 +++++++++++++ 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/nova/api/openstack/compute/keypairs.py b/nova/api/openstack/compute/keypairs.py index 9acd3e10ce29..b3c7bbb111e3 100644 --- a/nova/api/openstack/compute/keypairs.py +++ b/nova/api/openstack/compute/keypairs.py @@ -317,13 +317,3 @@ class Controller(wsgi.Controller): kp_policies.BASE_POLICY_NAME, fatal=False): servers = resp_obj.obj['servers'] self._add_key_name(req, servers) - - -# NOTE(gmann): This function is not supposed to use 'body_deprecated_param' -# parameter as this is placed to handle scheduler_hint extension for V2.1. -def server_create(server_dict, create_kwargs, body_deprecated_param): - # NOTE(alex_xu): The v2.1 API compat mode, we strip the spaces for - # keypair create. But we didn't strip spaces at here for - # backward-compatible some users already created keypair and name with - # leading/trailing spaces by legacy v2 API. - create_kwargs['key_name'] = server_dict.get('key_name') diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index bae4b0d6399d..7d0bab32fb00 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -31,7 +31,6 @@ from nova.api.openstack.compute import block_device_mapping from nova.api.openstack.compute import block_device_mapping_v1 from nova.api.openstack.compute import config_drive from nova.api.openstack.compute import helpers -from nova.api.openstack.compute import keypairs from nova.api.openstack.compute.schemas import servers as schema_servers from nova.api.openstack.compute.views import servers as views_servers from nova.api.openstack import wsgi @@ -68,7 +67,6 @@ class ServersController(wsgi.Controller): block_device_mapping.server_create, block_device_mapping_v1.server_create, config_drive.server_create, - keypairs.server_create, ] @staticmethod @@ -429,6 +427,11 @@ class ServersController(wsgi.Controller): # all of extended code into ServersController. self._create_by_func_list(server_dict, create_kwargs, body) create_kwargs['user_data'] = server_dict.get('user_data') + # NOTE(alex_xu): The v2.1 API compat mode, we strip the spaces for + # keypair create. But we didn't strip spaces at here for + # backward-compatible some users already created keypair and name with + # leading/trailing spaces by legacy v2 API. + create_kwargs['key_name'] = server_dict.get('key_name') security_groups = server_dict.get('security_groups') if security_groups is not None: create_kwargs['security_groups'] = [ diff --git a/nova/tests/unit/api/openstack/compute/test_keypairs.py b/nova/tests/unit/api/openstack/compute/test_keypairs.py index af8b4e7057e8..25d3b3e61752 100644 --- a/nova/tests/unit/api/openstack/compute/test_keypairs.py +++ b/nova/tests/unit/api/openstack/compute/test_keypairs.py @@ -358,36 +358,6 @@ class KeypairsTestV21(test.TestCase): def _assert_keypair_type(self, res_dict): self.assertNotIn('type', res_dict['keypair']) - def test_create_server_keypair_name_with_leading_trailing(self): - req = fakes.HTTPRequest.blank(self.base_url + '/servers') - req.method = 'POST' - req.headers["content-type"] = "application/json" - req.body = jsonutils.dump_as_bytes({'server': {'name': 'test', - 'flavorRef': 1, - 'keypair_name': ' abc ', - 'imageRef': FAKE_UUID}}) - res = req.get_response(self.app_server) - self.assertEqual(400, res.status_code) - self.assertIn(b'keypair_name', res.body) - - @mock.patch.object(compute_api.API, 'create') - def test_create_server_keypair_name_with_leading_trailing_compat_mode( - self, mock_create): - mock_create.return_value = ( - objects.InstanceList(objects=[ - fakes.stub_instance_obj(ctxt=None, id=1)]), - None) - req = fakes.HTTPRequest.blank(self.base_url + '/servers') - req.method = 'POST' - req.headers["content-type"] = "application/json" - req.body = jsonutils.dump_as_bytes({'server': {'name': 'test', - 'flavorRef': 1, - 'keypair_name': ' abc ', - 'imageRef': FAKE_UUID}}) - req.set_legacy_v2() - res = req.get_response(self.app_server) - self.assertEqual(202, res.status_code) - class KeypairPolicyTestV21(test.NoDBTestCase): KeyPairController = keypairs_v21.KeypairController() diff --git a/nova/tests/unit/api/openstack/compute/test_serversV21.py b/nova/tests/unit/api/openstack/compute/test_serversV21.py index 6f5612968b6f..e08567f7bc31 100644 --- a/nova/tests/unit/api/openstack/compute/test_serversV21.py +++ b/nova/tests/unit/api/openstack/compute/test_serversV21.py @@ -3526,6 +3526,26 @@ class ServersControllerCreateTest(test.TestCase): self.assertEqual(FAKE_UUID, res["server"]["id"]) self._check_admin_password_len(res["server"]) + def test_create_server_keypair_name_with_leading_trailing(self): + self.body['server']['key_name'] = ' abc ' + self.req.body = jsonutils.dump_as_bytes(self.body) + self.assertRaises(exception.ValidationError, + self.controller.create, self.req, body=self.body) + + @mock.patch.object(compute_api.API, 'create') + def test_create_server_keypair_name_with_leading_trailing_compat_mode( + self, mock_create): + params = {'key_name': ' abc '} + + def fake_create(*args, **kwargs): + self.assertEqual(' abc ', kwargs['key_name']) + return (objects.InstanceList(objects=[fakes.stub_instance_obj( + self.req.environ['nova.context'])]), None) + mock_create.side_effect = fake_create + + self.req.set_legacy_v2() + self._test_create_extra(params) + def test_create_instance_invalid_flavor_href(self): flavor_ref = 'http://localhost/v2/flavors/asdf' self.body['server']['flavorRef'] = flavor_ref