def handle(self, request, data): host_id = data['host_id'] interface_id = data['id'] host_uuid = data['ihost_uuid'] try: if data['ports']: del data['uses'] else: uses = data['uses'][:] data['usesmodify'] = ','.join(uses) del data['ports'] del data['uses'] del data['id'] del data['host_id'] del data['ihost_uuid'] if not data['vlan_id'] or data['iftype'] != 'vlan': del data['vlan_id'] else: data['vlan_id'] = str(data['vlan_id']) data['imtu'] = str(data['imtu']) if data['iftype'] != 'ae': del data['txhashpolicy'] del data['aemode'] del data['primary_reselect'] elif data['aemode'] == 'active_standby': del data['txhashpolicy'] elif data['aemode'] != 'active_standby': del data['primary_reselect'] if not data['ifclass'] or data['ifclass'] == 'none': avail_port_list = sysinv.host_port_list( self.request, host_uuid) current_interface = sysinv.host_interface_get( self.request, interface_id) if (data['iftype'] != 'ae' or data['iftype'] != 'vlan' or data['iftype' != 'vf']): for p in avail_port_list: if p.interface_uuid == current_interface.uuid: data['ifname'] = p.get_port_display_name() break if 'sriov_numvfs' in data: data['sriov_numvfs'] = str(data['sriov_numvfs']) if 'sriov_vf_driver' in data: data['sriov_vf_driver'] = str(data['sriov_vf_driver']) # Explicitly set iftype when user selects pci-pt or pci-sriov ifclass = \ flatten(list(nt) for nt in self.fields['ifclass'].choices) if 'pci-passthrough' in ifclass or \ ('pci-sriov' in ifclass and data['sriov_numvfs']): current_interface = sysinv.host_interface_get( self.request, interface_id) if current_interface.iftype not in ['ethernet', 'vf']: # Only ethernet and VF interfaces can be pci-sriov msg = _('pci-passthrough or pci-sriov can only' ' be set on ethernet or VF interfaces') messages.error(request, msg) LOG.error(msg) # Redirect to failure pg redirect = reverse(self.failure_url, args=[host_id]) return shortcuts.redirect(redirect) else: data['iftype'] = current_interface.iftype del data['sriov_totalvfs'] if data['ifclass'] != 'pci-sriov': del data['sriov_numvfs'] del data['sriov_vf_driver'] if data['interface_networks_to_remove']: for n in data['interface_networks_to_remove']: sysinv.interface_network_remove(request, n) if data['interface_datanetworks_to_remove']: for n in data['interface_datanetworks_to_remove']: sysinv.interface_datanetwork_remove(request, n) # Assign networks to the interface ifnet_data = {} current_interface = sysinv.host_interface_get( self.request, interface_id) ifnet_data['interface_uuid'] = current_interface.uuid networks_to_add = data['networks_to_add'] datanetworks_to_add = data['datanetworks_to_add'] del data['networks'] del data['networks_to_add'] del data['interface_networks_to_remove'] del data['datanetworks'] del data['datanetworks_to_add'] del data['interface_datanetworks_to_remove'] interface = sysinv.host_interface_update(request, interface_id, **data) if networks_to_add: for n in networks_to_add: ifnet_data['network_uuid'] = n sysinv.interface_network_assign(request, **ifnet_data) elif datanetworks_to_add: for n in datanetworks_to_add: ifnet_data['datanetwork_uuid'] = n sysinv.interface_datanetwork_assign(request, **ifnet_data) msg = _('Interface "%s" was' ' successfully updated.') % data['ifname'] LOG.debug(msg) messages.success(request, msg) return interface except exc.ClientException as ce: # Allow REST API error message to appear on UI messages.error(request, ce) LOG.error(ce) # Redirect to failure page redirect = reverse(self.failure_url, args=[host_id]) return shortcuts.redirect(redirect) except Exception: msg = _('Failed to update interface "%s".') % data['ifname'] LOG.info(msg) redirect = reverse(self.failure_url, args=[host_id]) exceptions.handle(request, msg, redirect=redirect)
def __init__(self, *args, **kwargs): super(AddInterface, self).__init__(*args, **kwargs) # Populate Available Port Choices # Only include ports that are not already part of other interfaces this_interface_id = 0 current_interface = None if (type(self) is UpdateInterface): this_interface_id = kwargs['initial']['id'] current_interface = sysinv.host_interface_get( self.request, this_interface_id) else: self.fields['datanetworks_pci'].widget = \ forms.widgets.HiddenInput() host_uuid = kwargs['initial']['ihost_uuid'] # 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) # Populate Network Choices networks = sysinv.network_list(self.request) network_choices = _get_network_choices(networks) self.fields['networks'].choices = network_choices # Populate Data Network Choices by querying SysInv self.extras = {} used_datanets = [] ifdns = sysinv.interface_datanetwork_list_by_host( self.request, host_uuid) for i in ifdns: if not current_interface or \ i.interface_uuid != current_interface.uuid: iface = sysinv.host_interface_get(self.request, i.interface_uuid) if iface.ifclass == 'data': used_datanets.append(i.datanetwork_name) datanet_choices = [] datanet_filtered = [] initial_datanet_name = [] if getattr(self.request.user, 'services_region', None) == 'RegionOne' \ and getattr(settings, 'DC_MODE', False): nt_choices = self.fields['ifclass'].choices self.fields['ifclass'].choices = [ i for i in nt_choices if i[0] != 'data' ] datanets = sysinv.data_network_list(self.request) for dn in datanets: label = "{} (mtu={})".format(dn.name, dn.mtu) datanet = (str(dn.name), label) datanet_choices.append(datanet) if dn.name not in used_datanets: datanet_filtered.append(datanet) initial_datanet_name.append(str(dn.name)) self.fields['datanetworks_data'].choices = datanet_filtered self.fields['datanetworks_sriov'].choices = datanet_filtered if (type(self) is UpdateInterface): self.fields['datanetworks_pci'].choices = datanet_choices # set initial selection for UpdateInterface self.fields['datanetworks_data'].initial = initial_datanet_name self.fields['datanetworks_pci'].initial = initial_datanet_name self.fields['datanetworks_sriov'].initial = initial_datanet_name if current_interface: # update operation if not current_interface.uses: # update default interfaces self.fields['uses'].widget = forms.widgets.HiddenInput() avail_port_list = sysinv.host_port_list( self.request, host_uuid) for p in avail_port_list: if p.interface_uuid == this_interface_id: self.fields['ports'].initial = p.uuid else: # update non default interfaces avail_interface_list = sysinv.host_interface_list( self.request, host_uuid) interface_tuple_list = [] for i in avail_interface_list: if i.uuid != current_interface.uuid: interface_tuple_list.append( (i.uuid, "%s (%s, %s)" % (i.ifname, i.imac, i.ifclass))) uses_initial = [ i.uuid for i in avail_interface_list if i.ifname in current_interface.uses ] self.fields['uses'].initial = uses_initial self.fields['uses'].choices = interface_tuple_list if current_interface.vlan_id: self.fields['vlan_id'].initial = current_interface.vlan_id else: # add operation avail_interface_list = sysinv.host_interface_list( self.request, host_uuid) interface_tuple_list = [] for i in avail_interface_list: interface_tuple_list.append( (i.uuid, "%s (%s, %s)" % (i.ifname, i.imac, i.ifclass))) self.fields['uses'].choices = interface_tuple_list self.fields['ifclass'].initial = ('none', 'none')
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
def __init__(self, *args, **kwargs): super(AddInterface, self).__init__(*args, **kwargs) # Populate Available Port Choices # Only include ports that are not already part of other interfaces this_interface_id = 0 current_interface = None if (type(self) is UpdateInterface): this_interface_id = kwargs['initial']['id'] current_interface = sysinv.host_interface_get( self.request, this_interface_id) else: self.fields['providernetworks_sriov'].widget = \ forms.widgets.HiddenInput() self.fields['providernetworks_pci'].widget = \ forms.widgets.HiddenInput() host_uuid = kwargs['initial']['ihost_uuid'] # Retrieve SDN configuration sdn_enabled = kwargs['initial']['sdn_enabled'] sdn_l3_mode = kwargs['initial']['sdn_l3_mode_enabled'] # 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) # Populate Network Choices networks = sysinv.network_list(self.request) network_choices = _get_network_choices(networks) self.fields['networks'].choices = network_choices # Populate Provider Network Choices by querying Neutron self.extras = {} interfaces = sysinv.host_interface_list(self.request, host_uuid) used_providernets = [] for i in interfaces: if i.ifclass == 'data' and \ i.providernetworks and \ i.uuid != this_interface_id: used_providernets = used_providernets + \ i.providernetworks.split(",") providernet_choices = [] providernet_filtered = [] if getattr(self.request.user, 'services_region', None) == 'RegionOne' \ and getattr(settings, 'DC_MODE', False): nt_choices = self.fields['ifclass'].choices self.fields['ifclass'].choices = [ i for i in nt_choices if i[0] != 'data' ] else: providernets = neutron.provider_network_list(self.request) for provider in providernets: label = "{} (mtu={})".format(provider.name, provider.mtu) providernet = (str(provider.name), label) providernet_choices.append(providernet) if provider.name not in used_providernets: providernet_filtered.append(providernet) self.fields['providernetworks_data'].choices = providernet_filtered if (type(self) is UpdateInterface): self.fields['providernetworks_pci'].choices = providernet_choices self.fields['providernetworks_sriov'].choices = providernet_choices if current_interface: # update operation if not current_interface.uses: # update default interfaces self.fields['uses'].widget = forms.widgets.HiddenInput() avail_port_list = sysinv.host_port_list( self.request, host_uuid) for p in avail_port_list: if p.interface_uuid == this_interface_id: self.fields['ports'].initial = p.uuid else: # update non default interfaces avail_interface_list = sysinv.host_interface_list( self.request, host_uuid) interface_tuple_list = [] for i in avail_interface_list: if i.uuid != current_interface.uuid: interface_tuple_list.append( (i.uuid, "%s (%s, %s)" % (i.ifname, i.imac, i.ifclass))) uses_initial = [ i.uuid for i in avail_interface_list if i.ifname in current_interface.uses ] self.fields['uses'].initial = uses_initial self.fields['uses'].choices = interface_tuple_list if current_interface.vlan_id: self.fields['vlan_id'].initial = current_interface.vlan_id else: # add operation avail_interface_list = sysinv.host_interface_list( self.request, host_uuid) interface_tuple_list = [] for i in avail_interface_list: interface_tuple_list.append( (i.uuid, "%s (%s, %s)" % (i.ifname, i.imac, i.ifclass))) self.fields['uses'].choices = interface_tuple_list self.fields['ifclass'].initial = ('none', 'none')