Skip to content

Commit

Permalink
Add most recent connection task data to a source. Closes #610.
Browse files Browse the repository at this point in the history
  • Loading branch information
chambridge committed Feb 16, 2018
1 parent 709a069 commit 8f3d04e
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 18 deletions.
49 changes: 49 additions & 0 deletions docs/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,41 @@ definitions:
disable_ssl:
type: "boolean"
description: "If true disable SSL communication."
ScanDetails:
type: "object"
properties:
id:
type: "integer"
format: "int64"
start_time:
type: "string"
format: "date-time"
end_time:
type: "string"
format: "date-time"
status:
type: "string"
description: "The status of the scan job."
enum:
- created
- pending
- running
- paused
- canceled
- failed
- completed
systems_count:
type: "integer"
format: "int64"
description: "The total number of systems to be scanned in the source."
systems_scanned:
type: "integer"
format: "int64"
description: "The total number of systems that have been scanned in the source."
systems_failed:
type: "integer"
format: "int64"
description: "The total number of systems that failed during the scan of the source."
Source:
type: "object"
required:
Expand Down Expand Up @@ -1043,6 +1078,8 @@ definitions:
description: "The credentials to try with these hosts."
options:
$ref: "#/definitions/SourceOptions"
connection:
$ref: "#/definitions/ScanDetails"
SourcePagination:
allOf:
- $ref: "#/definitions/ListPagination"
Expand Down Expand Up @@ -1079,6 +1116,12 @@ definitions:
- canceled
- failed
- completed
start_time:
type: "string"
format: "date-time"
end_time:
type: "string"
format: "date-time"
systems_count:
type: "integer"
format: "int64"
Expand Down Expand Up @@ -1168,6 +1211,12 @@ definitions:
items:
$ref: "#/definitions/ScanTask"
description: "The scan tasks being processed."
start_time:
type: "string"
format: "date-time"
end_time:
type: "string"
format: "date-time"
systems_count:
type: "integer"
format: "int64"
Expand Down
49 changes: 48 additions & 1 deletion quipucords/api/source/tests_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
"""Test the API application."""

import json
from datetime import datetime
from unittest.mock import patch
from django.test import TestCase
from django.core.urlresolvers import reverse
from rest_framework import status
from api.models import Credential, Source
from api.models import (Credential, Source, ScanTask, ScanJob)
import api.messages as messages
from api.source.view import format_source
from api.serializers import SourceSerializer


def dummy_start():
Expand Down Expand Up @@ -118,6 +121,50 @@ def create_expect_400_with_query(self,
response_json = response.json()
self.assertEqual(response_json, expected_response)

#################################################
# Function Tests
#################################################
def test_format_source(self):
"""Test the format source method."""
start = datetime.now()
source = Source(
name='source1',
source_type='network',
port=22)
source.save()
end = datetime.now()
scan_task = ScanTask(source=source,
scan_type=ScanTask.SCAN_TYPE_CONNECT,
status=ScanTask.COMPLETED,
systems_count=10,
systems_scanned=9,
systems_failed=1,
start_time=start,
end_time=end)
scan_task.save()
scan_job = ScanJob(scan_type=ScanTask.SCAN_TYPE_CONNECT,
status=ScanTask.COMPLETED,
start_time=start,
end_time=end)
scan_job.save()
scan_job.sources.add(source)
scan_job.tasks.add(scan_task)
scan_job.save()

serializer = SourceSerializer(source)
json_source = serializer.data
out = format_source(json_source)
expected = {'id': 1,
'name': 'source1',
'source_type': 'network',
'port': 22,
'connection': {'id': 1, 'start_time': start,
'end_time': end, 'status': 'completed',
'systems_count': 10,
'systems_scanned': 9,
'systems_failed': 1}}
self.assertEqual(out, expected)

#################################################
# Network Tests
#################################################
Expand Down
56 changes: 39 additions & 17 deletions quipucords/api/source/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,44 @@ def expand_credential(json_source):
Take source object with credential id and pull object from db.
create slim dictionary version of the host credential with name an value
to return to user.
:param json_source: JSON source data from serializer
:returns: JSON data
"""
cred_ids = json_source.get('credentials', [])
slim_cred = Credential.objects.filter(pk__in=cred_ids).values('id', 'name')
slim_cred = Credential.objects.filter(
pk__in=cred_ids).values('id', 'name')
# Update source JSON with cred JSON
if slim_cred:
json_source[CREDENTIALS_KEY] = slim_cred
return json_source


def format_source(json_source):
"""Format source with credentials and most recent connection scan.
:param json_source: JSON source data from serializer
:returns: JSON data
"""
json_source = expand_credential(json_source)
source_id = json_source.get('id')

scan_task_qs = ScanTask.objects.filter(
source__id=source_id,
scan_type=ScanTask.SCAN_TYPE_CONNECT)
if scan_task_qs:
scan_task = scan_task_qs.latest('start_time')
scan_job = ScanJob.objects.filter(
tasks__id=scan_task.id).latest('start_time')
json_scan_job = {'id': scan_job.id,
'start_time': scan_job.start_time,
'end_time': scan_job.end_time,
'status': scan_job.status,
'systems_count': scan_task.systems_count,
'systems_scanned': scan_task.systems_scanned,
'systems_failed': scan_task.systems_failed}
json_source['connection'] = json_scan_job
return json_source


class SourceFilter(FilterSet):
Expand Down Expand Up @@ -87,7 +119,7 @@ def list(self, request): # pylint: disable=unused-argument
serializer = self.get_serializer(page, many=True)
for source in serializer.data:
# Create expanded host cred JSON
expand_credential(source)
format_source(source)
result.append(source)
return self.get_paginated_response(serializer.data)

Expand All @@ -96,7 +128,7 @@ def list(self, request): # pylint: disable=unused-argument
json_source = serializer.data

# Create expanded host cred JSON
expand_credential(json_source)
format_source(json_source)

result.append(json_source)
return Response(result)
Expand All @@ -108,17 +140,7 @@ def create(self, request, *args, **kwargs):

# Modify json for response
json_source = response.data
source_id = json_source.get('id')
if not source_id or (source_id and not isinstance(source_id, int)):
error = {
'id': [_(messages.COMMON_ID_INV)]
}
raise ValidationError(error)

get_object_or_404(self.queryset, pk=source_id)

# Create expanded host cred JSON
expand_credential(json_source)
format_source(json_source)

# check to see if a connection scan was requested
# through query parameter
Expand Down Expand Up @@ -161,7 +183,7 @@ def retrieve(self, request, pk=None): # pylint: disable=unused-argument
json_source = serializer.data

# Create expanded host cred JSON
expand_credential(json_source)
format_source(json_source)

return Response(json_source)

Expand All @@ -170,7 +192,7 @@ def update(self, request, *args, **kwargs):
"""Update a source."""
# Note: This method's implementation is basically a straight copy of
# rest_framework.mixins.UpdateModelMixin but modified to include the
# call to expand_credential. We should probably refactor things here
# call to format_source. We should probably refactor things here
# to reduce duplication of code.
source = self.get_object()
serializer = self.get_serializer(source, data=request.data,
Expand All @@ -180,6 +202,6 @@ def update(self, request, *args, **kwargs):
json_source = serializer.data

# Create expanded host cred JSON
expand_credential(json_source)
format_source(json_source)

return Response(json_source)

0 comments on commit 8f3d04e

Please sign in to comment.