From 74a584e3f586455941c011ea06596b30055f4826 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 | 3 +++ api/views.py | 16 +++++++++++++ machines/models.py | 9 ++++++++ topologie/models.py | 8 +++++++ 5 files changed, 90 insertions(+), 1 deletion(-) diff --git a/api/serializers.py b/api/serializers.py index 8f7c8035..09bfb3a4 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -459,16 +459,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', - 'radius', 'vlan_force', 'details', 'api_url') + '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 """ @@ -618,6 +632,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') # DHCP diff --git a/api/urls.py b/api/urls.py index 6003284b..b47a1a96 100644 --- a/api/urls.py +++ b/api/urls.py @@ -81,6 +81,7 @@ router.register_viewset(r'topologie/constructorswitch', views.ConstructorSwitchV router.register_viewset(r'topologie/switchbay', views.SwitchBayViewSet) router.register_viewset(r'topologie/building', views.BuildingViewSet) 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) # USERS router.register_viewset(r'users/user', views.UserViewSet) @@ -97,6 +98,8 @@ router.register_viewset(r'users/mailalias', views.MailAliasViewSet) router.register_viewset(r'services/regen', views.ServiceRegenViewSet, base_name='serviceregen') # DHCP router.register_view(r'dhcp/hostmacip', views.HostMacIpView), +# Switches config +router.register_view(r'switchs/ports-config', views.SwitchPortView), # DNS router.register_view(r'dns/zones', views.DNSZonesView), # MAILING diff --git a/api/views.py b/api/views.py index 60be3b46..ad32c561 100644 --- a/api/views.py +++ b/api/views.py @@ -389,6 +389,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. """ @@ -488,6 +495,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 + # DHCP diff --git a/machines/models.py b/machines/models.py index a0a84a8e..5113c34c 100644 --- a/machines/models.py +++ b/machines/models.py @@ -390,6 +390,15 @@ class IpType(RevMixin, AclMixin, models.Model): } for ip_set in self.ip_set.iter_cidrs() ] + @cached_property + def ip6_set_full_info(self): + if self.prefix_v6: + return { + 'network' : str(self.prefix_v6), + 'netmask' : 'ffff:ffff:ffff:ffff::' + } + else: + return None def ip_objects(self): """ Renvoie tous les objets ipv4 relié à ce type""" diff --git a/topologie/models.py b/topologie/models.py index 2849a50a..700b6c16 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -271,6 +271,14 @@ class Switch(AclMixin, Machine): """ Returns the 'main' interface of the switch """ return self.interface_set.first() + @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.main_interface())