Passed
Push — master ( 48952f...b1214c )
by Jaspar
34:32 queued 33:09
created

GmpV208Mixin.authenticate()   A

Complexity

Conditions 4

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nop 3
dl 0
loc 36
rs 9.6
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
# pylint: disable=arguments-differ, redefined-builtin, too-many-lines
20
21
"""
22
Module for communication with gvmd in
23
`Greenbone Management Protocol version 20.08`_
24
25
.. _Greenbone Management Protocol version 20.08:
26
    https://docs.greenbone.net/API/GMP/gmp-20.08.html
27
"""
28
import logging
29
from numbers import Integral
30
31
from typing import Any, Optional
32
33
from gvm.errors import InvalidArgument
34
from gvm.protocols.base import GvmProtocol
35
36
37
from gvm.utils import (
38
    to_bool,
39
)
40
from gvm.xml import XmlCommand
41
42
43
logger = logging.getLogger(__name__)
44
45
46
class GmpV208Mixin(GvmProtocol):
47
    def get_system_reports(
48
        self,
49
        *,
50
        name: Optional[str] = None,
51
        duration: Optional[int] = None,
52
        start_time: Optional[str] = None,
53
        end_time: Optional[str] = None,
54
        brief: Optional[bool] = None,
55
        slave_id: Optional[str] = None,
56
    ) -> Any:
57
        """Request a list of system reports
58
59
        Arguments:
60
            name: A string describing the required system report
61
            duration: The number of seconds into the past that the system report
62
                should include
63
            start_time: The start of the time interval the system report should
64
                include in ISO time format
65
            end_time: The end of the time interval the system report should
66
                include in ISO time format
67
            brief: Whether to include the actual system reports
68
            slave_id: UUID of GMP scanner from which to get the system reports
69
70
        Returns:
71
            The response. See :py:meth:`send_command` for details.
72
        """
73
        cmd = XmlCommand("get_system_reports")
74
75
        if name:
76
            cmd.set_attribute("name", name)
77
78
        if duration is not None:
79
            if not isinstance(duration, Integral):
80
                raise InvalidArgument("duration needs to be an integer number")
81
82
            cmd.set_attribute("duration", str(duration))
83
84
        if start_time:
85
            cmd.set_attribute("start_time", str(start_time))
86
87
        if end_time:
88
            cmd.set_attribute("end_time", str(end_time))
89
90
        if brief is not None:
91
            cmd.set_attribute("brief", to_bool(brief))
92
93
        if slave_id:
94
            cmd.set_attribute("slave_id", slave_id)
95
96
        return self._send_xml_command(cmd)
97
98
    def help(
99
        self, *, format: Optional[str] = None, help_type: Optional[str] = None
100
    ) -> Any:
101
        """Get the help text
102
103
        Arguments:
104
            format: One of "html", "rnc", "text" or "xml
105
            help_type: One of "brief" or "". Default ""
106
107
        Returns:
108
            The response. See :py:meth:`send_command` for details.
109
        """
110
        cmd = XmlCommand("help")
111
112
        if not help_type:
113
            help_type = ""
114
115
        if help_type not in ("", "brief"):
116
            raise InvalidArgument(
117
                'help_type argument must be an empty string or "brief"'
118
            )
119
120
        cmd.set_attribute("type", help_type)
121
122
        if format:
123
            if not format.lower() in ("html", "rnc", "text", "xml"):
124
                raise InvalidArgument(
125
                    "help format Argument must be one of html, rnc, text or "
126
                    "xml"
127
                )
128
129
            cmd.set_attribute("format", format)
130
131
        return self._send_xml_command(cmd)
132