aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfengyuanxing <feng.yuanxing@zte.com.cn>2018-01-26 16:01:42 +0800
committerfengyuanxing <feng.yuanxing@zte.com.cn>2018-01-26 16:01:42 +0800
commit09c65445af062c57846d624a1546e7915c85104b (patch)
tree1c3e27d2a3dbc7f25d106b554e972e4426059088
parentae55ee8a41431c64406c81b5db612c8ecbdef4c9 (diff)
Add validation code
Change-Id: I37591d5c4300cff28cf2fcc6a54109979300c711 Issue-ID: VFC-685 Signed-off-by: fengyuanxing <feng.yuanxing@zte.com.cn>
-rw-r--r--catalog/jobs/views.py16
-rw-r--r--catalog/packages/views.py25
-rw-r--r--catalog/serializers.py12
3 files changed, 30 insertions, 23 deletions
diff --git a/catalog/jobs/views.py b/catalog/jobs/views.py
index 34391068..b23156f4 100644
--- a/catalog/jobs/views.py
+++ b/catalog/jobs/views.py
@@ -22,8 +22,8 @@ from rest_framework import status
from catalog.jobs.job_get import GetJobInfoService
from catalog.pub.utils.jobutil import JobUtil
from catalog.pub.utils.values import ignore_case_get
-from catalog.serializers import JobResponseSerializer
-from catalog.serializers import PostJobResponseResultSerializer
+from catalog.serializers import PostResponseSerializer
+from catalog.serializers import GetJobResponseResultSerializer
from catalog.serializers import PostJobRequestSerializer
logger = logging.getLogger(__name__)
@@ -38,13 +38,13 @@ class JobView(APIView):
operation_description="Get job status",
manual_parameters=[input_job_id, input_response_id],
responses={
- status.HTTP_200_OK: JobResponseSerializer(),
- status.HTTP_500_INTERNAL_SERVER_ERROR: PostJobResponseResultSerializer()
+ status.HTTP_200_OK: PostResponseSerializer(),
+ status.HTTP_500_INTERNAL_SERVER_ERROR: GetJobResponseResultSerializer()
})
def get(self, request, job_id):
response_id = ignore_case_get(request.META, 'responseId')
ret = GetJobInfoService(job_id, response_id).do_biz()
- response_serializer = JobResponseSerializer(data=ret)
+ response_serializer = PostResponseSerializer(data=ret)
isValid = response_serializer.is_valid()
if not isValid:
message = 'Invalid resposne'
@@ -58,8 +58,8 @@ class JobView(APIView):
operation_description="Update job status",
manual_parameters=[input_job_id],
responses={
- status.HTTP_202_ACCEPTED: PostJobResponseResultSerializer(),
- status.HTTP_500_INTERNAL_SERVER_ERROR: PostJobResponseResultSerializer()
+ status.HTTP_202_ACCEPTED: GetJobResponseResultSerializer(),
+ status.HTTP_500_INTERNAL_SERVER_ERROR: GetJobResponseResultSerializer()
}
)
def post(self, request, job_id):
@@ -84,7 +84,7 @@ class JobView(APIView):
JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
response = Response(data={'result': 'ok'}, status=status.HTTP_202_ACCEPTED)
- response_serializer = PostJobResponseResultSerializer(data=response.data)
+ response_serializer = GetJobResponseResultSerializer(data=response.data)
isValid = response_serializer.is_valid()
if not isValid:
message = 'Invalid resposne'
diff --git a/catalog/packages/views.py b/catalog/packages/views.py
index a2861f06..7fcb8549 100644
--- a/catalog/packages/views.py
+++ b/catalog/packages/views.py
@@ -28,7 +28,7 @@ from drf_yasg.utils import no_body, swagger_auto_schema
from catalog.serializers import NsPackagesSerializer
from catalog.serializers import NfPackageSerializer
from catalog.serializers import NfPackageDistributeRequestSerializer
-from catalog.serializers import PostJobResponseResultSerializer
+from catalog.serializers import GetJobResponseResultSerializer
logger = logging.getLogger(__name__)
@@ -62,7 +62,7 @@ logger = logging.getLogger(__name__)
@api_view(http_method_names=['POST', 'GET'])
def nspackages_rc(request, *args, **kwargs):
logger.debug("Enter %s, method is %s", fun_name(), request.method)
- ret, normal_status, validation_response = None, None, None
+ ret, normal_status, validation_error = None, None, None
if request.method == 'GET':
# Gets ns package list
@@ -71,7 +71,7 @@ def nspackages_rc(request, *args, **kwargs):
responseSerializer = NsPackagesSerializer(data=ret[1])
if not responseSerializer.is_valid():
- validation_response = handleValidatonError(
+ validation_error = handleValidatonError(
responseSerializer, False)
elif request.method == 'POST':
# Distributes the package accroding to the given csarId
@@ -80,8 +80,8 @@ def nspackages_rc(request, *args, **kwargs):
ret = ns_package.ns_on_distribute(csar_id)
normal_status = status.HTTP_202_ACCEPTED
- if validation_response:
- return validation_response
+ if validation_error:
+ return validation_error
logger.debug("Leave %s, Return value is %s", fun_name(), ret)
if ret[0] != 0:
@@ -97,7 +97,7 @@ def nspackages_rc(request, *args, **kwargs):
operation_description="On distribute Nf package",
request_body=NfPackageDistributeRequestSerializer(),
responses={
- status.HTTP_202_ACCEPTED: PostJobResponseResultSerializer,
+ status.HTTP_202_ACCEPTED: GetJobResponseResultSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
'error message',
openapi.Schema(
@@ -120,20 +120,25 @@ def nfpackages_rc(request, *args, **kwargs):
fun_name(),
request.data,
request.method)
- ret, normal_status = None, None
+ ret, normal_status, validation_error = None, None, None
if request.method == 'GET':
ret = nf_package.nf_get_csars()
normal_status = status.HTTP_200_OK
elif request.method == 'POST':
- csar_id = ignore_case_get(request.data, "csarId")
- vim_ids = ignore_case_get(request.data, "vimIds")
- lab_vim_id = ignore_case_get(request.data, "labVimId")
+ request_serivalizer = NfPackageDistributeRequestSerializer(data=request.data)
+ if not request_serivalizer.is_valid():
+ validation_error = handleValidatonError(request_serivalizer, True)
+ return validation_error
+ csar_id = ignore_case_get(request_serivalizer.data, "csarId")
+ vim_ids = ignore_case_get(request_serivalizer.data, "vimIds")
+ lab_vim_id = ignore_case_get(request_serivalizer.data, "labVimId")
job_id = str(uuid.uuid4())
nf_package.NfDistributeThread(
csar_id, vim_ids, lab_vim_id, job_id).start()
ret = [0, {"jobId": job_id}]
normal_status = status.HTTP_202_ACCEPTED
logger.debug("Leave %s, Return value is %s", fun_name(), ret)
+
if ret[0] != 0:
return Response(
data={
diff --git a/catalog/serializers.py b/catalog/serializers.py
index c4321e2a..d5d30303 100644
--- a/catalog/serializers.py
+++ b/catalog/serializers.py
@@ -44,7 +44,7 @@ class JobResponseDescriptorSerializer(serializers.Serializer):
many=True, help_text="Response History List", required=False)
-class JobResponseSerializer(serializers.Serializer):
+class PostResponseSerializer(serializers.Serializer):
jobId = serializers.CharField(
help_text="Job Id",
required=False)
@@ -52,7 +52,7 @@ class JobResponseSerializer(serializers.Serializer):
help_text="Job Response Descriptor", required=False)
-class PostJobResponseResultSerializer(serializers.Serializer):
+class GetJobResponseResultSerializer(serializers.Serializer):
result = serializers.CharField(help_text="Result", required=True)
msg = serializers.CharField(help_text="Message", required=False)
@@ -85,13 +85,15 @@ class NsPackagesSerializer(serializers.ListSerializer):
class NfPackageDistributeRequestSerializer(serializers.Serializer):
- csar_id = serializers.CharField(help_text="CSAR ID", required=True)
+ csarId = serializers.CharField(help_text="CSAR ID", required=True)
vimIds = serializers.ListField(
- help_text="vim_ids",
+ help_text="A string for vimIds",
child=serializers.CharField(),
required=False)
labVimId = serializers.CharField(
- help_text="A list of VIM IDs.", required=False)
+ help_text="A list of VIM IDs.",
+ allow_blank=True,
+ required=False)
class NfPackageInfoSerializer(serializers.Serializer):