From 7028788cf221bfd0843959d0e08ca2d76366a008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 17 Mar 2018 18:05:41 +0000 Subject: [PATCH] API: Add support for DHCP --- api/serializers.py | 28 +++++++++++++++++++++++++++- api/urls.py | 3 +++ api/views.py | 26 +++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 2f7690d6..5cbf9438 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -24,7 +24,10 @@ Serializers for the API app from rest_framework import serializers from users.models import Club, Adherent -from machines.models import Service_link +from machines.models import ( + Interface, + Service_link, +) class ServiceLinkSerializer(serializers.ModelSerializer): @@ -49,6 +52,29 @@ class MailingMemberSerializer(serializers.ModelSerializer): fields = ('email', 'name', 'surname', 'pseudo',) +class InterfaceSerializer(serializers.ModelSerializer): + """Serialisation d'une interface, ipv4, domain et extension sont + des foreign_key, on les override et on les evalue avec des fonctions + get_...""" + ipv4 = IpListSerializer(read_only=True) + mac_address = serializers.SerializerMethodField('get_macaddress') + domain = serializers.SerializerMethodField('get_dns') + extension = serializers.SerializerMethodField('get_interface_extension') + + class Meta: + model = Interface + fields = ('ipv4', 'mac_address', 'domain', 'extension') + + def get_dns(self, obj): + return obj.domain.name + + def get_interface_extension(self, obj): + return obj.domain.extension.name + + def get_macaddress(self, obj): + return str(obj.mac_address) + + class ServicesSerializer(serializers.ModelSerializer): """Evaluation d'un Service, et serialisation""" server = serializers.SerializerMethodField('get_server_name') diff --git a/api/urls.py b/api/urls.py index bf76a3ee..ab8a5bed 100644 --- a/api/urls.py +++ b/api/urls.py @@ -35,6 +35,9 @@ urlpatterns = [ url(r'^services/(?P\w+)/(?P\w+)/regen/$', views.services_server_service_regen), url(r'^services/(?P\w+)/$', views.services_server), + # DHCP + url(r'^dhcp/mac-ip/$', views.dhcp_mac_ip), + # Mailings url(r'^mailing/standard/$', views.mailing_standard), url(r'^mailing/standard/(?P\w+)/members/$', views.mailing_standard_ml_members), diff --git a/api/views.py b/api/views.py index 2dac2719..97c60824 100644 --- a/api/views.py +++ b/api/views.py @@ -27,7 +27,7 @@ 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 re2o.utils import all_has_access +from re2o.utils import all_has_access, all_active_assigned_interfaces from users.models import Club from machines.models import Service_link, Service, Interface, Domain @@ -114,6 +114,30 @@ def services_server(request, server_name): return JSONSuccess(seria.data) +@csrf_exempt +@login_required +@permission_required('machines.serveur') +@accept_method(['GET']) +def dhcp_mac_ip(request): + """The list of all active interfaces with all the associated infos + (MAC, IP, IpType, DNS name and associated zone extension) + + Returns: + GET: + A JSON Success response with a field `data` containing: + * a list of dictionnaries (one for each interface) containing: + * a field `ipv4` containing: + * a field `ipv4`: the ip for this interface + * a field `ip_type`: the name of the IpType of this interface + * a field `mac_address`: the MAC of this interface + * a field `domain`: the DNS name for this interface + * a field `extension`: the extension for the DNS zone of this interface + """ + interfaces = all_active_assigned_interfaces() + seria = InterfaceSerializer(interfaces, many=True) + return JSONSuccess(seria.data) + + @csrf_exempt @login_required @permission_required('machines.serveur')