diff --git a/promgen/rest.py b/promgen/rest.py index 12854da07..c773061c2 100644 --- a/promgen/rest.py +++ b/promgen/rest.py @@ -1,7 +1,7 @@ # Copyright (c) 2019 LINE Corporation # These sources are released under the terms of the MIT license: see LICENSE -from rest_framework import viewsets +from rest_framework import permissions, viewsets from rest_framework.decorators import action from rest_framework.response import Response @@ -10,6 +10,18 @@ from promgen import filters, models, prometheus, renderers, serializers +class AllViewSet(viewsets.ViewSet): + permission_classes = [permissions.AllowAny] + + @action(detail=False, methods=["get"], renderer_classes=[renderers.RuleRenderer]) + def rules(self, request): + rules = models.Rule.objects.filter(enabled=True) + return Response( + serializers.AlertRuleSerializer(rules, many=True).data, + headers={"Content-Disposition": "attachment; filename=alert.rule.yml"}, + ) + + class ShardViewSet(viewsets.ModelViewSet): queryset = models.Shard.objects.all() filterset_class = filters.ShardFilter diff --git a/promgen/templates/promgen/navbar.html b/promgen/templates/promgen/navbar.html index 98c612767..e3f460ccf 100644 --- a/promgen/templates/promgen/navbar.html +++ b/promgen/templates/promgen/navbar.html @@ -35,7 +35,7 @@
  • API
  • Export Targets
  • -
  • Export Rules
  • +
  • Export Rules
  • Export URL
  • {% if EXTERNAL_LINKS %} diff --git a/promgen/urls.py b/promgen/urls.py index 90b4a3bfc..d006354d5 100644 --- a/promgen/urls.py +++ b/promgen/urls.py @@ -24,9 +24,10 @@ from rest_framework import routers router = routers.DefaultRouter() -router.register('service', rest.ServiceViewSet) -router.register('shard', rest.ShardViewSet) -router.register('project', rest.ProjectViewSet) +router.register("all", rest.AllViewSet, basename="all") +router.register("service", rest.ServiceViewSet) +router.register("shard", rest.ShardViewSet) +router.register("project", rest.ProjectViewSet) urlpatterns = [ @@ -104,14 +105,12 @@ url('', include('django.contrib.auth.urls')), url('', include('social_django.urls', namespace='social')), - # Public API - # Legacy API path('api/v1/config', csrf_exempt(views.ApiConfig.as_view())), + path("api/", include((router.urls, "api"), namespace="old-api")), + path('api/v1/rules', csrf_exempt(views.RulesConfig.as_view())), - # Promgen API path('api/v1/targets', csrf_exempt(views.ApiConfig.as_view()), name='config-targets'), - path('api/v1/rules', csrf_exempt(views.RulesConfig.as_view()), name='config-rules'), path('api/v1/urls', csrf_exempt(views.URLConfig.as_view()), name='config-urls'), path('api/v1/alerts', csrf_exempt(views.Alert.as_view()), name='alert'), path('api/v1/host/', views.HostDetail.as_view()), @@ -131,7 +130,8 @@ path("proxy/v1/silences", csrf_exempt(proxy.ProxySilences.as_view()), name="proxy-silence"), path("proxy/v1/silences/", csrf_exempt(proxy.ProxyDeleteSilence.as_view()), name="proxy-silence-delete"), - path("api/", include((router.urls, "api"), namespace="api")), + # Promgen rest API + path("rest/", include((router.urls, "api"), namespace="api")), ] try: