From f5267eae6c4c1a6dd635904b7e65f41c2fd1bdef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Thu, 24 May 2018 23:07:23 +0000 Subject: [PATCH] Add DHCP_hostmacip API view --- api/serializers.py | 13 +++++++++++++ api/urls.py | 1 + api/views.py | 12 +++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/api/serializers.py b/api/serializers.py index fe01bf02..0620d9cc 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -457,3 +457,16 @@ class WhitelistSerializer(NamespacedHMSerializer): class Meta: model = users.Whitelist fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url') + + +# DHCP + + +class HostMacIpSerializer(serializers.ModelSerializer): + hostname = serializers.CharField(source='domain.name', read_only=True) + extension = serializers.CharField(source='domain.extension.name', read_only=True) + ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True) + + class Meta: + model = machines.Interface + fields = ('hostname', 'extension', 'mac_address', 'ipv4') diff --git a/api/urls.py b/api/urls.py index 03e5026b..cc73ab2c 100644 --- a/api/urls.py +++ b/api/urls.py @@ -89,5 +89,6 @@ router.register(r'users/whitelists', views.WhitelistViewSet) urlpatterns = [ url(r'^', include(router.urls)), + url(r'^dhcp/hostmacip', views.HostMacIpView.as_view()), url(r'^token-auth/', views.ObtainExpiringAuthToken.as_view()) ] diff --git a/api/views.py b/api/views.py index ba4129c2..79f81d33 100644 --- a/api/views.py +++ b/api/views.py @@ -31,7 +31,7 @@ from django.conf import settings from rest_framework.authtoken.views import ObtainAuthToken from rest_framework.authtoken.models import Token from rest_framework.response import Response -from rest_framework import viewsets, status +from rest_framework import viewsets, status, generics import cotisations.models as cotisations import machines.models as machines @@ -39,6 +39,8 @@ import preferences.models as preferences import topologie.models as topologie import users.models as users +from re2o.utils import all_active_interfaces + from . import serializers @@ -310,6 +312,14 @@ class WhitelistViewSet(viewsets.ReadOnlyModelViewSet): queryset = users.Whitelist.objects.all() serializer_class = serializers.WhitelistSerializer + +# DHCP views + +class HostMacIpView(generics.ListAPIView): + queryset = all_active_interfaces() + serializer_class = serializers.HostMacIpSerializer + + # Subclass the standard rest_framework.auth_token.views.ObtainAuthToken # in order to renew the lease of the token and add expiration time class ObtainExpiringAuthToken(ObtainAuthToken):