handle AutoDiskConfigDisabledByImage at API layer

nova compute api layer might raise AutoDiskConfigDisabledByImage,
so API layer need to catch them and report correct info.
Also, create api handle lots of exceptions which inherit from Invalid,
this patch combined them and catch Invalid only.

Change-Id: Id0c89e96637d2f82b5bd204eba2c6291b9a5dd38
This commit is contained in:
jichenjc
2014-07-20 03:54:42 +08:00
parent 769b3f1c34
commit 39c170a734
3 changed files with 43 additions and 9 deletions

View File

@@ -983,23 +983,18 @@ class Controller(wsgi.Controller):
except (exception.ImageNotActive, except (exception.ImageNotActive,
exception.FlavorDiskTooSmall, exception.FlavorDiskTooSmall,
exception.FlavorMemoryTooSmall, exception.FlavorMemoryTooSmall,
exception.InvalidMetadata,
exception.InvalidRequest,
exception.MultiplePortsNotApplicable,
exception.InvalidFixedIpAndMaxCountRequest,
exception.NetworkNotFound, exception.NetworkNotFound,
exception.PortNotFound, exception.PortNotFound,
exception.FixedIpAlreadyInUse, exception.FixedIpAlreadyInUse,
exception.SecurityGroupNotFound, exception.SecurityGroupNotFound,
exception.InvalidBDM,
exception.PortRequiresFixedIP,
exception.NetworkRequiresSubnet,
exception.InstanceUserDataTooLarge, exception.InstanceUserDataTooLarge,
exception.InstanceUserDataMalformed) as error: exception.InstanceUserDataMalformed) as error:
raise exc.HTTPBadRequest(explanation=error.format_message()) raise exc.HTTPBadRequest(explanation=error.format_message())
except (exception.PortInUse, except (exception.PortInUse,
exception.NoUniqueMatch) as error: exception.NoUniqueMatch) as error:
raise exc.HTTPConflict(explanation=error.format_message()) raise exc.HTTPConflict(explanation=error.format_message())
except exception.Invalid as error:
raise exc.HTTPBadRequest(explanation=error.format_message())
# If the caller wanted a reservation_id, return it # If the caller wanted a reservation_id, return it
if ret_resv_id: if ret_resv_id:
@@ -1189,7 +1184,8 @@ class Controller(wsgi.Controller):
except exception.Invalid: except exception.Invalid:
msg = _("Invalid instance image.") msg = _("Invalid instance image.")
raise exc.HTTPBadRequest(explanation=msg) raise exc.HTTPBadRequest(explanation=msg)
except exception.NoValidHost as e: except (exception.NoValidHost,
exception.AutoDiskConfigDisabledByImage) as e:
raise exc.HTTPBadRequest(explanation=e.format_message()) raise exc.HTTPBadRequest(explanation=e.format_message())
return webob.Response(status_int=202) return webob.Response(status_int=202)
@@ -1396,7 +1392,8 @@ class Controller(wsgi.Controller):
except (exception.ImageNotActive, except (exception.ImageNotActive,
exception.FlavorDiskTooSmall, exception.FlavorDiskTooSmall,
exception.FlavorMemoryTooSmall, exception.FlavorMemoryTooSmall,
exception.InvalidMetadata) as error: exception.InvalidMetadata,
exception.AutoDiskConfigDisabledByImage) as error:
raise exc.HTTPBadRequest(explanation=error.format_message()) raise exc.HTTPBadRequest(explanation=error.format_message())
instance = self._get_server(context, req, id) instance = self._get_server(context, req, id)

View File

@@ -763,6 +763,22 @@ class ServerActionsControllerTest(test.TestCase):
self.assertEqual(instance_meta['kernel_id'], '1') self.assertEqual(instance_meta['kernel_id'], '1')
self.assertEqual(instance_meta['ramdisk_id'], '2') self.assertEqual(instance_meta['ramdisk_id'], '2')
@mock.patch.object(compute_api.API, 'rebuild')
def test_rebuild_instance_raise_auto_disk_config_exc(self, mock_rebuild):
body = {
"rebuild": {
"imageRef": self._image_href,
},
}
req = fakes.HTTPRequest.blank(self.url)
mock_rebuild.side_effect = exception.AutoDiskConfigDisabledByImage(
image='dummy')
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._action_rebuild,
req, FAKE_UUID, body)
def test_resize_server(self): def test_resize_server(self):
body = dict(resize=dict(flavorRef="http://localhost/3")) body = dict(resize=dict(flavorRef="http://localhost/3"))
@@ -871,6 +887,18 @@ class ServerActionsControllerTest(test.TestCase):
self.controller._action_resize, self.controller._action_resize,
req, FAKE_UUID, body) req, FAKE_UUID, body)
@mock.patch.object(compute_api.API, 'resize')
def test_resize_instance_raise_auto_disk_config_exc(self, mock_resize):
mock_resize.side_effect = exception.AutoDiskConfigDisabledByImage(
image='dummy')
body = dict(resize=dict(flavorRef="http://localhost/3"))
req = fakes.HTTPRequest.blank(self.url)
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller._action_resize,
req, FAKE_UUID, body)
def test_confirm_resize_server(self): def test_confirm_resize_server(self):
body = dict(confirmResize=None) body = dict(confirmResize=None)

View File

@@ -2166,6 +2166,15 @@ class ServersControllerCreateTest(test.TestCase):
self.controller.create, self.controller.create,
self.req, self.body) self.req, self.body)
@mock.patch.object(compute_api.API, 'create')
def test_create_instance_raise_auto_disk_config_exc(self, mock_create):
mock_create.side_effect = exception.AutoDiskConfigDisabledByImage(
image='dummy')
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.create,
self.req, self.body)
def test_create_instance_with_network_with_no_subnet(self): def test_create_instance_with_network_with_no_subnet(self):
self.flags(network_api_class='nova.network.neutronv2.api.API') self.flags(network_api_class='nova.network.neutronv2.api.API')
network = 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee' network = 'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee'