From 1a46f3110f7e937f84087e66032f2e3d6cf7a228 Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Sat, 14 Jul 2018 10:59:17 +0200 Subject: [PATCH] Serializers for reverse DNS --- api/serializers.py | 18 ++++++++++++++++++ api/urls.py | 1 + api/views.py | 9 +++++++++ machines/models.py | 11 +++++++++++ 4 files changed, 39 insertions(+) diff --git a/api/serializers.py b/api/serializers.py index 23a2b15e..3761907f 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -817,6 +817,24 @@ class DNSZonesSerializer(serializers.ModelSerializer): 'aaaa_records', 'cname_records', 'sshfp_records') +class DNSReverseZonesSerializer(serializers.ModelSerializer): + """Serialize the data about DNS Zones. + """ + soa = SOARecordSerializer(source='extension.soa') + extension = serializers.CharField(source='extension.name', read_only=True) + cidrs = serializers.CharField(source='ip_set_cidrs_as_str', read_only=True) + ns_records = NSRecordSerializer(many=True, source='extension.ns_set') + mx_records = MXRecordSerializer(many=True, source='extension.mx_set') + txt_records = TXTRecordSerializer(many=True, source='extension.txt_set') + ptr_records = ARecordSerializer(many=True, source='get_associated_ptr_records') + ptr_v6_records = AAAARecordSerializer(many=True, source='get_associated_ptr_v6_records') + + + class Meta: + model = machines.IpType + fields = ('type', 'extension', 'soa', 'ns_records', 'mx_records', + 'txt_records', 'ptr_records', 'ptr_v6_records', 'cidrs') + # MAILING diff --git a/api/urls.py b/api/urls.py index 37580db2..0c0c0e6c 100644 --- a/api/urls.py +++ b/api/urls.py @@ -104,6 +104,7 @@ router.register_view(r'localemail/users', views.LocalEmailUsersView), router.register_view(r'firewall/subnet-ports', views.SubnetPortsOpenView), # DNS router.register_view(r'dns/zones', views.DNSZonesView), +router.register_view(r'dns/reverse-zones', views.DNSReverseZonesView), # MAILING router.register_view(r'mailing/standard', views.StandardMailingView), router.register_view(r'mailing/club', views.ClubMailingView), diff --git a/api/views.py b/api/views.py index 715a31ac..9a3a772b 100644 --- a/api/views.py +++ b/api/views.py @@ -555,6 +555,15 @@ class DNSZonesView(generics.ListAPIView): .all()) serializer_class = serializers.DNSZonesSerializer +class DNSReverseZonesView(generics.ListAPIView): + """Exposes the detailed information about each extension (hostnames, + IPs, DNS records, etc.) in order to build the DNS zone files. + """ + queryset = (machines.IpType.objects.all()) + serializer_class = serializers.DNSReverseZonesSerializer + + + # MAILING diff --git a/machines/models.py b/machines/models.py index 7a211fe1..1cfee990 100644 --- a/machines/models.py +++ b/machines/models.py @@ -353,6 +353,17 @@ class IpType(RevMixin, AclMixin, models.Model): ): ipv6.check_and_replace_prefix(prefix=self.prefix_v6) + def get_associated_ptr_records(self): + from re2o.utils import all_active_assigned_interfaces + return (all_active_assigned_interfaces() + .filter(type__ip_type=self) + .filter(ipv4__isnull=False)) + + def get_associated_ptr_v6_records(self): + from re2o.utils import all_active_interfaces + return (all_active_interfaces(full=True) + .filter(type__ip_type=self)) + def clean(self): """ Nettoyage. Vérifie : - Que ip_stop est après ip_start