8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00
re2o/topologie/api/serializers.py

295 lines
8 KiB
Python
Raw Permalink Normal View History

# -*- 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
# 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.
from rest_framework import serializers
import machines.models as machines
2021-02-10 10:06:09 +00:00
import topologie.models as topologie
from api.serializers import (NamespacedHIField, NamespacedHMSerializer,
NamespacedHRField)
from machines.api.serializers import Ipv6ListSerializer, VlanSerializer
class StackSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.Stack` objects"""
class Meta:
model = topologie.Stack
fields = (
"name",
"stack_id",
"details",
"member_id_min",
"member_id_max",
"api_url",
)
class AccessPointSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.AccessPoint` objects"""
class Meta:
model = topologie.AccessPoint
fields = ("user", "name", "active", "location", "api_url")
class SwitchSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.Switch` objects"""
port_amount = serializers.IntegerField(source="number")
class Meta:
model = topologie.Switch
fields = (
"user",
"name",
"active",
"port_amount",
"stack",
"stack_member_id",
"model",
"switchbay",
"api_url",
)
class ServerSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.Server` objects"""
class Meta:
model = topologie.Server
fields = ("user", "name", "active", "api_url")
class ModelSwitchSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.ModelSwitch` objects"""
class Meta:
model = topologie.ModelSwitch
fields = ("reference", "constructor", "api_url")
class ConstructorSwitchSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.ConstructorSwitch` objects"""
class Meta:
model = topologie.ConstructorSwitch
fields = ("name", "api_url")
class SwitchBaySerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.SwitchBay` objects"""
class Meta:
model = topologie.SwitchBay
fields = ("name", "building", "info", "api_url")
class BuildingSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.Building` objects"""
class Meta:
model = topologie.Building
fields = ("name", "dormitory", "api_url")
2021-02-10 10:06:09 +00:00
class DormitorySerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.Dormitory` objects"""
class Meta:
model = topologie.Dormitory
fields = ("name", "api_url")
2021-02-10 10:06:09 +00:00
class PortProfileSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.Room` objects"""
class Meta:
model = topologie.PortProfile
fields = (
"name",
"profil_default",
"vlan_untagged",
"vlan_tagged",
"radius_type",
"radius_mode",
"speed",
"mac_limit",
"flow_control",
"dhcp_snooping",
"dhcpv6_snooping",
"dhcpv6_snooping",
"arp_protect",
"ra_guard",
"loop_protect",
"api_url",
)
class RoomSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `topologie.models.Room` objects"""
class Meta:
model = topologie.Room
2020-09-01 12:01:19 +00:00
fields = ("name", "building", "details", "api_url")
class PortProfileSerializer(NamespacedHMSerializer):
vlan_untagged = VlanSerializer(read_only=True)
class Meta:
model = topologie.PortProfile
fields = (
"name",
"profil_default",
"vlan_untagged",
"vlan_tagged",
"radius_type",
"radius_mode",
"speed",
"mac_limit",
"flow_control",
"dhcp_snooping",
"dhcpv6_snooping",
"arp_protect",
"ra_guard",
"loop_protect",
"vlan_untagged",
"vlan_tagged",
)
class InterfaceVlanSerializer(NamespacedHMSerializer):
domain = serializers.CharField(read_only=True)
ipv4 = serializers.CharField(read_only=True)
ipv6 = Ipv6ListSerializer(read_only=True, many=True)
vlan_id = serializers.IntegerField(
source="machine_type.ip_type.vlan.vlan_id", read_only=True
)
class Meta:
model = machines.Interface
fields = ("ipv4", "ipv6", "domain", "vlan_id")
class InterfaceRoleSerializer(NamespacedHMSerializer):
interface = InterfaceVlanSerializer(
source="machine.interface_set", read_only=True, many=True
)
class Meta:
model = machines.Interface
fields = ("interface",)
class RoleSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `machines.models.OuverturePort` objects."""
servers = InterfaceRoleSerializer(read_only=True, many=True)
class Meta:
model = machines.Role
fields = ("role_type", "servers", "specific_role")
class VlanPortSerializer(NamespacedHMSerializer):
class Meta:
model = machines.Vlan
fields = ("vlan_id", "name")
class ProfilSerializer(NamespacedHMSerializer):
vlan_untagged = VlanSerializer(read_only=True)
vlan_tagged = VlanPortSerializer(read_only=True, many=True)
class Meta:
model = topologie.PortProfile
fields = (
"name",
"profil_default",
"vlan_untagged",
"vlan_tagged",
"radius_type",
"radius_mode",
"speed",
"mac_limit",
"flow_control",
"dhcp_snooping",
"dhcpv6_snooping",
"arp_protect",
"ra_guard",
"loop_protect",
"vlan_untagged",
"vlan_tagged",
)
class ModelSwitchSerializer(NamespacedHMSerializer):
constructor = serializers.CharField(read_only=True)
class Meta:
model = topologie.ModelSwitch
fields = ("reference", "firmware", "constructor")
class SwitchBaySerializer(NamespacedHMSerializer):
class Meta:
model = topologie.SwitchBay
fields = ("name",)
class PortsSerializer(NamespacedHMSerializer):
2021-02-10 10:06:09 +00:00
"""Serialize `machines.models.Ipv6List` objects."""
get_port_profile = ProfilSerializer(read_only=True)
class Meta:
model = topologie.Port
fields = ("state", "port", "pretty_name", "get_port_profile")
class SwitchPortSerializer(serializers.ModelSerializer):
"""Serialize the data about the switches"""
ports = PortsSerializer(many=True, read_only=True)
model = ModelSwitchSerializer(read_only=True)
switchbay = SwitchBaySerializer(read_only=True)
class Meta:
model = topologie.Switch
fields = (
"short_name",
"model",
"switchbay",
"ports",
"ipv4",
"ipv6",
"interfaces_subnet",
"interfaces6_subnet",
"automatic_provision",
"rest_enabled",
"web_management_enabled",
"get_radius_key_value",
"get_management_cred_value",
"get_radius_servers",
"list_modules",
)