From 9ebe025e1385a0a4fa8e24ad62cc1c0f8808fb9c Mon Sep 17 00:00:00 2001 From: ssmith Date: Mon, 18 May 2020 18:08:35 -0400 Subject: Reworks resource tests into model validation where possible. Change-Id: Ieb77cc38a42345107c2cac62ba1499bd7974ad77 Signed-off-by: Sean Smith --- src/resource_inventory/admin.py | 16 +++++++++++++--- src/resource_inventory/forms.py | 31 +++++++++++++++++++++++++++++++ src/resource_inventory/models.py | 14 +++++++++++++- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/resource_inventory/forms.py (limited to 'src/resource_inventory') diff --git a/src/resource_inventory/admin.py b/src/resource_inventory/admin.py index 439dad3..2444a98 100644 --- a/src/resource_inventory/admin.py +++ b/src/resource_inventory/admin.py @@ -10,6 +10,8 @@ from django.contrib import admin +from resource_inventory.forms import InterfaceConfigurationForm + from resource_inventory.models import ( ResourceProfile, InterfaceProfile, @@ -32,9 +34,10 @@ from resource_inventory.models import ( Image, RemoteInfo, PhysicalNetwork, - NetworkConnection + NetworkConnection, ) + admin.site.register([ ResourceProfile, InterfaceProfile, @@ -43,7 +46,6 @@ admin.site.register([ RamProfile, ResourceTemplate, ResourceConfiguration, - InterfaceConfiguration, Server, Interface, Network, @@ -57,4 +59,12 @@ admin.site.register([ Image, PhysicalNetwork, NetworkConnection, - RemoteInfo]) + RemoteInfo] +) + + +class InterfaceConfigurationAdmin(admin.ModelAdmin): + form = InterfaceConfigurationForm + + +admin.site.register(InterfaceConfiguration, InterfaceConfigurationAdmin) diff --git a/src/resource_inventory/forms.py b/src/resource_inventory/forms.py new file mode 100644 index 0000000..fb8c102 --- /dev/null +++ b/src/resource_inventory/forms.py @@ -0,0 +1,31 @@ +############################################################################## +# Copyright (c) 2020 Sawyer Bergeron, Sean Smith, and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +from django.core.exceptions import ValidationError +from django import forms + +from resource_inventory.models import Network, InterfaceConfiguration + + +class InterfaceConfigurationForm(forms.ModelForm): + class Meta: + model = InterfaceConfiguration + fields = ['profile', 'resource_config', 'connections'] + + def clean(self): + connections = self.cleaned_data.get('connections') + resource_config = self.cleaned_data.get('resource_config') + + valid_nets = set(Network.objects.filter(bundle=resource_config.template)) + curr_nets = set([conn.network for conn in connections]) + + if not curr_nets.issubset(valid_nets): + raise ValidationError("Cannot have network connection to network outside pod") + + return self.cleaned_data diff --git a/src/resource_inventory/models.py b/src/resource_inventory/models.py index d1b7a75..33d98a0 100644 --- a/src/resource_inventory/models.py +++ b/src/resource_inventory/models.py @@ -439,6 +439,9 @@ class InterfaceConfiguration(models.Model): def __str__(self): return "type " + str(self.profile) + " on host " + str(self.resource_config) + def save(self, *args, **kwargs): + super().save(*args, **kwargs) + """ OPNFV / Software configuration models @@ -504,12 +507,21 @@ class Interface(models.Model): mac_address = models.CharField(max_length=17) bus_address = models.CharField(max_length=50) config = models.ManyToManyField(Vlan) - acts_as = models.OneToOneField(InterfaceConfiguration, null=True, on_delete=models.SET_NULL) + acts_as = models.OneToOneField(InterfaceConfiguration, null=True, on_delete=models.CASCADE) profile = models.ForeignKey(InterfaceProfile, on_delete=models.CASCADE) def __str__(self): return self.mac_address + " on host " + str(self.profile.host.name) + def clean(self, *args, **kwargs): + if self.acts_as.profile != self.profile: + raise ValidationError("Interface Configuration's Interface Profile does not match Interface Profile chosen for Interface.") + super().clean(*args, **kwargs) + + def save(self, *args, **kwargs): + self.full_clean() + super().save(*args, **kwargs) + """ Some Enums for dealing with global constants. -- cgit 1.2.3-korg