add nick
[laas.git] / src / resource_inventory / forms.py
1 ##############################################################################
2 # Copyright (c) 2020 Sawyer Bergeron, Sean Smith, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from django.core.exceptions import ValidationError
11 from django import forms
12
13 from resource_inventory.models import Network, InterfaceConfiguration
14
15
16 class InterfaceConfigurationForm(forms.ModelForm):
17     class Meta:
18         model = InterfaceConfiguration
19         fields = ['profile', 'resource_config', 'connections']
20
21     def clean(self):
22         connections = self.cleaned_data.get('connections')
23         resource_config = self.cleaned_data.get('resource_config')
24
25         valid_nets = set(Network.objects.filter(bundle=resource_config.template))
26         curr_nets = set([conn.network for conn in connections])
27
28         if not curr_nets.issubset(valid_nets):
29             raise ValidationError("Cannot have network connection to network outside pod")
30
31         return self.cleaned_data