Example #1
0
def update(request, pk):
    """
    Update Vip Request

    :param request:
    :param pk: Identifier Vip Request
    :return: Data Serialized Post Update
    """

    data = request.DATA
    user = request.user

    if not is_valid_int_greater_zero_param(pk):
        raise exceptions.InvalidIdVipRequestException()

    vip_ports = data.get("vip_ports_to_pools")

    req_vip_serializer = RequestVipSerializer(
        data=data
    )

    if not req_vip_serializer.is_valid():
        log.error(req_vip_serializer.errors)
        raise api_exceptions.ValidationException()

    RequisicaoVips.objects.get(pk=pk)

    with distributedlock(LOCK_VIP % pk):

        obj_req_vip = req_vip_serializer.object
        obj_req_vip.id = int(pk)
        obj_req_vip.filter_valid = True
        obj_req_vip.validado = False
        set_l7_filter_for_vip(obj_req_vip)
        obj_req_vip.set_new_variables(data)
        obj_req_vip.save(user)

        vip_port_serializer = VipPortToPoolSerializer(data=vip_ports, many=True)

        if not vip_port_serializer.is_valid():
            raise api_exceptions.ValidationException("Invalid Port Vip To Pool")

        vip_port_to_pool_pks = [port['id'] for port in vip_ports if port.get('id')]

        vip_port_to_pool_to_remove = VipPortToPool.objects.filter(
            requisicao_vip=obj_req_vip
        ).exclude(
            id__in=vip_port_to_pool_pks
        )

        # valid if pools member can linked by environment/environment vip relationship rule
        server_pool_ips_can_associate_with_vip_request(obj_req_vip, vip_port_to_pool_to_remove)

        for v_port_to_del in vip_port_to_pool_to_remove:
            v_port_to_del.delete(user)

        for vip_port in vip_ports:
            vip_port_obj = VipPortToPool()
            vip_port_obj.id = vip_port.get('id')
            vip_port_obj.server_pool = ServerPool(id=vip_port.get('server_pool'))
            vip_port_obj.port_vip = vip_port.get('port_vip')
            vip_port_obj.requisicao_vip = obj_req_vip
            vip_port_obj.save(user)

        return req_vip_serializer.data
Example #2
0
def update(request, pk):
    """
    Update Vip Request

    :param request:
    :param pk: Identifier Vip Request
    :return: Data Serialized Post Update
    """

    data = request.DATA
    user = request.user

    if not is_valid_int_greater_zero_param(pk):
        raise exceptions.InvalidIdVipRequestException()

    vip_ports = data.get("vip_ports_to_pools")

    req_vip_serializer = RequestVipSerializer(
        data=data
    )

    if not req_vip_serializer.is_valid():
        log.error(req_vip_serializer.errors)
        raise api_exceptions.ValidationException()

    #test if request exists
    RequisicaoVips.objects.get(pk=pk)

    with distributedlock(LOCK_VIP % pk):

        obj_req_vip = req_vip_serializer.object
        #compatibility issues
        if obj_req_vip.trafficreturn is None:
            obj_req_vip.trafficreturn = RequisicaoVips.objects.get(pk=pk).trafficreturn

        obj_req_vip.id = int(pk)
        obj_req_vip.filter_valid = True
        obj_req_vip.validado = False
        set_l7_filter_for_vip(obj_req_vip)
        obj_req_vip.set_new_variables(data)


        old_trafficreturn = RequisicaoVips.objects.get(pk=pk).trafficreturn
        if old_trafficreturn.id != obj_req_vip.trafficreturn.id:
            if obj_req_vip.trafficreturn.nome_opcao_txt == "DSRL3": 
                dsrl3_to_vip_obj = DsrL3_to_Vip()
                dsrl3_to_vip_obj.get_dsrl3(obj_req_vip, user)
            else:
                try:
                    dsrl3_to_vip_obj = DsrL3_to_Vip.get_by_vip_id(obj_req_vip.id)
                    dsrl3_to_vip_obj.delete(user)
                except ObjectDoesNotExist, e:
                    pass

        obj_req_vip.save()

        vip_port_serializer = VipPortToPoolSerializer(data=vip_ports, many=True)

        if not vip_port_serializer.is_valid():
            raise api_exceptions.ValidationException("Invalid Port Vip To Pool")

        vip_port_to_pool_pks = [port['id'] for port in vip_ports if port.get('id')]

        vip_port_to_pool_to_remove = VipPortToPool.objects.filter(
            requisicao_vip=obj_req_vip
        ).exclude(
            id__in=vip_port_to_pool_pks
        )

        # valid if pools member can linked by environment/environment vip relationship rule
        server_pool_ips_can_associate_with_vip_request(obj_req_vip, vip_port_to_pool_to_remove)

        for v_port_to_del in vip_port_to_pool_to_remove:
            v_port_to_del.delete()

        for vip_port in vip_ports:
            vip_port_obj = VipPortToPool()
            vip_port_obj.id = vip_port.get('id')
            vip_port_obj.server_pool = ServerPool(id=vip_port.get('server_pool'))
            vip_port_obj.port_vip = vip_port.get('port_vip')
            vip_port_obj.requisicao_vip = obj_req_vip
            vip_port_obj.save()

        return req_vip_serializer.data