From ee3f4fb598bc51d98ce3755e068afe6c87146e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 17 Mar 2018 17:50:03 +0000 Subject: [PATCH] API: Generic services views --- api/serializers.py | 34 ++++++++++++++++-- api/urls.py | 7 ++++ api/views.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index a526d463..8175c508 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -18,7 +18,37 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -"""api.serializers - +""" Serializers for the API app """ + +from rest_framework import serializers +from machines.models import Service_link + + +class ServiceLinkSerializer(serializers.ModelSerializer): + name = serializers.CharField(source='service.service_type') + + class Meta: + model = Service_link + fields = ('name',) + + +class ServicesSerializer(serializers.ModelSerializer): + """Evaluation d'un Service, et serialisation""" + server = serializers.SerializerMethodField('get_server_name') + service = serializers.SerializerMethodField('get_service_name') + need_regen = serializers.SerializerMethodField('get_regen_status') + + class Meta: + model = Service_link + fields = ('server', 'service', 'need_regen') + + def get_server_name(self, obj): + return str(obj.server.domain.name) + + def get_service_name(self, obj): + return str(obj.service) + + def get_regen_status(self, obj): + return obj.need_regen() diff --git a/api/urls.py b/api/urls.py index 9bac173e..7420e109 100644 --- a/api/urls.py +++ b/api/urls.py @@ -26,5 +26,12 @@ from __future__ import unicode_literals from django.conf.urls import url +from . import views + + urlpatterns = [ + # Services + url(r'^services/$', views.services), + url(r'^services/(?P\w+)/(?P\w+)/regen/$', views.services_server_service_regen), + url(r'^services/(?P\w+)/$', views.services_server), ] diff --git a/api/views.py b/api/views.py index 12864394..71471157 100644 --- a/api/views.py +++ b/api/views.py @@ -23,3 +23,89 @@ The views for the API app. They should all return JSON data and not fallback on HTML pages such as the login and index pages for a better integration. """ + +from django.contrib.auth.decorators import login_required, permission_required +from django.views.decorators.csrf import csrf_exempt + +from machines.models import Service_link, Service, Interface, Domain + +from .serializers import * +from .utils import JSONError, JSONSuccess, accept_method + + +@csrf_exempt +@login_required +@permission_required('machines.serveur') +@accept_method(['GET']) +def services(request): + """The list of the different services and servers couples + + Return: + GET: + A JSONSuccess response with a field `data` containing: + * a list of dictionnaries (one for each service-server couple) containing: + * a field `server`: the server name + * a field `service`: the service name + * a field `need_regen`: does the service need a regeneration ? + """ + service_link = Service_link.objects.all().select_related('server__domain').select_related('service') + seria = ServicesSerializer(service_link, many=True) + return JSONSuccess(seria.data) + +@csrf_exempt +@login_required +@permission_required('machines.serveur') +@accept_method(['GET', 'POST']) +def services_server_service_regen(request, server_name, service_name): + """The status of a particular service linked to a particular server. + Mark the service as regenerated if POST used. + + Returns: + GET: + A JSONSucess response with a field `data` containing: + * a field `need_regen`: does the service need a regeneration ? + + POST: + An empty JSONSuccess response. + """ + query = Service_link.objects.filter( + service__in=Service.objects.filter(service_type=service_name), + server__in=Interface.objects.filter( + domain__in=Domain.objects.filter(name=server_name) + ) + ) + if not query: + return JSONError("This service is not active for this server") + + service = query.first() + if request.method == 'GET': + return JSONSuccess({'need_regen': service.need_regen()}) + else: + service.done_regen() + return JSONSuccess() + + +@csrf_exempt +@login_required +@permission_required('machines.serveur') +@accept_method(['GET']) +def services_server(request, server_name): + """The list of services attached to a specific server + + Returns: + GET: + A JSONSuccess response with a field `data` containing: + * a list of dictionnaries (one for each service) containing: + * a field `name`: the name of a service + """ + query = Service_link.objects.filter( + server__in=Interface.objects.filter( + domain__in=Domain.objects.filter(name=server_name) + ) + ) + if not query: + return JSONError("This service is not active for this server") + + services = query.all() + seria = ServiceLinkSerializer(services, many=True) + return JSONSuccess(seria.data)