Completed
Push — master ( af96e1...147191 )
by Jaspar
23s queued 16s
created

TicketsMixin.clone_ticket()   A

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 2
dl 0
loc 19
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
# pylint:  disable=redefined-builtin
20
21
from enum import Enum
22
23
from typing import Any, Optional
24
25
from gvm.errors import InvalidArgument, InvalidArgumentType, RequiredArgument
26
from gvm.utils import add_filter, to_bool
27
from gvm.xml import XmlCommand
28
29
30
class TicketStatus(Enum):
31
    """Enum for ticket status"""
32
33
    OPEN = 'Open'
34
    FIXED = 'Fixed'
35
    CLOSED = 'Closed'
36
37
38
def get_ticket_status_from_string(
39
    ticket_status: Optional[str],
40
) -> Optional[TicketStatus]:
41
    """Convert a ticket status string into a TicketStatus instance"""
42
    if not ticket_status:
43
        return None
44
45
    try:
46
        return TicketStatus[ticket_status.upper()]
47
    except KeyError:
48
        raise InvalidArgument(
49
            argument='ticket_status',
50
            function=get_ticket_status_from_string.__name__,
51
        ) from None
52
53
54
class TicketsMixin:
55
    def clone_ticket(self, ticket_id: str) -> Any:
56
        """Clone an existing ticket
57
58
        Arguments:
59
            ticket_id: UUID of an existing ticket to clone from
60
61
        Returns:
62
            The response. See :py:meth:`send_command` for details.
63
        """
64
        if not ticket_id:
65
            raise RequiredArgument(
66
                function=self.clone_ticket.__name__, argument='ticket_id'
67
            )
68
69
        cmd = XmlCommand("create_ticket")
70
71
        _copy = cmd.add_element("copy", ticket_id)
72
73
        return self._send_xml_command(cmd)
74
75
    def create_ticket(
76
        self,
77
        *,
78
        result_id: str,
79
        assigned_to_user_id: str,
80
        note: str,
81
        comment: Optional[str] = None,
82
    ) -> Any:
83
        """Create a new ticket
84
85
        Arguments:
86
            result_id: UUID of the result the ticket applies to
87
            assigned_to_user_id: UUID of a user the ticket should be assigned to
88
            note: A note about opening the ticket
89
            comment: Comment for the ticket
90
91
        Returns:
92
            The response. See :py:meth:`send_command` for details.
93
        """
94
        if not result_id:
95
            raise RequiredArgument(
96
                function=self.create_ticket.__name__, argument='result_id'
97
            )
98
99
        if not assigned_to_user_id:
100
            raise RequiredArgument(
101
                function=self.create_ticket.__name__,
102
                argument='assigned_to_user_id',
103
            )
104
105
        if not note:
106
            raise RequiredArgument(
107
                function=self.create_ticket.__name__, argument='note'
108
            )
109
110
        cmd = XmlCommand("create_ticket")
111
112
        _result = cmd.add_element("result")
113
        _result.set_attribute("id", result_id)
114
115
        _assigned = cmd.add_element("assigned_to")
116
        _user = _assigned.add_element("user")
117
        _user.set_attribute("id", assigned_to_user_id)
118
119
        _note = cmd.add_element("open_note", note)
120
121
        if comment:
122
            cmd.add_element("comment", comment)
123
124
        return self._send_xml_command(cmd)
125
126
    def delete_ticket(
127
        self, ticket_id: str, *, ultimate: Optional[bool] = False
128
    ):
129
        """Deletes an existing ticket
130
131
        Arguments:
132
            ticket_id: UUID of the ticket to be deleted.
133
            ultimate: Whether to remove entirely, or to the trashcan.
134
        """
135
        if not ticket_id:
136
            raise RequiredArgument(
137
                function=self.delete_ticket.__name__, argument='ticket_id'
138
            )
139
140
        cmd = XmlCommand("delete_ticket")
141
        cmd.set_attribute("ticket_id", ticket_id)
142
        cmd.set_attribute("ultimate", to_bool(ultimate))
143
144
        return self._send_xml_command(cmd)
145
146 View Code Duplication
    def get_tickets(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
147
        self,
148
        *,
149
        trash: Optional[bool] = None,
150
        filter: Optional[str] = None,
151
        filter_id: Optional[str] = None,
152
    ) -> Any:
153
        """Request a list of tickets
154
155
        Arguments:
156
            filter: Filter term to use for the query
157
            filter_id: UUID of an existing filter to use for the query
158
            trash: True to request the tickets in the trashcan
159
160
        Returns:
161
            The response. See :py:meth:`send_command` for details.
162
        """
163
        cmd = XmlCommand("get_tickets")
164
165
        add_filter(cmd, filter, filter_id)
166
167
        if trash is not None:
168
            cmd.set_attribute("trash", to_bool(trash))
169
170
        return self._send_xml_command(cmd)
171
172
    def get_ticket(self, ticket_id: str) -> Any:
173
        """Request a single ticket
174
175
        Arguments:
176
            ticket_id: UUID of an existing ticket
177
178
        Returns:
179
            The response. See :py:meth:`send_command` for details.
180
        """
181
        if not ticket_id:
182
            raise RequiredArgument(
183
                function=self.get_ticket.__name__, argument='ticket_id'
184
            )
185
186
        cmd = XmlCommand("get_tickets")
187
        cmd.set_attribute("ticket_id", ticket_id)
188
        return self._send_xml_command(cmd)
189
190
    def modify_ticket(
191
        self,
192
        ticket_id: str,
193
        *,
194
        status: Optional[TicketStatus] = None,
195
        note: Optional[str] = None,
196
        assigned_to_user_id: Optional[str] = None,
197
        comment: Optional[str] = None,
198
    ) -> Any:
199
        """Modify a single ticket
200
201
        Arguments:
202
            ticket_id: UUID of an existing ticket
203
            status: New status for the ticket
204
            note: Note for the status change. Required if status is set.
205
            assigned_to_user_id: UUID of the user the ticket should be assigned
206
                to
207
            comment: Comment for the ticket
208
209
        Returns:
210
            The response. See :py:meth:`send_command` for details.
211
        """
212
        if not ticket_id:
213
            raise RequiredArgument(
214
                function=self.modify_ticket.__name__, argument='ticket_id'
215
            )
216
217
        if status and not note:
218
            raise RequiredArgument(
219
                function=self.modify_ticket.__name__, argument='note'
220
            )
221
222
        if note and not status:
223
            raise RequiredArgument(
224
                function=self.modify_ticket.__name__, argument='status'
225
            )
226
227
        cmd = XmlCommand("modify_ticket")
228
        cmd.set_attribute("ticket_id", ticket_id)
229
230
        if assigned_to_user_id:
231
            _assigned = cmd.add_element("assigned_to")
232
            _user = _assigned.add_element("user")
233
            _user.set_attribute("id", assigned_to_user_id)
234
235
        if status:
236
            if not isinstance(status, TicketStatus):
237
                raise InvalidArgumentType(
238
                    function=self.modify_ticket.__name__,
239
                    argument='status',
240
                    arg_type=TicketStatus.__name__,
241
                )
242
243
            cmd.add_element('status', status.value)
244
            cmd.add_element('{}_note'.format(status.name.lower()), note)
245
246
        if comment:
247
            cmd.add_element("comment", comment)
248
249
        return self._send_xml_command(cmd)
250