Skip to content

Commit

Permalink
Merge pull request #554 from hoangpn/add-farm-rest-api
Browse files Browse the repository at this point in the history
[IMPROVEMENT] Implement retrieval "Farm" APIs
  • Loading branch information
vincent-olivert-riera authored Nov 20, 2024
2 parents 57cebc3 + 3eeaf4c commit 7f3f578
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions promgen/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ class RuleFilter(django_filters.rest_framework.FilterSet):
name = django_filters.CharFilter(field_name="name", lookup_expr="contains")
parent = django_filters.CharFilter(field_name="parent__name", lookup_expr="contains")
enabled = django_filters.BooleanFilter(field_name="enabled")


class FarmFilter(django_filters.rest_framework.FilterSet):
name = django_filters.CharFilter(field_name="name", lookup_expr="contains")
source = django_filters.CharFilter(field_name="source", lookup_expr="exact")
21 changes: 21 additions & 0 deletions promgen/rest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2019 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

from django.core.serializers import get_serializer
from django.http import HttpResponse
from django.views.generic import View
from rest_framework import permissions, viewsets
Expand Down Expand Up @@ -110,3 +111,23 @@ def targets(self, request, name):
prometheus.render_config(project=self.get_object()),
content_type="application/json",
)


class FarmViewSet(viewsets.ModelViewSet):
queryset = models.Farm.objects.all()
filterset_class = filters.FarmFilter
serializer_class = serializers.FarmSerializer
lookup_value_regex = "[^/]+"
lookup_field = "id"

def retrieve(self, request, id):
farm = self.get_object()
farm_data = self.get_serializer(farm).data

hosts = farm.host_set.all()
hosts_data = serializers.HostSerializer(hosts, many=True).data
farm_detail = {
**farm_data,
"hosts": hosts_data
}
return Response(farm_detail)
16 changes: 16 additions & 0 deletions promgen/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,19 @@ def to_representation(self, obj):
"labels": obj.labels,
"annotations": annotations,
}


class FarmSerializer(serializers.ModelSerializer):
url = WebLinkField()

class Meta:
model = models.Farm
fields = '__all__'


class HostSerializer(serializers.ModelSerializer):
url = WebLinkField()

class Meta:
model = models.Host
exclude = ("id", "farm")
12 changes: 12 additions & 0 deletions promgen/tests/examples/rest.farm.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": 1,
"url": "http://promgen.example.com/farm/1",
"name": "test-farm",
"source": "promgen",
"hosts": [
{
"name": "host.example.com",
"url": "http://promgen.example.com/host/host.example.com"
}
]
}
7 changes: 7 additions & 0 deletions promgen/tests/examples/rest.farm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"id": 1,
"url": "http://promgen.example.com/farm/1",
"name": "test-farm","source":"promgen"
}
]
39 changes: 39 additions & 0 deletions promgen/tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


from django.test import override_settings
from django.urls import reverse

from promgen import models, rest, tests

Expand All @@ -21,3 +22,41 @@ def test_alert(self):
response = self.fireAlert()
self.assertEqual(response.status_code, 202)
self.assertCount(models.Alert, 1, "Alert Queued")

@override_settings(PROMGEN=tests.SETTINGS)
def test_retrieve_farm(self):
expected = tests.Data("examples", "rest.farm.json").json()

# Check retrieving all farms
response = self.client.get(reverse("api:farm-list"))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), expected)

# Check retrieving all farms whose "name" contains "farm"
response = self.client.get(reverse("api:farm-list"), {"name": "farm"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), expected)

# Check retrieving all farms whose "source" is "promgen"
response = self.client.get(reverse("api:farm-list"), {"source": "promgen"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), expected)

# Check retrieving farms with a non-existent "name" returns an empty list
response = self.client.get(reverse("api:farm-list"), {"name": "other-name"})
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 0)

# Check retrieving farms with a non-existent "source" returns an empty list
response = self.client.get(reverse("api:farm-list"), {"source": "other-source"})
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 0)


farm = models.Farm.objects.get(id=1)
models.Host.objects.create(name="host.example.com", farm=farm)
expected = tests.Data("examples", "rest.farm.1.json").json()

# Check retrieving the farm whose "id" is "1", including the list of hosts.
response = self.client.get(reverse("api:farm-detail", args=[1]))
self.assertEqual(response.json(), expected)
1 change: 1 addition & 0 deletions promgen/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
router.register("service", rest.ServiceViewSet)
router.register("shard", rest.ShardViewSet)
router.register("project", rest.ProjectViewSet)
router.register("farm", rest.FarmViewSet)


urlpatterns = [
Expand Down

0 comments on commit 7f3f578

Please sign in to comment.