Skip to content

Commit

Permalink
Add: Introduce a new type for representing an Entity ID
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernricks authored and greenbonebot committed Jun 14, 2024
1 parent 2de5129 commit ce16191
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
2 changes: 2 additions & 0 deletions gvm/protocols/gmp/requests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ._aggregates import Aggregates, AggregateStatistic, SortOrder
from ._auth import Authentication
from ._entity_id import EntityID
from ._entity_type import EntityType
from ._feed import Feed, FeedType
from ._help import Help, HelpFormat
Expand All @@ -18,6 +19,7 @@
"Aggregates",
"AggregateStatistic",
"Authentication",
"EntityID",
"EntityType",
"Feed",
"FeedType",
Expand Down
4 changes: 2 additions & 2 deletions gvm/protocols/gmp/requests/_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Iterable, Optional, Union
from uuid import UUID

from gvm._enum import Enum
from gvm.errors import InvalidArgumentType, RequiredArgument
from gvm.protocols.core import Request
from gvm.xml import XmlCommand

from ._entity_id import EntityID
from ._entity_type import EntityType


Expand Down Expand Up @@ -41,7 +41,7 @@ def get_aggregates(
resource_type: Union[EntityType, str],
*,
filter_string: Optional[str] = None,
filter_id: Optional[Union[str, UUID]] = None,
filter_id: Optional[EntityID] = None,
sort_criteria: Optional[
Iterable[dict[str, Union[str, SortOrder, AggregateStatistic]]]
] = None,
Expand Down
8 changes: 8 additions & 0 deletions gvm/protocols/gmp/requests/_entity_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Union
from uuid import UUID

EntityID = Union[str, UUID]
17 changes: 9 additions & 8 deletions gvm/protocols/gmp/requests/_port_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Optional, Union
from uuid import UUID

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 PortRangeType(Enum):
"""Enum for port range type"""
Expand All @@ -21,7 +22,7 @@ class PortRangeType(Enum):

class PortList:
@classmethod
def clone_port_list(cls, port_list_id: Union[str, UUID]) -> Request:
def clone_port_list(cls, port_list_id: EntityID) -> Request:
"""Clone an existing port list
Args:
Expand Down Expand Up @@ -70,7 +71,7 @@ def create_port_list(
@classmethod
def create_port_range(
cls,
port_list_id: Union[str, UUID],
port_list_id: EntityID,
start: int,
end: int,
port_range_type: Union[str, PortRangeType],
Expand Down Expand Up @@ -124,7 +125,7 @@ def create_port_range(

@classmethod
def delete_port_list(
cls, port_list_id: Union[str, UUID], *, ultimate: bool = False
cls, port_list_id: EntityID, *, ultimate: bool = False
) -> Request:
"""Deletes an existing port list
Expand All @@ -144,7 +145,7 @@ def delete_port_list(
return cmd

@classmethod
def delete_port_range(cls, port_range_id: Union[str, UUID]) -> Request:
def delete_port_range(cls, port_range_id: EntityID) -> Request:
"""Deletes an existing port range
Args:
Expand All @@ -166,7 +167,7 @@ def get_port_lists(
cls,
*,
filter_string: Optional[str] = None,
filter_id: Optional[Union[str, UUID]] = None,
filter_id: Optional[EntityID] = None,
details: Optional[bool] = None,
targets: Optional[bool] = None,
trash: Optional[bool] = None,
Expand Down Expand Up @@ -196,7 +197,7 @@ def get_port_lists(
return cmd

@classmethod
def get_port_list(cls, port_list_id: Union[str, UUID]) -> Request:
def get_port_list(cls, port_list_id: EntityID) -> Request:
"""Request a single port list
Args:
Expand All @@ -219,7 +220,7 @@ def get_port_list(cls, port_list_id: Union[str, UUID]) -> Request:
@classmethod
def modify_port_list(
cls,
port_list_id: Union[str, UUID],
port_list_id: EntityID,
*,
comment: Optional[str] = None,
name: Optional[str] = None,
Expand Down
5 changes: 3 additions & 2 deletions gvm/protocols/gmp/requests/_resource_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Optional, Union
from uuid import UUID

from gvm._enum import Enum
from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.xml import XmlCommand

from ._entity_id import EntityID


class ResourceType(Enum):
"""Enum for resource types"""
Expand Down Expand Up @@ -79,7 +80,7 @@ def get_resource_names(
@classmethod
def get_resource_name(
cls,
resource_id: Union[str, UUID],
resource_id: EntityID,
resource_type: Union[ResourceType, str],
) -> Request:
"""Request a single resource name
Expand Down
7 changes: 4 additions & 3 deletions gvm/protocols/gmp/requests/_system_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
#

from numbers import Integral
from typing import Optional, Union
from uuid import UUID
from typing import Optional

from gvm.errors import InvalidArgument
from gvm.protocols.core import Request
from gvm.utils import to_bool
from gvm.xml import XmlCommand

from ._entity_id import EntityID


class SystemReports:
@classmethod
Expand All @@ -23,7 +24,7 @@ def get_system_reports(
start_time: Optional[str] = None,
end_time: Optional[str] = None,
brief: Optional[bool] = None,
slave_id: Optional[Union[str, UUID]] = None,
slave_id: Optional[EntityID] = None,
) -> Request:
"""Request a list of system reports
Expand Down
7 changes: 3 additions & 4 deletions gvm/protocols/gmp/requests/_trashcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later

from typing import Union
from uuid import UUID

from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.xml import XmlCommand

from ._entity_id import EntityID


class TrashCan:
@staticmethod
Expand All @@ -21,7 +20,7 @@ def empty_trashcan() -> Request:
return XmlCommand("empty_trashcan")

@classmethod
def restore_from_trashcan(cls, entity_id: Union[str, UUID]) -> Request:
def restore_from_trashcan(cls, entity_id: EntityID) -> Request:
"""Restore an entity from the trashcan
Args:
Expand Down
9 changes: 5 additions & 4 deletions gvm/protocols/gmp/requests/_user_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# SPDX-License-Identifier: GPL-3.0-or-later
#

from typing import Optional, Union
from uuid import UUID
from typing import Optional

from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.utils import to_base64
from gvm.xml import XmlCommand

from ._entity_id import EntityID


class UserSettings:
@staticmethod
Expand All @@ -28,7 +29,7 @@ def get_user_settings(*, filter_string: Optional[str] = None) -> Request:
return cmd

@classmethod
def get_user_setting(cls, setting_id: Union[str, UUID]) -> Request:
def get_user_setting(cls, setting_id: EntityID) -> Request:
"""Request a single user setting
Args:
Expand All @@ -48,7 +49,7 @@ def get_user_setting(cls, setting_id: Union[str, UUID]) -> Request:
def modify_user_setting(
cls,
*,
setting_id: Optional[Union[str, UUID]] = None,
setting_id: Optional[EntityID] = None,
name: Optional[str] = None,
value: Optional[str] = None,
) -> Request:
Expand Down

0 comments on commit ce16191

Please sign in to comment.