def retrieve(self, request, pk=None): """Get a scan job.""" scan = get_object_or_404(self.queryset, pk=pk) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(scan, json_scan) return Response(json_scan)
def _create_async_merge_report_job(details_report_data): """Retrieve merge report job status. :param details_report_data: Details report data to fingerprint :returns: Response for http request """ has_errors, validation_result = validate_details_report_json( details_report_data) if has_errors: return Response(validation_result, status=status.HTTP_400_BAD_REQUEST) # Create FC model and save data details_report = create_details_report(details_report_data) # Create new job to run merge_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_FINGERPRINT, details_report=details_report) merge_job.save() merge_job.log_current_status() job_serializer = ScanJobSerializer(merge_job) response_data = job_serializer.data # start fingerprint job start_scan.send(sender=__name__, instance=merge_job) return Response(response_data, status=status.HTTP_201_CREATED)
def list(self, request): # pylint: disable=unused-argument """List the collection of scan jobs.""" queryset = self.filter_queryset(self.get_queryset()) result = [] for scan in queryset: serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(scan, json_scan) result.append(json_scan) return Response(result)
def retrieve(self, request, pk=None): """Get a scan job.""" if not pk or (pk and not is_int(pk)): error = {'id': [_(messages.COMMON_ID_INV)]} raise ValidationError(error) scan = get_object_or_404(self.queryset, pk=pk) serializer = ScanJobSerializer(scan) json_scan = serializer.data json_scan = expand_scanjob(json_scan) return Response(json_scan)
def _get_async_merge_report_status(merge_job_id): """Retrieve merge report job status. :param merge_job_id: ScanJob id for this merge. :returns: Response for http request """ merge_job = get_object_or_404(ScanJob.objects.all(), pk=merge_job_id) if merge_job.scan_type != ScanTask.SCAN_TYPE_FINGERPRINT: return Response(status=status.HTTP_404_NOT_FOUND) job_serializer = ScanJobSerializer(merge_job) response_data = job_serializer.data return Response(response_data, status=status.HTTP_200_OK)
def cancel(self, request, pk=None): """Cancel the running scan.""" scan = get_object_or_404(self.queryset, pk=pk) if (scan.status == ScanTask.COMPLETED or scan.status == ScanTask.FAILED or scan.status == ScanTask.CANCELED): err_msg = _(messages.NO_CANCEL) return JsonResponse({'non_field_errors': [err_msg]}, status=400) scan.cancel() cancel_scan.send(sender=self.__class__, instance=scan) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(scan, json_scan) return Response(json_scan, status=200)
def pause(self, request, pk=None): """Pause the running scan.""" scan = get_object_or_404(self.queryset, pk=pk) if scan.status == ScanTask.RUNNING: scan.pause() pause_scan.send(sender=self.__class__, instance=scan) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(scan, json_scan) return Response(json_scan, status=200) elif scan.status == ScanTask.PAUSED: err_msg = _(messages.ALREADY_PAUSED) return JsonResponse({'non_field_errors': [err_msg]}, status=400) err_msg = _(messages.NO_PAUSE) return JsonResponse({'non_field_errors': [err_msg]}, status=400)
def jobs(request, pk=None): """Get the jobs of a scan.""" # pylint: disable=invalid-name if pk is not None: if not is_int(pk): return Response(status=status.HTTP_404_NOT_FOUND) result = [] scan = get_object_or_404(Scan.objects.all(), pk=pk) if request.method == 'GET': job_queryset = get_job_queryset_query_set(scan, request.query_params) paginator = StandardResultsSetPagination() page = paginator.paginate_queryset(job_queryset, request) if page is not None: serializer = ScanJobSerializer(page, many=True) for scan in serializer.data: json_scan = expand_scanjob(scan) result.append(json_scan) return paginator.get_paginated_response(serializer.data) for job in job_queryset: job_serializer = ScanJobSerializer(job) job_json = job_serializer.data job_json = expand_scanjob(job_serializer.data) result.append(job_json) return Response(result) else: job_data = {} job_data['scan'] = pk job_serializer = ScanJobSerializer(data=job_data) job_serializer.is_valid(raise_exception=True) job_serializer.save() scanjob_obj = ScanJob.objects.get(pk=job_serializer.data['id']) scanjob_obj.log_current_status() start_scan.send(sender=ScanViewSet.__class__, instance=scanjob_obj) return Response(job_serializer.data, status=status.HTTP_201_CREATED)
def cancel(self, request, pk=None): """Cancel the running scan.""" if not pk or (pk and not is_int(pk)): error = {'id': [_(messages.COMMON_ID_INV)]} raise ValidationError(error) scan = get_object_or_404(self.queryset, pk=pk) if (scan.status == ScanTask.COMPLETED or scan.status == ScanTask.FAILED or scan.status == ScanTask.CANCELED): err_msg = _(messages.NO_CANCEL) return JsonResponse({'non_field_errors': [err_msg]}, status=400) scan.cancel() cancel_scan.send(sender=self.__class__, instance=scan) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(json_scan) return Response(json_scan, status=200)
def list(self, request): # pylint: disable=unused-argument """List the collection of scan jobs.""" result = [] queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) for scan in serializer.data: expand_scanjob(scan) result.append(scan) return self.get_paginated_response(serializer.data) for scan in queryset: serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(json_scan) result.append(json_scan) return Response(result)
def restart(self, request, pk=None): """Restart a paused scan.""" scan = get_object_or_404(self.queryset, pk=pk) if scan.status == ScanTask.PAUSED: fact_endpoint = request.build_absolute_uri(reverse('facts-list')) scan.restart() restart_scan.send(sender=self.__class__, instance=scan, fact_endpoint=fact_endpoint) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(scan, json_scan) return Response(json_scan, status=200) elif scan.status == ScanTask.RUNNING: err_msg = _(messages.ALREADY_RUNNING) return JsonResponse({'non_field_errors': [err_msg]}, status=400) err_msg = _(messages.NO_RESTART) return JsonResponse({'non_field_errors': [err_msg]}, status=400)
def restart(self, request, pk=None): """Restart a paused scan.""" if not pk or (pk and not is_int(pk)): error = {'id': [_(messages.COMMON_ID_INV)]} raise ValidationError(error) scan = get_object_or_404(self.queryset, pk=pk) if scan.status == ScanTask.PAUSED: scan.restart() restart_scan.send(sender=self.__class__, instance=scan) serializer = ScanJobSerializer(scan) json_scan = serializer.data expand_scanjob(json_scan) return Response(json_scan, status=200) elif scan.status == ScanTask.RUNNING: err_msg = _(messages.ALREADY_RUNNING) return JsonResponse({'non_field_errors': [err_msg]}, status=400) err_msg = _(messages.NO_RESTART) return JsonResponse({'non_field_errors': [err_msg]}, status=400)
def pause(self, request, pk=None): """Pause the running scan.""" if not pk or (pk and not is_int(pk)): error = {'id': [_(messages.COMMON_ID_INV)]} raise ValidationError(error) scan = get_object_or_404(self.queryset, pk=pk) if scan.status == ScanTask.RUNNING: # Kill job before changing job state pause_scan.send(sender=self.__class__, instance=scan) scan.pause() serializer = ScanJobSerializer(scan) json_scan = serializer.data json_scan = expand_scanjob(json_scan) return Response(json_scan, status=200) elif scan.status == ScanTask.PAUSED: err_msg = _(messages.ALREADY_PAUSED) return JsonResponse({'non_field_errors': [err_msg]}, status=400) err_msg = _(messages.NO_PAUSE) return JsonResponse({'non_field_errors': [err_msg]}, status=400)
def test_expand_scanjob(self): """Test view expand_scanjob.""" scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_INSPECT) scan_job.save() scan_job.sources.add(self.source) # Job in created state self.assertEqual(scan_job.status, ScanTask.CREATED) tasks = scan_job.tasks.all() self.assertEqual(len(tasks), 0) # Queue job to run scan_job.queue() task = scan_job.tasks.all()[1] task.update_stats('TEST_VC.', sys_count=2, sys_failed=1, sys_scanned=1) scan_job = ScanJob.objects.filter(pk=scan_job.id).first() serializer = ScanJobSerializer(scan_job) json_scan = serializer.data expand_scanjob(json_scan) self.assertEqual(json_scan.get('systems_count'), 2) self.assertEqual(json_scan.get('systems_failed'), 1) self.assertEqual(json_scan.get('systems_scanned'), 1)