コード例 #1
0
    def test_update_pool_member(self):
        save_member_mock = patch(
            'networkapi.requisicaovips.models.ServerPoolMember.save').start()

        pool = ServerPool(id=1, default_limit=1)
        pool_member = ServerPoolMember()
        dict = {
            'nome_equips': 'equip_name',
            'weight': 1,
            'priority': 1,
            'port_real': 80
        }
        ip = Ip(id=1)
        ipv6 = Ipv6(id=1)
        update_pool_member(pool, pool_member, dict, ip, ipv6, self.user)

        self.assertEquals(pool, pool_member.server_pool)
        self.assertEquals(1, pool_member.limit)
        self.assertEquals(ip, pool_member.ip)
        self.assertEquals(ipv6, pool_member.ipv6)
        self.assertEquals('equip_name', pool_member.identifier)
        self.assertEquals(1, pool_member.weight)
        self.assertEquals(1, pool_member.priority)
        self.assertEquals(80, pool_member.port_real)
        self.assertTrue(save_member_mock.called)
コード例 #2
0
    def test_validate_change_of_environment_given_pool_associated_to_one_or_more_pool_members(
            self):
        pool = self.mock_server_pool(created=False)
        pool.serverpoolmember_set.exclude = lambda id__in: [ServerPoolMember()]
        pool.vipporttopool_set.count = lambda: 0

        with self.assertRaises(UpdateEnvironmentServerPoolMemberException):
            validate_change_of_environment(1, pool)
コード例 #3
0
def add_reals_before_script(port_vip, vip, ip, ip_type, priority, weight, port_real, user):
    """
        Add real in VIP before execute script.
        The script access the db when is executing.
        This method is called if code returns 0.
    """

    server_pool_member = ServerPoolMember()
    server_pool = ServerPool.objects.get(
        vipporttopool__port_vip=port_vip, vipporttopool__requisicao_vip=vip)
    server_pool_member.prepare_and_save(
        server_pool, ip, ip_type, priority, weight, port_real, user, True)
コード例 #4
0
 def create_pool_member(self):
     pool = self.create_server_pool_model()
     member = ServerPoolMember(server_pool=pool,
                               identifier='member_1',
                               ip=self.create_ipv4(),
                               priority=1,
                               weight=0,
                               limit=pool.default_limit,
                               port_real=8080)
     member.equipment = Equipamento(
         id=1, nome="l-59c0df40-624d-4174-ad7e-a67e54bb3ced")
     return member
コード例 #5
0
def _create_pool_member(members, pool):
    """Creates pool members"""
    for member in members:
        ip = Ip.get_by_pk(member['ip']['id']) if member['ip'] else None
        ipv6 = Ipv6.get_by_pk(member['ipv6']['id']) if member['ipv6'] else None
        identifier = ip.ip_formated if ip else ipv6.ip_formated

        pool_member = ServerPoolMember()
        pool_member.server_pool = pool
        pool_member.ip = ip
        pool_member.ipv6 = ipv6
        pool_member.identifier = identifier
        pool_member.weight = member['weight']
        pool_member.priority = member['priority']
        pool_member.port_real = member['port_real']
        pool_member.member_status = member['member_status']
        pool_member.limit = member['limit']
        pool_member.save()

        # vip with dsrl3 using pool
        if pool.dscp:

            mbs = pool_member.get_spm_by_eqpt_id(pool_member.equipment.id)

            # check all the pools related to this pool vip request to filter
            # dscp value
            related_viprequestports = pool.vips[0].viprequestport_set.all()
            vippools = [
                p.viprequestportpool_set.all()[0].server_pool_id
                for p in related_viprequestports
            ]

            sps = ServerPool.objects.filter(serverpoolmember__in=mbs).exclude(
                id__in=vippools)
            dscps = [sp.dscp for sp in sps]

            mb_name = '{}:{}'.format(
                (ip.ip_formated if ip else ipv6.ip_formated),
                member['port_real'])
            if pool.dscp in dscps:
                raise ValidationAPIException(
                    'DRSL3 Restriction: Pool Member {} cannot be insert'
                    ' in Pool {}, because already in other pool'.format(
                        mb_name, pool.identifier))

            if pool_member.port_real != pool.default_port:
                raise ValidationAPIException(
                    'DRSL3 Restriction: Pool Member {} cannot have different'
                    ' port of Pool {}'.format(mb_name, pool.identifier))
コード例 #6
0
def save_server_pool_member(user, pool, list_server_pool_member):

    list_pool_member = list()
    old_priorities_list = list()

    pool_members_to_be_removed = get_pool_members_to_be_removed(
        list_server_pool_member)
    remove_pool_members(pool_members_to_be_removed, pool, user)

    if list_server_pool_member:
        apply_new_priorities = False

        for dic in list_server_pool_member:
            ip_object, ipv6_object = get_ip_objects(dic)

            pool_member_id = dic['id_pool_member']
            if pool_member_id:
                pool_member = ServerPoolMember.objects.get(id=pool_member_id)
                old_member_priority = pool_member.priority
                old_priorities_list.append(old_member_priority)

                update_pool_member(pool, pool_member, dic, ip_object,
                                   ipv6_object, user)

                if (old_member_priority != pool_member.priority
                        and pool.pool_created):
                    apply_new_priorities = True
            else:
                pool_member = ServerPoolMember()
                update_pool_member(pool, pool_member, dic, ip_object,
                                   ipv6_object, user)
                pool_member.save()

                old_priorities_list.append(dic['priority'])

                # execute script to create real if pool already created
                # commits transaction. Rolls back if script returns error
                if pool.pool_created:
                    ip_id = ip_object and ip_object.id or ipv6_object and ipv6_object.id
                    deploy_pool_member_config(ip_id, pool.id, dic['port_real'],
                                              pool_member, user)

            list_pool_member.append(pool_member)

        # Applies new priority in pool - only 1 script run for all members
        if (apply_new_priorities):
            apply_priorities(list_pool_member, old_priorities_list, pool, user)

    return list_pool_member
コード例 #7
0
    def administrate_real(self,
                          user,
                          vip_id,
                          equip_id,
                          ip_id,
                          operation,
                          network_version,
                          port_vip=None,
                          port_real=None):

        # Valid VIP ID
        if not is_valid_int_greater_zero_param(vip_id):
            self.log.error(u'The vip_id parameter is not a valid value: %s.',
                           vip_id)
            raise InvalidValueError(None, 'vip_id', vip_id)

        # Valid Equipament ID
        if not is_valid_int_greater_zero_param(equip_id):
            self.log.error(u'The equip_id parameter is not a valid value: %s.',
                           equip_id)
            raise InvalidValueError(None, 'equip_id', equip_id)

        # Valid IP ID
        if not is_valid_int_greater_zero_param(ip_id):
            self.log.error(u'The ip_id parameter is not a valid value: %s.',
                           ip_id)
            raise InvalidValueError(None, 'ip_id', ip_id)

        # Valid operation
        if operation not in ['add', 'del', 'ena', 'dis', 'chk']:
            self.log.error(
                u'The operation parameter is not a valid value: %s.',
                operation)
            raise InvalidValueError(None, 'operation', operation)

        # Valid network version
        if network_version not in ['v4', 'v6']:
            self.log.error(
                u'The network_version parameter is not a valid value: %s.',
                network_version)
            raise InvalidValueError(None, 'network_version', network_version)

        # User permission
        if (operation == 'chk'):
            if not has_perm(user, AdminPermission.VIP_ALTER_SCRIPT,
                            AdminPermission.READ_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)
        else:
            if not has_perm(user, AdminPermission.VIP_ALTER_SCRIPT,
                            AdminPermission.WRITE_OPERATION, None, equip_id,
                            AdminPermission.EQUIP_UPDATE_CONFIG_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

        # new_call = True - New calls for Add/Del/Enable/Disable/Check with new params (Port Vip and Port Real)
        # new_call = False = Old calls for compatibility
        new_call = False
        if port_vip is not None and port_real is not None:
            # Valid ports
            if not is_valid_int_greater_zero_param(port_vip):
                self.log.error(
                    u'The port_vip parameter is not a valid value: %s.',
                    port_vip)
                raise InvalidValueError(None, 'port_vip', port_vip)

            if not is_valid_int_greater_zero_param(port_real):
                self.log.error(
                    u'The port_vip parameter is not a valid value: %s.',
                    port_real)
                raise InvalidValueError(None, 'port_real', port_real)

            new_call = True

        # Find Request VIP by ID to check if it exist
        vip = RequisicaoVips.get_by_pk(vip_id)
        # Get variables
        variables_map = vip.variables_to_map()
        # Valid variables
        # vip.set_variables(variables_map)

        evip = EnvironmentVip.get_by_values(variables_map.get('finalidade'),
                                            variables_map.get('cliente'),
                                            variables_map.get('ambiente'))

        # Valid network_version - IPv4
        if network_version == IP_VERSION.IPv4[0]:

            # Find IpEquipamento to check if it exist
            IpEquip = IpEquipamento().get_by_ip_equipment(ip_id, equip_id)

            real_name = IpEquip.equipamento.nome
            end_ip = '%s.%s.%s.%s' % (IpEquip.ip.oct1, IpEquip.ip.oct2,
                                      IpEquip.ip.oct3, IpEquip.ip.oct4)

            # Valid Real
            RequisicaoVips.valid_real_server(end_ip, IpEquip.equipamento, evip,
                                             False)

        # Valid network_version - IPv6
        elif network_version == IP_VERSION.IPv6[0]:

            # Find Ipv6Equipament to check if it exist
            Ipv6Equip = Ipv6Equipament().get_by_ip_equipment(ip_id, equip_id)

            real_name = Ipv6Equip.equipamento.nome
            end_ip = '%s:%s:%s:%s:%s:%s:%s:%s' % (
                Ipv6Equip.ip.block1, Ipv6Equip.ip.block2, Ipv6Equip.ip.block3,
                Ipv6Equip.ip.block4, Ipv6Equip.ip.block5, Ipv6Equip.ip.block6,
                Ipv6Equip.ip.block7, Ipv6Equip.ip.block8)

            # Valid Real
            RequisicaoVips.valid_real_server(end_ip, Ipv6Equip.equipamento,
                                             evip, False)

        if (operation == 'chk'):

            if IP_VERSION.IPv4[0] == network_version:
                if new_call:
                    command = VIP_REALS_v4_CHECK % (vip_id, ip_id, port_real,
                                                    port_vip)
                else:
                    command = VIP_REAL_v4_CHECK % (vip_id, real_name, end_ip)
            else:
                if new_call:
                    command = VIP_REALS_v6_CHECK % (vip_id, ip_id, port_real,
                                                    port_vip)
                else:
                    command = VIP_REAL_v6_CHECK % (vip_id, real_name, end_ip)
        else:

            with distributedlock(LOCK_VIP_IP_EQUIP %
                                 (vip_id, ip_id, equip_id)):

                if (operation == 'add'):

                    if IP_VERSION.IPv4[0] == network_version:
                        if new_call:
                            command = VIP_REALS_v4_CREATE % (
                                vip_id, ip_id, port_real, port_vip)
                            ServerPoolMember().save_specified_port(
                                vip_id, port_vip, IpEquip.ip,
                                IP_VERSION.IPv4[1], port_real, user)
                        else:
                            command = VIP_REAL_v4_CREATE % (vip_id, real_name,
                                                            end_ip)
                            ServerPoolMember().save_with_default_port(
                                vip_id, IpEquip.ip, IP_VERSION.IPv4[1], user)

                    else:
                        if new_call:
                            command = VIP_REALS_v6_CREATE % (
                                vip_id, ip_id, port_real, port_vip)
                            ServerPoolMember().save_specified_port(
                                vip_id, port_vip, Ipv6Equip.ip,
                                IP_VERSION.IPv6[1], port_real, user)
                        else:
                            command = VIP_REAL_v6_CREATE % (vip_id, real_name,
                                                            end_ip)
                            ServerPoolMember().save_with_default_port(
                                vip_id, Ipv6Equip.ip, IP_VERSION.IPv6[1], user)

                elif (operation == 'del'):

                    if IP_VERSION.IPv4[0] == network_version:
                        if new_call:
                            command = VIP_REALS_v4_REMOVE % (
                                vip_id, ip_id, port_real, port_vip)
                            pool_members = ServerPoolMember.objects.filter(
                                ip=ip_id,
                                server_pool__vipporttopool__requisicao_vip__id=
                                vip_id,
                                server_pool__vipporttopool__port_vip=port_vip,
                                port_real=port_real)
                            [
                                pool_member.delete()
                                for pool_member in pool_members
                            ]
                        else:
                            command = VIP_REAL_v4_REMOVE % (vip_id, real_name,
                                                            end_ip)
                            pool_members = ServerPoolMember.objects.filter(
                                ip=ip_id,
                                server_pool__vipporttopool__requisicao_vip__id=
                                vip_id)
                            [
                                pool_member.delete()
                                for pool_member in pool_members
                            ]
                    else:
                        if new_call:
                            command = VIP_REALS_v6_REMOVE % (
                                vip_id, ip_id, port_real, port_vip)
                            pool_members = ServerPoolMember.objects.filter(
                                ipv6=ip_id,
                                server_pool__vipporttopool__requisicao_vip__id=
                                vip_id,
                                server_pool__vipporttopool__port_vip=port_vip,
                                port_real=port_real)
                            [
                                pool_member.delete()
                                for pool_member in pool_members
                            ]
                        else:
                            command = VIP_REAL_v6_REMOVE % (vip_id, real_name,
                                                            end_ip)
                            pool_members = ServerPoolMember.objects.filter(
                                ipv6=ip_id,
                                server_pool__vipporttopool__requisicao_vip__id=
                                vip_id)
                            [
                                pool_member.delete()
                                for pool_member in pool_members
                            ]

                elif (operation == 'ena'):

                    if IP_VERSION.IPv4[0] == network_version:
                        if new_call:
                            command = VIP_REALS_v4_ENABLE % (
                                vip_id, ip_id, port_real, port_vip)
                        else:
                            command = VIP_REAL_v4_ENABLE % (vip_id, real_name,
                                                            end_ip)
                    else:
                        if new_call:
                            command = VIP_REALS_v6_ENABLE % (
                                vip_id, ip_id, port_real, port_vip)
                        else:
                            command = VIP_REAL_v6_ENABLE % (vip_id, real_name,
                                                            end_ip)

                elif (operation == 'dis'):

                    if IP_VERSION.IPv4[0] == network_version:
                        if new_call:
                            command = VIP_REALS_v4_DISABLE % (
                                vip_id, ip_id, port_real, port_vip)
                        else:
                            command = VIP_REAL_v4_DISABLE % (vip_id, real_name,
                                                             end_ip)
                    else:
                        if new_call:
                            command = VIP_REALS_v6_DISABLE % (
                                vip_id, ip_id, port_real, port_vip)
                        else:
                            command = VIP_REAL_v6_DISABLE % (vip_id, real_name,
                                                             end_ip)

        self.log.info(command)
        # Execute script
        code, stdout, stderr = exec_script(command)
        self.log.info(stdout)

        map = dict()
        success_map = dict()

        # Return XML
        if code == 0:
            success_map['codigo'] = '%04d' % code
            success_map['descricao'] = {'stdout': stdout, 'stderr': stderr}

            map['sucesso'] = success_map
            return self.response(dumps_networkapi(map))

        elif code == 12:
            success_map['codigo'] = '0'
            success_map['descricao'] = {'stdout': '0', 'stderr': ''}

            map['sucesso'] = success_map
            self.rollback_changes(operation, new_call, network_version, vip_id,
                                  ip_id, port_real, port_vip, real_name,
                                  end_ip, user)
            return self.response(dumps_networkapi(map))

        else:
            self.rollback_changes(operation, new_call, network_version, vip_id,
                                  ip_id, port_real, port_vip, real_name,
                                  end_ip, user)
            return self.response_error(2, stdout + stderr)