summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2018-10-19 17:08:23 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2018-10-19 17:08:23 +0800
commit50a99289362a394c8441889ed54edb2ac7c86848 (patch)
tree3ca69c42a1a5f01918852c183c32bb542b24e2e7
parent313669691c4f187d13e133a271d415ce27545240 (diff)
Update stub api for vfc
Change-Id: I2fcdf2a1724b24fd989250c4a1f5d43365d69269 Issue-ID: VFC-1150 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
-rw-r--r--mgr/mgr/samples/data/urlmap.conf13
-rw-r--r--mgr/mgr/samples/views.py33
2 files changed, 34 insertions, 12 deletions
diff --git a/mgr/mgr/samples/data/urlmap.conf b/mgr/mgr/samples/data/urlmap.conf
new file mode 100644
index 0000000..224212f
--- /dev/null
+++ b/mgr/mgr/samples/data/urlmap.conf
@@ -0,0 +1,13 @@
+GET ##
+^/api/ms1/v1/samples/(?P<sampleId>[0-9a-zA-Z\-\_]+)$ ##
+200 ##
+{
+ "sampleId": "<sampleId>"
+}
+=##=
+GET ##
+^/api/ms1/v1/yyyy/2$ ##
+200 ##
+{
+ "status": "ng"
+} \ No newline at end of file
diff --git a/mgr/mgr/samples/views.py b/mgr/mgr/samples/views.py
index 0c25d4f..5a56260 100644
--- a/mgr/mgr/samples/views.py
+++ b/mgr/mgr/samples/views.py
@@ -14,6 +14,7 @@
import logging
import json
+import re
from rest_framework.views import APIView
from rest_framework.decorators import api_view
@@ -25,9 +26,8 @@ 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"})
-}
+# ("GET", "^/api/ms1/v1/samples/(?P<sampleId>[0-9a-zA-Z\-\_]+)$", 200, '{"sampleId": "<sampleId>"}')
+_stub_mapping_ = []
class SampleList(APIView):
@@ -52,25 +52,34 @@ def reloadstub(request, *args, **kwargs):
file_name = kwargs.get('fileName')
logger.info("[reloadstub]file name is %s", file_name)
global _stub_mapping_
+ _stub_mapping_ = []
with open("/tmp/%s" % file_name) as url_mapping_file:
for block in url_mapping_file.read().split("=##="):
- items = block.split("|")
+ if not block.strip():
+ continue
+ items = block.split("##")
if len(items) != 4:
logger.warn("Abnormal block: %s", block)
continue
- method = items[0].strip()
- uri = items[1].strip()
+ method = items[0].strip().upper()
+ uri_re = re.compile(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)
+ data = items[3].strip()
+ _stub_mapping_.append((method, uri_re, code, data))
+ return Response(data={"reloadstub": len(_stub_mapping_)}, 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])
+ for method, uri_re, code, data in _stub_mapping_:
+ if method != request.method.upper():
+ continue
+ re_match = uri_re.match(request.path)
+ if not re_match:
+ continue
+ for k, v in re_match.groupdict().items():
+ data = data.replace('<%s>' % k, v)
+ return Response(data=json.loads(data), status=code)
return Response(data={"stub": "stub"}, status=status.HTTP_200_OK)