From 46fe444ead947d3e6b71bcbd28fac21f3e03bea4 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Thu, 12 Jul 2018 00:11:55 +0200 Subject: [PATCH] =?UTF-8?q?Repare=20le=20get=5Finstance=20de=20role=20+=20?= =?UTF-8?q?renvoie=20les=20r=C3=A9glages=20suppl=C3=A9mentaires=20optional?= =?UTF-8?q?topologie=20pour=20les=20switchs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/serializers.py | 7 ++++++- machines/models.py | 13 +++++++++---- preferences/models.py | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 9efa26fe..2a339f19 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -342,10 +342,15 @@ class OptionalMachineSerializer(NamespacedHMSerializer): class OptionalTopologieSerializer(NamespacedHMSerializer): """Serialize `preferences.models.OptionalTopologie` objects. """ + switchs_management_interface_ip= serializers.CharField(read_only=True) + class Meta: model = preferences.OptionalTopologie fields = ('radius_general_policy', 'vlan_decision_ok', - 'vlan_decision_nok') + 'vlan_decision_nok', 'switchs_ip_type', 'switchs_web_management', + 'switchs_web_management_ssl', 'switchs_rest_management', + 'switchs_management_utils', 'switchs_management_interface_ip', + 'provision_switchs_enabled') class GeneralOptionSerializer(NamespacedHMSerializer): diff --git a/machines/models.py b/machines/models.py index 5d3e2cac..2d23efca 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1534,18 +1534,23 @@ class Role(RevMixin, AclMixin, models.Model): ) @classmethod - def get_instance(cls, machineid, *_args, **_kwargs): - """Get the Machine instance with machineid. + def get_instance(cls, roleid, *_args, **_kwargs): + """Get the Role instance with roleid. :param userid: The id :return: The user """ - return cls.objects.get(pk=machineid) + return cls.objects.get(pk=roleid) @classmethod def interface_for_roletype(cls, roletype): - """Return interfaces for a roletype""" + """Return interface for a roletype""" return Interface.objects.filter(role=cls.objects.filter(specific_role=roletype)) + @classmethod + def all_interfaces_for_roletype(cls, roletype): + """Return all interfaces for a roletype""" + return Interface.objects.filter(machine__interface__role=cls.objects.filter(specific_role=roletype)) + def save(self, *args, **kwargs): super(Role, self).save(*args, **kwargs) diff --git a/preferences/models.py b/preferences/models.py index 34ae3eba..6386f455 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -258,6 +258,24 @@ class OptionalTopologie(AclMixin, PreferencesModel): return None return self.switchs_management_interface.ipv4 + @cached_property + def switchs_management_utils(self): + """Used for switch_conf, return a list of ip on vlans""" + from machines.models import Role, Ipv6List, Interface + def return_ips_dict(interfaces): + return {'ipv4' : [str(interface.ipv4) for interface in interfaces], 'ipv6' : Ipv6List.objects.filter(interface__in=interfaces).values_list('ipv6', flat=True)} + + ntp_servers = Role.all_interfaces_for_roletype("ntp-server").filter(type__ip_type=self.switchs_ip_type) + log_servers = Role.all_interfaces_for_roletype("log-server").filter(type__ip_type=self.switchs_ip_type) + radius_servers = Role.all_interfaces_for_roletype("radius-server").filter(type__ip_type=self.switchs_ip_type) + dhcp_servers = Role.all_interfaces_for_roletype("dhcp-server") + subnet = None + subnet6 = None + if self.switchs_ip_type: + subnet = self.switchs_ip_type.ip_set_full_info + subnet6 = self.switchs_ip_type.ip6_set_full_info + return {'ntp_servers': return_ips_dict(ntp_servers), 'log_servers': return_ips_dict(log_servers), 'radius_servers': return_ips_dict(radius_servers), 'dhcp_servers': return_ips_dict(dhcp_servers), 'subnet': subnet, 'subnet6': subnet6} + @cached_property def provision_switchs_enabled(self): """Return true if all settings are ok : switchs on automatic provision,