Skip to content

Commit

Permalink
feat: add edited_at field to activation
Browse files Browse the repository at this point in the history
Expose this field in API for activations. Also write the field
to rulebook process log if the activation has been edited.

implements AAP-38382
  • Loading branch information
bzwei committed Jan 24, 2025
1 parent 542cd81 commit 4d70d4c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/aap_eda/api/serializers/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import yaml
from django.conf import settings
from django.utils import timezone
from rest_framework import serializers

from aap_eda.api.constants import (
Expand Down Expand Up @@ -254,6 +255,7 @@ class Meta:
"current_job_id",
"created_at",
"modified_at",
"edited_at",
"status_message",
"awx_token_id",
"eda_credentials",
Expand All @@ -265,6 +267,7 @@ class Meta:
"id",
"created_at",
"modified_at",
"edited_at",
"rulebook_name",
]

Expand Down Expand Up @@ -315,6 +318,7 @@ class Meta:
"rules_fired_count",
"created_at",
"modified_at",
"edited_at",
"created_by",
"modified_by",
"status_message",
Expand Down Expand Up @@ -365,6 +369,7 @@ def to_representation(self, activation):
"rules_fired_count": rules_fired_count,
"created_at": activation.created_at,
"modified_at": activation.modified_at,
"edited_at": activation.edited_at,
"status_message": activation.status_message,
"awx_token_id": activation.awx_token_id,
"log_level": activation.log_level,
Expand Down Expand Up @@ -617,6 +622,7 @@ def validate(self, data):
def prepare_update(self, activation: models.Activation):
rulebook_id = self.validated_data.get("rulebook_id")
self.validated_data["user_id"] = self.context["request"].user.id
self.validated_data["edited_at"] = timezone.now()
if rulebook_id:
rulebook = models.Rulebook.objects.get(id=rulebook_id)
self.validated_data["rulebook_name"] = rulebook.name
Expand Down Expand Up @@ -818,6 +824,7 @@ class Meta:
"rules_fired_count",
"created_at",
"modified_at",
"edited_at",
"created_by",
"modified_by",
"restarted_at",
Expand All @@ -830,7 +837,13 @@ class Meta:
"source_mappings",
"skip_audit_events",
]
read_only_fields = ["id", "created_at", "modified_at", "restarted_at"]
read_only_fields = [
"id",
"created_at",
"modified_at",
"edited_at",
"restarted_at",
]

def to_representation(self, activation):
decision_environment = (
Expand Down Expand Up @@ -915,6 +928,7 @@ def to_representation(self, activation):
"rules_fired_count": rules_fired_count,
"created_at": activation.created_at,
"modified_at": activation.modified_at,
"edited_at": activation.edited_at,
"restarted_at": restarted_at,
"status_message": activation.status_message,
"awx_token_id": activation.awx_token_id,
Expand Down Expand Up @@ -977,6 +991,7 @@ class Meta:
"user_id",
"created_at",
"modified_at",
"edited_at",
"awx_token_id",
"rulebook_id",
"eda_credentials",
Expand All @@ -988,6 +1003,7 @@ class Meta:
"id",
"created_at",
"modified_at",
"edited_at",
]


Expand Down
17 changes: 17 additions & 0 deletions src/aap_eda/core/migrations/0057_activation_edited_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.16 on 2025-01-23 19:06

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0056_alter_activation_options"),
]

operations = [
migrations.AddField(
model_name="activation",
name="edited_at",
field=models.DateTimeField(null=True),
),
]
1 change: 1 addition & 0 deletions src/aap_eda/core/models/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Meta:
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
created_at = models.DateTimeField(auto_now_add=True, null=False)
modified_at = models.DateTimeField(auto_now=True, null=False)
edited_at = models.DateTimeField(null=True)
status_updated_at = models.DateTimeField(null=True)
status_message = models.TextField(null=True, default=None)
latest_instance = models.OneToOneField(
Expand Down
7 changes: 7 additions & 0 deletions src/aap_eda/services/activation/activation_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ def _start_activation_instance(self):

self.db_instance.refresh_from_db()
log_handler = self.container_logger_class(self.latest_instance.id)
if self.db_instance.edited_at:
edited_at = self.db_instance.edited_at.strftime(
"%Y-%m-%dT%H:%M:%S.%fZ"
)
log_handler.write(
f"The activation was edited at {edited_at}", flush=True
)

LOGGER.info(
"Starting container for activation instance: "
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/api/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,8 +1115,10 @@ def test_update_activation(
)
assert response.status_code == status.HTTP_200_OK
data = response.data
assert data["edited_at"] is not None
activation = models.Activation.objects.filter(id=data["id"]).first()
assert activation.name == "another_name"
assert activation.edited_at is not None


@pytest.mark.django_db
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/services/activation/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ def test_start_first_run(
)
assert rulebook_process_queue.queue_name == job_mock.origin

logs = models.RulebookProcessLog.objects.filter(
activation_instance=basic_activation.latest_instance
).all()
assert not any("The activation was edited at" in log.log for log in logs)


@pytest.mark.django_db
def test_monitor_to_running_status(
Expand Down Expand Up @@ -423,6 +428,8 @@ def test_start_restart(
preseed_credential_types,
):
"""Test start verb for a restarted activation."""
running_activation.edited_at = timezone.now()
running_activation.save(update_fields=["edited_at"])
activation_manager = ActivationManager(
db_instance=running_activation,
container_engine=container_engine_mock,
Expand All @@ -449,6 +456,10 @@ def test_start_restart(
process=running_activation.latest_instance,
)
assert rulebook_process_queue.queue_name == job_mock.origin
logs = models.RulebookProcessLog.objects.filter(
activation_instance=running_activation.latest_instance
).all()
assert any("The activation was edited at" in log.log for log in logs)


@pytest.mark.django_db
Expand Down

0 comments on commit 4d70d4c

Please sign in to comment.