diff options
author | maopengzhang <zhang.maopeng1@zte.com.cn> | 2019-04-28 17:27:54 +0800 |
---|---|---|
committer | maopengzhang <zhang.maopeng1@zte.com.cn> | 2019-04-28 17:27:54 +0800 |
commit | 42abd8c99e0c0bcda1ca9d66a467b70c55ce3f6d (patch) | |
tree | cf6b0fe932c226199d2ac265c5b800bf27abb43e | |
parent | 752bf0132e7db519597e32525b33db4fd56a7643 (diff) |
Update job api models
Update job api models
Change-Id: Ic24f4fc9670518a2ef157dd0dc9fa6c64e323e3e
Issue-ID: VFC-1241
Signed-off-by: maopengzhang <zhang.maopeng1@zte.com.cn>
-rw-r--r-- | lcm/jobs/api_model.py | 60 | ||||
-rw-r--r-- | lcm/jobs/job_get.py | 45 | ||||
-rw-r--r-- | lcm/jobs/serializers.py | 76 | ||||
-rw-r--r-- | lcm/jobs/views.py | 40 | ||||
-rw-r--r-- | lcm/ns/tests/test_ns_heal.py | 1 | ||||
-rw-r--r-- | lcm/pub/utils/values.py | 9 |
6 files changed, 174 insertions, 57 deletions
diff --git a/lcm/jobs/api_model.py b/lcm/jobs/api_model.py new file mode 100644 index 00000000..87266fa4 --- /dev/null +++ b/lcm/jobs/api_model.py @@ -0,0 +1,60 @@ +# Copyright 2019 ZTE Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + + +class JobHistory(object): + def __init__(self, status="", progress="", statusDescription="", errorCode="", responseId=""): + self.status = status + self.progress = progress + self.statusDescription = statusDescription + self.errorCode = errorCode + self.responseId = responseId + + +class JobDescriptor(object): + def __init__(self, status="", progress=0, statusDescription="", errorCode="", responseId="", responseHistoryList=None): + self.status = status + self.progress = progress + self.statusDescription = statusDescription + self.errorCode = errorCode + self.responseId = responseId + self.responseHistoryList = responseHistoryList + + +class JobQueryResp(object): + def __init__(self, jobId="", responseDescriptor=None): + self.jobId = jobId + self.responseDescriptor = responseDescriptor + + +class JobUpdReq(object): + def __init__(self, progress="", desc="", errcode=""): + self.progress = progress + self.desc = desc + self.errcode = errcode + + def load(self, data): + self.progress = data["progress"] + self.desc = data["desc"] + self.errcode = data["errcode"] + + +class JobUpdResp(object): + def __init__(self, result="", msg=""): + self.result = result + self.msg = msg + + def load(self, data): + self.result = data["result"] + self.msg = data["msg"] diff --git a/lcm/jobs/job_get.py b/lcm/jobs/job_get.py index 4619f62d..48edce9c 100644 --- a/lcm/jobs/job_get.py +++ b/lcm/jobs/job_get.py @@ -14,6 +14,10 @@ import logging from lcm.pub.utils.jobutil import JobUtil +from lcm.pub.utils.values import remove_none_key +from lcm.jobs.api_model import JobQueryResp, JobDescriptor, JobHistory +from lcm.jobs.serializers import JobQueryRespSerializer + logger = logging.getLogger(__name__) @@ -27,20 +31,27 @@ class GetJobInfoService(object): logger.debug("[getjob]job_id=%s, response_id=%s", self.job_id, self.response_id) jobs = JobUtil.query_job_status(self.job_id, self.response_id) if not jobs: - return {"jobId": self.job_id} - ret = { - "jobId": self.job_id, - "responseDescriptor": { - "status": jobs[0].status, - "progress": jobs[0].progress, - "statusDescription": jobs[0].descp, - "errorCode": jobs[0].errcode, - "responseId": jobs[0].indexid, - "responseHistoryList": [ - { - "status": job.status, - "progress": job.progress, - "statusDescription": job.descp, - "errorCode": job.errcode, - "responseId": job.indexid} for job in jobs[1:]]}} - return ret + job_query_resp = JobQueryResp(self.job_id) + job_query_resp_serializer = JobQueryRespSerializer(job_query_resp) + return remove_none_key(job_query_resp_serializer.data) + job_query_resp = JobQueryResp( + self.job_id, + JobDescriptor( + jobs[0].status, + jobs[0].progress, + jobs[0].descp, + jobs[0].errcode, + jobs[0].indexid, + [ + JobHistory( + job.status, + job.progress, + job.descp, + job.errcode, + job.indexid + ) for job in jobs[1:] + ] + ) + ) + job_query_resp_serializer = JobQueryRespSerializer(job_query_resp) + return remove_none_key(job_query_resp_serializer.data) diff --git a/lcm/jobs/serializers.py b/lcm/jobs/serializers.py index 63e55b30..55500fb9 100644 --- a/lcm/jobs/serializers.py +++ b/lcm/jobs/serializers.py @@ -16,34 +16,74 @@ from rest_framework import serializers class JobHistorySerializer(serializers.Serializer): - status = serializers.CharField(help_text="Status of job", required=True) - progress = serializers.CharField(help_text="Progress of job", required=True) - statusDescription = serializers.CharField(help_text="Description of job", required=False, allow_null=True) - errorCode = serializers.CharField(help_text="Error code of job", required=False, allow_null=True) - responseId = serializers.CharField(help_text="Response index of job", required=True) + status = serializers.CharField( + help_text="Status of job", + required=True) + progress = serializers.CharField( + help_text="Progress of job", + required=True) + statusDescription = serializers.CharField( + help_text="Description of job", + required=False, + allow_null=True) + errorCode = serializers.CharField( + help_text="Error code of job", + required=False, + allow_null=True) + responseId = serializers.CharField( + help_text="Response index of job", + required=True) class JobDescriptorSerializer(serializers.Serializer): - status = serializers.CharField(help_text="Status of job", required=True) + status = serializers.CharField( + help_text="Status of job", + required=True) # progress = serializers.CharField(help_text="Progress of job", required=True) - progress = serializers.IntegerField(help_text="Progress of job", required=True) - statusDescription = serializers.CharField(help_text="Description of job", required=False, allow_null=True) - errorCode = serializers.CharField(help_text="Error code of job", required=False, allow_null=True) - responseId = serializers.CharField(help_text="Response index of job", required=True) - responseHistoryList = JobHistorySerializer(help_text="History of job", many=True) + progress = serializers.IntegerField( + help_text="Progress of job", + required=True) + statusDescription = serializers.CharField( + help_text="Description of job", + required=False, + allow_null=True) + errorCode = serializers.CharField( + help_text="Error code of job", + required=False, + allow_null=True) + responseId = serializers.CharField( + help_text="Response index of job", + required=True) + responseHistoryList = JobHistorySerializer( + help_text="History of job", + many=True) class JobQueryRespSerializer(serializers.Serializer): - jobId = serializers.CharField(help_text="UUID of job", required=True) - responseDescriptor = JobDescriptorSerializer(help_text="Descriptor of job", required=False) + jobId = serializers.CharField( + help_text="UUID of job", + required=True) + responseDescriptor = JobDescriptorSerializer( + help_text="Descriptor of job", + required=False) class JobUpdReqSerializer(serializers.Serializer): - progress = serializers.CharField(help_text="Progress of job", required=True) - desc = serializers.CharField(help_text="Desc of job", required=False) - errcode = serializers.CharField(help_text="Error code of job", required=False) + progress = serializers.CharField( + help_text="Progress of job", + required=True) + desc = serializers.CharField( + help_text="Desc of job", + required=False) + errcode = serializers.CharField( + help_text="Error code of job", + required=False) class JobUpdRespSerializer(serializers.Serializer): - result = serializers.CharField(help_text="Result of job update", required=True) - msg = serializers.CharField(help_text="Detail of job update", required=False) + result = serializers.CharField( + help_text="Result of job update", + required=True) + msg = serializers.CharField( + help_text="Detail of job update", + required=False) diff --git a/lcm/jobs/views.py b/lcm/jobs/views.py index f60189ef..dddb853c 100644 --- a/lcm/jobs/views.py +++ b/lcm/jobs/views.py @@ -20,11 +20,13 @@ from rest_framework.views import APIView from rest_framework import status from drf_yasg.utils import swagger_auto_schema +from lcm.pub.enum import JOB_ERROR_CODE from lcm.jobs.job_get import GetJobInfoService from lcm.pub.utils.jobutil import JobUtil +from lcm.jobs.api_model import JobUpdReq, JobUpdResp from lcm.jobs.serializers import JobUpdReqSerializer, JobUpdRespSerializer from lcm.jobs.serializers import JobQueryRespSerializer -from lcm.pub.exceptions import NSLCMException +from lcm.pub.exceptions import BadRequestException, NSLCMException logger = logging.getLogger(__name__) @@ -76,31 +78,27 @@ class JobView(APIView): req_serializer = JobUpdReqSerializer(data=request.data) if not req_serializer.is_valid(): - raise NSLCMException(req_serializer.errors) + raise BadRequestException(req_serializer.errors) jobs = JobUtil.query_job_status(job_id) if not jobs: raise NSLCMException("Job(%s) does not exist.") - if jobs[-1].errcode != '255': - progress = request.data.get('progress') - desc = request.data.get('desc', '%s' % progress) - errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255' + if jobs[-1].errcode != JOB_ERROR_CODE.ERROR: + job_up_req = JobUpdReq() + job_up_req.load(request.data) + desc = job_up_req.desc + errcode = JOB_ERROR_CODE.NO_ERROR if job_up_req.errcode in ('true', 'active') else JOB_ERROR_CODE.ERROR logger.debug("errcode=%s", errcode) - JobUtil.add_job_status(job_id, progress, desc, error_code=errcode) - - resp_serializer = JobUpdRespSerializer(data={'result': 'ok'}) - if not resp_serializer.is_valid(): - raise NSLCMException(req_serializer.errors) - + JobUtil.add_job_status(job_id, job_up_req.progress, desc, error_code=errcode) + job_update_resp = JobUpdResp('ok') + resp_serializer = JobUpdRespSerializer(job_update_resp) return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED) + except NSLCMException as e: + job_update_resp = JobUpdResp('error', e.message) + resp_serializer = JobUpdRespSerializer(job_update_resp) + return Response(data=resp_serializer.data, status=status.HTTP_400_BAD_REQUEST) except Exception as e: - resp_serializer = JobUpdRespSerializer(data={ - 'result': 'error', - 'msg': e.message}) - if not resp_serializer.is_valid(): - logger.error(resp_serializer.errors) - return Response(data={ - 'result': 'error', - 'msg': resp_serializer.errors}, status=status.HTTP_202_ACCEPTED) - return Response(data=resp_serializer.data, status=status.HTTP_202_ACCEPTED) + job_update_resp = JobUpdResp('error', e.message) + resp_serializer = JobUpdRespSerializer(job_update_resp) + return Response(data=resp_serializer.data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/lcm/ns/tests/test_ns_heal.py b/lcm/ns/tests/test_ns_heal.py index 2f2f05c3..88b7ac78 100644 --- a/lcm/ns/tests/test_ns_heal.py +++ b/lcm/ns/tests/test_ns_heal.py @@ -117,7 +117,6 @@ class TestHealNsViews(TestCase): heal_ns_json = HEAL_NS_DICT.copy() heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_ns_json, format='json') - print response.data self.assertEqual(response.data["error"], "NS Not Found") self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) diff --git a/lcm/pub/utils/values.py b/lcm/pub/utils/values.py index fbb75f68..c2ce3c09 100644 --- a/lcm/pub/utils/values.py +++ b/lcm/pub/utils/values.py @@ -28,6 +28,15 @@ def ignore_case_get(args, key, def_val=""): return def_val +def remove_none_key(data, none_list=None): + none_list = none_list if none_list else [None, '', 'NULL', 'None', 'null', {}, '{}'] + if isinstance(data, dict): + data = dict([(k, remove_none_key(v)) for k, v in data.iteritems() if v not in none_list]) + if isinstance(data, list): + data = [remove_none_key(s) for s in data if s not in none_list] + return data + + def update_value(origin_data, new_data): logger.debug(origin_data) if not isinstance(origin_data, dict): |