Passed
Push — master ( 147191...48952f )
by Björn
110:14 queued 108:31
created

GroupsMixin.create_group()   B

Complexity

Conditions 5

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nop 6
dl 0
loc 39
rs 8.9332
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, List, Optional
20
21
from gvm.errors import RequiredArgument
22
from gvm.utils import add_filter, to_bool, to_comma_list
23
from gvm.xml import XmlCommand
24
25
26
class GroupsMixin:
27
    def clone_group(self, group_id: str) -> Any:
28
        """Clone an existing group
29
30
        Arguments:
31
            group_id: UUID of an existing group to clone from
32
33
        Returns:
34
            The response. See :py:meth:`send_command` for details.
35
        """
36
        if not group_id:
37
            raise RequiredArgument(
38
                function=self.clone_group.__name__, argument='group_id'
39
            )
40
41
        cmd = XmlCommand("create_group")
42
        cmd.add_element("copy", group_id)
43
        return self._send_xml_command(cmd)
44
45
    def create_group(
46
        self,
47
        name: str,
48
        *,
49
        comment: Optional[str] = None,
50
        special: Optional[bool] = False,
51
        users: Optional[List[str]] = None,
52
    ) -> Any:
53
        """Create a new group
54
55
        Arguments:
56
            name: Name of the new group
57
            comment: Comment for the group
58
            special: Create permission giving members full access to each
59
                other's entities
60
            users: List of user names to be in the group
61
62
        Returns:
63
            The response. See :py:meth:`send_command` for details.
64
        """
65
        if not name:
66
            raise RequiredArgument(
67
                function=self.create_group.__name__, argument='name'
68
            )
69
70
        cmd = XmlCommand("create_group")
71
        cmd.add_element("name", name)
72
73
        if comment:
74
            cmd.add_element("comment", comment)
75
76
        if special:
77
            _xmlspecial = cmd.add_element("specials")
78
            _xmlspecial.add_element("full")
79
80
        if users:
81
            cmd.add_element("users", to_comma_list(users))
82
83
        return self._send_xml_command(cmd)
84
85
    def delete_group(
86
        self, group_id: str, *, ultimate: Optional[bool] = False
87
    ) -> Any:
88
        """Deletes an existing group
89
90
        Arguments:
91
            group_id: UUID of the group to be deleted.
92
            ultimate: Whether to remove entirely, or to the trashcan.
93
        """
94
        if not group_id:
95
            raise RequiredArgument(
96
                function=self.delete_group.__name__, argument='group_id'
97
            )
98
99
        cmd = XmlCommand("delete_group")
100
        cmd.set_attribute("group_id", group_id)
101
        cmd.set_attribute("ultimate", to_bool(ultimate))
102
103
        return self._send_xml_command(cmd)
104
105 View Code Duplication
    def get_groups(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
106
        self,
107
        *,
108
        filter_string: Optional[str] = None,
109
        filter_id: Optional[str] = None,
110
        trash: Optional[bool] = None,
111
    ) -> Any:
112
        """Request a list of groups
113
114
        Arguments:
115
            filter_string: Filter term to use for the query
116
            filter_id: UUID of an existing filter to use for the query
117
            trash: Whether to get the trashcan groups instead
118
119
        Returns:
120
            The response. See :py:meth:`send_command` for details.
121
        """
122
        cmd = XmlCommand("get_groups")
123
124
        add_filter(cmd, filter_string, filter_id)
125
126
        if trash is not None:
127
            cmd.set_attribute("trash", to_bool(trash))
128
129
        return self._send_xml_command(cmd)
130
131
    def get_group(self, group_id: str) -> Any:
132
        """Request a single group
133
134
        Arguments:
135
            group_id: UUID of an existing group
136
137
        Returns:
138
            The response. See :py:meth:`send_command` for details.
139
        """
140
        cmd = XmlCommand("get_groups")
141
142
        if not group_id:
143
            raise RequiredArgument(
144
                function=self.get_group.__name__, argument='group_id'
145
            )
146
147
        cmd.set_attribute("group_id", group_id)
148
        return self._send_xml_command(cmd)
149
150 View Code Duplication
    def modify_group(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
151
        self,
152
        group_id: str,
153
        *,
154
        comment: Optional[str] = None,
155
        name: Optional[str] = None,
156
        users: Optional[List[str]] = None,
157
    ) -> Any:
158
        """Modifies an existing group.
159
160
        Arguments:
161
            group_id: UUID of group to modify.
162
            comment: Comment on group.
163
            name: Name of group.
164
            users: List of user names to be in the group
165
166
        Returns:
167
            The response. See :py:meth:`send_command` for details.
168
        """
169
        if not group_id:
170
            raise RequiredArgument(
171
                function=self.modify_group.__name__, argument='group_id'
172
            )
173
174
        cmd = XmlCommand("modify_group")
175
        cmd.set_attribute("group_id", group_id)
176
177
        if comment:
178
            cmd.add_element("comment", comment)
179
180
        if name:
181
            cmd.add_element("name", name)
182
183
        if users:
184
            cmd.add_element("users", to_comma_list(users))
185
186
        return self._send_xml_command(cmd)
187