2020-04-20 18:43:11 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2020-11-23 16:06:37 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
|
2020-04-20 18:43:11 +00:00
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2018 Maël Kervella
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2021-02-10 10:06:09 +00:00
|
|
|
from rest_framework import generics, viewsets
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
import machines.models as machines
|
|
|
|
from re2o.utils import all_active_interfaces
|
|
|
|
|
2021-02-10 10:06:09 +00:00
|
|
|
from . import serializers
|
|
|
|
|
|
|
|
|
2020-04-20 18:43:11 +00:00
|
|
|
class MachineViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Machine` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Machine.objects.all()
|
|
|
|
serializer_class = serializers.MachineSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.MachineType` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.MachineType.objects.all()
|
|
|
|
serializer_class = serializers.MachineTypeSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class IpTypeViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.IpType` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.IpType.objects.all()
|
|
|
|
serializer_class = serializers.IpTypeSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class VlanViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Vlan` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Vlan.objects.all()
|
|
|
|
serializer_class = serializers.VlanSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class NasViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Nas` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Nas.objects.all()
|
|
|
|
serializer_class = serializers.NasSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class SOAViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.SOA` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.SOA.objects.all()
|
|
|
|
serializer_class = serializers.SOASerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ExtensionViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Extension` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Extension.objects.all()
|
|
|
|
serializer_class = serializers.ExtensionSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class MxViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Mx` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Mx.objects.all()
|
|
|
|
serializer_class = serializers.MxSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class NsViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Ns` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Ns.objects.all()
|
|
|
|
serializer_class = serializers.NsSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class TxtViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Txt` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Txt.objects.all()
|
|
|
|
serializer_class = serializers.TxtSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class DNameViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.DName` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.DName.objects.all()
|
|
|
|
serializer_class = serializers.DNameSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class SrvViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Srv` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Srv.objects.all()
|
|
|
|
serializer_class = serializers.SrvSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class SshFpViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.SshFp` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.SshFp.objects.all()
|
|
|
|
serializer_class = serializers.SshFpSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Interface` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Interface.objects.all()
|
|
|
|
serializer_class = serializers.InterfaceSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Ipv6List` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Ipv6List.objects.all()
|
|
|
|
serializer_class = serializers.Ipv6ListSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class DomainViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Domain` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Domain.objects.all()
|
|
|
|
serializer_class = serializers.DomainSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class IpListViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.IpList` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.IpList.objects.all()
|
|
|
|
serializer_class = serializers.IpListSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Service` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Service.objects.all()
|
|
|
|
serializer_class = serializers.ServiceSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Service_link` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Service_link.objects.all()
|
|
|
|
serializer_class = serializers.ServiceLinkSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
"""Exposes list and details of `machines.models.OuverturePortList`
|
|
|
|
objects.
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = machines.OuverturePortList.objects.all()
|
|
|
|
serializer_class = serializers.OuverturePortListSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.OuverturePort` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.OuverturePort.objects.all()
|
|
|
|
serializer_class = serializers.OuverturePortSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class RoleViewSet(viewsets.ReadOnlyModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of `machines.models.Machine` objects."""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
queryset = machines.Role.objects.all()
|
|
|
|
serializer_class = serializers.RoleSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceRegenViewSet(viewsets.ModelViewSet):
|
2021-02-10 10:06:09 +00:00
|
|
|
"""Exposes list and details of the services to regen"""
|
2020-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
serializer_class = serializers.ServiceRegenSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = machines.Service_link.objects.select_related(
|
|
|
|
"server__domain"
|
|
|
|
).select_related("service")
|
|
|
|
if "hostname" in self.request.GET:
|
|
|
|
hostname = self.request.GET["hostname"]
|
|
|
|
queryset = queryset.filter(server__domain__name__iexact=hostname)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
class HostMacIpView(generics.ListAPIView):
|
|
|
|
"""Exposes the associations between hostname, mac address and IPv4 in
|
|
|
|
order to build the DHCP lease files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
serializer_class = serializers.HostMacIpSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return all_active_interfaces()
|
|
|
|
|
|
|
|
|
|
|
|
class SubnetPortsOpenView(generics.ListAPIView):
|
|
|
|
queryset = machines.IpType.objects.all()
|
|
|
|
serializer_class = serializers.SubnetPortsOpenSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class InterfacePortsOpenView(generics.ListAPIView):
|
|
|
|
queryset = machines.Interface.objects.filter(port_lists__isnull=False).distinct()
|
|
|
|
serializer_class = serializers.InterfacePortsOpenSerializer
|
|
|
|
|
2021-02-10 10:06:09 +00:00
|
|
|
|
2020-04-20 18:43:11 +00:00
|
|
|
class DNSZonesView(generics.ListAPIView):
|
|
|
|
"""Exposes the detailed information about each extension (hostnames,
|
|
|
|
IPs, DNS records, etc.) in order to build the DNS zone files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = (
|
|
|
|
machines.Extension.objects.prefetch_related("soa")
|
|
|
|
.prefetch_related("ns_set")
|
|
|
|
.prefetch_related("ns_set__ns")
|
|
|
|
.prefetch_related("origin")
|
|
|
|
.prefetch_related("mx_set")
|
|
|
|
.prefetch_related("mx_set__name")
|
|
|
|
.prefetch_related("txt_set")
|
|
|
|
.prefetch_related("srv_set")
|
|
|
|
.prefetch_related("srv_set__target")
|
|
|
|
.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()
|
2021-02-10 10:06:09 +00:00
|
|
|
serializer_class = serializers.DNSReverseZonesSerializer
|