From ae0f134cc7f40dc0f3570c0c807692305fa0bc8a Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 11 Jul 2018 19:46:13 +0200 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9e=20sp=C3=A9cific=20role,=20l'utilise?= =?UTF-8?q?=20pour=20get=20l'ip=20du=20serveur=20des=20config=20switchs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/serializers.py | 2 +- .../migrations/0094_role_specific_role.py | 20 +++++++++++++++++++ machines/models.py | 13 ++++++++++++ preferences/models.py | 18 ++++++++++++++++- .../preferences/display_preferences.html | 4 ++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 machines/migrations/0094_role_specific_role.py diff --git a/api/serializers.py b/api/serializers.py index 4605b79b..f06e1b92 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -696,7 +696,7 @@ class RoleSerializer(NamespacedHMSerializer): class Meta: model = machines.Role - fields = ('role_type', 'servers') + fields = ('role_type', 'servers', 'specific_role') class VlanPortSerializer(NamespacedHMSerializer): diff --git a/machines/migrations/0094_role_specific_role.py b/machines/migrations/0094_role_specific_role.py new file mode 100644 index 00000000..73cade7b --- /dev/null +++ b/machines/migrations/0094_role_specific_role.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-07-11 16:49 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0093_merge_20180710_0226'), + ] + + operations = [ + migrations.AddField( + model_name='role', + name='specific_role', + field=models.CharField(blank=True, choices=[('dhcp-server', 'dhcp-server'), ('switch-conf-server', 'switch-conf-server'), ('dns-recursif-server', 'dns-recursif-server'), ('ntp-server', 'ntp-server'), ('radius-server', 'radius-server'), ('ntp-server', 'ntp-server'), ('log-server', 'log-server'), ('ldap-master-server', 'ldap-master-server'), ('ldap-backup-server', 'ldap-backup-server'), ('smtp-server', 'smtp-server'), ('postgresql-server', 'postgresql-server'), ('mysql-server', 'mysql-server'), ('sql-client', 'sql-client'), ('gateway', 'gateway')], max_length=32, null=True), + ), + ] diff --git a/machines/models.py b/machines/models.py index 212d65e8..d5cdc5a1 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1652,6 +1652,19 @@ class Role(RevMixin, AclMixin, models.Model): machine__interface__role=cls.objects.filter(specific_role=roletype) ) + @classmethod + def get_instance(cls, machineid, *_args, **_kwargs): + """Get the Machine instance with machineid. + :param userid: The id + :return: The user + """ + return cls.objects.get(pk=machineid) + + @classmethod + def interface_for_roletype(cls, roletype): + """Return interfaces for a roletype""" + return Interface.objects.filter(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 65db5d97..c1d19a23 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -231,11 +231,27 @@ class OptionalTopologie(AclMixin, PreferencesModel): from topologie.models import Switch return Switch.objects.filter(automatic_provision=True) + @cached_property + def switchs_management_interface(self): + """Return the ip of the interface that the switch have to contact to get it's config""" + if self.switchs_ip_type: + from machines.models import Role, Interface + return Interface.objects.filter(machine__interface__in=Role.interface_for_roletype("switch-conf-server")).filter(type__ip_type=self.switchs_ip_type).first() + else: + return None + + @cached_property + def switchs_management_interface_ip(self): + """Same, but return the ipv4""" + if not self.switchs_management_interface: + return None + return self.switchs_management_interface.ipv4 + @cached_property def provision_switchs_enabled(self): """Return true if all settings are ok : switchs on automatic provision, ip_type""" - return bool(self.provisioned_switchs and self.switchs_ip_type and SwitchManagementCred.objects.filter(default_switch=True).exists()) + return bool(self.provisioned_switchs and self.switchs_ip_type and SwitchManagementCred.objects.filter(default_switch=True).exists() and self.switchs_management_interface_ip) class Meta: permissions = ( diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index 85c4c5cd..5ea1f0e6 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -152,6 +152,10 @@ with this program; if not, write to the Free Software Foundation, Inc., Plage d'ip de management des switchs {{ topologieoptions.switchs_ip_type }} {% if topologieoptions.switchs_ip_type %} OK{% else %}Manquant{% endif %} + + Serveur des config des switchs + {{ topologieoptions.switchs_management_interface }} {% if topologieoptions.switchs_management_interface %} - {{ topologieoptions.switchs_management_interface_ip }} OK{% else %}Manquant{% endif %} +
Creds de management des switchs