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 typing import Any, Optional |
22
|
|
|
|
23
|
|
|
from gvm.errors import RequiredArgument |
24
|
|
|
from gvm.utils import add_filter |
25
|
|
|
from gvm.xml import XmlCommand |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class HostsMixin: |
29
|
|
|
def create_host(self, name: str, *, comment: Optional[str] = None) -> Any: |
30
|
|
|
"""Create a new host host |
31
|
|
|
|
32
|
|
|
Arguments: |
33
|
|
|
name: Name for the new host host |
34
|
|
|
comment: Comment for the new host host |
35
|
|
|
|
36
|
|
|
Returns: |
37
|
|
|
The response. See :py:meth:`send_command` for details. |
38
|
|
|
""" |
39
|
|
|
if not name: |
40
|
|
|
raise RequiredArgument( |
41
|
|
|
function=self.create_host.__name__, argument='name' |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
cmd = XmlCommand("create_asset") |
45
|
|
|
host = cmd.add_element("asset") |
46
|
|
|
host.add_element("type", "host") |
47
|
|
|
host.add_element("name", name) |
48
|
|
|
|
49
|
|
|
if comment: |
50
|
|
|
host.add_element("comment", comment) |
51
|
|
|
|
52
|
|
|
return self._send_xml_command(cmd) |
53
|
|
|
|
54
|
|
|
def delete_host(self, host_id: str) -> Any: |
55
|
|
|
"""Deletes an existing host |
56
|
|
|
|
57
|
|
|
Arguments: |
58
|
|
|
host_id: UUID of the single host to delete. |
59
|
|
|
""" |
60
|
|
|
if not host_id: |
61
|
|
|
raise RequiredArgument( |
62
|
|
|
function=self.delete_host.__name__, |
63
|
|
|
argument='host_id', |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
cmd = XmlCommand("delete_asset") |
67
|
|
|
cmd.set_attribute("asset_id", host_id) |
68
|
|
|
|
69
|
|
|
return self._send_xml_command(cmd) |
70
|
|
|
|
71
|
|
|
def get_hosts( |
72
|
|
|
self, |
73
|
|
|
*, |
74
|
|
|
filter: Optional[str] = None, |
75
|
|
|
filter_id: Optional[str] = None, |
76
|
|
|
) -> Any: |
77
|
|
|
"""Request a list of hosts |
78
|
|
|
|
79
|
|
|
Arguments: |
80
|
|
|
filter: Filter term to use for the query |
81
|
|
|
filter_id: UUID of an existing filter to use for the query |
82
|
|
|
|
83
|
|
|
Returns: |
84
|
|
|
The response. See :py:meth:`send_command` for details. |
85
|
|
|
""" |
86
|
|
|
|
87
|
|
|
cmd = XmlCommand("get_assets") |
88
|
|
|
|
89
|
|
|
cmd.set_attribute("type", "host") |
90
|
|
|
|
91
|
|
|
add_filter(cmd, filter, filter_id) |
92
|
|
|
|
93
|
|
|
return self._send_xml_command(cmd) |
94
|
|
|
|
95
|
|
|
def get_host(self, host_id: str) -> Any: |
96
|
|
|
"""Request a single host |
97
|
|
|
|
98
|
|
|
Arguments: |
99
|
|
|
host_id: UUID of an existing host |
100
|
|
|
|
101
|
|
|
Returns: |
102
|
|
|
The response. See :py:meth:`send_command` for details. |
103
|
|
|
""" |
104
|
|
|
cmd = XmlCommand("get_assets") |
105
|
|
|
|
106
|
|
|
if not host_id: |
107
|
|
|
raise RequiredArgument( |
108
|
|
|
function=self.get_host.__name__, argument='host_id' |
109
|
|
|
) |
110
|
|
|
|
111
|
|
|
cmd.set_attribute("asset_id", host_id) |
112
|
|
|
cmd.set_attribute("type", "host") |
113
|
|
|
|
114
|
|
|
return self._send_xml_command(cmd) |
115
|
|
|
|
116
|
|
|
def modify_host( |
117
|
|
|
self, host_id: str, *, comment: Optional[str] = None |
118
|
|
|
) -> Any: |
119
|
|
|
"""Modifies an existing host. |
120
|
|
|
|
121
|
|
|
Arguments: |
122
|
|
|
host_id: UUID of the host to be modified. |
123
|
|
|
comment: Comment for the host. Not passing a comment |
124
|
|
|
arguments clears the comment for this host. |
125
|
|
|
|
126
|
|
|
Returns: |
127
|
|
|
The response. See :py:meth:`send_command` for details. |
128
|
|
|
""" |
129
|
|
|
if not host_id: |
130
|
|
|
raise RequiredArgument( |
131
|
|
|
function=self.modify_host.__name__, argument='host_id' |
132
|
|
|
) |
133
|
|
|
|
134
|
|
|
cmd = XmlCommand("modify_asset") |
135
|
|
|
cmd.set_attribute("asset_id", host_id) |
136
|
|
|
if not comment: |
137
|
|
|
comment = "" |
138
|
|
|
cmd.add_element("comment", comment) |
139
|
|
|
|
140
|
|
|
return self._send_xml_command(cmd) |
141
|
|
|
|