Updated resize to call compute API with instance_type identifiers instead of flavor identifiers. Updated tests.
This commit is contained in:
@@ -17,11 +17,10 @@ import base64
|
||||
import traceback
|
||||
|
||||
from webob import exc
|
||||
import webob
|
||||
from xml.dom import minidom
|
||||
import webob
|
||||
|
||||
from nova import compute
|
||||
from nova import db
|
||||
from nova import exception
|
||||
from nova import flags
|
||||
from nova import log as logging
|
||||
@@ -29,13 +28,14 @@ from nova import utils
|
||||
from nova.api.openstack import common
|
||||
from nova.api.openstack import create_instance_helper as helper
|
||||
from nova.api.openstack import ips
|
||||
from nova.api.openstack import wsgi
|
||||
from nova.compute import instance_types
|
||||
from nova.scheduler import api as scheduler_api
|
||||
import nova.api.openstack
|
||||
import nova.api.openstack.views.addresses
|
||||
import nova.api.openstack.views.flavors
|
||||
import nova.api.openstack.views.images
|
||||
import nova.api.openstack.views.servers
|
||||
from nova.api.openstack import wsgi
|
||||
import nova.api.openstack
|
||||
from nova.scheduler import api as scheduler_api
|
||||
|
||||
|
||||
LOG = logging.getLogger('nova.api.openstack.servers')
|
||||
@@ -438,13 +438,21 @@ class ControllerV10(Controller):
|
||||
|
||||
def _action_resize(self, input_dict, req, id):
|
||||
""" Resizes a given instance to the flavor size requested """
|
||||
if 'resize' in input_dict and 'flavorId' in input_dict['resize']:
|
||||
flavor_id = input_dict['resize']['flavorId']
|
||||
self.compute_api.resize(req.environ['nova.context'], id,
|
||||
flavor_id)
|
||||
else:
|
||||
LOG.exception(_("Missing 'flavorId' argument for resize"))
|
||||
raise exc.HTTPUnprocessableEntity()
|
||||
try:
|
||||
flavor_id = input_dict["resize"]["flavorId"]
|
||||
except (KeyError, TypeError):
|
||||
msg = _("Resize requests require 'flavorId' attribute.")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
try:
|
||||
i_type = instance_types.get_instance_type_by_flavor_id(flavor_id)
|
||||
except exception.FlavorNotFound:
|
||||
msg = _("Unable to locate requested flavor.")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
context = req.environ["nova.context"]
|
||||
self.compute_api.resize(context, id, i_type["id"])
|
||||
|
||||
return webob.Response(status_int=202)
|
||||
|
||||
def _action_rebuild(self, info, request, instance_id):
|
||||
@@ -555,17 +563,26 @@ class ControllerV11(Controller):
|
||||
def _action_resize(self, input_dict, req, id):
|
||||
""" Resizes a given instance to the flavor size requested """
|
||||
try:
|
||||
if 'resize' in input_dict and 'flavorRef' in input_dict['resize']:
|
||||
flavor_ref = input_dict['resize']['flavorRef']
|
||||
flavor_id = common.get_id_from_href(flavor_ref)
|
||||
self.compute_api.resize(req.environ['nova.context'], id,
|
||||
flavor_id)
|
||||
else:
|
||||
LOG.exception(_("Missing 'flavorRef' argument for resize"))
|
||||
raise exc.HTTPUnprocessableEntity()
|
||||
except Exception, e:
|
||||
LOG.exception(_("Error in resize %s"), e)
|
||||
raise exc.HTTPBadRequest()
|
||||
flavor = input_dict["resize"]["flavor"]
|
||||
except (KeyError, TypeError):
|
||||
msg = _("Resize requests require a flavor to resize instance.")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
try:
|
||||
flavor_id = flavor["id"]
|
||||
except (KeyError, TypeError):
|
||||
msg = _("Resize flavor requires 'id' attribute.")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
try:
|
||||
i_type = instance_types.get_instance_type_by_flavor_id(flavor_id)
|
||||
except exception.FlavorNotFound:
|
||||
msg = _("Unable to locate requested flavor.")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
context = req.environ["nova.context"]
|
||||
self.compute_api.resize(context, id, i_type["id"])
|
||||
|
||||
return webob.Response(status_int=202)
|
||||
|
||||
def _action_rebuild(self, info, request, instance_id):
|
||||
|
||||
@@ -132,11 +132,8 @@ def get_instance_type_by_name(name):
|
||||
# flavors.
|
||||
def get_instance_type_by_flavor_id(flavor_id):
|
||||
"""Retrieve instance type by flavor_id."""
|
||||
if flavor_id is None:
|
||||
return get_default_instance_type()
|
||||
ctxt = context.get_admin_context()
|
||||
try:
|
||||
ctxt = context.get_admin_context()
|
||||
return db.instance_type_get_by_flavor_id(ctxt, flavor_id)
|
||||
except exception.DBError, e:
|
||||
LOG.exception(_('DB error: %s') % e)
|
||||
raise exception.ApiError(_("Unknown flavor: %s") % flavor_id)
|
||||
except ValueError:
|
||||
raise exception.FlavorNotFound(flavor_id=flavor_id)
|
||||
|
||||
@@ -2167,11 +2167,16 @@ class ServersTest(test.TestCase):
|
||||
self.assertEqual(self.resize_called, True)
|
||||
|
||||
def test_resize_server_v11(self):
|
||||
|
||||
req = webob.Request.blank('/v1.1/servers/1/action')
|
||||
req.content_type = 'application/json'
|
||||
req.method = 'POST'
|
||||
body_dict = dict(resize=dict(flavorRef="http://localhost/3"))
|
||||
body_dict = {
|
||||
"resize": {
|
||||
"flavor": {
|
||||
"id": 3,
|
||||
},
|
||||
},
|
||||
}
|
||||
req.body = json.dumps(body_dict)
|
||||
|
||||
self.resize_called = False
|
||||
@@ -2185,8 +2190,8 @@ class ServersTest(test.TestCase):
|
||||
self.assertEqual(res.status_int, 202)
|
||||
self.assertEqual(self.resize_called, True)
|
||||
|
||||
def test_resize_bad_flavor_fails(self):
|
||||
req = self.webreq('/1/action', 'POST', dict(resize=dict(derp=3)))
|
||||
def test_resize_bad_flavor_data(self):
|
||||
req = self.webreq('/1/action', 'POST', {"resize": "bad_data"})
|
||||
|
||||
self.resize_called = False
|
||||
|
||||
@@ -2196,14 +2201,54 @@ class ServersTest(test.TestCase):
|
||||
self.stubs.Set(nova.compute.api.API, 'resize', resize_mock)
|
||||
|
||||
res = req.get_response(fakes.wsgi_app())
|
||||
self.assertEqual(res.status_int, 422)
|
||||
self.assertEqual(res.status_int, 400)
|
||||
self.assertEqual(self.resize_called, False)
|
||||
|
||||
def test_resize_invalid_flavorid(self):
|
||||
req = self.webreq('/1/action', 'POST', {"resize": {"flavorId": 300}})
|
||||
res = req.get_response(fakes.wsgi_app())
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_resize_nonint_flavorid(self):
|
||||
req = self.webreq('/1/action', 'POST', {"resize": {"flavorId": "a"}})
|
||||
res = req.get_response(fakes.wsgi_app())
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_resize_invalid_flavorid_v1_1(self):
|
||||
req = webob.Request.blank('/v1.1/servers/1/action')
|
||||
req.content_type = 'application/json'
|
||||
req.method = 'POST'
|
||||
resize_body = {
|
||||
"resize": {
|
||||
"image": {
|
||||
"id": 300,
|
||||
},
|
||||
},
|
||||
}
|
||||
req.body = json.dumps(resize_body)
|
||||
res = req.get_response(fakes.wsgi_app())
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_resize_nonint_flavorid_v1_1(self):
|
||||
req = webob.Request.blank('/v1.1/servers/1/action')
|
||||
req.content_type = 'application/json'
|
||||
req.method = 'POST'
|
||||
resize_body = {
|
||||
"resize": {
|
||||
"image": {
|
||||
"id": "a",
|
||||
},
|
||||
},
|
||||
}
|
||||
req.body = json.dumps(resize_body)
|
||||
res = req.get_response(fakes.wsgi_app())
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_resize_raises_fails(self):
|
||||
req = self.webreq('/1/action', 'POST', dict(resize=dict(flavorId=3)))
|
||||
|
||||
def resize_mock(*args):
|
||||
raise Exception('hurr durr')
|
||||
raise Exception("An error occurred.")
|
||||
|
||||
self.stubs.Set(nova.compute.api.API, 'resize', resize_mock)
|
||||
|
||||
@@ -2241,7 +2286,7 @@ class ServersTest(test.TestCase):
|
||||
req = self.webreq('/1/action', 'POST', dict(confirmResize=None))
|
||||
|
||||
def confirm_resize_mock(*args):
|
||||
raise Exception('hurr durr')
|
||||
raise Exception("An error occurred.")
|
||||
|
||||
self.stubs.Set(nova.compute.api.API, 'confirm_resize',
|
||||
confirm_resize_mock)
|
||||
@@ -2268,7 +2313,7 @@ class ServersTest(test.TestCase):
|
||||
req = self.webreq('/1/action', 'POST', dict(revertResize=None))
|
||||
|
||||
def revert_resize_mock(*args):
|
||||
raise Exception('hurr durr')
|
||||
raise Exception("An error occurred.")
|
||||
|
||||
self.stubs.Set(nova.compute.api.API, 'revert_resize',
|
||||
revert_resize_mock)
|
||||
|
||||
Reference in New Issue
Block a user