Skip to content

Commit

Permalink
Update CLI to support Scan job. Closes #700 (#803)
Browse files Browse the repository at this point in the history
* Updated man page
-Added scan job & scan job tests

* update start with code from utils
  • Loading branch information
abaiken authored and kholdaway committed Feb 22, 2018
1 parent 4e8e522 commit 28b4144
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 41 deletions.
12 changes: 2 additions & 10 deletions docs/source/man.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,28 +358,20 @@ The ``qpc scan list`` command returns the summary details for all created scan o

The ``qpc scan job`` command returns the the list of scan jobs or a single scan job associated with a scan object. The output of this command includes the scan job identifiers as well as the status of each job and the results.

**qpc scan job** --name** *scan_name* (**--id=** *scan_job_identifier*| --all) **--status=** *(created | pending | running | paused | canceled | completed | failed)* **[--results]**
**qpc scan job** --name** *scan_name* **--id=** *scan_job_identifier* **--status=** *(created | pending | running | paused | canceled | completed | failed)*

``--name=name``

Required. Contains the name of the scan object of which to display the scan jobs.

``--id=scan_job_identifier``

Optional. Contains the identifier of the scan job to show. Mutually exclusive with the ``--all`` option.

``--all``

Optional. Lists all scan jobs related to the provided scan object. Mutually exclusive with the ``--id`` option
Optional. Contains the identifier of a specified scan job to show.

``--status=status``

Optional. Filters the results by scan job state. This value must be ``created``, ``pending``, ``running``, ``paused``, ``canceled``, ``completed``, or ``failed``.

``--results``

Optional. Displays the results of the scan job instead of the status. The results are the raw output of the scan before that output is consolidated into a report. Because the results can include many lines of data, you might want to redirect the output of this command to a file if you use the ``--results`` option.

The ``qpc scan show`` command is the same as the ``qpc scan list`` command, except that it returns summary details for a single specified scan object.

**qpc scan show --name** *name*
Expand Down
6 changes: 4 additions & 2 deletions qpc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
ScanListCommand, ScanShowCommand,
ScanPauseCommand, ScanCancelCommand,
ScanRestartCommand, ScanEditCommand,
ScanClearCommand, ScanStatusCommand)
ScanClearCommand, ScanStatusCommand,
ScanJobCommand)
from qpc.report.commands import (ReportSummaryCommand,
ReportDetailCommand)
from qpc.translation import _
Expand Down Expand Up @@ -88,7 +89,8 @@ def __init__(self, name='cli', usage=None, shortdesc=None,
ScanListCommand, ScanShowCommand,
ScanPauseCommand, ScanCancelCommand,
ScanRestartCommand, ScanEditCommand,
ScanClearCommand, ScanStatusCommand])
ScanClearCommand, ScanStatusCommand,
ScanJobCommand])
self._add_subcommand(report.SUBCOMMAND,
[ReportSummaryCommand, ReportDetailCommand])
ensure_data_dir_exists()
Expand Down
1 change: 1 addition & 0 deletions qpc/scan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
EDIT = 'edit'
START = 'start'
LIST = 'list'
JOB = 'job'
SHOW = 'show'
STATUS = 'status'
PAUSE = 'pause'
Expand Down
1 change: 1 addition & 0 deletions qpc/scan/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
from qpc.scan.restart import ScanRestartCommand
from qpc.scan.clear import ScanClearCommand
from qpc.scan.status import ScanStatusCommand
from qpc.scan.job import ScanJobCommand
110 changes: 110 additions & 0 deletions qpc/scan/job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env python
#
# Copyright (c) 2018 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 3 (GPLv3). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3
# along with this software; if not, see
# https://www.gnu.org/licenses/gpl-3.0.txt.
#
"""ScanListCommand is used to list system scans."""

from __future__ import print_function
import sys
import urllib.parse as urlparse
from requests import codes
from qpc.utils import pretty_print
from qpc.clicommand import CliCommand
import qpc.scan as scan
from qpc.scan.utils import _get_scan_object_id
from qpc.request import GET
from qpc.translation import _
import qpc.messages as messages


# pylint: disable=too-few-public-methods
class ScanJobCommand(CliCommand):
"""Defines the job command.
This command is for listing the existing scan jobs for each scan.
"""

SUBCOMMAND = scan.SUBCOMMAND
ACTION = scan.JOB

def __init__(self, subparsers):
"""Create command."""
# pylint: disable=no-member
CliCommand.__init__(self, self.SUBCOMMAND, self.ACTION,
subparsers.add_parser(self.ACTION), GET,
scan.SCAN_URI, [codes.ok])
self.parser.add_argument('--name', dest='name', metavar='NAME',
help=_(messages.SCAN_NAME_HELP),
required=True)
self.parser.add_argument('--id', dest='id',
metavar='ID',
help=_(messages.SCAN_JOB_ID_HELP),
required=False)
self.parser.add_argument('--status', dest='status',
choices=[scan.SCAN_STATUS_CREATED,
scan.SCAN_STATUS_PENDING,
scan.SCAN_STATUS_RUNNING,
scan.SCAN_STATUS_PAUSED,
scan.SCAN_STATUS_CANCELED,
scan.SCAN_STATUS_COMPLETED,
scan.SCAN_STATUS_FAILED],
metavar='STATUS',
help=_(messages.SCAN_STATUS_FILTER_HELP),
required=False)

def _build_req_params(self):
"""Add filter by scan_type/state query param."""
found, scan_object_id = _get_scan_object_id(self.parser,
self.args.name)
if not found:
sys.exit(1)
if 'id' in self.args and self.args.id:
self.req_path = scan.SCAN_JOB_URI + self.args.id + '/'
else:
self.req_path += scan_object_id + 'jobs/'
if 'status' in self.args and self.args.status:
self.req_params = {'status': self.args.status}

def _handle_response_success(self):
# pylint: disable=no-member
if self.response.status_code in [codes.ok]:
json_data = self.response.json()
count = json_data.get('count', 0)
results = json_data.get('results', [])
if count == 0:
# if GET is used for single scan job,
# count doesn't exist and will be 0
if 'id' in self.args and self.args.id:
data = pretty_print(json_data)
print(data)
elif 'status' in self.args \
and self.args.status \
and (results != []):
data = pretty_print(json_data)
print(data)
else:
print(_(messages.SCAN_LIST_NO_SCANS))
sys.exit(1)
else:
data = pretty_print(results)
print(data)
if json_data.get('next'):
next_link = json_data.get('next')
params = urlparse.parse_qs(urlparse.urlparse(next_link).query)
page = params.get('page', ['1'])[0]
if self.req_params:
self.req_params['page'] = page
else:
self.req_params = {'page': page}
input(_(messages.NEXT_RESULTS))
self._do_command()
else:
print(_(messages.SCAN_LIST_NO_SCANS))
sys.exit(1)
33 changes: 4 additions & 29 deletions qpc/scan/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
from __future__ import print_function
import sys
from requests import codes
from qpc.request import POST, GET, request
from qpc.request import POST
from qpc.clicommand import CliCommand
import qpc.scan as scan
from qpc.scan.utils import _get_scan_object_id
from qpc.translation import _
import qpc.messages as messages

Expand All @@ -42,38 +43,12 @@ def __init__(self, subparsers):
help=_(messages.SCAN_NAME_HELP),
required=True)

def _get_scan_id(self):
"""Grab the scan id from the scan object if it exists.
:returns Boolean on whether or not the scan object was found &
the scan object id for the path
"""
found = False
scan_object_id = None
response = request(parser=self.parser, method=GET,
path=scan.SCAN_URI,
params={'name': self.args.name},
payload=None)
if response.status_code == codes.ok: # pylint: disable=no-member
json_data = response.json()
count = json_data.get('count', 0)
results = json_data.get('results', [])
if count >= 1:
for result in results:
if result['name'] == self.args.name:
scan_object_id = str(result['id']) + '/'
found = True
if not found or count == 0:
print(_(messages.SCAN_DOES_NOT_EXIST % self.args.name))
else:
print(_(messages.SCAN_DOES_NOT_EXIST % self.args.name))
return found, scan_object_id

def _validate_args(self):
CliCommand._validate_args(self)
if self.args.name:
# check for existence of scan object
found, scan_object_id = self._get_scan_id()
found, scan_object_id = _get_scan_object_id(self.parser,
self.args.name)
if found is False:
sys.exit(1)
else:
Expand Down
Loading

0 comments on commit 28b4144

Please sign in to comment.