diff options
-rw-r--r-- | catalog/jobs/__init__.py | 13 | ||||
-rw-r--r-- | catalog/jobs/job_get.py | 46 | ||||
-rw-r--r-- | catalog/jobs/tests/__init__.py | 13 | ||||
-rw-r--r-- | catalog/jobs/tests/tests.py | 40 | ||||
-rw-r--r-- | catalog/jobs/urls.py | 23 | ||||
-rw-r--r-- | catalog/jobs/views.py | 45 | ||||
-rw-r--r-- | catalog/urls.py | 1 |
7 files changed, 181 insertions, 0 deletions
diff --git a/catalog/jobs/__init__.py b/catalog/jobs/__init__.py new file mode 100644 index 00000000..c7b6818e --- /dev/null +++ b/catalog/jobs/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2017 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. diff --git a/catalog/jobs/job_get.py b/catalog/jobs/job_get.py new file mode 100644 index 00000000..32ee2431 --- /dev/null +++ b/catalog/jobs/job_get.py @@ -0,0 +1,46 @@ +# Copyright 2017 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. +import logging + +from catalog.pub.utils.jobutil import JobUtil + +logger = logging.getLogger(__name__) + + +class GetJobInfoService(object): + def __init__(self, job_id, response_id=0): + self.job_id = job_id + self.response_id = response_id if response_id else 0 + + def do_biz(self): + 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 diff --git a/catalog/jobs/tests/__init__.py b/catalog/jobs/tests/__init__.py new file mode 100644 index 00000000..c7b6818e --- /dev/null +++ b/catalog/jobs/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2017 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. diff --git a/catalog/jobs/tests/tests.py b/catalog/jobs/tests/tests.py new file mode 100644 index 00000000..37996a05 --- /dev/null +++ b/catalog/jobs/tests/tests.py @@ -0,0 +1,40 @@ +# Copyright 2017 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. +from django.test import TestCase, Client +from rest_framework import status + +from catalog.pub.database.models import JobModel, JobStatusModel + + +class JobsViewTest(TestCase): + def setUp(self): + self.job_id = 'test_job_id' + self.client = Client() + + def tearDown(self): + JobModel.objects.all().delete() + + def test_job_normal(self): + JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save() + JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst').save() + response = self.client.get("/api/catalog/v1/jobs/%s" % self.job_id) + self.failUnlessEqual(status.HTTP_200_OK, response.status_code) + + def test_job_when_jobid_not_exist(self): + job_id = 'test_new_job_id' + JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save() + JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst').save() + response = self.client.get("/api/catalog/v1/jobs/%s" % job_id) + self.assertIn('jobId', response.data) + self.assertNotIn('responseDescriptor', response.data) diff --git a/catalog/jobs/urls.py b/catalog/jobs/urls.py new file mode 100644 index 00000000..d2e000fd --- /dev/null +++ b/catalog/jobs/urls.py @@ -0,0 +1,23 @@ +# Copyright 2017 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. +from django.conf.urls import patterns, url +from rest_framework.urlpatterns import format_suffix_patterns + +from catalog.jobs.views import JobView + +urlpatterns = patterns('', + url(r'^api/catalog/v1/jobs/(?P<job_id>[0-9a-zA-Z_-]+)$', JobView.as_view()), + ) + +urlpatterns = format_suffix_patterns(urlpatterns) diff --git a/catalog/jobs/views.py b/catalog/jobs/views.py new file mode 100644 index 00000000..d8825429 --- /dev/null +++ b/catalog/jobs/views.py @@ -0,0 +1,45 @@ +# Copyright 2017 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. +import logging + +from rest_framework.response import Response +from rest_framework.views import APIView + +from catalog.jobs.job_get import GetJobInfoService +from catalog.pub.utils.jobutil import JobUtil +from catalog.pub.utils.values import ignore_case_get + +logger = logging.getLogger(__name__) + + +class JobView(APIView): + def get(self, request, job_id): + response_id = ignore_case_get(request.META, 'responseId') + ret = GetJobInfoService(job_id, response_id).do_biz() + return Response(data=ret) + + def post(self, request, job_id): + try: + logger.debug("Enter JobView:post, %s, %s ", job_id, request.data) + jobs = JobUtil.query_job_status(job_id) + if len(jobs) > 0 and jobs[-1].errcode == '255': + return Response(data={'result': 'ok'}) + progress = request.data.get('progress') + desc = request.data.get('desc', '%s' % progress) + errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255' + logger.debug("errcode=%s", errcode) + JobUtil.add_job_status(job_id, progress, desc, error_code=errcode) + return Response(data={'result': 'ok'}) + except Exception as e: + return Response(data={'result': 'error', 'msg': e.message}) diff --git a/catalog/urls.py b/catalog/urls.py index 9d4d8f26..fbdf7e4b 100644 --- a/catalog/urls.py +++ b/catalog/urls.py @@ -18,6 +18,7 @@ from catalog.pub.config.config import REG_TO_MSB_WHEN_START, REG_TO_MSB_REG_URL, urlpatterns = [ url(r'^', include('catalog.samples.urls')), url(r'^', include('catalog.packages.urls')), + url(r'^', include('catalog.jobs.urls')), url(r'^', include('catalog.swagger.urls')), ] |