Esempio n. 1
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['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')
Esempio n. 2
0
    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)
Esempio n. 3
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')