Merge "Merge server create for keypair extension"

This commit is contained in:
Zuul 2018-07-24 13:47:16 +00:00 committed by Gerrit Code Review
commit 50713f9cb7
4 changed files with 25 additions and 42 deletions

View File

@ -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')

View File

@ -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'] = [

View File

@ -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()

View File

@ -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