def getresponse(self): res = self.Response(status=self.status()) # If the host is 10.0.0.1, return the port as an error code if self.host == "10.0.0.1": res.status = self.port return res # Extract important information from the action string to assure sanity match = re.search('tenants/(.+?)/(.+)\.(json|xml)$', self.action) tenant = match.group(1) path = match.group(2) format = match.group(3) data = { 'data': { 'method': self.method, 'action': self.action, 'body': self.body, 'tenant': tenant, 'path': path, 'format': format, 'key_file': self.key_file, 'cert_file': self.cert_file } } # Serialize it to the proper format so the API client can handle it if data['data']['format'] == 'json': res.content = Serializer().serialize(data, "application/json") else: res.content = Serializer().serialize(data, "application/xml") return res
def _deserialize(self, data, content_type): """Deserialize the request body to the specefied content type. Uses self._serialization_metadata if it exists, which is a dict mapping MIME types to information needed to serialize to that type. """ _metadata = getattr(type(self), "_serialization_metadata", {}) serializer = Serializer(_metadata) return serializer.deserialize(data, content_type)
def _serialize(self, data, content_type, default_xmlns): """Serialize the given dict to the provided content_type. Uses self._serialization_metadata if it exists, which is a dict mapping MIME types to information needed to serialize to that type. """ _metadata = getattr(type(self), "_serialization_metadata", {}) serializer = Serializer(_metadata, default_xmlns) try: return serializer.serialize(data, content_type) except exception.InvalidContentType: raise webob.exc.HTTPNotAcceptable()
def deserialize(self, data, status_code): """ Deserializes a an xml or json string into a dictionary """ if status_code == 204: return data return Serializer(self._serialization_metadata).\ deserialize(data, self.content_type())
def new_network_request(tenant_id, network_name='new_name', format='xml', custom_req_body=None): method = 'POST' path = "/tenants/%(tenant_id)s/networks.%(format)s" % locals() data = custom_req_body or {'network': {'name': '%s' % network_name}} content_type = "application/%s" % format body = Serializer().serialize(data, content_type) return create_request(path, body, content_type, method)
def put_attachment_request(tenant_id, network_id, port_id, attachment_id, format='xml'): method = 'PUT' path = "/tenants/%(tenant_id)s/networks/" \ "%(network_id)s/ports/%(port_id)s/attachment.%(format)s" % locals() data = {'attachment': {'id': attachment_id}} content_type = "application/%s" % format body = Serializer().serialize(data, content_type) return create_request(path, body, content_type, method)
def update_port_request(tenant_id, network_id, port_id, port_state, format='xml', custom_req_body=None): method = 'PUT' path = "/tenants/%(tenant_id)s/networks" \ "/%(network_id)s/ports/%(port_id)s.%(format)s" % locals() data = custom_req_body or {'port': {'state': '%s' % port_state}} content_type = "application/%s" % format body = Serializer().serialize(data, content_type) return create_request(path, body, content_type, method)
def serialize(self, data): """ Serializes a dictionary with a single key (which can contain any structure) into either xml or json """ if data is None: return None elif type(data) is dict: return Serializer().serialize(data, self.content_type()) else: raise Exception("unable to serialize object of type = '%s'" \ % type(data))