From 050be203cb43a12ca430eadfd30c87690b33b9cf Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Mon, 12 Sep 2011 20:10:57 +0000 Subject: Add support for vendor content types --- nova/api/openstack/versions.py | 7 +++++++ nova/api/openstack/wsgi.py | 20 +++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/versions.py b/nova/api/openstack/versions.py index e2f892fb6..04d9915ca 100644 --- a/nova/api/openstack/versions.py +++ b/nova/api/openstack/versions.py @@ -100,13 +100,17 @@ class Versions(wsgi.Resource): body_serializers = { 'application/atom+xml': VersionsAtomSerializer(metadata=metadata), 'application/xml': VersionsXMLSerializer(metadata=metadata), + 'application/vnd.openstack.compute+xml': + VersionsXMLSerializer(metadata=metadata), } serializer = wsgi.ResponseSerializer( body_serializers=body_serializers, headers_serializer=headers_serializer) supported_content_types = ('application/json', + 'application/vnd.openstack.compute+json', 'application/xml', + 'application/vnd.openstack.compute+xml', 'application/atom+xml') deserializer = VersionsRequestDeserializer( supported_content_types=supported_content_types) @@ -383,12 +387,15 @@ def create_resource(version='1.0'): body_serializers = { 'application/xml': VersionsXMLSerializer(), + 'application/vnd.openstack.compute+xml': VersionsXMLSerializer(), 'application/atom+xml': VersionsAtomSerializer(), } serializer = wsgi.ResponseSerializer(body_serializers) supported_content_types = ('application/json', + 'application/vnd.openstack.compute+json', 'application/xml', + 'application/vnd.openstack.compute+xml', 'application/atom+xml') deserializer = wsgi.RequestDeserializer( supported_content_types=supported_content_types) diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 8641e960a..ee6b87403 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -28,8 +28,12 @@ class Request(webob.Request): Based on the query extension then the Accept header. """ + LOG.info('supported = %s' % repr(supported_content_types)) supported_content_types = supported_content_types or \ - ('application/json', 'application/xml') + ('application/json', + 'application/vnd.openstack.compute+json', + 'application/xml', + 'application/vnd.openstack.compute+xml') parts = self.path.rsplit('.', 1) if len(parts) > 1: @@ -51,7 +55,10 @@ class Request(webob.Request): if not "Content-Type" in self.headers: return None - allowed_types = ("application/xml", "application/json") + allowed_types = ('application/json', + 'application/vnd.openstack.compute+json', + 'application/xml', + 'application/vnd.openstack.compute+xml') content_type = self.content_type if content_type not in allowed_types: @@ -191,11 +198,16 @@ class RequestDeserializer(object): supported_content_types=None): self.supported_content_types = supported_content_types or \ - ('application/json', 'application/xml') + ('application/json', + 'application/vnd.openstack.compute+json', + 'application/xml', + 'application/vnd.openstack.compute+xml') self.body_deserializers = { 'application/xml': XMLDeserializer(), + 'application/vnd.openstack.compute+xml': XMLDeserializer(), 'application/json': JSONDeserializer(), + 'application/vnd.openstack.compute+json': JSONDeserializer(), } self.body_deserializers.update(body_deserializers or {}) @@ -409,7 +421,9 @@ class ResponseSerializer(object): def __init__(self, body_serializers=None, headers_serializer=None): self.body_serializers = { 'application/xml': XMLDictSerializer(), + 'application/vnd.openstack.compute+xml': XMLDictSerializer(), 'application/json': JSONDictSerializer(), + 'application/vnd.openstack.compute+json': JSONDictSerializer(), } self.body_serializers.update(body_serializers or {}) -- cgit From 9f83b51ae2afeb45ed9bdcb8c3b63ced78f8050e Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Wed, 14 Sep 2011 16:19:18 +0000 Subject: Remove debugging --- nova/api/openstack/wsgi.py | 1 - 1 file changed, 1 deletion(-) (limited to 'nova/api') diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 6eb1953b4..c342f6267 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -29,7 +29,6 @@ class Request(webob.Request): Based on the query extension then the Accept header. """ - LOG.info('supported = %s' % repr(supported_content_types)) supported_content_types = supported_content_types or \ ('application/json', 'application/vnd.openstack.compute+json', -- cgit From 2b41dd235b50e3003a42e0b860c5915d06d86071 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Wed, 14 Sep 2011 17:23:40 +0000 Subject: Map vendor content types to their standard content type before serializing or deserializing. This is so we don't have to litter the code with both types when they are treated identically --- nova/api/openstack/wsgi.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index c342f6267..1c4724c8f 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -19,6 +19,21 @@ XMLNS_ATOM = 'http://www.w3.org/2005/Atom' LOG = logging.getLogger('nova.api.openstack.wsgi') +# The vendor content types should serialize identically to the non-vendor +# content types. So to avoid littering the code with both options, we +# map the vendor to the other when looking up the type +_CONTENT_TYPE_MAP = { + 'application/vnd.openstack.compute+json': 'application/json', + 'application/vnd.openstack.compute+xml': 'application/xml', +} + +_SUPPORTED_CONTENT_TYPES = ( + 'application/json', + 'application/vnd.openstack.compute+json', + 'application/xml', + 'application/vnd.openstack.compute+xml', +) + class Request(webob.Request): """Add some Openstack API-specific logic to the base webob.Request.""" @@ -30,10 +45,7 @@ class Request(webob.Request): """ supported_content_types = supported_content_types or \ - ('application/json', - 'application/vnd.openstack.compute+json', - 'application/xml', - 'application/vnd.openstack.compute+xml') + _SUPPORTED_CONTENT_TYPES parts = self.path.rsplit('.', 1) if len(parts) > 1: @@ -55,10 +67,7 @@ class Request(webob.Request): if not "Content-Type" in self.headers: return None - allowed_types = ('application/json', - 'application/vnd.openstack.compute+json', - 'application/xml', - 'application/vnd.openstack.compute+xml') + allowed_types = _SUPPORTED_CONTENT_TYPES content_type = self.content_type if content_type not in allowed_types: @@ -198,16 +207,11 @@ class RequestDeserializer(object): supported_content_types=None): self.supported_content_types = supported_content_types or \ - ('application/json', - 'application/vnd.openstack.compute+json', - 'application/xml', - 'application/vnd.openstack.compute+xml') + _SUPPORTED_CONTENT_TYPES self.body_deserializers = { 'application/xml': XMLDeserializer(), - 'application/vnd.openstack.compute+xml': XMLDeserializer(), 'application/json': JSONDeserializer(), - 'application/vnd.openstack.compute+json': JSONDeserializer(), } self.body_deserializers.update(body_deserializers or {}) @@ -261,6 +265,7 @@ class RequestDeserializer(object): def get_body_deserializer(self, content_type): try: + content_type = _CONTENT_TYPE_MAP.get(content_type, content_type) return self.body_deserializers[content_type] except (KeyError, TypeError): raise exception.InvalidContentType(content_type=content_type) @@ -425,9 +430,7 @@ class ResponseSerializer(object): def __init__(self, body_serializers=None, headers_serializer=None): self.body_serializers = { 'application/xml': XMLDictSerializer(), - 'application/vnd.openstack.compute+xml': XMLDictSerializer(), 'application/json': JSONDictSerializer(), - 'application/vnd.openstack.compute+json': JSONDictSerializer(), } self.body_serializers.update(body_serializers or {}) @@ -452,6 +455,7 @@ class ResponseSerializer(object): def serialize_body(self, response, data, content_type, action): response.headers['Content-Type'] = content_type if data is not None: + content_type = _CONTENT_TYPE_MAP.get(content_type, content_type) serializer = self.get_body_serializer(content_type) response.body = serializer.serialize(data, action) -- cgit From 1b836a4159bd324572f71dab4abcef5a8d3292e5 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Wed, 14 Sep 2011 17:29:28 +0000 Subject: Add copyright --- nova/api/openstack/wsgi.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'nova/api') diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index 1c4724c8f..c68a57cb5 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -1,3 +1,19 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# 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. import json from lxml import etree -- cgit From d678d8c4d024a4154ecd2ea77a42063ad1253364 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Wed, 14 Sep 2011 17:32:33 +0000 Subject: Remove unnecessary vendor content types now that they are mapped to standard content types automatically --- nova/api/openstack/versions.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/versions.py b/nova/api/openstack/versions.py index f4ae498b4..75a1d0ba4 100644 --- a/nova/api/openstack/versions.py +++ b/nova/api/openstack/versions.py @@ -101,8 +101,6 @@ class Versions(wsgi.Resource): body_serializers = { 'application/atom+xml': VersionsAtomSerializer(metadata=metadata), 'application/xml': VersionsXMLSerializer(metadata=metadata), - 'application/vnd.openstack.compute+xml': - VersionsXMLSerializer(metadata=metadata), } serializer = wsgi.ResponseSerializer( body_serializers=body_serializers, @@ -307,7 +305,6 @@ def create_resource(version='1.0'): body_serializers = { 'application/xml': VersionsXMLSerializer(), - 'application/vnd.openstack.compute+xml': VersionsXMLSerializer(), 'application/atom+xml': VersionsAtomSerializer(), } serializer = wsgi.ResponseSerializer(body_serializers) -- cgit From 8638db07c4ad2177249a70708969c7a1cba09037 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Wed, 14 Sep 2011 17:55:15 +0000 Subject: Don't report the wrong content type if a mapped type doesn't exist --- nova/api/openstack/wsgi.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/wsgi.py b/nova/api/openstack/wsgi.py index c68a57cb5..fad516d4d 100644 --- a/nova/api/openstack/wsgi.py +++ b/nova/api/openstack/wsgi.py @@ -281,8 +281,8 @@ class RequestDeserializer(object): def get_body_deserializer(self, content_type): try: - content_type = _CONTENT_TYPE_MAP.get(content_type, content_type) - return self.body_deserializers[content_type] + ctype = _CONTENT_TYPE_MAP.get(content_type, content_type) + return self.body_deserializers[ctype] except (KeyError, TypeError): raise exception.InvalidContentType(content_type=content_type) @@ -471,13 +471,13 @@ class ResponseSerializer(object): def serialize_body(self, response, data, content_type, action): response.headers['Content-Type'] = content_type if data is not None: - content_type = _CONTENT_TYPE_MAP.get(content_type, content_type) serializer = self.get_body_serializer(content_type) response.body = serializer.serialize(data, action) def get_body_serializer(self, content_type): try: - return self.body_serializers[content_type] + ctype = _CONTENT_TYPE_MAP.get(content_type, content_type) + return self.body_serializers[ctype] except (KeyError, TypeError): raise exception.InvalidContentType(content_type=content_type) -- cgit