def clean(self): cleaned_data = super(UpdateInterface, self).clean() cleaned_data['iftype'] = cleaned_data.get('iftypedata') cleaned_data.pop('iftypedata', None) ifclass = cleaned_data.get('ifclass') interface_id = cleaned_data.get('id') networks = cleaned_data.pop('networks', []) interface_networks = sysinv.interface_network_list_by_interface( self.request, interface_id) network_ids = [] networks_to_add = [] networks_to_remove = [] interface_networks_to_remove = [] if ifclass == 'platform' and networks: for i in interface_networks: networks_to_remove.append(i.network_id) for n in networks: network = sysinv.network_get(self.request, n) network_ids.append(str(network.id)) if network.id in networks_to_remove: networks_to_remove.remove(network.id) else: networks_to_add.append(str(network.id)) for i in interface_networks: if i.network_id in networks_to_remove: interface_networks_to_remove.append(i.uuid) else: for i in interface_networks: interface_networks_to_remove.append(i.uuid) cleaned_data['networks'] = network_ids cleaned_data['networks_to_add'] = networks_to_add cleaned_data['interface_networks_to_remove'] = \ interface_networks_to_remove return cleaned_data
def clean(self): cleaned_data = super(UpdateInterface, self).clean() cleaned_data['iftype'] = cleaned_data.get('iftypedata') cleaned_data.pop('iftypedata', None) ifclass = cleaned_data.get('ifclass') interface_id = cleaned_data.get('id') networks = cleaned_data.pop('networks', []) interface_networks = sysinv.interface_network_list_by_interface( self.request, interface_id) network_ids = [] networks_to_add = [] networks_to_remove = [] interface_networks_to_remove = [] if ifclass == 'platform' and networks: for i in interface_networks: networks_to_remove.append(i.network_id) for n in networks: network = sysinv.network_get(self.request, n) network_ids.append(network.uuid) if network.id in networks_to_remove: networks_to_remove.remove(network.id) else: networks_to_add.append(network.uuid) for i in interface_networks: if i.network_id in networks_to_remove: interface_networks_to_remove.append(i.uuid) else: for i in interface_networks: interface_networks_to_remove.append(i.uuid) cleaned_data['networks'] = network_ids cleaned_data['networks_to_add'] = networks_to_add cleaned_data['interface_networks_to_remove'] = \ interface_networks_to_remove datanetwork_names = cleaned_data.pop('datanetworks', []) interface_datanetworks = \ sysinv.interface_datanetwork_list_by_interface( self.request, interface_id) datanetwork_uuids = [] datanetworks_to_add = [] datanetworks_to_remove = [] interface_datanetworks_to_remove = [] if ifclass in ['data', 'pci-passthrough', 'pci-sriov'] and \ datanetwork_names: for i in interface_datanetworks: datanetworks_to_remove.append(i.datanetwork_name) datanetworks_list = sysinv.data_network_list(self.request) for n in datanetwork_names.split(","): for dn in datanetworks_list: if dn.name == n: datanetwork_uuids.append(dn.uuid) if dn.name in datanetworks_to_remove: datanetworks_to_remove.remove(dn.name) else: datanetworks_to_add.append(dn.uuid) for i in interface_datanetworks: if i.datanetwork_name in datanetworks_to_remove: interface_datanetworks_to_remove.append(i.uuid) else: for i in interface_datanetworks: interface_datanetworks_to_remove.append(i.uuid) cleaned_data['datanetworks'] = datanetwork_uuids cleaned_data['datanetworks_to_add'] = datanetworks_to_add cleaned_data['interface_datanetworks_to_remove'] = \ interface_datanetworks_to_remove return cleaned_data
def __init__(self, *args, **kwargs): super(UpdateInterface, self).__init__(*args, **kwargs) ifclass_val = kwargs['initial']['ifclass'] host_uuid = kwargs['initial']['ihost_uuid'] this_interface_id = kwargs['initial']['id'] iftype_val = kwargs['initial']['iftype'] interface_networks = sysinv.interface_network_list_by_interface( self.request, this_interface_id) if ifclass_val == 'platform': # Load the networks associated with this interface network_choices = self.fields['networks'].choices network_choice_dict = dict(network_choices) initial_networks = [] for i in interface_networks: for uuid in network_choice_dict.keys(): if i.network_uuid == uuid: initial_networks.append(uuid) self.fields['networks'].initial = initial_networks for i in interface_networks: if i.network_type == 'mgmt': self.fields['aemode'].choices = self.MGMT_AE_MODE_CHOICES break else: self.fields['aemode'].choices = self.AE_MODE_CHOICES if ifclass_val in ['data', 'pci-passthrough', 'pci-sriov']: interface_datanetworks =\ sysinv.interface_datanetwork_list_by_interface( self.request, this_interface_id) # Load the networks associated with this interface if ifclass_val == 'data': datanetwork_choices = self.fields['datanetworks_data'].choices elif ifclass_val == 'pci-passthrough': datanetwork_choices = self.fields['datanetworks_pci'].choices elif ifclass_val == 'pci-sriov': datanetwork_choices = self.fields['datanetworks_sriov'].choices datanetwork_choice_dict = dict(datanetwork_choices) initial_datanetworks = [] for i in interface_datanetworks: for name in datanetwork_choice_dict.keys(): if i.datanetwork_name == name: initial_datanetworks.append(name) self.fields['datanetworks_data'].initial = initial_datanetworks self.fields['datanetworks_pci'].initial = initial_datanetworks self.fields['datanetworks_sriov'].initial = initial_datanetworks # Populate Address Pool selections pools = sysinv.address_pool_list(self.request) self.fields['ipv4_pool'].choices = _get_ipv4_pool_choices(pools) self.fields['ipv6_pool'].choices = _get_ipv6_pool_choices(pools) self.fields['ipv4_pool'].initial = kwargs['initial'].get('ipv4_pool') self.fields['ipv6_pool'].initial = kwargs['initial'].get('ipv6_pool') # Setting field to read-only doesn't actually work so we're making # it disabled. This has the effect of not allowing the data through # to the form submission, so we require a hidden field to carry the # actual value through (iftype data) self.fields['iftype'].widget.attrs['disabled'] = 'disabled' self.fields['iftype'].required = False self.fields['iftype'].choices = self.INTERFACE_TYPE_CHOICES self.fields['iftypedata'].initial = kwargs['initial'].get('iftype') self.fields['iftype'].initial = kwargs['initial'].get('iftype') # Load the ifclass choices ifclass_choices = [] used_choices = [] if ifclass_val: label = "{}".format(ifclass_val) choice = (str(ifclass_val), label) used_choices.append(str(ifclass_val)) ifclass_choices.append(choice) else: label = "{}".format("none") val = ("none", label) used_choices.append("none") ifclass_choices.append(val) if iftype_val == 'ethernet': choices_list = [ 'none', 'platform', 'data', 'pci-sriov', 'pci-passthrough' ] elif iftype_val == 'ae': choices_list = ['none', 'platform', 'data'] elif iftype_val == 'vf': choices_list = ['none', 'data'] else: choices_list = ['platform', 'data'] choices_list = flatten(choices_list) if getattr(self.request.user, 'services_region', None) == 'RegionOne' \ and getattr(settings, 'DC_MODE', False): # Data is not available when in RegionOne of SystemController choices_list.remove('data') for choice in choices_list: if choice not in used_choices: label = "{}".format(choice) val = (str(choice), label) ifclass_choices.append(val) self.fields['ifclass'].choices = ifclass_choices if not ifclass_val: del kwargs['initial']['ifclass'] self.fields['ifclass'].initial = ('none', 'none') # Get the total possible number of VFs for SRIOV interface class port_list = sysinv.host_port_list(self.request, host_uuid) for p in port_list: if p.interface_uuid == this_interface_id: if p.sriov_totalvfs: self.fields['sriov_totalvfs'].initial = p.sriov_totalvfs else: self.fields['sriov_totalvfs'].initial = 0 break initial_numvfs = kwargs['initial']['sriov_numvfs'] if initial_numvfs: self.fields['sriov_numvfs'].initial = initial_numvfs else: self.fields['sriov_numvfs'].initial = 0