Skip to content

Commit

Permalink
feat: Add CRUD log messages (#1005)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkayyali812 authored Aug 15, 2024
1 parent 7139ddd commit 86dbe2c
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ __pycache__/
Taskfile.yml
Taskfile.yaml
/tools/deploy/environment.properties

venv/
71 changes: 71 additions & 0 deletions src/aap_eda/api/views/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from aap_eda.api.serializers.activation import is_activation_valid
from aap_eda.core import models
from aap_eda.core.enums import Action, ActivationStatus, ProcessParentType
from aap_eda.core.utils import logging_utils
from aap_eda.tasks.orchestrator import (
delete_rulebook_process,
restart_rulebook_process,
Expand All @@ -41,6 +42,8 @@

logger = logging.getLogger(__name__)

resource_name = "RulebookActivation"


class ActivationViewSet(
mixins.DestroyModelMixin,
Expand Down Expand Up @@ -94,6 +97,16 @@ def create(self, request):
process_parent_id=response.id,
)

logger.info(
logging_utils.generate_simple_audit_log(
"Create",
resource_name,
response.name,
response.id,
response.organization,
)
)

return Response(
serializers.ActivationReadSerializer(response).data,
status=status.HTTP_201_CREATED,
Expand All @@ -117,13 +130,23 @@ def destroy(self, request, *args, **kwargs):
"deleted.",
)

audit_log = logging_utils.generate_simple_audit_log(
"Delete",
resource_name,
activation.name,
activation.id,
activation.organization,
)

activation.status = ActivationStatus.DELETING
activation.save(update_fields=["status"])
logger.info(f"Now deleting {activation.name} ...")

delete_rulebook_process(
process_parent_type=ProcessParentType.ACTIVATION,
process_parent_id=activation.id,
)
logger.info(audit_log)

return Response(status=status.HTTP_204_NO_CONTENT)

Expand All @@ -132,6 +155,17 @@ def destroy(self, request, *args, **kwargs):
)
def retrieve(self, request, pk: int):
activation = self.get_object()

logger.info(
logging_utils.generate_simple_audit_log(
"Read",
resource_name,
activation.name,
activation.id,
activation.organization,
)
)

return Response(serializers.ActivationReadSerializer(activation).data)

@extend_schema(
Expand All @@ -152,6 +186,15 @@ def list(self, request):
)
result = self.paginate_queryset(serializer.data)

logger.info(
logging_utils.generate_simple_audit_log(
"ListActivations",
resource_name,
"*",
"*",
"*",
)
)
return self.get_paginated_response(result)

@extend_schema(
Expand Down Expand Up @@ -263,6 +306,16 @@ def enable(self, request, pk):
process_parent_id=pk,
)

logger.info(
logging_utils.generate_simple_audit_log(
"Enable",
resource_name,
activation.name,
activation.id,
activation.organization,
)
)

return Response(status=status.HTTP_204_NO_CONTENT)

@extend_schema(
Expand Down Expand Up @@ -292,6 +345,15 @@ def disable(self, request, pk):
process_parent_id=activation.id,
)

logger.info(
logging_utils.generate_simple_audit_log(
"Disable",
resource_name,
activation.name,
activation.id,
activation.organization,
)
)
return Response(status=status.HTTP_204_NO_CONTENT)

@extend_schema(
Expand Down Expand Up @@ -335,6 +397,15 @@ def restart(self, request, pk):
process_parent_id=activation.id,
)

logger.info(
logging_utils.generate_simple_audit_log(
"Restart",
resource_name,
activation.name,
activation.id,
activation.organization,
)
)
return Response(status=status.HTTP_204_NO_CONTENT)

def _check_deleting(self, activation):
Expand Down
64 changes: 62 additions & 2 deletions src/aap_eda/api/views/decision_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from django_filters import rest_framework as defaultfilters
from drf_spectacular.utils import (
Expand All @@ -24,6 +25,7 @@

from aap_eda.api import exceptions as api_exc, filters, serializers
from aap_eda.core import models
from aap_eda.core.utils import logging_utils
from aap_eda.utils import str_to_bool

from .mixins import (
Expand All @@ -32,6 +34,44 @@
ResponseSerializerMixin,
)

logger = logging.getLogger(__name__)

resource_name = "DecisionEnvironment"


class CreateDecisionEnvironmentMixin(CreateModelMixin):
def create(self, request, *args, **kwargs):
response = super().create(request, *args, **kwargs)

logger.info(
logging_utils.generate_simple_audit_log(
"Create",
resource_name,
response.data["name"],
response.data["id"],
logging_utils.get_organization_name_from_data(response),
)
)

return response


class PartialUpdateOnlyDecisionEnvironmentMixin(PartialUpdateOnlyModelMixin):
def partial_update(self, request, *args, **kwargs):
response = super().partial_update(request, *args, **kwargs)

logger.info(
logging_utils.generate_simple_audit_log(
"Update",
resource_name,
response.data["name"],
response.data["id"],
logging_utils.get_organization_name_from_data(response),
)
)

return response


@extend_schema_view(
list=extend_schema(
Expand Down Expand Up @@ -66,8 +106,8 @@
)
class DecisionEnvironmentViewSet(
ResponseSerializerMixin,
CreateModelMixin,
PartialUpdateOnlyModelMixin,
CreateDecisionEnvironmentMixin,
PartialUpdateOnlyDecisionEnvironmentMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
Expand Down Expand Up @@ -116,6 +156,16 @@ def retrieve(self, request, pk):
else None
)

logger.info(
logging_utils.generate_simple_audit_log(
"Read",
resource_name,
decision_environment.data["name"],
decision_environment.data["id"],
decision_environment.data["organization"],
)
)

return Response(
serializers.DecisionEnvironmentReadSerializer(
decision_environment.data
Expand Down Expand Up @@ -159,4 +209,14 @@ def destroy(self, request, *args, **kwargs):
)

self.perform_destroy(instance)

logger.info(
logging_utils.generate_simple_audit_log(
"Delete",
resource_name,
instance.name,
instance.id,
instance.organization,
)
)
return Response(status=status.HTTP_204_NO_CONTENT)
69 changes: 68 additions & 1 deletion src/aap_eda/api/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from ansible_base.rbac.api.related import check_related_permissions
from ansible_base.rbac.models import RoleDefinition
from django.db import transaction
Expand All @@ -29,9 +31,33 @@
from aap_eda.api import exceptions as api_exc, filters, serializers
from aap_eda.core import models
from aap_eda.core.enums import Action
from aap_eda.core.utils import logging_utils

from .mixins import ResponseSerializerMixin

logger = logging.getLogger(__name__)

resource_name = "Project"


class DestroyProjectMixin(mixins.DestroyModelMixin):
def destroy(self, request, *args, **kwargs):
project = self.get_object()

response = super().destroy(request, *args, **kwargs)

logger.info(
logging_utils.generate_simple_audit_log(
"Delete",
resource_name,
project.name,
project.id,
project.organization,
)
)

return response


@extend_schema_view(
list=extend_schema(
Expand All @@ -57,7 +83,7 @@ class ProjectViewSet(
ResponseSerializerMixin,
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
DestroyProjectMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet,
):
Expand Down Expand Up @@ -109,6 +135,17 @@ def create(self, request):
project.import_task_id = job.id
serializer = self.get_serializer(project)
headers = self.get_success_headers(serializer.data)

logger.info(
logging_utils.generate_simple_audit_log(
"Create",
resource_name,
project.name,
project.id,
project.organization,
)
)

return Response(
serializer.data, status=status.HTTP_201_CREATED, headers=headers
)
Expand Down Expand Up @@ -144,6 +181,16 @@ def retrieve(self, request, pk):
else None
)

logger.info(
logging_utils.generate_simple_audit_log(
"Read",
resource_name,
project.data["name"],
project.data["id"],
project.data["organization"].name,
)
)

return Response(serializers.ProjectReadSerializer(project.data).data)

@extend_schema(
Expand Down Expand Up @@ -186,6 +233,16 @@ def partial_update(self, request, pk):
model_to_dict(project),
)

logger.info(
logging_utils.generate_simple_audit_log(
"Update",
resource_name,
project.name,
project.id,
project.organization,
)
)

return Response(serializers.ProjectSerializer(project).data)

@extend_schema(
Expand Down Expand Up @@ -228,5 +285,15 @@ def sync(self, request, pk):
project.import_error = None
project.save()

logger.info(
logging_utils.generate_simple_audit_log(
"Sync",
resource_name,
project.name,
project.id,
project.organization,
)
)

serializer = serializers.ProjectSerializer(project)
return Response(status=status.HTTP_202_ACCEPTED, data=serializer.data)
Loading

0 comments on commit 86dbe2c

Please sign in to comment.