File: _groups.py

package info (click to toggle)
python-gvm 26.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,316 kB
  • sloc: python: 46,784; makefile: 18
file content (166 lines) | stat: -rw-r--r-- 4,473 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Optional

from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.utils import to_bool, to_comma_list
from gvm.xml import XmlCommand

from .._entity_id import EntityID


class Groups:
    @classmethod
    def clone_group(cls, group_id: EntityID) -> Request:
        """Clone an existing group

        Args:
            group_id: UUID of an existing group to clone from
        """
        if not group_id:
            raise RequiredArgument(
                function=cls.clone_group.__name__, argument="group_id"
            )

        cmd = XmlCommand("create_group")
        cmd.add_element("copy", str(group_id))

        return cmd

    @classmethod
    def create_group(
        cls,
        name: str,
        *,
        comment: Optional[str] = None,
        special: Optional[bool] = False,
        users: Optional[list[str]] = None,
    ) -> Request:
        """Create a new group

        Args:
            name: Name of the new group
            comment: Comment for the group
            special: Create permission giving members full access to each
                other's entities
            users: List of user names to be in the group
        """
        if not name:
            raise RequiredArgument(
                function=cls.create_group.__name__, argument="name"
            )

        cmd = XmlCommand("create_group")
        cmd.add_element("name", name)

        if comment:
            cmd.add_element("comment", comment)

        if special:
            xml_special = cmd.add_element("specials")
            xml_special.add_element("full")

        if users:
            cmd.add_element("users", to_comma_list(users))

        return cmd

    @classmethod
    def delete_group(
        cls, group_id: EntityID, *, ultimate: Optional[bool] = False
    ) -> Request:
        """Deletes an existing group

        Args:
            group_id: UUID of the group to be deleted.
            ultimate: Whether to remove entirely, or to the trashcan.
        """
        if not group_id:
            raise RequiredArgument(
                function=cls.delete_group.__name__, argument="group_id"
            )

        cmd = XmlCommand("delete_group")
        cmd.set_attribute("group_id", str(group_id))
        cmd.set_attribute("ultimate", to_bool(ultimate))

        return cmd

    @staticmethod
    def get_groups(
        *,
        filter_string: Optional[str] = None,
        filter_id: Optional[EntityID] = None,
        trash: Optional[bool] = None,
    ) -> Request:
        """Request a list of groups

        Args:
            filter_string: Filter term to use for the query
            filter_id: UUID of an existing filter to use for the query
            trash: Whether to get the trashcan groups instead
        """
        cmd = XmlCommand("get_groups")

        cmd.add_filter(filter_string, filter_id)

        if trash is not None:
            cmd.set_attribute("trash", to_bool(trash))

        return cmd

    @classmethod
    def get_group(cls, group_id: EntityID) -> Request:
        """Request a single group

        Args:
            group_id: UUID of an existing group
        """
        cmd = XmlCommand("get_groups")

        if not group_id:
            raise RequiredArgument(
                function=cls.get_group.__name__, argument="group_id"
            )

        cmd.set_attribute("group_id", str(group_id))
        return cmd

    @classmethod
    def modify_group(
        cls,
        group_id: EntityID,
        *,
        comment: Optional[str] = None,
        name: Optional[str] = None,
        users: Optional[list[str]] = None,
    ) -> Request:
        """Modifies an existing group.

        Args:
            group_id: UUID of group to modify.
            comment: Comment on group.
            name: Name of group.
            users: List of user names to be in the group
        """
        if not group_id:
            raise RequiredArgument(
                function=cls.modify_group.__name__, argument="group_id"
            )

        cmd = XmlCommand("modify_group")
        cmd.set_attribute("group_id", str(group_id))

        if comment:
            cmd.add_element("comment", comment)

        if name:
            cmd.add_element("name", name)

        if users:
            cmd.add_element("users", to_comma_list(users))

        return cmd