diff options
-rw-r--r-- | mgr/mgr/samples/urls.py | 5 | ||||
-rw-r--r-- | mgr/mgr/samples/views.py | 50 | ||||
-rw-r--r-- | mgr/mgr/urls.py | 2 |
3 files changed, 55 insertions, 2 deletions
diff --git a/mgr/mgr/samples/urls.py b/mgr/mgr/samples/urls.py index e5e66d7..ade08a5 100644 --- a/mgr/mgr/samples/urls.py +++ b/mgr/mgr/samples/urls.py @@ -16,5 +16,8 @@ from django.conf.urls import url from mgr.samples import views urlpatterns = [ - url(r'^samples/$', views.SampleList.as_view()) + url(r'^samples/$', views.SampleList.as_view()), + url(r'^api/vnfmgr/v1/reloadstub/(?P<fileName>[0-9a-zA-Z\-\_\.]+)$', views.reloadstub, name='reloadstub'), + url(r'^api/vnfmgr/v1/reg2msb/(?P<msName>[0-9a-zA-Z\-\_]+)$', views.reg2msb, name='reg2msb'), + url(r'^(?P<uri>[0-9a-zA-Z\-\_/]+)$', views.stub, name='stub') ] diff --git a/mgr/mgr/samples/views.py b/mgr/mgr/samples/views.py index 5162f23..0c25d4f 100644 --- a/mgr/mgr/samples/views.py +++ b/mgr/mgr/samples/views.py @@ -13,14 +13,64 @@ # limitations under the License. import logging +import json from rest_framework.views import APIView +from rest_framework.decorators import api_view +from rest_framework import status from rest_framework.response import Response +from mgr.pub.utils.restcall import req_by_msb +from mgr.pub.config.config import REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM + logger = logging.getLogger(__name__) +_stub_mapping_ = { + ("GET", "/api/ms1/v1/samples/1"): (200, {"status": "ok"}) +} + class SampleList(APIView): def get(self, request, format=None): logger.debug("get") return Response({"status": "active"}) + + +@api_view(http_method_names=['GET']) +def reg2msb(request, *args, **kwargs): + ms_name = kwargs.get('msName') + logger.info("[reg2msb]ms name is %s", ms_name) + reg_param = REG_TO_MSB_REG_PARAM.copy() + reg_param['serviceName'] = ms_name + reg_param['url'] = '/api/%s/v1' % ms_name + req_by_msb(REG_TO_MSB_REG_URL, "POST", json.JSONEncoder().encode(reg_param)) + return Response(data={"regok": ms_name}, status=status.HTTP_200_OK) + + +@api_view(http_method_names=['GET']) +def reloadstub(request, *args, **kwargs): + file_name = kwargs.get('fileName') + logger.info("[reloadstub]file name is %s", file_name) + global _stub_mapping_ + with open("/tmp/%s" % file_name) as url_mapping_file: + for block in url_mapping_file.read().split("=##="): + items = block.split("|") + if len(items) != 4: + logger.warn("Abnormal block: %s", block) + continue + method = items[0].strip() + uri = items[1].strip() + code = int(items[2].strip()) + data = json.loads(items[3].strip()) + _stub_mapping_[(method, uri)] = (code, data) + return Response(data={"reloadstub": "ok"}, status=status.HTTP_200_OK) + + +@api_view(http_method_names=['POST', 'GET', 'DELETE', 'PUT']) +def stub(request, *args, **kwargs): + logger.info("[stub][%s][%s], data=%s", request.method, request.path, request.data) + global _stub_mapping_ + match_result = _stub_mapping_.get((request.method.upper(), request.path)) + if match_result: + return Response(data=match_result[1], status=match_result[0]) + return Response(data={"stub": "stub"}, status=status.HTTP_200_OK) diff --git a/mgr/mgr/urls.py b/mgr/mgr/urls.py index 262da0b..ecd5d20 100644 --- a/mgr/mgr/urls.py +++ b/mgr/mgr/urls.py @@ -17,9 +17,9 @@ from django.conf.urls import include, url from mgr.pub.config.config import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM urlpatterns = [ - url(r'^', include('mgr.samples.urls')), url(r'^', include('mgr.vnfreg.urls')), url(r'^', include('mgr.swagger.urls')), + url(r'^', include('mgr.samples.urls')), ] # regist to MSB when startup |