From 2b3eb6b01593d5a26f3cdea95b2b4f29352597e4 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 5 Nov 2024 11:25:34 +0000 Subject: [PATCH] api: Add response body schemas for server IPs APIs We also resolve a small mishap that has led to some unused schemas. Change-Id: Iad8ab646f5c14ec1396fd48616e09d4271ccb304 Signed-off-by: Stephen Finucane --- nova/api/openstack/compute/ips.py | 9 ++- .../openstack/compute/schemas/addresses.py | 25 ------- nova/api/openstack/compute/schemas/ips.py | 67 +++++++++++++++++-- .../compute/views/{addresses.py => ips.py} | 0 nova/api/openstack/compute/views/servers.py | 6 +- 5 files changed, 72 insertions(+), 35 deletions(-) delete mode 100644 nova/api/openstack/compute/schemas/addresses.py rename nova/api/openstack/compute/views/{addresses.py => ips.py} (100%) diff --git a/nova/api/openstack/compute/ips.py b/nova/api/openstack/compute/ips.py index 13a9a2b89aa8..ab52a61b4804 100644 --- a/nova/api/openstack/compute/ips.py +++ b/nova/api/openstack/compute/ips.py @@ -16,8 +16,8 @@ from webob import exc from nova.api.openstack import common -from nova.api.openstack.compute.schemas import addresses as schema -from nova.api.openstack.compute.views import addresses as views_addresses +from nova.api.openstack.compute.schemas import ips as schema +from nova.api.openstack.compute.views import ips as views from nova.api.openstack import wsgi from nova.api import validation from nova.compute import api as compute @@ -25,9 +25,10 @@ from nova.i18n import _ from nova.policies import ips as ips_policies +@validation.validated class IPsController(wsgi.Controller): """The servers addresses API controller for the OpenStack API.""" - _view_builder_class = views_addresses.ViewBuilder + _view_builder_class = views.ViewBuilder def __init__(self): super(IPsController, self).__init__() @@ -35,6 +36,7 @@ class IPsController(wsgi.Controller): @wsgi.expected_errors(404) @validation.query_schema(schema.index_query) + @validation.response_body_schema(schema.index_response) def index(self, req, server_id): context = req.environ["nova.context"] instance = common.get_instance(self._compute_api, context, server_id) @@ -45,6 +47,7 @@ class IPsController(wsgi.Controller): @wsgi.expected_errors(404) @validation.query_schema(schema.show_query) + @validation.response_body_schema(schema.show_response) def show(self, req, server_id, id): context = req.environ["nova.context"] instance = common.get_instance(self._compute_api, context, server_id) diff --git a/nova/api/openstack/compute/schemas/addresses.py b/nova/api/openstack/compute/schemas/addresses.py deleted file mode 100644 index c1fcc7b484ed..000000000000 --- a/nova/api/openstack/compute/schemas/addresses.py +++ /dev/null @@ -1,25 +0,0 @@ -# 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. - -# TODO(stephenfin): Remove additionalProperties in a future API version -index_query = { - 'type': 'object', - 'properties': {}, - 'additionalProperties': True, -} - -# TODO(stephenfin): Remove additionalProperties in a future API version -show_query = { - 'type': 'object', - 'properties': {}, - 'additionalProperties': True, -} diff --git a/nova/api/openstack/compute/schemas/ips.py b/nova/api/openstack/compute/schemas/ips.py index 1a58ab021b43..c6e468868b9f 100644 --- a/nova/api/openstack/compute/schemas/ips.py +++ b/nova/api/openstack/compute/schemas/ips.py @@ -10,8 +10,67 @@ # License for the specific language governing permissions and limitations # under the License. -# NOTE(stephenfin): These schemas are intentionally empty since these APIs have -# been removed +import copy -index_query = {} -show_query = {} +# TODO(stephenfin): Remove additionalProperties in a future API version +index_query = { + 'type': 'object', + 'properties': {}, + 'additionalProperties': True, +} + +# TODO(stephenfin): Remove additionalProperties in a future API version +show_query = { + 'type': 'object', + 'properties': {}, + 'additionalProperties': True, +} + +_ip_address = { + 'type': 'object', + 'properties': { + 'addr': { + 'type': 'string', + 'oneOf': [ + {'format': 'ipv4'}, + {'format': 'ipv6'}, + ], + }, + 'version': { + 'enum': [4, 6], + }, + }, + 'required': ['addr', 'version'], + 'additionalProperties': False, +} + +index_response = { + 'type': 'object', + 'properties': { + 'addresses': { + 'type': 'object', + 'patternProperties': { + # TODO(stephenfin): Surely there are some limitations on + # network names? + '^.+$': { + 'type': 'array', + 'items': copy.deepcopy(_ip_address), + }, + }, + }, + }, + 'required': ['addresses'], + 'additionalProperties': False, +} + +show_response = { + 'type': 'object', + 'patternProperties': { + # TODO(stephenfin): Surely there are some limitations on + # network names? + '^.+$': { + 'type': 'array', + 'items': copy.deepcopy(_ip_address), + }, + }, +} diff --git a/nova/api/openstack/compute/views/addresses.py b/nova/api/openstack/compute/views/ips.py similarity index 100% rename from nova/api/openstack/compute/views/addresses.py rename to nova/api/openstack/compute/views/ips.py diff --git a/nova/api/openstack/compute/views/servers.py b/nova/api/openstack/compute/views/servers.py index d3be41f8b173..444688a7fd4e 100644 --- a/nova/api/openstack/compute/views/servers.py +++ b/nova/api/openstack/compute/views/servers.py @@ -19,9 +19,9 @@ from oslo_serialization import jsonutils from nova.api.openstack import api_version_request from nova.api.openstack import common -from nova.api.openstack.compute.views import addresses as views_addresses from nova.api.openstack.compute.views import flavors as views_flavors from nova.api.openstack.compute.views import images as views_images +from nova.api.openstack.compute.views import ips as views_ips from nova import availability_zones as avail_zone from nova.compute import api as compute from nova.compute import vm_states @@ -69,8 +69,8 @@ class ViewBuilder(common.ViewBuilder): def __init__(self): """Initialize view builder.""" super(ViewBuilder, self).__init__() - self._address_builder = views_addresses.ViewBuilder() self._image_builder = views_images.ViewBuilder() + self._ips_builder = views_ips.ViewBuilder() self._flavor_builder = views_flavors.ViewBuilder() self.compute_api = compute.API() @@ -614,7 +614,7 @@ class ViewBuilder(common.ViewBuilder): context = request.environ["nova.context"] networks = common.get_networks_for_instance(context, instance) - return self._address_builder.index( + return self._ips_builder.index( request, networks, extend_address, )["addresses"]