8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-04 17:06:27 +00:00

Serialisation de la conf des swicthes

This commit is contained in:
Gabriel Detraz 2018-07-02 11:00:32 +02:00
parent 53ed7989b7
commit 74a584e3f5
5 changed files with 90 additions and 1 deletions

View file

@ -459,16 +459,30 @@ class BuildingSerializer(NamespacedHMSerializer):
class SwitchPortSerializer(NamespacedHMSerializer): class SwitchPortSerializer(NamespacedHMSerializer):
"""Serialize `topologie.models.Port` objects """Serialize `topologie.models.Port` objects
""" """
get_port_profil = NamespacedHIField(view_name='portprofile-detail', read_only=True)
class Meta: class Meta:
model = topologie.Port model = topologie.Port
fields = ('switch', 'port', 'room', 'machine_interface', 'related', fields = ('switch', 'port', 'room', 'machine_interface', 'related',
'radius', 'vlan_force', 'details', 'api_url') 'state', 'get_port_profil', 'details', 'api_url')
extra_kwargs = { extra_kwargs = {
'related': {'view_name': 'switchport-detail'}, 'related': {'view_name': 'switchport-detail'},
'api_url': {'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): class RoomSerializer(NamespacedHMSerializer):
"""Serialize `topologie.models.Room` objects """Serialize `topologie.models.Room` objects
""" """
@ -618,6 +632,45 @@ class ServiceRegenSerializer(NamespacedHMSerializer):
'api_url': {'view_name': 'serviceregen-detail'} '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 # DHCP

View file

@ -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/switchbay', views.SwitchBayViewSet)
router.register_viewset(r'topologie/building', views.BuildingViewSet) router.register_viewset(r'topologie/building', views.BuildingViewSet)
router.register_viewset(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_viewset(r'topologie/room', views.RoomViewSet)
# USERS # USERS
router.register_viewset(r'users/user', views.UserViewSet) 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') router.register_viewset(r'services/regen', views.ServiceRegenViewSet, base_name='serviceregen')
# DHCP # DHCP
router.register_view(r'dhcp/hostmacip', views.HostMacIpView), router.register_view(r'dhcp/hostmacip', views.HostMacIpView),
# Switches config
router.register_view(r'switchs/ports-config', views.SwitchPortView),
# DNS # DNS
router.register_view(r'dns/zones', views.DNSZonesView), router.register_view(r'dns/zones', views.DNSZonesView),
# MAILING # MAILING

View file

@ -389,6 +389,13 @@ class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = serializers.SwitchPortSerializer 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): class RoomViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `topologie.models.Room` objects. """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) queryset = queryset.filter(server__domain__name__iexact=hostname)
return queryset 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 # DHCP

View file

@ -390,6 +390,15 @@ class IpType(RevMixin, AclMixin, models.Model):
} for ip_set in self.ip_set.iter_cidrs() } 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): def ip_objects(self):
""" Renvoie tous les objets ipv4 relié à ce type""" """ Renvoie tous les objets ipv4 relié à ce type"""

View file

@ -271,6 +271,14 @@ class Switch(AclMixin, Machine):
""" Returns the 'main' interface of the switch """ """ Returns the 'main' interface of the switch """
return self.interface_set.first() 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): def __str__(self):
return str(self.main_interface()) return str(self.main_interface())