diff options
author | Jenkins <jenkins@review.openstack.org> | 2014-06-25 07:40:48 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-06-25 07:40:48 +0000 |
commit | 868a8a07e068e1b35271ea14d7d05397dffd9de9 (patch) | |
tree | ca51ac4f17d76ff20a6d5af2329c4cc4580ddce8 /nova/api/openstack | |
parent | 1a6d32b9690b4bff709dc83bcf4c2d3a65fd7c3e (diff) | |
parent | 58b264cad586d1310ed47d9834e671c2cd25a6e6 (diff) | |
download | nova-868a8a07e068e1b35271ea14d7d05397dffd9de9.tar.gz |
Merge "Add API schema for v2.1/v3 aggregates API"
Diffstat (limited to 'nova/api/openstack')
-rw-r--r-- | nova/api/openstack/compute/plugins/v3/aggregates.py | 72 | ||||
-rw-r--r-- | nova/api/openstack/compute/schemas/v3/aggregates.py | 88 |
2 files changed, 102 insertions, 58 deletions
diff --git a/nova/api/openstack/compute/plugins/v3/aggregates.py b/nova/api/openstack/compute/plugins/v3/aggregates.py index 38e55245e2..364d0587bc 100644 --- a/nova/api/openstack/compute/plugins/v3/aggregates.py +++ b/nova/api/openstack/compute/plugins/v3/aggregates.py @@ -16,13 +16,13 @@ """The Aggregate admin API extension.""" import datetime -import functools -import six from webob import exc +from nova.api.openstack.compute.schemas.v3 import aggregates from nova.api.openstack import extensions from nova.api.openstack import wsgi +from nova.api import validation from nova.compute import api as compute_api from nova import exception from nova.openstack.common.gettextutils import _ @@ -38,24 +38,6 @@ def _get_context(req): return req.environ['nova.context'] -def get_host_from_body(fn): - """Makes sure that the host exists.""" - @functools.wraps(fn) - def wrapped(self, req, id, body, *args, **kwargs): - if not self.is_valid_body(body, fn.wsgi_action): - raise exc.HTTPBadRequest(explanation=_("Invalid request body")) - host = body[fn.wsgi_action].get('host') - if host is None: - raise exc.HTTPBadRequest( - explanation=_("Could not find host to be set in " - "request body")) - if not isinstance(host, six.string_types): - raise exc.HTTPBadRequest( - explanation=_("The value of host must be a string")) - return fn(self, req, id, host, *args, **kwargs) - return wrapped - - class AggregateController(wsgi.Controller): """The Host Aggregates API controller for the OpenStack API.""" def __init__(self): @@ -72,28 +54,16 @@ class AggregateController(wsgi.Controller): @extensions.expected_errors((400, 409)) @wsgi.response(201) + @validation.schema(aggregates.create) def create(self, req, body): """Creates an aggregate, given its name and optional availability zone. """ context = _get_context(req) authorize(context, action='create') - if not self.is_valid_body(body, 'aggregate'): - raise exc.HTTPBadRequest(explanation=_("Invalid request body")) - try: - host_aggregate = body["aggregate"] - name = host_aggregate["name"] - except KeyError as e: - msg = _("Could not find %s parameter in the request") % e.args[0] - raise exc.HTTPBadRequest(explanation=msg) + host_aggregate = body["aggregate"] + name = host_aggregate["name"] avail_zone = host_aggregate.get("availability_zone") - try: - utils.check_string_length(name, "Aggregate name", 1, 255) - if avail_zone is not None: - utils.check_string_length(avail_zone, "Availability_zone", 1, - 255) - except exception.InvalidInput as e: - raise exc.HTTPBadRequest(explanation=e.format_message()) try: aggregate = self.api.create_aggregate(context, name, avail_zone) @@ -115,30 +85,12 @@ class AggregateController(wsgi.Controller): return self._marshall_aggregate(aggregate) @extensions.expected_errors((400, 404, 409)) + @validation.schema(aggregates.update) def update(self, req, id, body): """Updates the name and/or availability_zone of given aggregate.""" context = _get_context(req) authorize(context, action='update') - if not self.is_valid_body(body, 'aggregate'): - raise exc.HTTPBadRequest(explanation=_("Invalid request body")) updates = body["aggregate"] - if len(updates) < 1: - raise exc.HTTPBadRequest( - explanation=_("Request body is empty")) - for key in updates.keys(): - if key not in ["name", "availability_zone"]: - msg = _("Invalid key %s in request body.") % key - raise exc.HTTPBadRequest(explanation=msg) - - try: - if 'name' in updates: - utils.check_string_length(updates['name'], "Aggregate name", 1, - 255) - if updates.get("availability_zone") is not None: - utils.check_string_length(updates['availability_zone'], - "Availability_zone", 1, 255) - except exception.InvalidInput as e: - raise exc.HTTPBadRequest(explanation=e.format_message()) try: aggregate = self.api.update_aggregate(context, id, updates) @@ -163,11 +115,13 @@ class AggregateController(wsgi.Controller): raise exc.HTTPNotFound(explanation=e.format_message()) @extensions.expected_errors((400, 404, 409)) - @get_host_from_body @wsgi.action('add_host') @wsgi.response(202) - def _add_host(self, req, id, host): + @validation.schema(aggregates.add_host) + def _add_host(self, req, id, body): """Adds a host to the specified aggregate.""" + host = body['add_host']['host'] + context = _get_context(req) authorize(context, action='add_host') try: @@ -181,11 +135,13 @@ class AggregateController(wsgi.Controller): return self._marshall_aggregate(aggregate) @extensions.expected_errors((400, 404, 409)) - @get_host_from_body @wsgi.action('remove_host') @wsgi.response(202) - def _remove_host(self, req, id, host): + @validation.schema(aggregates.remove_host) + def _remove_host(self, req, id, body): """Removes a host from the specified aggregate.""" + host = body['remove_host']['host'] + context = _get_context(req) authorize(context, action='remove_host') try: diff --git a/nova/api/openstack/compute/schemas/v3/aggregates.py b/nova/api/openstack/compute/schemas/v3/aggregates.py new file mode 100644 index 0000000000..fee605971a --- /dev/null +++ b/nova/api/openstack/compute/schemas/v3/aggregates.py @@ -0,0 +1,88 @@ +# Copyright 2014 NEC Corporation. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from nova.api.validation import parameter_types + +create = { + 'type': 'object', + 'properties': { + 'type': 'object', + 'aggregate': { + 'type': 'object', + 'properties': { + 'name': parameter_types.name, + 'availability_zone': parameter_types.name, + }, + 'required': ['name'], + 'additionalProperties': False, + }, + }, + 'required': ['aggregate'], + 'additionalProperties': False, +} + +update = { + 'type': 'object', + 'properties': { + 'type': 'object', + 'aggregate': { + 'type': 'object', + 'properties': { + 'name': parameter_types.name, + 'availability_zone': parameter_types.name + }, + 'additionalProperties': False, + 'anyOf': [ + {'required': ['name']}, + {'required': ['availability_zone']} + ] + }, + }, + 'required': ['aggregate'], + 'additionalProperties': False, +} + +add_host = { + 'type': 'object', + 'properties': { + 'type': 'object', + 'add_host': { + 'type': 'object', + 'properties': { + 'host': parameter_types.hostname, + }, + 'required': ['host'], + 'additionalProperties': False, + }, + }, + 'required': ['add_host'], + 'additionalProperties': False, +} + +remove_host = { + 'type': 'object', + 'properties': { + 'type': 'object', + 'remove_host': { + 'type': 'object', + 'properties': { + 'host': parameter_types.hostname, + }, + 'required': ['host'], + 'additionalProperties': False, + }, + }, + 'required': ['remove_host'], + 'additionalProperties': False, +} |