Exemplo n.º 1
0
 def get_data_networks_data(self):
     try:
         datanets = \
             stx_sysinv.data_network_list(self.tab_group.request)
     except Exception:
         datanets = []
         msg = _('Unable to get provider network list.')
         exceptions.check_message(["Connection", "refused"], msg)
         raise
     return datanets
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    def handle(self, request, data):
        host_id = data['host_id']

        try:
            del data['host_id']

            if data['ports']:
                del data['uses']
            else:
                uses = data['uses'][:]
                data['uses'] = uses
                del data['ports']

            datanetwork_uuids = []
            if data['datanetworks']:
                datanetworks_list = sysinv.data_network_list(self.request)
                for n in data['datanetworks'].split(","):
                    for dn in datanetworks_list:
                        if dn.name == n:
                            datanetwork_uuids.append(dn.uuid)

            if not data['vlan_id'] or data['iftype'] != 'vlan':
                del data['vlan_id']
            else:
                data['vlan_id'] = str(data['vlan_id'])

            network_ids = []
            network_uuids = []
            network_types = []
            if data['networks']:
                for n in data['networks']:
                    network = sysinv.network_get(request, n)
                    network_ids.append(str(network.id))
                    network_uuids.append(network.uuid)
                    network_types.append(network.type)

            if any(network_type in ['mgmt', 'cluster-host', 'oam', 'storage']
                   for network_type in network_types):
                del data['imtu']
            else:
                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 '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'])

            if data['ifclass'] != 'pci-sriov':
                del data['sriov_numvfs']
                del data['sriov_vf_driver']

            del data['datanetworks']
            del data['networks']
            interface = sysinv.host_interface_create(request, **data)

            ifnet_data = {}
            for n in network_uuids:
                ifnet_data['interface_uuid'] = interface.uuid
                ifnet_data['network_uuid'] = n
                sysinv.interface_network_assign(request, **ifnet_data)

            for n in datanetwork_uuids:
                ifnet_data['interface_uuid'] = interface.uuid
                ifnet_data['datanetwork_uuid'] = n
                sysinv.interface_datanetwork_assign(request, **ifnet_data)

            msg = _('Interface "%s" was successfully'
                    ' created.') % 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 create interface "%s".') % data['ifname']
            LOG.info(msg)
            redirect = reverse(self.failure_url, args=[host_id])
            exceptions.handle(request, msg, redirect=redirect)
Exemplo n.º 4
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')