Skip to content

Commit

Permalink
feat: create or dump matching target
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed May 20, 2024
1 parent c7b5db9 commit 513efed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/gvm_sync_targets/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)}.")
10 changes: 9 additions & 1 deletion src/gvm_sync_targets/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand All @@ -23,6 +27,8 @@
"DeleteAssetResponse",
"CreateAssetResponse",
"GetTargetsResponse",
"DeleteTargetResponse",
"CreateTargetResponse",
]

_MODEL_MAP: Mapping[str, type[Response]] = {
Expand All @@ -31,6 +37,8 @@
"delete_asset_response": DeleteAssetResponse,
"create_asset_response": CreateAssetResponse,
"get_targets_response": GetTargetsResponse,
"delete_target_response": DeleteTargetResponse,
"create_target_response": CreateTargetResponse,
}


Expand Down
8 changes: 8 additions & 0 deletions src/gvm_sync_targets/models/targets_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 513efed

Please sign in to comment.