From aec2c645a24c4db42ff40630577b7c6313a69d2c Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Mon, 2 Jul 2018 11:00:32 +0200 Subject: [PATCH] Serialisation de la conf des swicthes --- api/serializers.py | 55 ++++++++++++++++++++++++++++++++++++++++++++- api/urls.py | 5 ++++- api/views.py | 16 +++++++++++++ topologie/models.py | 8 +++++++ 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 65a82eb6..9b425c4d 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -467,16 +467,30 @@ class BuildingSerializer(NamespacedHMSerializer): class SwitchPortSerializer(NamespacedHMSerializer): """Serialize `topologie.models.Port` objects """ + + get_port_profil = NamespacedHIField(view_name='portprofile-detail', read_only=True) + class Meta: model = topologie.Port fields = ('switch', 'port', 'room', 'machine_interface', 'related', - 'custom_profile', 'state', 'details', 'api_url') + 'custom_profile', 'state', 'get_port_profil', 'details', 'api_url') extra_kwargs = { 'related': {'view_name': 'switchport-detail'}, 'api_url': {'view_name': 'switchport-detail'}, } +class PortProfileSerializer(NamespacedHMSerializer): + """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): """Serialize `topologie.models.Room` objects """ @@ -644,6 +658,45 @@ class ServiceRegenSerializer(NamespacedHMSerializer): 'api_url': {'view_name': 'serviceregen-detail'} } +# Switches et ports + +class ProfilSerializer(NamespacedHMSerializer): + 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') + + +class ModelSwitchSerializer(NamespacedHMSerializer): + class Meta: + model = topologie.ModelSwitch + fields = ('reference',) + + +class SwitchBaySerializer(NamespacedHMSerializer): + class Meta: + model = topologie.SwitchBay + fields = ('name',) + + +class PortsSerializer(NamespacedHMSerializer): + """Serialize `machines.models.Ipv6List` objects. + """ + get_port_profil = ProfilSerializer(read_only=True) + + class Meta: + model = topologie.Port + fields = ('state', 'port', 'get_port_profil') + + +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', 'subnet', 'subnet6') # LOCAL EMAILS diff --git a/api/urls.py b/api/urls.py index abc466e1..9de02578 100644 --- a/api/urls.py +++ b/api/urls.py @@ -81,7 +81,8 @@ router.register_viewset(r'topologie/modelswitch', views.ModelSwitchViewSet) router.register_viewset(r'topologie/constructorswitch', views.ConstructorSwitchViewSet) router.register_viewset(r'topologie/switchbay', views.SwitchBayViewSet) router.register_viewset(r'topologie/building', views.BuildingViewSet) -router.register(r'topologie/switchport', views.SwitchPortViewSet, base_name='switchport') +router.register_viewset(r'topologie/switchport', views.SwitchPortViewSet, base_name='switchport') +router.register_viewset(r'topologie/portprofile', views.PortProfileViewSet, base_name='portprofile') router.register_viewset(r'topologie/room', views.RoomViewSet) router.register(r'topologie/portprofile', views.PortProfileViewSet) # USERS @@ -105,6 +106,8 @@ router.register_view(r'localemail/users', views.LocalEmailUsersView), # Firewall router.register_view(r'firewall/subnet-ports', views.SubnetPortsOpenView), router.register_view(r'firewall/interface-ports', views.InterfacePortsOpenView), +# Switches config +router.register_view(r'switchs/ports-config', views.SwitchPortView), # DNS router.register_view(r'dns/zones', views.DNSZonesView), router.register_view(r'dns/reverse-zones', views.DNSReverseZonesView), diff --git a/api/views.py b/api/views.py index 6e5265f6..3c2fe7d3 100644 --- a/api/views.py +++ b/api/views.py @@ -397,6 +397,13 @@ class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = serializers.SwitchPortSerializer +class PortProfileViewSet(viewsets.ReadOnlyModelViewSet): + """Exposes list and details of `topologie.models.PortProfile` objects. + """ + queryset = topologie.PortProfile.objects.all() + serializer_class = serializers.PortProfileSerializer + + class RoomViewSet(viewsets.ReadOnlyModelViewSet): """Exposes list and details of `topologie.models.Room` objects. """ @@ -515,6 +522,15 @@ class ServiceRegenViewSet(viewsets.ModelViewSet): queryset = queryset.filter(server__domain__name__iexact=hostname) return queryset +# Config des switches + +class SwitchPortView(generics.ListAPIView): + """Exposes the associations between hostname, mac address and IPv4 in + order to build the DHCP lease files. + """ + queryset = topologie.Switch.objects.all().prefetch_related('ports__custom_profile') + serializer_class = serializers.SwitchPortSerializer + # LOCAL EMAILS diff --git a/topologie/models.py b/topologie/models.py index b63bece6..aa1c4ef8 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -292,6 +292,14 @@ class Switch(AclMixin, Machine): def get_name(self): return self.name or self.main_interface().domain.name + @cached_property + def subnet(self): + return self.main_interface().type.ip_type.ip_set_full_info + + @cached_property + def subnet6(self): + return self.main_interface().type.ip_type.ip6_set_full_info + def __str__(self): return str(self.get_name)