Skip to content

Commit

Permalink
Validating image id for rebuild.
Browse files Browse the repository at this point in the history
bug: 886701

Change-Id: I20ad03edca390af9203569e02ae0c1af5bb7beaf
  • Loading branch information
ironcamel committed Jan 19, 2012
1 parent 4672ec7 commit 942f040
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
3 changes: 3 additions & 0 deletions nova/api/openstack/compute/servers.py
Expand Up @@ -1081,6 +1081,9 @@ def _action_rebuild(self, req, id, body):
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)

Expand Down
8 changes: 8 additions & 0 deletions nova/compute/api.py
Expand Up @@ -1255,13 +1255,21 @@ def reboot(self, context, instance, reboot_type):
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])
@scheduler_api.reroute_compute("rebuild")
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)

Expand Down
33 changes: 23 additions & 10 deletions nova/tests/api/openstack/compute/test_server_actions.py
Expand Up @@ -158,6 +158,7 @@ def setUp(self):
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)
Expand All @@ -167,6 +168,7 @@ def setUp(self):
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()

Expand Down Expand Up @@ -267,7 +269,7 @@ def test_rebuild_accepted_minimum(self):

body = {
"rebuild": {
"imageRef": "http://localhost/images/2",
"imageRef": self._image_href,
},
}

Expand Down Expand Up @@ -305,7 +307,7 @@ def test_rebuild_accepted_with_metadata(self):

body = {
"rebuild": {
"imageRef": "http://localhost/images/2",
"imageRef": self._image_href,
"metadata": metadata,
},
}
Expand All @@ -318,7 +320,7 @@ def test_rebuild_accepted_with_metadata(self):
def test_rebuild_accepted_with_bad_metadata(self):
body = {
"rebuild": {
"imageRef": "http://localhost/images/2",
"imageRef": self._image_href,
"metadata": "stack",
},
}
Expand All @@ -331,7 +333,7 @@ def test_rebuild_accepted_with_bad_metadata(self):
def test_rebuild_bad_entity(self):
body = {
"rebuild": {
"imageId": 2,
"imageId": self._image_href,
},
}

Expand All @@ -343,7 +345,7 @@ def test_rebuild_bad_entity(self):
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",
Expand All @@ -359,7 +361,7 @@ def test_rebuild_bad_personality(self):
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"),
Expand All @@ -378,7 +380,7 @@ def test_rebuild_admin_pass(self):

body = {
"rebuild": {
"imageRef": "http://localhost/images/2",
"imageRef": self._image_href,
"adminPass": "asdf",
},
}
Expand All @@ -396,7 +398,7 @@ def server_not_found(self, instance_id):

body = {
"rebuild": {
"imageRef": "http://localhost/images/2",
"imageRef": self._image_href,
},
}

Expand All @@ -405,6 +407,17 @@ def server_not_found(self, instance_id):
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',
Expand All @@ -413,7 +426,7 @@ def test_rebuild_accessIP(self):

body = {
"rebuild": {
"imageRef": "http://localhost/images/2",
"imageRef": self._image_href,
"accessIPv4": "172.19.0.1",
"accessIPv6": "fe80::1",
},
Expand All @@ -424,7 +437,7 @@ def test_rebuild_accessIP(self):
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()
Expand Down

0 comments on commit 942f040

Please sign in to comment.