Skip to content

Commit

Permalink
feat: Rename args to source_args (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsong-rh authored Feb 12, 2024
1 parent 461e156 commit 816c5b4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
15 changes: 8 additions & 7 deletions src/aap_eda/api/serializers/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _updated_listener_ruleset(validated_data):
{
"name": validated_data["name"],
"type": validated_data["source_type"],
"args": validated_data["args"],
"args": validated_data["source_args"],
}
]
return swap_sources(validated_data["rulebook_rulesets"], sources_info)
Expand All @@ -104,11 +104,12 @@ def to_internal_value(self, data) -> dict:
try:
parsed_args = yaml.safe_load(data)
except yaml.YAMLError:
raise ValidationError("Invalid YAML format for 'args'")
raise ValidationError("Invalid YAML format for 'source_args'")

if not isinstance(parsed_args, dict):
raise ValidationError(
"The 'args' field must be a YAML object (dictionary)"
"The 'source_args' field must be a YAML "
"object (dictionary)"
)

return parsed_args
Expand All @@ -123,7 +124,7 @@ class EventStreamSerializer(serializers.ModelSerializer):
validators=[validators.check_if_de_exists]
)
user = serializers.SerializerMethodField()
args = YAMLSerializerField(required=False, allow_null=True)
source_args = YAMLSerializerField(required=False, allow_null=True)
credentials = serializers.SerializerMethodField()

class Meta:
Expand All @@ -135,7 +136,7 @@ class Meta:
]
fields = [
"name",
"args",
"source_args",
"source_type",
"channel_name",
"is_enabled",
Expand Down Expand Up @@ -164,7 +165,7 @@ class EventStreamCreateSerializer(serializers.ModelSerializer):
validators=[validators.check_if_de_exists]
)
user = serializers.HiddenField(default=serializers.CurrentUserDefault())
args = YAMLSerializerField()
source_args = YAMLSerializerField()
channel_name = serializers.CharField(
default=_get_default_channel_name(),
validators=[
Expand All @@ -188,7 +189,7 @@ class Meta:
"description",
"is_enabled",
"source_type",
"args",
"source_args",
"channel_name",
"decision_environment_id",
"rulebook_id",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.7 on 2024-02-12 19:40

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("core", "0024_remove_eventstream_listener_args_and_more"),
]

operations = [
migrations.RenameField(
model_name="eventstream",
old_name="args",
new_name="source_args",
),
]
2 changes: 1 addition & 1 deletion src/aap_eda/core/models/event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class EventStream(StatusHandlerModelMixin, models.Model):
)
channel_name = models.TextField(null=True, default=None)
source_type = models.TextField(null=False)
args = models.JSONField(null=True, default=None)
source_args = models.JSONField(null=True, default=None)
system_vault_credential = models.OneToOneField(
"Credential",
null=True,
Expand Down
30 changes: 15 additions & 15 deletions tests/integration/api/test_event_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ def test_list_event_streams(
models.EventStream(
name="test-event_stream-1",
source_type="ansible.eda.range",
args={"limit": 5, "delay": 1},
source_args={"limit": 5, "delay": 1},
user=default_user,
decision_environment_id=default_de.id,
),
models.EventStream(
name="test-event_stream-2",
source_type="ansible.eda.range",
args={"limit": 6, "delay": 2},
source_args={"limit": 6, "delay": 2},
user=default_user,
decision_environment_id=default_de.id,
),
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_retrieve_event_stream(
event_stream = models.EventStream.objects.create(
name="test-event_stream-1",
source_type="ansible.eda.range",
args=args,
source_args=args,
user=default_user,
decision_environment_id=default_de.id,
)
Expand All @@ -204,7 +204,7 @@ def test_retrieve_event_stream(
assert response.status_code == status.HTTP_200_OK
assert response.data["name"] == event_stream.name
assert response.data["source_type"] == event_stream.source_type
assert yaml.safe_load(response.data["args"]) == args
assert yaml.safe_load(response.data["source_args"]) == args
assert response.data["user"] == "luke.skywalker"
assert len(response.data["credentials"]) == 2
assert response.data["credentials"][0]["name"] == credentials[0].name
Expand All @@ -230,7 +230,7 @@ def test_create_event_stream(
data_in = {
"name": "test_event_stream",
"source_type": f"{source_type}",
"args": f"{args}",
"source_args": f"{args}",
"decision_environment_id": default_de.id,
}
response = client.post(f"{api_url_v1}/event-streams/", data=data_in)
Expand All @@ -239,7 +239,7 @@ def test_create_event_stream(
assert result["name"] == "test_event_stream"
assert result["source_type"] == source_type
assert result["user"] == "test.admin"
assert yaml.safe_load(response.data["args"]) == args
assert yaml.safe_load(response.data["source_args"]) == args

event_stream = models.EventStream.objects.first()
rulesets = yaml.safe_load(event_stream.rulebook_rulesets)
Expand All @@ -265,7 +265,7 @@ def test_create_event_stream_with_credential(
data_in = {
"name": "test_event_stream",
"source_type": "ansible.eda.range",
"args": f"{args}",
"source_args": f"{args}",
"decision_environment_id": default_de.id,
"credentials": [credential.id],
}
Expand All @@ -275,7 +275,7 @@ def test_create_event_stream_with_credential(
assert result["name"] == "test_event_stream"
assert result["source_type"] == "ansible.eda.range"
assert result["credentials"][0]["name"] == credential.name
assert yaml.safe_load(response.data["args"]) == args
assert yaml.safe_load(response.data["source_args"]) == args


@pytest.mark.parametrize(
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_create_event_stream_with_bad_rulebook(
data_in = {
"name": "test_event_stream",
"source_type": "ansible.eda.range",
"args": f"{args}",
"source_args": f"{args}",
"decision_environment_id": default_de.id,
}
response = client.post(f"{api_url_v1}/event-streams/", data=data_in)
Expand All @@ -327,7 +327,7 @@ def test_create_event_stream_bad_channel_name(
data_in = {
"name": "test_event_stream",
"source_type": "ansible.eda.range",
"args": f"{args}",
"source_args": f"{args}",
"channel_name": "abc-def",
"decision_environment_id": default_de.id,
}
Expand All @@ -348,15 +348,15 @@ def test_create_event_stream_bad_args(
data_in = {
"name": "test_event_stream",
"source_type": "ansible.eda.range",
"args": "gobbledegook",
"source_args": "gobbledegook",
"decision_environment_id": default_de.id,
}
response = client.post(f"{api_url_v1}/event-streams/", data=data_in)
assert response.status_code == status.HTTP_400_BAD_REQUEST
result = response.data
assert (
str(result["args"][0])
== "The 'args' field must be a YAML object (dictionary)"
str(result["source_args"][0])
== "The 'source_args' field must be a YAML object (dictionary)"
)


Expand All @@ -372,7 +372,7 @@ def test_create_event_stream_empty_args(
}
response = client.post(f"{api_url_v1}/event-streams/", data=data_in)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.data["args"][0] == "This field is required."
assert response.data["source_args"][0] == "This field is required."


@pytest.mark.django_db
Expand Down Expand Up @@ -415,7 +415,7 @@ def test_list_event_stream_instances(
event_stream = models.EventStream.objects.create(
name="test-event_stream-1",
source_type="ansible.eda.range",
args=args,
source_args=args,
user=default_user,
decision_environment_id=default_de.id,
)
Expand Down

0 comments on commit 816c5b4

Please sign in to comment.