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
|
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
from typing import Optional
from gvm._enum import Enum
from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.utils import to_bool
from gvm.xml import XmlCommand
from .._entity_id import EntityID
class HostsOrdering(Enum):
"""Enum for host ordering during scans"""
SEQUENTIAL = "sequential"
RANDOM = "random"
REVERSE = "reverse"
class Hosts:
@classmethod
def create_host(
cls, name: str, *, comment: Optional[str] = None
) -> Request:
"""Create a new host host
Args:
name: Name for the new host host
comment: Comment for the new host host
"""
if not name:
raise RequiredArgument(
function=cls.create_host.__name__, argument="name"
)
cmd = XmlCommand("create_asset")
host = cmd.add_element("asset")
host.add_element("type", "host")
host.add_element("name", name)
if comment:
host.add_element("comment", comment)
return cmd
@classmethod
def delete_host(cls, host_id: EntityID) -> Request:
"""Deletes an existing host
Args:
host_id: UUID of the single host to delete.
"""
if not host_id:
raise RequiredArgument(
function=cls.delete_host.__name__,
argument="host_id",
)
cmd = XmlCommand("delete_asset")
cmd.set_attribute("asset_id", str(host_id))
return cmd
@staticmethod
def get_hosts(
*,
filter_string: Optional[str] = None,
filter_id: Optional[EntityID] = None,
details: Optional[bool] = None,
) -> Request:
"""Request a list of hosts
Args:
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
details: Whether to include additional information (e.g. tags)
"""
cmd = XmlCommand("get_assets")
cmd.set_attribute("type", "host")
cmd.add_filter(filter_string, filter_id)
if details is not None:
cmd.set_attribute("details", to_bool(details))
return cmd
@classmethod
def get_host(
cls, host_id: EntityID, *, details: Optional[bool] = None
) -> Request:
"""Request a single host
Arguments:
host_id: UUID of an existing host
details: Whether to include additional information (e.g. tags)
"""
cmd = XmlCommand("get_assets")
if not host_id:
raise RequiredArgument(
function=cls.get_host.__name__, argument="host_id"
)
cmd.set_attribute("asset_id", str(host_id))
cmd.set_attribute("type", "host")
if details is not None:
cmd.set_attribute("details", to_bool(details))
return cmd
@classmethod
def modify_host(
cls, host_id: EntityID, *, comment: Optional[str] = None
) -> Request:
"""Modifies an existing host.
Args:
host_id: UUID of the host to be modified.
comment: Comment for the host. Not passing a comment
arguments clears the comment for this host.
"""
if not host_id:
raise RequiredArgument(
function=cls.modify_host.__name__, argument="host_id"
)
cmd = XmlCommand("modify_asset")
cmd.set_attribute("asset_id", str(host_id))
if not comment:
comment = ""
cmd.add_element("comment", comment)
return cmd
|