summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNaveed Massjouni <naveedm9@gmail.com>2012-01-18 19:31:01 +0000
committerNaveed Massjouni <naveedm9@gmail.com>2012-01-19 19:49:29 +0000
commit942f0404a3d7052830cdc52c17c85de7cd3bc0da (patch)
treef412e624e42d0bcf307c74116563b151bd1425b0
parent4672ec7c95845ddd1df29ffa88848c22df512a42 (diff)
downloadnova-942f0404a3d7052830cdc52c17c85de7cd3bc0da.tar.gz
nova-942f0404a3d7052830cdc52c17c85de7cd3bc0da.tar.xz
nova-942f0404a3d7052830cdc52c17c85de7cd3bc0da.zip
Validating image id for rebuild.
bug: 886701 Change-Id: I20ad03edca390af9203569e02ae0c1af5bb7beaf
-rw-r--r--nova/api/openstack/compute/servers.py3
-rw-r--r--nova/compute/api.py8
-rw-r--r--nova/tests/api/openstack/compute/test_server_actions.py33
3 files changed, 34 insertions, 10 deletions
diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py
index 0d8165784..988b403b1 100644
--- a/nova/api/openstack/compute/servers.py
+++ b/nova/api/openstack/compute/servers.py
@@ -1081,6 +1081,9 @@ class Controller(wsgi.Controller):
except exception.InstanceNotFound:
msg = _("Instance could not be found")
raise exc.HTTPNotFound(explanation=msg)
+ except exception.ImageNotFound as error:
+ msg = _("Cannot find image for rebuild")
+ raise exc.HTTPBadRequest(explanation=msg)
instance = self._get_server(context, id)
diff --git a/nova/compute/api.py b/nova/compute/api.py
index 012217584..3b4cfdeaf 100644
--- a/nova/compute/api.py
+++ b/nova/compute/api.py
@@ -1255,6 +1255,12 @@ class API(base.Base):
instance['uuid'],
params={'reboot_type': reboot_type})
+ def _validate_image_href(self, context, image_href):
+ """Throws an ImageNotFound exception if image_href does not exist."""
+ (image_service, image_id) = nova.image.get_image_service(context,
+ image_href)
+ image_service.show(context, image_id)
+
@wrap_check_policy
@check_instance_state(vm_state=[vm_states.ACTIVE, vm_states.SHUTOFF],
task_state=[None, task_states.RESIZE_VERIFY])
@@ -1262,6 +1268,8 @@ class API(base.Base):
def rebuild(self, context, instance, image_href, admin_password, **kwargs):
"""Rebuild the given instance with the provided attributes."""
+ self._validate_image_href(context, image_href)
+
files_to_inject = kwargs.pop('files_to_inject', [])
self._check_injected_file_quota(context, files_to_inject)
diff --git a/nova/tests/api/openstack/compute/test_server_actions.py b/nova/tests/api/openstack/compute/test_server_actions.py
index 763e5e0ea..a0c4c390e 100644
--- a/nova/tests/api/openstack/compute/test_server_actions.py
+++ b/nova/tests/api/openstack/compute/test_server_actions.py
@@ -158,6 +158,7 @@ class ServerActionsControllerTest(test.TestCase):
fakes.stub_out_nw_api(self.stubs)
fakes.stub_out_rate_limiting(self.stubs)
fakes.stub_out_compute_api_snapshot(self.stubs)
+ fakes.stub_out_image_service(self.stubs)
service_class = 'nova.image.glance.GlanceImageService'
self.service = utils.import_object(service_class)
self.context = context.RequestContext(1, None)
@@ -167,6 +168,7 @@ class ServerActionsControllerTest(test.TestCase):
self.flags(allow_instance_snapshots=True)
self.uuid = FAKE_UUID
self.url = '/v2/fake/servers/%s/action' % self.uuid
+ self._image_href = '155d900f-4e14-4e4c-a73d-069cbf4541e6'
self.controller = servers.Controller()
@@ -267,7 +269,7 @@ class ServerActionsControllerTest(test.TestCase):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
},
}
@@ -305,7 +307,7 @@ class ServerActionsControllerTest(test.TestCase):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
"metadata": metadata,
},
}
@@ -318,7 +320,7 @@ class ServerActionsControllerTest(test.TestCase):
def test_rebuild_accepted_with_bad_metadata(self):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
"metadata": "stack",
},
}
@@ -331,7 +333,7 @@ class ServerActionsControllerTest(test.TestCase):
def test_rebuild_bad_entity(self):
body = {
"rebuild": {
- "imageId": 2,
+ "imageId": self._image_href,
},
}
@@ -343,7 +345,7 @@ class ServerActionsControllerTest(test.TestCase):
def test_rebuild_bad_personality(self):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
"personality": [{
"path": "/path/to/file",
"contents": "INVALID b64",
@@ -359,7 +361,7 @@ class ServerActionsControllerTest(test.TestCase):
def test_rebuild_personality(self):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
"personality": [{
"path": "/path/to/file",
"contents": base64.b64encode("Test String"),
@@ -378,7 +380,7 @@ class ServerActionsControllerTest(test.TestCase):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
"adminPass": "asdf",
},
}
@@ -396,7 +398,7 @@ class ServerActionsControllerTest(test.TestCase):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
},
}
@@ -405,6 +407,17 @@ class ServerActionsControllerTest(test.TestCase):
self.controller._action_rebuild,
req, FAKE_UUID, body)
+ def test_rebuild_with_bad_image(self):
+ body = {
+ "rebuild": {
+ "imageRef": "foo",
+ },
+ }
+ req = fakes.HTTPRequest.blank(self.url)
+ self.assertRaises(webob.exc.HTTPBadRequest,
+ self.controller._action_rebuild,
+ req, FAKE_UUID, body)
+
def test_rebuild_accessIP(self):
attributes = {
'access_ip_v4': '172.19.0.1',
@@ -413,7 +426,7 @@ class ServerActionsControllerTest(test.TestCase):
body = {
"rebuild": {
- "imageRef": "http://localhost/images/2",
+ "imageRef": self._image_href,
"accessIPv4": "172.19.0.1",
"accessIPv6": "fe80::1",
},
@@ -424,7 +437,7 @@ class ServerActionsControllerTest(test.TestCase):
req = fakes.HTTPRequest.blank(self.url)
context = req.environ['nova.context']
update(context, mox.IgnoreArg(),
- image_ref='http://localhost/images/2',
+ image_ref=self._image_href,
vm_state=vm_states.REBUILDING,
task_state=None, progress=0, **attributes).AndReturn(None)
self.mox.ReplayAll()