mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-29 06:44:21 +00:00
feat/PRWLR-4177 Add /tasks endpoints and data model (#35)
* feat(Backend): PRWLR-4177 add Task model and migrations * feat(Tasks): PRWLR-4177 add RLSTask class * feat(API): PRWLR-4177 add Task serializers * feat(Backend, DB): PRWLR-4177 refactor db variables and add policy on task runner tasks * feat(API): PRWLR-4177 add Tasks filters and sort fields * feat(API, Tasks): PRWLR-4177 add deletion tasks and revoke logic to /tasks * test(Task): PRWLR-4177 add deletion tasks unit tests * test(Views): PRWLR-4177 add Tasks views unit tests and update outdated ones * chore(API): PRWLR-4177 improve drf-spectacular annotations * chore(API): PRWLR-4177 add PROGRESS task state * chore(API): PRWLR-4177 update spec * chore(API): PRWLR-4177 remove force query parameter from DELETE /tasks * feat(Backend): PRWLR-4177 add APITimeoutError and raise when TaskResult is not created * feat(Backend): PRWLR-4177 add specific error class for task timeouts
This commit is contained in:
1 parent
f5462c9b27
commit
ec67fc12e0
15 files changed
+1426
-161
No files matched your search
@@ -0,0 +1,25 @@
|
||||
from celery.utils.log import get_task_logger
|
||||
|
||||
logger = get_task_logger(__name__)
|
||||
|
||||
|
||||
def delete_instance(model, pk: str):
|
||||
"""
|
||||
Deletes an instance of the specified model.
|
||||
|
||||
This function retrieves an instance of the provided model using its primary key
|
||||
and deletes it from the database.
|
||||
|
||||
Args:
|
||||
model (Model): The Django model class from which to delete an instance.
|
||||
pk (str): The primary key of the instance to delete.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the number of objects deleted and a dictionary
|
||||
with the count of deleted objects per model,
|
||||
including related models if applicable.
|
||||
|
||||
Raises:
|
||||
model.DoesNotExist: If no instance with the provided primary key exists.
|
||||
"""
|
||||
return model.objects.get(pk=pk).delete()
|
||||
@@ -1,10 +1,13 @@
|
||||
from celery import shared_task
|
||||
|
||||
from api.decorators import set_tenant
|
||||
from api.models import Provider
|
||||
from config.celery import RLSTask
|
||||
from tasks.jobs.connection import check_provider_connection
|
||||
from tasks.jobs.deletion import delete_instance
|
||||
|
||||
|
||||
@shared_task(name="provider-connection-check")
|
||||
@shared_task(base=RLSTask, name="provider-connection-check")
|
||||
@set_tenant
|
||||
def check_provider_connection_task(provider_id: str):
|
||||
"""
|
||||
@@ -19,3 +22,21 @@ def check_provider_connection_task(provider_id: str):
|
||||
- 'error' (str or None): The error message if the connection failed, otherwise `None`.
|
||||
"""
|
||||
return check_provider_connection(provider_id)
|
||||
|
||||
|
||||
@shared_task(base=RLSTask, name="provider-deletion")
|
||||
@set_tenant
|
||||
def delete_provider_task(provider_id: str):
|
||||
"""
|
||||
Task to delete a specific Provider instance.
|
||||
|
||||
Args:
|
||||
provider_id (str): The primary key of the `Provider` instance to be deleted.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing:
|
||||
- The number of instances deleted.
|
||||
- A dictionary with the count of deleted instances per model,
|
||||
including related models if cascading deletes were triggered.
|
||||
"""
|
||||
return delete_instance(model=Provider, pk=provider_id)
|
||||
@@ -0,0 +1,22 @@
|
||||
import pytest
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from api.models import Provider
|
||||
from tasks.jobs.deletion import delete_instance
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestDeleteInstance:
|
||||
def test_delete_instance_success(self, providers_fixture):
|
||||
instance = providers_fixture[0]
|
||||
result = delete_instance(Provider, instance.id)
|
||||
|
||||
assert result
|
||||
with pytest.raises(ObjectDoesNotExist):
|
||||
Provider.objects.get(pk=instance.id)
|
||||
|
||||
def test_delete_instance_does_not_exist(self):
|
||||
non_existent_pk = "babf6796-cfcc-4fd3-9dcf-88d012247645"
|
||||
|
||||
with pytest.raises(ObjectDoesNotExist):
|
||||
delete_instance(Provider, non_existent_pk)
|
||||
Reference in new issue
Block a user