def __call__(self, req): """Generate a WSGI response based on the exception passed to ctor.""" # Replace the body with fault details. code = self.wrapped_exc.status_int fault_name = self._fault_names.get(code, "cloudServersFault") fault_data = { fault_name: { 'code': code, 'message': self.wrapped_exc.explanation } } if code == 413: retry = self.wrapped_exc.headers['Retry-After'] fault_data[fault_name]['retryAfter'] = retry # 'code' is an attribute on the fault tag itself metadata = {'attributes': {fault_name: 'code'}} content_type = req.best_match_content_type() xml_serializer = { '1.0': wsgi.XMLDictSerializer(metadata, wsgi.XMLNS_V10), '1.1': wsgi.XMLDictSerializer(metadata, wsgi.XMLNS_V11), }[common.get_version_from_href(req.url)] serializer = { 'application/xml': xml_serializer, 'application/json': wsgi.JSONDictSerializer(), }[content_type] self.wrapped_exc.body = serializer.serialize(fault_data) self.wrapped_exc.content_type = content_type return self.wrapped_exc
def create_resource(version='1.0'): controller = { '1.0': LimitsControllerV10, '1.1': LimitsControllerV11, }[version]() xmlns = { '1.0': wsgi.XMLNS_V10, '1.1': wsgi.XMLNS_V11, }[version] metadata = { "attributes": { "limit": [ "verb", "URI", "uri", "regex", "value", "unit", "resetTime", "next-available", "remaining", "name" ], }, "plurals": { "rate": "limit", }, } xml_serializer = { '1.0': wsgi.XMLDictSerializer(xmlns=xmlns, metadata=metadata), '1.1': LimitsXMLSerializer(), }[version] body_serializers = { 'application/xml': xml_serializer, } serializer = wsgi.ResponseSerializer(body_serializers) return wsgi.Resource(controller, serializer=serializer)
def test_xml(self): input_dict = dict(servers=dict(a=(2, 3))) expected_xml = '<serversxmlns="asdf"><a>(2,3)</a></servers>' serializer = wsgi.XMLDictSerializer(xmlns="asdf") result = serializer.serialize(input_dict) result = result.replace('\n', '').replace(' ', '') self.assertEqual(result, expected_xml)
def create_resource(version): controller = { '1.0': ControllerV10, '1.1': ControllerV11, }[version]() metadata = { 'list_collections': { 'public': { 'item_name': 'ip', 'item_key': 'addr' }, 'private': { 'item_name': 'ip', 'item_key': 'addr' }, }, } xml_serializer = { '1.0': wsgi.XMLDictSerializer(metadata=metadata, xmlns=wsgi.XMLNS_V11), '1.1': IPXMLSerializer(), }[version] serializer = wsgi.ResponseSerializer({'application/xml': xml_serializer}) return wsgi.Resource(controller, serializer=serializer)
def test_xml_contains_unicode(self): input_dict = dict(test=u'\u89e3\u7801') expected_xml = '<test>\xe8\xa7\xa3\xe7\xa0\x81</test>' serializer = wsgi.XMLDictSerializer() result = serializer.serialize(input_dict) result = result.replace('\n', '').replace(' ', '') self.assertEqual(expected_xml, result)
def test_429_fault(self): metadata = {'attributes': {"overLimit": 'code'}} serializer = wsgi.XMLDictSerializer(metadata=metadata, xmlns=common.XML_NS_V11) fixture = { "overLimit": { "message": "sorry", "code": 429, "retryAfter": 4, }, } output = serializer.serialize(fixture) actual = minidom.parseString(self._prepare_xml(output)) expected = minidom.parseString( self._prepare_xml(""" <overLimit code="429" xmlns="%s"> <message>sorry</message> <retryAfter>4</retryAfter> </overLimit> """) % common.XML_NS_V11) self.assertEqual(expected.toxml(), actual.toxml())
def get_resources(self): resources = [] metadata = _get_metadata() body_serializers = { 'application/xml': wsgi.XMLDictSerializer(metadata=metadata, xmlns=wsgi.XMLNS_V11), } serializer = wsgi.ResponseSerializer(body_serializers, None) body_deserializers = { 'application/xml': SecurityGroupXMLDeserializer(), } deserializer = wsgi.RequestDeserializer(body_deserializers) res = extensions.ResourceExtension('os-security-groups', controller=SecurityGroupController(), deserializer=deserializer, serializer=serializer) resources.append(res) body_deserializers = { 'application/xml': SecurityGroupRulesXMLDeserializer(), } deserializer = wsgi.RequestDeserializer(body_deserializers) res = extensions.ResourceExtension('os-security-group-rules', controller=SecurityGroupRulesController(), deserializer=deserializer, serializer=serializer) resources.append(res) return resources
def create_resource(version='1.0'): controller = { '1.0': ControllerV10, '1.1': ControllerV11, }[version]() metadata = { "attributes": { "image": ["id", "name", "updated", "created", "status", "serverId", "progress", "serverRef"], "link": ["rel", "type", "href"], }, } xml_serializer = { '1.0': wsgi.XMLDictSerializer(metadata, wsgi.XMLNS_V10), '1.1': ImageXMLSerializer(), }[version] body_serializers = { 'application/xml': xml_serializer, } serializer = wsgi.ResponseSerializer(body_serializers) return wsgi.Resource(controller, serializer=serializer)
def create_resource(): body_serializers = { 'application/xml': wsgi.XMLDictSerializer(xmlns=wsgi.XMLNS_V11), } serializer = wsgi.ResponseSerializer(body_serializers) return wsgi.Resource(Controller(), serializer=serializer)
def create_resource(version='1.0'): controller = { '1.0': ControllerV10, '1.1': ControllerV11, }[version]() metadata = { "attributes": { "server": [ "id", "imageId", "name", "flavorId", "hostId", "status", "progress", "adminPass", "flavorRef", "imageRef" ], "link": ["rel", "type", "href"], }, "dict_collections": { "metadata": { "item_name": "meta", "item_key": "key" }, }, "list_collections": { "public": { "item_name": "ip", "item_key": "addr" }, "private": { "item_name": "ip", "item_key": "addr" }, }, } xmlns = { '1.0': wsgi.XMLNS_V10, '1.1': wsgi.XMLNS_V11, }[version] headers_serializer = HeadersSerializer() xml_serializer = { '1.0': wsgi.XMLDictSerializer(metadata, wsgi.XMLNS_V10), '1.1': ServerXMLSerializer(), }[version] body_serializers = { 'application/xml': xml_serializer, } body_deserializers = { 'application/xml': helper.ServerXMLDeserializer(), } serializer = wsgi.ResponseSerializer(body_serializers, headers_serializer) deserializer = wsgi.RequestDeserializer(body_deserializers) return wsgi.Resource(controller, deserializer, serializer)
def create_resource(): metadata = { "attributes": { "account": ["id", "name", "description", "manager"], }, } body_serializers = { 'application/xml': wsgi.XMLDictSerializer(metadata=metadata), } serializer = wsgi.ResponseSerializer(body_serializers) return wsgi.Resource(Controller(), serializer=serializer)
def __call__(self, request): """ Return the wrapped exception with a serialized body conforming to our error format. """ content_type = request.best_match_content_type() metadata = {"attributes": {"overLimitFault": "code"}} xml_serializer = { '1.0': wsgi.XMLDictSerializer(metadata, wsgi.XMLNS_V10), '1.1': wsgi.XMLDictSerializer(metadata, wsgi.XMLNS_V11), }[common.get_version_from_href(request.url)] serializer = { 'application/xml': xml_serializer, 'application/json': wsgi.JSONDictSerializer(), }[content_type] content = serializer.serialize(self.content) self.wrapped_exc.body = content return self.wrapped_exc
def __init__(self, application, ext_mgr=None): if ext_mgr is None: ext_mgr = ExtensionManager() self.ext_mgr = ext_mgr mapper = nova.api.openstack.v2.ProjectMapper() serializer = wsgi.ResponseSerializer( {'application/xml': wsgi.XMLDictSerializer()}) # extended resources for resource in ext_mgr.get_resources(): LOG.debug(_('Extended resource: %s'), resource.collection) if resource.serializer is None: resource.serializer = serializer kargs = dict(controller=wsgi.Resource(resource.controller, resource.deserializer, resource.serializer), collection=resource.collection_actions, member=resource.member_actions) if resource.parent: kargs['parent_resource'] = resource.parent mapper.resource(resource.collection, resource.collection, **kargs) # extended actions action_resources = self._action_ext_resources(application, ext_mgr, mapper) for action in ext_mgr.get_actions(): LOG.debug(_('Extended action: %s'), action.action_name) resource = action_resources[action.collection] resource.add_action(action.action_name, action.handler) # extended requests req_controllers = self._request_ext_resources(application, ext_mgr, mapper) for request_ext in ext_mgr.get_request_extensions(): LOG.debug(_('Extended request: %s'), request_ext.key) controller = req_controllers[request_ext.key] if request_ext.handler: controller.add_handler(request_ext.handler) if request_ext.pre_handler: controller.add_pre_handler(request_ext.pre_handler) self._router = routes.middleware.RoutesMiddleware( self._dispatch, mapper) super(ExtensionMiddleware, self).__init__(application)
def create_resource(): metadata = { "attributes": { "user": ["id", "name", "access", "secret", "admin"], }, } body_serializers = { 'application/xml': wsgi.XMLDictSerializer(metadata=metadata), } serializer = wsgi.ResponseSerializer(body_serializers) return wsgi.Resource(Controller(), serializer=serializer)
def create_resource(): metadata = { 'attributes': { 'backupSchedule': [], }, } body_serializers = { 'application/xml': wsgi.XMLDictSerializer(xmlns=wsgi.XMLNS_V10, metadata=metadata), } serializer = wsgi.ResponseSerializer(body_serializers) return wsgi.Resource(Controller(), serializer=serializer)
def get_resources(self): resources = [] metadata = _get_metadata() body_serializers = { 'application/xml': wsgi.XMLDictSerializer(metadata=metadata, xmlns=wsgi.XMLNS_V11)} serializer = wsgi.ResponseSerializer(body_serializers, None) res = extensions.ResourceExtension( 'os-virtual-interfaces', controller=ServerVirtualInterfaceController(), parent=dict(member_name='server', collection_name='servers'), serializer=serializer) resources.append(res) return resources
def create_resource(version='1.0'): controller = { '1.0': ControllerV10, '1.1': ControllerV11, }[version]() xml_serializer = { '1.0': wsgi.XMLDictSerializer(xmlns=wsgi.XMLNS_V10), '1.1': FlavorXMLSerializer(), }[version] body_serializers = { 'application/xml': xml_serializer, } serializer = wsgi.ResponseSerializer(body_serializers) return wsgi.Resource(controller, serializer=serializer)
def _setup_ext_routes(self, mapper, ext_mgr): serializer = wsgi.ResponseSerializer( {'application/xml': wsgi.XMLDictSerializer()}) for resource in ext_mgr.get_resources(): LOG.debug(_('Extended resource: %s'), resource.collection) if resource.serializer is None: resource.serializer = serializer kargs = dict(controller=wsgi.Resource(resource.controller, resource.deserializer, resource.serializer), collection=resource.collection_actions, member=resource.member_actions) if resource.parent: kargs['parent_resource'] = resource.parent mapper.resource(resource.collection, resource.collection, **kargs)
def test_400_fault(self): metadata = {'attributes': {"badRequest": 'code'}} serializer = wsgi.XMLDictSerializer(metadata=metadata, xmlns=common.XML_NS_V11) fixture = { "badRequest": { "message": "scram", "code": 400, }, } output = serializer.serialize(fixture) actual = minidom.parseString(self._prepare_xml(output)) expected = minidom.parseString(self._prepare_xml(""" <badRequest code="400" xmlns="%s"> <message>scram</message> </badRequest> """) % common.XML_NS_V11) self.assertEqual(expected.toxml(), actual.toxml())
def test_404_fault(self): metadata = {'attributes': {"itemNotFound": 'code'}} serializer = wsgi.XMLDictSerializer(metadata=metadata, xmlns=common.XML_NS_V11) fixture = { "itemNotFound": { "message": "sorry", "code": 404, }, } output = serializer.serialize(fixture) actual = minidom.parseString(self._prepare_xml(output)) expected = minidom.parseString(self._prepare_xml(""" <itemNotFound code="404" xmlns="%s"> <message>sorry</message> </itemNotFound> """) % common.XML_NS_V11) self.assertEqual(expected.toxml(), actual.toxml())
def serialize(self, data, action='default', template=None): """Serialize data. :param data: The data to serialize. :param action: The action, for identifying the template to use. If no template is provided, get_template() will be called with this action to retrieve the template. :param template: The template to use in serialization. """ # No template provided, look one up if template is None: template = self.get_template(action) # Still couldn't find a template; try the base # XMLDictSerializer if template is None: serial = wsgi.XMLDictSerializer() return serial.serialize(data, action=action) # Serialize the template return template.serialize(data, encoding='UTF-8', xml_declaration=True)
def create_resource(version): controller = { '1.0': Controller, '1.1': ControllerV11, }[version]() metadata = { "attributes": { "zone": ["id", "api_url", "name", "capabilities"], }, } body_serializers = { 'application/xml': wsgi.XMLDictSerializer(xmlns=wsgi.XMLNS_V10, metadata=metadata), } serializer = wsgi.ResponseSerializer(body_serializers) body_deserializers = { 'application/xml': helper.ServerXMLDeserializer(), } deserializer = wsgi.RequestDeserializer(body_deserializers) return wsgi.Resource(controller, deserializer, serializer)