Merge "Fix scheduler_hints parameter of v3 API"
This commit is contained in:
commit
619dc06c44
@ -15,38 +15,11 @@
|
||||
import webob.exc
|
||||
|
||||
from nova.api.openstack import extensions
|
||||
from nova.api.openstack import wsgi
|
||||
from nova.openstack.common.gettextutils import _
|
||||
|
||||
ALIAS = "os-scheduler-hints"
|
||||
|
||||
|
||||
class SchedulerHintsController(wsgi.Controller):
|
||||
|
||||
@staticmethod
|
||||
def _extract_scheduler_hints(body):
|
||||
hints = {}
|
||||
|
||||
attr = '%s:scheduler_hints' % ALIAS
|
||||
try:
|
||||
if attr in body:
|
||||
hints.update(body[attr])
|
||||
# Fail if non-dict provided
|
||||
except ValueError:
|
||||
msg = _("Malformed scheduler_hints attribute")
|
||||
raise webob.exc.HTTPBadRequest(reason=msg)
|
||||
|
||||
return hints
|
||||
|
||||
@wsgi.extends
|
||||
def create(self, req, body):
|
||||
hints = self._extract_scheduler_hints(body)
|
||||
|
||||
if 'server' in body:
|
||||
body['server']['scheduler_hints'] = hints
|
||||
yield
|
||||
|
||||
|
||||
class SchedulerHints(extensions.V3APIExtensionBase):
|
||||
"""Pass arbitrary key/value pairs to the scheduler."""
|
||||
|
||||
@ -55,12 +28,14 @@ class SchedulerHints(extensions.V3APIExtensionBase):
|
||||
version = 1
|
||||
|
||||
def get_controller_extensions(self):
|
||||
controller = SchedulerHintsController()
|
||||
ext = extensions.ControllerExtension(self, 'servers', controller)
|
||||
return [ext]
|
||||
return []
|
||||
|
||||
def get_resources(self):
|
||||
return []
|
||||
|
||||
def server_create(self, server_dict, create_kwargs):
|
||||
create_kwargs['scheduler_hints'] = server_dict.get('scheduler_hints')
|
||||
scheduler_hints = server_dict.get(ALIAS + ':scheduler_hints', {})
|
||||
if not isinstance(scheduler_hints, dict):
|
||||
msg = _("Malformed scheduler_hints attribute")
|
||||
raise webob.exc.HTTPBadRequest(reason=msg)
|
||||
create_kwargs['scheduler_hints'] = scheduler_hints
|
||||
|
@ -90,8 +90,8 @@ class SchedulerHintsTestCase(test.TestCase):
|
||||
'name': 'server_test',
|
||||
'image_ref': 'cedef40a-ed67-4d10-800e-17455edce175',
|
||||
'flavor_ref': '1',
|
||||
'os-scheduler-hints:scheduler_hints': {'a': 'b'},
|
||||
},
|
||||
'os-scheduler-hints:scheduler_hints': {'a': 'b'},
|
||||
}
|
||||
|
||||
req.body = jsonutils.dumps(body)
|
||||
@ -107,8 +107,8 @@ class SchedulerHintsTestCase(test.TestCase):
|
||||
'name': 'server_test',
|
||||
'image_ref': 'cedef40a-ed67-4d10-800e-17455edce175',
|
||||
'flavor_ref': '1',
|
||||
'os-scheduler-hints:scheduler_hints': 'non-dict',
|
||||
},
|
||||
'os-scheduler-hints:scheduler_hints': 'here',
|
||||
}
|
||||
|
||||
req.body = jsonutils.dumps(body)
|
||||
@ -228,7 +228,7 @@ class ServersControllerCreateTest(test.TestCase):
|
||||
|
||||
def test_create_instance_with_scheduler_hints_disabled(self):
|
||||
hints = {'a': 'b'}
|
||||
params = {'scheduler_hints': hints}
|
||||
params = {'os-scheduler-hints:scheduler_hints': hints}
|
||||
old_create = compute_api.API.create
|
||||
|
||||
def create(*args, **kwargs):
|
||||
@ -242,7 +242,7 @@ class ServersControllerCreateTest(test.TestCase):
|
||||
|
||||
def test_create_instance_with_scheduler_hints_enabled(self):
|
||||
hints = {'a': 'b'}
|
||||
params = {'scheduler_hints': hints}
|
||||
params = {'os-scheduler-hints:scheduler_hints': hints}
|
||||
old_create = compute_api.API.create
|
||||
|
||||
def create(*args, **kwargs):
|
||||
|
Loading…
Reference in New Issue
Block a user