From 626a4fdd412b50684e461f45a71109f8ddf111ce Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 15 Nov 2024 17:06:50 +0000 Subject: [PATCH] api: Add response body schemas for snapshots APIs Change-Id: I9115c5841da0c5cdd6d83a39746b4ff33a2e69be Signed-off-by: Stephen Finucane --- .../openstack/compute/schemas/snapshots.py | 82 +++++++++++++++++++ nova/api/openstack/compute/snapshots.py | 22 +++-- 2 files changed, 96 insertions(+), 8 deletions(-) diff --git a/nova/api/openstack/compute/schemas/snapshots.py b/nova/api/openstack/compute/schemas/snapshots.py index 964db98c55..b1501b7d8c 100644 --- a/nova/api/openstack/compute/schemas/snapshots.py +++ b/nova/api/openstack/compute/schemas/snapshots.py @@ -51,3 +51,85 @@ show_query = { 'properties': {}, 'additionalProperties': True } + +_snapshot_response = { + 'type': 'object', + 'properties': { + 'createdAt': {'type': 'string', 'format': 'date-time'}, + 'displayDescription': {'type': ['string', 'null']}, + 'displayName': {'type': ['string', 'null']}, + 'id': {'type': 'string', 'format': 'uuid'}, + 'volumeId': {'type': 'string', 'format': 'uuid'}, + 'size': {'type': 'integer'}, + 'status': { + 'type': 'string', + # https://github.com/openstack/cinder/blob/26.0.0/cinder/objects/fields.py#L120-L129 + 'enum': [ + 'error', + 'available', + 'creating', + 'deleting', + 'deleted', + # 'updating' is omitted since it is unused in Cinder + 'error_deleting', + 'unmanaging', + 'backing-up', + 'restoring', + ], + }, + }, + 'required': [ + 'createdAt', + 'displayDescription', + 'displayName', + 'id', + 'volumeId', + 'size', + 'status', + ], + 'additionalProperties': False, +} + +show_response = { + 'type': 'object', + 'properties': { + 'snapshot': _snapshot_response, + }, + 'required': ['snapshot'], + 'additionalProperties': False, +} + +delete_response = {'type': 'null'} + +index_response = { + 'type': 'object', + 'properties': { + 'snapshots': { + 'type': 'array', + 'items': _snapshot_response, + }, + }, + 'required': ['snapshots'], + 'additionalProperties': False, +} + +detail_response = { + 'type': 'object', + 'properties': { + 'snapshots': { + 'type': 'array', + 'items': _snapshot_response, + }, + }, + 'required': ['snapshots'], + 'additionalProperties': False, +} + +create_response = { + 'type': 'object', + 'properties': { + 'snapshot': _snapshot_response, + }, + 'required': ['snapshot'], + 'additionalProperties': False, +} diff --git a/nova/api/openstack/compute/snapshots.py b/nova/api/openstack/compute/snapshots.py index 8515fbf5cf..8ebf86eefa 100644 --- a/nova/api/openstack/compute/snapshots.py +++ b/nova/api/openstack/compute/snapshots.py @@ -34,21 +34,22 @@ def _translate_snapshot_detail_view(context, vol): return _translate_snapshot_summary_view(context, vol) -def _translate_snapshot_summary_view(context, vol): +def _translate_snapshot_summary_view(context, snapshot): """Maps keys for snapshots summary view.""" d = {} - d['id'] = vol['id'] - d['volumeId'] = vol['volume_id'] - d['status'] = vol['status'] + d['id'] = snapshot['id'] + d['volumeId'] = snapshot['volume_id'] + d['status'] = snapshot['status'] # NOTE(gagupta): We map volume_size as the snapshot size - d['size'] = vol['volume_size'] - d['createdAt'] = vol['created_at'] - d['displayName'] = vol['display_name'] - d['displayDescription'] = vol['display_description'] + d['size'] = snapshot['volume_size'] + d['createdAt'] = snapshot['created_at'] + d['displayName'] = snapshot['display_name'] + d['displayDescription'] = snapshot['display_description'] return d +@validation.validated class SnapshotController(wsgi.Controller): """The Snapshots API controller for the OpenStack API.""" @@ -59,6 +60,7 @@ class SnapshotController(wsgi.Controller): @wsgi.api_version("2.1", MAX_PROXY_API_SUPPORT_VERSION) @wsgi.expected_errors(404) @validation.query_schema(schema.show_query) + @validation.response_body_schema(schema.show_response) def show(self, req, id): """Return data about the given snapshot.""" context = req.environ['nova.context'] @@ -76,6 +78,7 @@ class SnapshotController(wsgi.Controller): @wsgi.api_version("2.1", MAX_PROXY_API_SUPPORT_VERSION) @wsgi.response(202) @wsgi.expected_errors(404) + @validation.response_body_schema(schema.delete_response) def delete(self, req, id): """Delete a snapshot.""" context = req.environ['nova.context'] @@ -91,6 +94,7 @@ class SnapshotController(wsgi.Controller): @wsgi.api_version("2.1", MAX_PROXY_API_SUPPORT_VERSION) @wsgi.expected_errors(()) @validation.query_schema(schema.index_query) + @validation.response_body_schema(schema.index_response) def index(self, req): """Returns a summary list of snapshots.""" context = req.environ['nova.context'] @@ -102,6 +106,7 @@ class SnapshotController(wsgi.Controller): @wsgi.api_version("2.1", MAX_PROXY_API_SUPPORT_VERSION) @wsgi.expected_errors(()) @validation.query_schema(schema.detail_query) + @validation.response_body_schema(schema.detail_response) def detail(self, req): """Returns a detailed list of snapshots.""" context = req.environ['nova.context'] @@ -122,6 +127,7 @@ class SnapshotController(wsgi.Controller): @wsgi.api_version("2.1", MAX_PROXY_API_SUPPORT_VERSION) @wsgi.expected_errors((400, 403)) @validation.schema(schema.create) + @validation.response_body_schema(schema.create_response) def create(self, req, body): """Creates a new snapshot.""" context = req.environ['nova.context']