8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-27 17:33:45 +00:00
re2o/machines/serializers.py

235 lines
8.3 KiB
Python
Raw Normal View History

# -*- mode: python; coding: utf-8 -*-
2017-01-15 23:01:18 +00:00
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017 Gabriel Détraz
# Copyright © 2017 Goulven Kermarec
# Copyright © 2017 Augustin Lemesle
#
# 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.
2016-11-12 15:12:24 +00:00
#Augustin Lemesle
2016-07-18 21:22:59 +00:00
from rest_framework import serializers
from machines.models import Interface, IpType, Extension, IpList, MachineType, Domain, Text, Mx, Service_link, Ns, OuverturePortList, OuverturePort
2016-07-18 21:22:59 +00:00
2016-11-16 18:30:48 +00:00
class IpTypeField(serializers.RelatedField):
def to_representation(self, value):
return value.type
class IpListSerializer(serializers.ModelSerializer):
ip_type = IpTypeField(read_only=True)
class Meta:
model = IpList
fields = ('ipv4', 'ip_type')
class InterfaceSerializer(serializers.ModelSerializer):
ipv4 = IpListSerializer(read_only=True)
2016-12-19 22:59:54 +00:00
mac_address = serializers.SerializerMethodField('get_macaddress')
2016-12-25 23:01:48 +00:00
domain = serializers.SerializerMethodField('get_dns')
extension = serializers.SerializerMethodField('get_interface_extension')
2016-12-19 22:59:54 +00:00
2016-07-18 21:22:59 +00:00
class Meta:
model = Interface
2016-12-26 17:00:43 +00:00
fields = ('ipv4', 'mac_address', 'domain', 'extension')
2016-12-24 19:04:53 +00:00
def get_dns(self, obj):
2016-12-26 17:00:43 +00:00
return obj.domain.name
def get_interface_extension(self, obj):
return obj.domain.extension.name
2016-11-16 18:30:48 +00:00
2016-12-19 22:59:54 +00:00
def get_macaddress(self, obj):
return str(obj.mac_address)
2017-10-03 02:42:49 +00:00
class FullInterfaceSerializer(serializers.ModelSerializer):
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', 'ipv6', '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)
2016-11-16 18:30:48 +00:00
class ExtensionNameField(serializers.RelatedField):
def to_representation(self, value):
2016-12-19 19:55:35 +00:00
return value.name
2016-07-19 19:43:24 +00:00
2016-11-20 03:39:10 +00:00
class TypeSerializer(serializers.ModelSerializer):
extension = ExtensionNameField(read_only=True)
ouverture_ports_tcp_in = serializers.SerializerMethodField('get_port_policy_input_tcp')
ouverture_ports_tcp_out = serializers.SerializerMethodField('get_port_policy_output_tcp')
ouverture_ports_udp_in = serializers.SerializerMethodField('get_port_policy_input_udp')
ouverture_ports_udp_out = serializers.SerializerMethodField('get_port_policy_output_udp')
2016-11-20 03:39:10 +00:00
class Meta:
model = IpType
fields = ('type', 'extension', 'domaine_ip_start', 'domaine_ip_stop', 'ouverture_ports_tcp_in', 'ouverture_ports_tcp_out', 'ouverture_ports_udp_in', 'ouverture_ports_udp_out', )
def get_port_policy(self, obj, protocole, io):
if obj.ouverture_ports is None:
return []
return map(str, obj.ouverture_ports.ouvertureport_set.filter(protocole=protocole).filter(io=io))
def get_port_policy_input_tcp(self, obj):
return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.IN)
def get_port_policy_output_tcp(self, obj):
return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.OUT)
def get_port_policy_input_udp(self, obj):
return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.IN)
def get_port_policy_output_udp(self, obj):
return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.OUT)
2016-11-20 03:39:10 +00:00
class ExtensionSerializer(serializers.ModelSerializer):
origin = serializers.SerializerMethodField('get_origin_ip')
zone_entry = serializers.SerializerMethodField('get_zone_name')
class Meta:
model = Extension
fields = ('name', 'origin', 'zone_entry')
def get_origin_ip(self, obj):
return obj.origin.ipv4
def get_zone_name(self, obj):
return str(obj.dns_entry)
class MxSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField('get_entry_name')
zone = serializers.SerializerMethodField('get_zone_name')
mx_entry = serializers.SerializerMethodField('get_mx_name')
2016-11-16 18:30:48 +00:00
2016-11-12 15:12:24 +00:00
class Meta:
model = Mx
fields = ('zone', 'priority', 'name', 'mx_entry')
def get_entry_name(self, obj):
return str(obj.name)
def get_zone_name(self, obj):
return obj.zone.name
def get_mx_name(self, obj):
return str(obj.dns_entry)
2017-09-05 16:18:41 +00:00
class TextSerializer(serializers.ModelSerializer):
zone = serializers.SerializerMethodField('get_zone_name')
text_entry = serializers.SerializerMethodField('get_text_name')
2017-09-05 16:18:41 +00:00
class Meta:
model = Text
fields = ('zone','text_entry','field1', 'field2')
2017-09-05 16:18:41 +00:00
def get_zone_name(self, obj):
return str(obj.zone.name)
def get_text_name(self, obj):
return str(obj.dns_entry)
class NsSerializer(serializers.ModelSerializer):
zone = serializers.SerializerMethodField('get_zone_name')
ns = serializers.SerializerMethodField('get_domain_name')
ns_entry = serializers.SerializerMethodField('get_text_name')
2016-07-18 21:22:59 +00:00
2016-11-18 17:15:57 +00:00
class Meta:
model = Ns
fields = ('zone', 'ns', 'ns_entry')
def get_zone_name(self, obj):
return obj.zone.name
def get_domain_name(self, obj):
return str(obj.ns)
2016-11-18 17:15:57 +00:00
def get_text_name(self, obj):
return str(obj.dns_entry)
2016-12-24 19:04:53 +00:00
class DomainSerializer(serializers.ModelSerializer):
extension = serializers.SerializerMethodField('get_zone_name')
cname = serializers.SerializerMethodField('get_alias_name')
cname_entry = serializers.SerializerMethodField('get_cname_name')
2016-07-18 21:22:59 +00:00
2016-11-18 17:15:57 +00:00
class Meta:
2016-12-24 19:04:53 +00:00
model = Domain
fields = ('name', 'extension', 'cname', 'cname_entry')
def get_zone_name(self, obj):
2016-12-25 23:01:48 +00:00
return obj.extension.name
def get_alias_name(self, obj):
return str(obj.cname)
2016-12-24 19:04:53 +00:00
def get_cname_name(self, obj):
return str(obj.dns_entry)
class ServiceServersSerializer(serializers.ModelSerializer):
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()
class OuverturePortsSerializer(serializers.Serializer):
ipv4 = serializers.SerializerMethodField()
ipv6 = serializers.SerializerMethodField()
def get_ipv4():
return {i.ipv4.ipv4:
{
"tcp_in":[j.tcp_ports_in() for j in i.port_lists.all()],
"tcp_out":[j.tcp_ports_out()for j in i.port_lists.all()],
"udp_in":[j.udp_ports_in() for j in i.port_lists.all()],
"udp_out":[j.udp_ports_out() for j in i.port_lists.all()],
}
for i in Interface.objects.all() if i.ipv4
}
def get_ipv6():
return {i.ipv6:
{
"tcp_in":[j.tcp_ports_in() for j in i.port_lists.all()],
"tcp_out":[j.tcp_ports_out()for j in i.port_lists.all()],
"udp_in":[j.udp_ports_in() for j in i.port_lists.all()],
"udp_out":[j.udp_ports_out() for j in i.port_lists.all()],
}
for i in Interface.objects.all() if i.ipv6
}