示例#1
0
    def create(self, data):
        """ Creates a new Port Channel """

        log.info("Create Channel")
        log.debug(data)

        interfaces = data.get('interfaces')
        nome = data.get('name')
        lacp = data.get('lacp')
        int_type = data.get('int_type')
        vlan_nativa = data.get('vlan')
        envs_vlans = data.get('envs_vlans')

        api_interface_facade.verificar_vlan_nativa(vlan_nativa)

        # Checks if Port Channel name already exists on equipment
        api_interface_facade.check_channel_name_on_equipment(nome, interfaces)

        self.channel = PortChannel()
        self.channel.nome = str(nome)
        self.channel.lacp = convert_string_or_int_to_boolean(lacp, True)
        self.channel.create()

        ifaces_on_channel = []
        for interface in interfaces:

            iface = Interface.objects.get(id=interface)
            type_obj = TipoInterface.objects.get(tipo=int_type)

            if iface.channel:
                raise InterfaceError(
                    'Interface %s is already a Channel' % iface.interface
                    )

            if iface.equipamento.id not in ifaces_on_channel:
                ifaces_on_channel.append(int(iface.equipamento.id))
                if len(ifaces_on_channel) > 2:
                    raise InterfaceError('More than one equipment selected')

            iface.channel = self.channel
            iface.int_type = type_obj
            iface.vlan_nativa = vlan_nativa
            iface.save()

            log.debug("interface updated %s" % iface.id)

            self._dissociate_ifaces_envs(iface)

            if 'trunk' in int_type.lower():
                self._create_ifaces_on_trunks(iface, envs_vlans)

        return {'channels': self.channel.id}
示例#2
0
    def create(self, data):
        """ Creates a new Port Channel """

        try:
            interfaces = data.get('interfaces')
            nome = data.get('nome')
            lacp = data.get('lacp')
            int_type = data.get('int_type')
            vlan_nativa = data.get('vlan')
            envs_vlans = data.get('envs')

            api_interface_facade.verificar_vlan_nativa(vlan_nativa)

            # Checks if Port Channel name already exists on equipment
            interfaces = str(interfaces).split('-')
            api_interface_facade.check_channel_name_on_equipment(
                nome, interfaces)

            self.channel = PortChannel()
            self.channel.nome = str(nome)
            self.channel.lacp = convert_string_or_int_to_boolean(lacp)
            self.channel.create(user)

            int_type = TipoInterface.get_by_name(str(int_type))

            ifaces_on_channel = []
            for interface in interfaces:

                if interface:
                    iface = Interface.get_by_pk(int(interface))

                    self._update_interfaces_from_a_channel(
                        iface, vlan_nativa, ifaces_on_channel, int_type)

                    if 'trunk' in int_type.tipo:
                        self._create_ifaces_on_trunks(sw_router, envs_vlans)

        except Exception as err:
            return {"error": str(err)}

        return {'port_channel': self.channel}
示例#3
0
    def update(self, data):

        try:
            id_channel = data.get('id')
            name = data.get('name')
            lacp = data.get('lacp')
            int_type = data.get('int_type')
            vlan_nativa = data.get('vlan')
            envs_vlans = data.get('envs_vlans')
            interfaces = data.get('interfaces')
            protected = data.get('protected')

            self.channel = PortChannel.get_by_pk(int(id_channel))

            if not interfaces:
                raise InterfaceError('No interfaces selected')

            if not is_valid_int_greater_zero_param(name):
                raise InvalidValueError(None, 'Channel number',
                                        'must be integer.')

            api_interface_facade.verificar_vlan_nativa(vlan_nativa)

            # Dissociate old interfaces
            interfaces_old = Interface.objects.filter(
                channel__id=int(id_channel)
                )
            log.debug(interfaces_old)
            server = None
            for i in interfaces_old:
                server = i.ligacao_front.equipamento.id
                i.channel = None
                i.save()
                log.debug(i.id)

            api_interface_facade.check_channel_name_on_equipment(name,
                                                                 interfaces)

            # update channel
            self.channel.nome = str(name)
            self.channel.lacp = convert_string_or_int_to_boolean(lacp, True)
            self.channel.save()

            type_obj = TipoInterface.objects.get(tipo=int_type)

            ifaces_on_channel = list()

            for interface in interfaces:

                iface = Interface.objects.get(id=int(interface))

                if server:
                    if not int(iface.ligacao_front.equipamento.id) == int(server):
                        raise Exception('Interface is connected to another server. Ids: %s %s ' %
                                        (iface.ligacao_front.equipamento.id, server))

                if iface.channel:
                    raise InterfaceError(
                        'Interface %s is already in a Channel'
                        % iface.interface
                        )

                if iface.equipamento.id not in ifaces_on_channel:
                    ifaces_on_channel.append(int(iface.equipamento.id))
                    if len(ifaces_on_channel) > 2:
                        raise InterfaceError(
                            'More than one equipment selected.'
                            )

                iface.channel = self.channel
                iface.tipo = type_obj
                iface.vlan_nativa = vlan_nativa
                iface.protegida = convert_string_or_int_to_boolean(protected,
                                                                   True)
                iface.save()

                log.debug("interface updated %s" % iface.id)

                self._dissociate_ifaces_envs(iface)

                # associate the new envs
                if 'trunk' in int_type.lower():
                    self._create_ifaces_on_trunks(iface, envs_vlans)

        except Exception as err:
            log.error(str(err))
            raise Exception({"error": str(err)})

        return {'channels': self.channel.id}
示例#4
0
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to add Rack.
        URL: channel/inserir/
        """
        try:
            self.log.info('Inserir novo Channel')

            # User permission
            if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(
                    3,
                    u'There is no value to the networkapi tag  of XML request.'
                )

            channel_map = networkapi_map.get('channel')
            if channel_map is None:
                return self.response_error(
                    3,
                    u'There is no value to the channel tag  of XML request.')

            # Get XML data
            interfaces = channel_map.get('interfaces')
            nome = channel_map.get('nome')
            lacp = channel_map.get('lacp')
            int_type = channel_map.get('int_type')
            vlan_nativa = channel_map.get('vlan')
            envs_vlans = channel_map.get('envs')

            port_channel = PortChannel()
            interface = Interface()
            amb = Ambiente()
            cont = []

            api_interface_facade.verificar_vlan_nativa(vlan_nativa)

            # verifica se o nome do port channel já existe no equipamento
            interfaces = str(interfaces).split('-')
            api_interface_facade.check_channel_name_on_equipment(
                nome, interfaces)

            # cria o port channel
            port_channel.nome = str(nome)
            port_channel.lacp = convert_string_or_int_to_boolean(lacp)
            port_channel.create(user)

            int_type = TipoInterface.get_by_name(str(int_type))

            for var in interfaces:
                if not var == '' and not var is None:
                    interf = interface.get_by_pk(int(var))

                    try:
                        sw_router = interf.get_switch_and_router_interface_from_host_interface(
                            interf.protegida)
                    except:
                        raise InterfaceError('Interface não conectada')

                    if sw_router.channel is not None:
                        raise InterfaceError(
                            'Interface %s já está em um Channel' %
                            sw_router.interface)

                    if cont is []:
                        cont.append(int(sw_router.equipamento.id))
                    elif not sw_router.equipamento.id in cont:
                        cont.append(int(sw_router.equipamento.id))
                        if len(cont) > 2:
                            raise InterfaceError(
                                'Mais de dois equipamentos foram selecionados')

                    if sw_router.ligacao_front is not None:
                        ligacao_front_id = sw_router.ligacao_front.id
                    else:
                        ligacao_front_id = None
                    if sw_router.ligacao_back is not None:
                        ligacao_back_id = sw_router.ligacao_back.id
                    else:
                        ligacao_back_id = None

                    Interface.update(user,
                                     sw_router.id,
                                     interface=sw_router.interface,
                                     protegida=sw_router.protegida,
                                     descricao=sw_router.descricao,
                                     ligacao_front_id=ligacao_front_id,
                                     ligacao_back_id=ligacao_back_id,
                                     tipo=int_type,
                                     vlan_nativa=vlan_nativa,
                                     channel=port_channel)

                    if 'trunk' in int_type.tipo:
                        interface_list = EnvironmentInterface.objects.all(
                        ).filter(interface=sw_router.id)
                        for int_env in interface_list:
                            int_env.delete()
                        if type(envs_vlans) is not list:
                            d = envs_vlans
                            envs_vlans = []
                            envs_vlans.append(d)
                        for i in envs_vlans:
                            amb = amb.get_by_pk(int(i.get('env')))
                            amb_int = EnvironmentInterface()
                            amb_int.interface = sw_router
                            amb_int.ambiente = amb
                            try:
                                range_vlans = i.get('vlans')
                            except:
                                range_vlans = None
                                pass
                            if range_vlans:
                                api_interface_facade.verificar_vlan_range(
                                    amb, range_vlans)
                                amb_int.vlans = range_vlans
                            amb_int.create(user)

            port_channel_map = dict()
            port_channel_map['port_channel'] = port_channel

            return self.response(
                dumps_networkapi({'port_channel': port_channel_map}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)