1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from rest_framework.views import APIView
20 from rest_framework.decorators import api_view
21 from rest_framework import status
22 from rest_framework.response import Response
24 from mgr.pub.utils.restcall import req_by_msb
25 from mgr.pub.config.config import REG_TO_MSB_REG_URL, REG_TO_MSB_REG_PARAM
27 logger = logging.getLogger(__name__)
29 # ("GET", "^/api/ms1/v1/samples/(?P<sampleId>[0-9a-zA-Z\-\_]+)$", 200, '{"sampleId": "<sampleId>"}')
33 class SampleList(APIView):
34 def get(self, request, format=None):
36 return Response({"status": "active"})
39 @api_view(http_method_names=['GET'])
40 def reg2msb(request, *args, **kwargs):
41 ms_name = kwargs.get('msName')
42 logger.info("[reg2msb]ms name is %s", ms_name)
43 reg_param = REG_TO_MSB_REG_PARAM.copy()
44 reg_param['serviceName'] = ms_name
45 reg_param['url'] = '/api/%s/v1' % ms_name
46 req_by_msb(REG_TO_MSB_REG_URL, "POST", json.JSONEncoder().encode(reg_param))
47 return Response(data={"regok": ms_name}, status=status.HTTP_200_OK)
50 @api_view(http_method_names=['GET'])
51 def reloadstub(request, *args, **kwargs):
52 file_name = kwargs.get('fileName')
53 logger.info("[reloadstub]file name is %s", file_name)
56 with open("/tmp/%s" % file_name) as url_mapping_file:
57 for block in url_mapping_file.read().split("=##="):
60 items = block.split("##")
62 logger.warn("Abnormal block: %s", block)
64 method = items[0].strip().upper()
65 uri_re = re.compile(items[1].strip())
66 code = int(items[2].strip())
67 data = items[3].strip()
68 _stub_mapping_.append((method, uri_re, code, data))
69 return Response(data={"reloadstub": len(_stub_mapping_)}, status=status.HTTP_200_OK)
72 @api_view(http_method_names=['POST', 'GET', 'DELETE', 'PUT'])
73 def stub(request, *args, **kwargs):
74 logger.info("[stub][%s][%s], data=%s", request.method, request.path, request.data)
76 for method, uri_re, code, data in _stub_mapping_:
77 if method != request.method.upper():
79 re_match = uri_re.match(request.path)
82 for k, v in re_match.groupdict().items():
83 data = data.replace('<%s>' % k, v)
84 return Response(data=json.loads(data), status=code)
85 return Response(data={"stub": "stub"}, status=status.HTTP_200_OK)