Update job api models
[vfc/nfvo/lcm.git] / lcm / jobs / job_get.py
1 # Copyright 2016 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import logging
15
16 from lcm.pub.utils.jobutil import JobUtil
17 from lcm.pub.utils.values import remove_none_key
18 from lcm.jobs.api_model import JobQueryResp, JobDescriptor, JobHistory
19 from lcm.jobs.serializers import JobQueryRespSerializer
20
21
22 logger = logging.getLogger(__name__)
23
24
25 class GetJobInfoService(object):
26     def __init__(self, job_id, response_id=0):
27         self.job_id = job_id
28         self.response_id = response_id if response_id else 0
29
30     def do_biz(self):
31         logger.debug("[getjob]job_id=%s, response_id=%s", self.job_id, self.response_id)
32         jobs = JobUtil.query_job_status(self.job_id, self.response_id)
33         if not jobs:
34             job_query_resp = JobQueryResp(self.job_id)
35             job_query_resp_serializer = JobQueryRespSerializer(job_query_resp)
36             return remove_none_key(job_query_resp_serializer.data)
37         job_query_resp = JobQueryResp(
38             self.job_id,
39             JobDescriptor(
40                 jobs[0].status,
41                 jobs[0].progress,
42                 jobs[0].descp,
43                 jobs[0].errcode,
44                 jobs[0].indexid,
45                 [
46                     JobHistory(
47                         job.status,
48                         job.progress,
49                         job.descp,
50                         job.errcode,
51                         job.indexid
52                     ) for job in jobs[1:]
53                 ]
54             )
55         )
56         job_query_resp_serializer = JobQueryRespSerializer(job_query_resp)
57         return remove_none_key(job_query_resp_serializer.data)