Merge Multipart API with InfraWorkload
[multicloud/framework.git] / multivimbroker / multivimbroker / forwarder / base.py
1 # Copyright (c) 2017-2018 VMware, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13 import logging
14
15 from django.http import HttpResponse
16 from rest_framework import status
17
18 import multivimbroker.pub.exceptions as exceptions
19 from multivimbroker.pub.utils.syscomm import getHeadersKeys
20 from multivimbroker.pub.utils.syscomm import getMultivimDriver
21 from multivimbroker.pub.utils.restcall import req_by_msb
22 from multivimbroker.pub.utils.restcall import req_by_msb_multipart
23
24
25 logger = logging.getLogger(__name__)
26
27
28 class BaseHandler(object):
29
30     def _request(self, route_uri, method, body="", headers=None,
31                  multipart=False):
32
33         try:
34             if multipart:
35                 return self._multipart_req(route_uri, method, body, headers)
36             retcode, content, status_code, resp = \
37                 req_by_msb(route_uri, method, body, headers)
38             if retcode != 0:
39                 # Execptions are handled within req_by_msb
40                 logger.error("Status code is %s, detail is %s.",
41                              status_code, content)
42
43         except exceptions.NotFound as e:
44             return HttpResponse(str(e), status=status.HTTP_404_NOT_FOUND)
45
46         except Exception as e:
47             content = e
48             status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
49             logger.exception("exception: %s" % e)
50
51         response = HttpResponse(content, status=status_code)
52         if retcode == 0:
53             for k in getHeadersKeys(resp):
54                 response[k] = resp[k]
55         return response
56
57     def _multipart_req(self, route_uri, method, body, headers=None):
58
59         try:
60             retcode, content, status_code, resp = \
61                 req_by_msb_multipart(route_uri, method, body, headers)
62             if retcode != 0:
63                 # Execptions are handled within req_by_msb
64                 logger.error("Status code is %s, detail is %s.",
65                              status_code, content)
66
67         except exceptions.NotFound as e:
68             return HttpResponse(str(e), status=status.HTTP_404_NOT_FOUND)
69
70         except Exception as e:
71             content = e
72             status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
73             logger.exception("exception: %s" % e)
74
75         response = HttpResponse(content, status=status_code)
76         return response
77
78     def send(self, vimid, full_path, body, method, headers=None,
79              multipart=False):
80
81         try:
82             url = getMultivimDriver(vimid, full_path=full_path)
83         except exceptions.VimBrokerException as e:
84             logging.exception("vimbroker exception: %s" % e)
85             return HttpResponse(e.content, status=e.status_code)
86         except Exception as e:
87             logging.exception("unkown exception: %s" % e)
88             return HttpResponse(str(e),
89                                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
90         return self._request(url, method, body=body, headers=headers,
91                              multipart=multipart)