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

UserSettingsMixin.get_user_setting()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 2
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 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
from typing import Any, Optional
20
21
from gvm.errors import RequiredArgument
22
from gvm.utils import to_base64
23
from gvm.xml import XmlCommand
24
25
26
class UserSettingsMixin:
27
    def get_user_settings(self, *, filter_string: Optional[str] = None) -> Any:
28
        """Request a list of user settings
29
30
        Arguments:
31
            filter_string: Filter term to use for the query
32
33
        Returns:
34
            The response. See :py:meth:`send_command` for details.
35
        """
36
        cmd = XmlCommand("get_settings")
37
38
        if filter_string:
39
            cmd.set_attribute("filter", filter_string)
40
41
        return self._send_xml_command(cmd)
42
43
    def get_user_setting(self, setting_id: str) -> Any:
44
        """Request a single user setting
45
46
        Arguments:
47
            setting_id: UUID of an existing setting
48
49
        Returns:
50
            The response. See :py:meth:`send_command` for details.
51
        """
52
        cmd = XmlCommand("get_settings")
53
54
        if not setting_id:
55
            raise RequiredArgument(
56
                function=self.get_user_setting.__name__, argument='setting_id'
57
            )
58
59
        cmd.set_attribute("setting_id", setting_id)
60
        return self._send_xml_command(cmd)
61
62
    def modify_user_setting(
63
        self,
64
        setting_id: Optional[str] = None,
65
        name: Optional[str] = None,
66
        value: Optional[str] = None,
67
    ) -> Any:
68
        """Modifies an existing usersetting.
69
70
        Arguments:
71
            setting_id: UUID of the setting to be changed.
72
            name: The name of the setting. Either setting_id or name must be
73
                passed.
74
            value: The value of the setting.
75
76
        Returns:
77
            The response. See :py:meth:`send_command` for details.
78
        """
79
        if not setting_id and not name:
80
            raise RequiredArgument(
81
                function=self.modify_user_setting.__name__,
82
                argument='setting_id or name argument',
83
            )
84
85
        if value is None:
86
            raise RequiredArgument(
87
                function=self.modify_user_setting.__name__,
88
                argument='value argument',
89
            )
90
91
        cmd = XmlCommand("modify_setting")
92
93
        if setting_id:
94
            cmd.set_attribute("setting_id", setting_id)
95
        else:
96
            cmd.add_element("name", name)
97
98
        cmd.add_element("value", to_base64(value))
99
100
        return self._send_xml_command(cmd)
101