summaryrefslogtreecommitdiffstats
path: root/nova/api
diff options
context:
space:
mode:
authorChris Behrens <cbehrens@codestud.com>2011-07-06 05:41:47 -0700
committerChris Behrens <cbehrens@codestud.com>2011-07-06 05:41:47 -0700
commitca152762aa73a93583be2ada237cf8bbbcc99220 (patch)
tree4976e1a448ead8b6ae2e62d4d49073ffe5930687 /nova/api
parentb2fe710c59ba266b9afd67db1cae60a6db5c71e3 (diff)
downloadnova-ca152762aa73a93583be2ada237cf8bbbcc99220.tar.gz
nova-ca152762aa73a93583be2ada237cf8bbbcc99220.tar.xz
nova-ca152762aa73a93583be2ada237cf8bbbcc99220.zip
clean up OS API servers getting
Diffstat (limited to 'nova/api')
-rw-r--r--nova/api/openstack/servers.py94
1 files changed, 37 insertions, 57 deletions
diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py
index 9a3872113..ce04a1eab 100644
--- a/nova/api/openstack/servers.py
+++ b/nova/api/openstack/servers.py
@@ -46,34 +46,14 @@ FLAGS = flags.FLAGS
class Controller(object):
""" The Server API base controller class for the OpenStack API """
- # These are a list of possible query string paramters to the
- # /servers query that a user should be able to do. Specify this
- # in your subclasses. When admin api is off, unknown options will
- # get filtered out without error.
- servers_search_options = []
-
def __init__(self):
self.compute_api = compute.API()
self.helper = helper.CreateInstanceHelper(self)
- def _remove_invalid_options(self, context, search_options):
- if FLAGS.allow_admin_api and context.is_admin:
- # Allow all options
- return
- # Otherwise, strip out all unknown options
- unknown_options = [opt for opt in search_options
- if opt not in self.servers_search_options]
- unk_opt_str = ", ".join(unknown_options)
- log_msg = _("Stripping out options '%(unk_opt_str)s' from servers "
- "query") % locals()
- LOG.debug(log_msg)
- for opt in unknown_options:
- search_options.pop(opt, None)
-
def index(self, req):
""" Returns a list of server names and ids for a given user """
try:
- servers = self._servers_from_request(req, is_detail=False)
+ servers = self._get_servers(req, is_detail=False)
except exception.Invalid as err:
return exc.HTTPBadRequest(explanation=str(err))
except exception.NotFound:
@@ -83,7 +63,7 @@ class Controller(object):
def detail(self, req):
""" Returns a list of server details for a given user """
try:
- servers = self._servers_from_request(req, is_detail=True)
+ servers = self._get_servers(req, is_detail=True)
except exception.Invalid as err:
return exc.HTTPBadRequest(explanation=str(err))
except exception.NotFound as err:
@@ -99,13 +79,21 @@ class Controller(object):
def _action_rebuild(self, info, request, instance_id):
raise NotImplementedError()
- def _servers_search(self, context, req, is_detail, search_opts=None):
+ def _get_servers(self, req, is_detail):
"""Returns a list of servers, taking into account any search
options specified.
"""
- if search_opts is None:
- search_opts = {}
+ search_opts = {}
+ search_opts.update(req.str_GET)
+
+ context = req.environ['nova.context']
+ remove_invalid_options(context, search_opts,
+ self._get_server_search_options())
+
+ # Convert recurse_zones into a boolean
+ search_opts['recurse_zones'] = utils.bool_from_str(
+ search_opts.get('recurse_zones', False))
# If search by 'status', we need to convert it to 'state'
# If the status is unknown, bail.
@@ -145,26 +133,6 @@ class Controller(object):
for inst in limited_list]
return dict(servers=servers)
- def _servers_from_request(self, req, is_detail):
- """Returns a list of servers based on the request.
-
- Checks for search options and strips out options that should
- not be available to non-admins.
- """
-
- search_opts = {}
- search_opts.update(req.str_GET)
-
- context = req.environ['nova.context']
- self._remove_invalid_options(context, search_opts)
-
- # Convert recurse_zones into a boolean
- search_opts['recurse_zones'] = utils.bool_from_str(
- search_opts.get('recurse_zones', False))
-
- return self._servers_search(context, req, is_detail,
- search_opts=search_opts)
-
@scheduler_api.redirect_handler
def show(self, req, id):
""" Returns server details by server id """
@@ -569,12 +537,6 @@ class Controller(object):
class ControllerV10(Controller):
"""v1.0 OpenStack API controller"""
- # These are a list of possible query string paramters to the
- # /servers query that a user should be able to do. When admin api
- # is off, unknown options will get filtered out without error.
- servers_search_options = ["reservation_id", "fixed_ip",
- "name", "recurse_zones"]
-
@scheduler_api.redirect_handler
def delete(self, req, id):
""" Destroys a server """
@@ -636,16 +598,14 @@ class ControllerV10(Controller):
""" Determine the admin password for a server on creation """
return self.helper._get_server_admin_password_old_style(server)
+ def _get_server_search_options(self):
+ """Return server search options allowed by non-admin"""
+ return 'reservation_id', 'fixed_ip', 'name', 'recurse_zones'
+
class ControllerV11(Controller):
"""v1.1 OpenStack API controller"""
- # These are a list of possible query string paramters to the
- # /servers query that a user should be able to do. When admin api
- # is off, unknown options will get filtered out without error.
- servers_search_options = ["reservation_id", "name", "recurse_zones",
- "status", "image", "flavor", "changes-since"]
-
@scheduler_api.redirect_handler
def delete(self, req, id):
""" Destroys a server """
@@ -812,6 +772,11 @@ class ControllerV11(Controller):
""" Determine the admin password for a server on creation """
return self.helper._get_server_admin_password_new_style(server)
+ def _get_server_search_options(self):
+ """Return server search options allowed by non-admin"""
+ return ('reservation_id', 'name', 'recurse_zones',
+ 'status', 'image', 'flavor', 'changes-since')
+
class HeadersSerializer(wsgi.ResponseHeadersSerializer):
@@ -982,3 +947,18 @@ def create_resource(version='1.0'):
deserializer = wsgi.RequestDeserializer(body_deserializers)
return wsgi.Resource(controller, deserializer, serializer)
+
+
+def remove_invalid_options(context, search_options, allowed_search_options):
+ """Remove search options that are not valid for non-admin API/context"""
+ if FLAGS.allow_admin_api and context.is_admin:
+ # Allow all options
+ return
+ # Otherwise, strip out all unknown options
+ unknown_options = [opt for opt in search_options
+ if opt not in allowed_search_options]
+ unk_opt_str = ", ".join(unknown_options)
+ log_msg = _("Removing options '%(unk_opt_str)s' from query") % locals()
+ LOG.debug(log_msg)
+ for opt in unknown_options:
+ search_options.pop(opt, None)