Add registry API to MultiCloud framework
[multicloud/framework.git] / multivimbroker / multivimbroker / forwarder / views.py
index e1013c7..9eba2ec 100644 (file)
 # 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 logging
-import re
-
-from django.http import HttpResponse
-from django.views.decorators.csrf import csrf_exempt
-
-from rest_framework import status
-
-from multivimbroker.pub.utils.restcall import req_by_msb
-from multivimbroker.pub.msapi.extsys import get_vim_by_id
-
-logger = logging.getLogger(__name__)
-
-
-@csrf_exempt
-def route(request, vimid=''):
-    """ get vim info from vimid from local cache first
-        and then to ESR if cache miss
-    """
-    content = ""
-    status_code = status.HTTP_200_OK
-    try:
-        vim = get_vim_by_id(vimid)
-
-        # if vim type is openstack, use latest "newton" version as default
-        if vim["type"] == "openstack":
-            vim["type"] = "multivim-newton"
-
-        route_uri = re.sub('multivim', vim["type"], request.get_full_path())
-        retcode, content, status_code = \
-            req_by_msb(route_uri, request.method, request.body)
-        if retcode != 0:
-            # Execptions are handled within req_by_msb
-            logger.error("Status code is %s, detail is %s.",
-                         status_code, content)
-    except Exception as e:
-        content = e
-        status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
-    return HttpResponse(content, status_code)
+
+from rest_framework.views import APIView
+from multivimbroker.forwarder.base import BaseHandler
+
+#
+
+
+class BaseServer(BaseHandler, APIView):
+
+    def get(self, request, vimid):
+        raise NotImplementedError()
+
+    def post(self, request, vimid):
+        raise NotImplementedError()
+
+    def put(self, request, vimid):
+        raise NotImplementedError()
+
+    def delete(self, request, vimid):
+        raise NotImplementedError()
+
+    def head(self, request, vimid):
+        raise NotImplementedError()
+
+    def patch(self, request, vimid):
+        raise NotImplementedError()
+
+
+# proxy handler
+class Identity(BaseServer):
+
+    def get(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "GET")
+
+    def post(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "POST")
+
+
+class Registry(BaseServer):
+
+    def post(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "POST")
+
+    def delete(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body,
+                         "DELETE")
+
+
+# forward  handler
+class Forward(BaseServer):
+
+    def get(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "GET")
+
+    def post(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "POST",
+                         headers=None)
+
+    def patch(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "PATCH",
+                         headers=None)
+
+    def delete(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body,
+                         "DELETE", headers=None)
+
+    def head(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "HEAD")
+
+    def put(self, request, vimid):
+
+        return self.send(vimid, request.get_full_path(), request.body, "PUT",
+                         headers=None)