From 3629306437f03a4ca00d9bca5e8562b44c75dd2e Mon Sep 17 00:00:00 2001 From: Chrxxxxs <134285960+Chrxxxxs@users.noreply.github.com> Date: Tue, 3 Jun 2025 21:59:35 +0200 Subject: [PATCH] add argument task_limit for get_tasks to easily (de-)limit the amount of tasks requested --- gvm/protocols/gmp/_gmp224.py | 3 +++ gvm/protocols/gmp/requests/v224/_tasks.py | 8 ++++++++ tests/protocols/gmp/requests/v224/test_tasks.py | 14 ++++++++++++++ .../gmpv224/entities/tasks/test_get_tasks.py | 14 ++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/gvm/protocols/gmp/_gmp224.py b/gvm/protocols/gmp/_gmp224.py index f235fbf72..1d8846afd 100644 --- a/gvm/protocols/gmp/_gmp224.py +++ b/gvm/protocols/gmp/_gmp224.py @@ -3843,6 +3843,7 @@ def get_tasks( *, filter_string: Optional[str] = None, filter_id: Optional[EntityID] = None, + task_limit: Optional[int] = None, trash: Optional[bool] = None, details: Optional[bool] = None, schedules_only: Optional[bool] = None, @@ -3853,6 +3854,7 @@ def get_tasks( Args: filter_string: Filter term to use for the query filter_id: UUID of an existing filter to use for the query + task_limit: Limits the amount of tasks being returned (Default:10) trash: Whether to get the trashcan tasks instead details: Whether to include full task details schedules_only: Whether to only include id, name and schedule @@ -3864,6 +3866,7 @@ def get_tasks( Tasks.get_tasks( filter_string=filter_string, filter_id=filter_id, + task_limit=task_limit, trash=trash, details=details, schedules_only=schedules_only, diff --git a/gvm/protocols/gmp/requests/v224/_tasks.py b/gvm/protocols/gmp/requests/v224/_tasks.py index 4f73922d9..f8c7e5e4f 100644 --- a/gvm/protocols/gmp/requests/v224/_tasks.py +++ b/gvm/protocols/gmp/requests/v224/_tasks.py @@ -197,6 +197,7 @@ def get_tasks( *, filter_string: Optional[str] = None, filter_id: Optional[EntityID] = None, + task_limit: Optional[int] = None, trash: Optional[bool] = None, details: Optional[bool] = None, schedules_only: Optional[bool] = None, @@ -207,6 +208,7 @@ def get_tasks( Args: filter_string: Filter term to use for the query filter_id: UUID of an existing filter to use for the query + task_limit: Limits the amount of tasks being returned (Default:10) trash: Whether to get the trashcan tasks instead details: Whether to include full task details schedules_only: Whether to only include id, name and schedule @@ -217,6 +219,12 @@ def get_tasks( cmd = XmlCommand("get_tasks") cmd.set_attribute("usage_type", "scan") + if task_limit is not None: + if filter_string is None: + filter_string = f"rows={task_limit}" + else: + filter_string += f" rows={task_limit}" + cmd.add_filter(filter_string, filter_id) if trash is not None: diff --git a/tests/protocols/gmp/requests/v224/test_tasks.py b/tests/protocols/gmp/requests/v224/test_tasks.py index f14a7f2b3..07fa116fd 100644 --- a/tests/protocols/gmp/requests/v224/test_tasks.py +++ b/tests/protocols/gmp/requests/v224/test_tasks.py @@ -414,6 +414,20 @@ def test_get_tasks_with_filter_id(self): b'', ) + def test_get_tasks_with_task_limit(self): + request = Tasks().get_tasks(task_limit=5) + self.assertEqual( + bytes(request), + b'', + ) + + def test_get_tasks_with_task_limit_and_filter_string(self): + request = Tasks().get_tasks(filter_string="foo", task_limit=5) + self.assertEqual( + bytes(request), + b'', + ) + def test_get_tasks_with_trash(self): request = Tasks().get_tasks(trash=True) self.assertEqual( diff --git a/tests/protocols/gmpv224/entities/tasks/test_get_tasks.py b/tests/protocols/gmpv224/entities/tasks/test_get_tasks.py index 44247fc8f..963e8c9f4 100644 --- a/tests/protocols/gmpv224/entities/tasks/test_get_tasks.py +++ b/tests/protocols/gmpv224/entities/tasks/test_get_tasks.py @@ -26,6 +26,20 @@ def test_get_tasks_with_filter_id(self): b'' ) + def test_get_tasks_with_task_limit(self): + self.gmp.get_tasks(task_limit="5") + + self.connection.send.has_been_called_with( + b'' + ) + + def test_get_tasks_with_task_limit_and_filter_string(self): + self.gmp.get_tasks(filter_string="foo", task_limit="5") + + self.connection.send.has_been_called_with( + b'' + ) + def test_get_tasks_from_trash(self): self.gmp.get_tasks(trash=True)