summaryrefslogtreecommitdiffstats
path: root/nova/api
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2012-09-12 12:50:53 +0100
committerMark McLoughlin <markmc@redhat.com>2012-09-13 16:10:29 +0100
commitd1ad73eebd28f33c98b56c033c2c4d305d66d5dc (patch)
tree5c6478c53d19bfcc1146825fa0a43bcd6dee1313 /nova/api
parente8d0f9940edd1043162c187ec92f93e8c9fafb02 (diff)
downloadnova-d1ad73eebd28f33c98b56c033c2c4d305d66d5dc.tar.gz
nova-d1ad73eebd28f33c98b56c033c2c4d305d66d5dc.tar.xz
nova-d1ad73eebd28f33c98b56c033c2c4d305d66d5dc.zip
Add entity body validation helper
Add a _valid_body() helper for the servers API to avoid repeating the same tests in multiple methods. Include a check that the 'server' value is actually a dict. Also remove the superfluous checking of req.body which is a leftover from when the body wasn't a method parameter. Change-Id: If8114cc76d68567005c85c803f29e30e034db89a
Diffstat (limited to 'nova/api')
-rw-r--r--nova/api/openstack/compute/servers.py13
-rw-r--r--nova/api/openstack/wsgi.py17
2 files changed, 19 insertions, 11 deletions
diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py
index fc4905a11..4c4b86aa8 100644
--- a/nova/api/openstack/compute/servers.py
+++ b/nova/api/openstack/compute/servers.py
@@ -613,10 +613,7 @@ class Controller(wsgi.Controller):
@wsgi.deserializers(xml=CreateDeserializer)
def create(self, req, body):
"""Creates a new server for a given user."""
- if not body:
- raise exc.HTTPUnprocessableEntity()
-
- if not 'server' in body:
+ if not self.is_valid_body(body, 'server'):
raise exc.HTTPUnprocessableEntity()
body['server']['key_name'] = self._get_key_name(req, body)
@@ -815,13 +812,7 @@ class Controller(wsgi.Controller):
@wsgi.serializers(xml=ServerTemplate)
def update(self, req, id, body):
"""Update server then pass on to version-specific controller."""
- if len(req.body) == 0:
- raise exc.HTTPUnprocessableEntity()
-
- if not body:
- raise exc.HTTPUnprocessableEntity()
-
- if not 'server' in body:
+ if not self.is_valid_body(body, 'server'):
raise exc.HTTPUnprocessableEntity()
ctxt = req.environ['nova.context']
diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py
index 658b59645..a9fa3847a 100644
--- a/nova/api/openstack/wsgi.py
+++ b/nova/api/openstack/wsgi.py
@@ -1102,6 +1102,23 @@ class Controller(object):
else:
self._view_builder = None
+ @staticmethod
+ def is_valid_body(body, entity_name):
+ if not (body and entity_name in body):
+ return False
+
+ def is_dict(d):
+ try:
+ d.get(None)
+ return True
+ except AttributeError:
+ return False
+
+ if not is_dict(body[entity_name]):
+ return False
+
+ return True
+
class Fault(webob.exc.HTTPException):
"""Wrap webob.exc.HTTPException to provide API friendly response."""