diff --git a/src/gvm_sync_targets/cli/__init__.py b/src/gvm_sync_targets/cli/__init__.py index 0e27c18..8c4b759 100644 --- a/src/gvm_sync_targets/cli/__init__.py +++ b/src/gvm_sync_targets/cli/__init__.py @@ -10,8 +10,7 @@ from gvm.protocols.gmp import Gmp from gvm_sync_targets import __version__ -from gvm_sync_targets.models import ModelTransform -from gvm_sync_targets.models.targets_response import GetTargetsResponse +from gvm_sync_targets.models import GetTargetsResponse, ModelTransform from gvm_sync_targets.util import get_all_hosts, read_lines @@ -71,4 +70,14 @@ def gvm_sync_targets( for target in targets.targets: click.echo(target) + resp: GetTargetsResponse = gmp.get_targets( + filter_string='name="All Hosts"', tasks=True + ) + + if resp.targets: + target = resp.targets[0] + click.echo(target) + else: + gmp.create_target("All Hosts", asset_hosts_filter="") + click.echo(f"Added {len(to_add)} hosts, removed {len(to_remove)}.") diff --git a/src/gvm_sync_targets/models/__init__.py b/src/gvm_sync_targets/models/__init__.py index 42db30b..db3b38c 100644 --- a/src/gvm_sync_targets/models/__init__.py +++ b/src/gvm_sync_targets/models/__init__.py @@ -14,7 +14,11 @@ ) from gvm_sync_targets.models.auth_response import AuthenticateResponse from gvm_sync_targets.models.response import Response -from gvm_sync_targets.models.targets_response import GetTargetsResponse +from gvm_sync_targets.models.targets_response import ( + CreateTargetResponse, + DeleteTargetResponse, + GetTargetsResponse, +) from gvm_sync_targets.util import Element __all__ = [ @@ -23,6 +27,8 @@ "DeleteAssetResponse", "CreateAssetResponse", "GetTargetsResponse", + "DeleteTargetResponse", + "CreateTargetResponse", ] _MODEL_MAP: Mapping[str, type[Response]] = { @@ -31,6 +37,8 @@ "delete_asset_response": DeleteAssetResponse, "create_asset_response": CreateAssetResponse, "get_targets_response": GetTargetsResponse, + "delete_target_response": DeleteTargetResponse, + "create_target_response": CreateTargetResponse, } diff --git a/src/gvm_sync_targets/models/targets_response.py b/src/gvm_sync_targets/models/targets_response.py index 7103213..df9d640 100644 --- a/src/gvm_sync_targets/models/targets_response.py +++ b/src/gvm_sync_targets/models/targets_response.py @@ -72,3 +72,11 @@ class GetTargetsResponse(Response, tag="get_targets_response"): sort: Sort pagination: Pagination = element("targets") asset_count: Count = element("target_count") + + +class DeleteTargetResponse(Response, tag="delete_target_response"): + pass + + +class CreateTargetResponse(Response, tag="create_target_response"): + uuid: str = attr("id")