X-Git-Url: https://gerrit.onap.org/r/gitweb?p=multicloud%2Fframework.git;a=blobdiff_plain;f=multivimbroker%2Fmultivimbroker%2Fforwarder%2Fviews.py;h=1e57d0ebb75a3e35e2a9f84388f5578bf2287612;hp=d1763c218b5866ba153430663727c0c90c86e812;hb=69a315e508da7716c0c01caeaafd3da5a3d20599;hpb=ca0a41b2842df32a7c71a77799adcb435a2d4df8 diff --git a/multivimbroker/multivimbroker/forwarder/views.py b/multivimbroker/multivimbroker/forwarder/views.py index d1763c2..1e57d0e 100644 --- a/multivimbroker/multivimbroker/forwarder/views.py +++ b/multivimbroker/multivimbroker/forwarder/views.py @@ -13,7 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import json +import re +import tempfile +from poster.encode import multipart_encode +from poster.streaminghttp import register_openers from rest_framework.views import APIView from rest_framework.views import Response @@ -21,6 +26,7 @@ from rest_framework.views import status from multivimbroker.forwarder.base import BaseHandler from multivimbroker.pub.utils.syscomm import originHeaders from multivimbroker.pub.utils import syscomm +from rest_framework.parsers import MultiPartParser class BaseServer(BaseHandler, APIView): @@ -107,11 +113,11 @@ class CheckCapacity(BaseServer): for vim in body.get("VIMs", []): url = request.get_full_path().replace( "check_vim_capacity", "%s/capacity_check" % vim) - resp = self.send(vim, url, newbody, "POST") - if resp.status_code != status.HTTP_200_OK: + resp = self.send(vim, url, json.dumps(newbody), "POST") + if int(resp.status_code) != status.HTTP_200_OK: continue try: - resp_body = json.loads(resp.body) + resp_body = json.loads(resp.content) except ValueError: continue if not resp_body.get("result", False): @@ -125,28 +131,65 @@ class Forward(BaseServer): def get(self, request, vimid): - return self.send(vimid, request.get_full_path(), request.body, "GET") + return self.send(vimid, request.get_full_path(), request.body, "GET", + headers=originHeaders(request)) def post(self, request, vimid): return self.send(vimid, request.get_full_path(), request.body, "POST", - headers=None) + headers=originHeaders(request)) def patch(self, request, vimid): return self.send(vimid, request.get_full_path(), request.body, "PATCH", - headers=None) + headers=originHeaders(request)) def delete(self, request, vimid): return self.send(vimid, request.get_full_path(), request.body, - "DELETE", headers=None) + "DELETE", headers=originHeaders(request)) def head(self, request, vimid): - return self.send(vimid, request.get_full_path(), request.body, "HEAD") + return self.send(vimid, request.get_full_path(), request.body, "HEAD", + headers=originHeaders(request)) def put(self, request, vimid): return self.send(vimid, request.get_full_path(), request.body, "PUT", - headers=None) + headers=originHeaders(request)) + + +# Multipart view +class MultiPartView(BaseServer): + + parser_classes = (MultiPartParser, ) + + def post(self, request, vimid): + try: + register_openers() + fileDict = dict(request.FILES.iterlists()) + params = {} + for key in fileDict.keys(): + fileObj = fileDict[key][0] + f = tempfile.NamedTemporaryFile(prefix="django_", + suffix=fileObj._name, + delete=False) + f.write(fileObj.file.read()) + f.seek(fileObj.file.tell(), 0) + fileObj.file.close() + params[key] = open(f.name, 'rb') + datagen, headers = multipart_encode(params) + regex = re.compile('^HTTP_') + for key, value in request.META.iteritems(): + if key.startswith("HTTP_"): + headers[regex.sub('', key).replace('_', '-')] = value + resp = self.send(vimid, request.path, datagen, "POST", + headers=headers) + finally: + for key in params: + fileRef = params[key] + if fileRef.closed is False: + fileRef.close() + os.remove(fileRef.name) + return resp