Exemplo n.º 1
0
class VariablebyPkView(APIView):

    @permission_classes((IsAuthenticated, Write))
    def delete(self, *args, **kwargs):
        try:
            log.info('DELETE VARIABLE')

            variable_id = kwargs['variable_id']

            if not is_valid_int_greater_zero_param(variable_id, False):
                raise api_exceptions.ValidationException(
                    'Variable id invalid.')

            facade.delete_variable(self.request.user, variable_id)
            data = dict()
            data['variable'] = 'ok'

            return Response(data, status=status.HTTP_200_OK)

        except ObjectDoesNotExist, exception:
            log.error(exception)
            raise exceptions.VariableDoesNotExistException()

        except Exception, exception:
            log.exception(exception)
            raise api_exceptions.ValidationException('Variable id invalid.')
Exemplo n.º 2
0
def save(request):
    """
    Save Vip Request

    :param request: Request
    :return: Data Serialized Post Save
    """

    data = request.DATA
    user = request.user

    req_vip_serializer = RequestVipSerializer(
        data=data
    )

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

    obj_req_vip = req_vip_serializer.object

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

    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.trafficreturn=OptionVip.get_by_pk(int(data['trafficreturn']))
    if obj_req_vip.trafficreturn is None:
        obj_req_vip.trafficreturn = OptionVip.get_by_pk(12)

    obj_req_vip.save(user)

    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)

    for v_port in obj_req_vip.vip_ports_to_pools:
        v_port.requisicao_vip = obj_req_vip
        v_port.save()

    # SYNC_VIP
    syncs.old_to_new(obj_req_vip)

    return req_vip_serializer.data
Exemplo n.º 3
0
    def delete(self, *args, **kwargs):
        try:
            log.info("DELETE VARIABLE")

            variable_id = kwargs['variable_id']

            if not is_valid_int_greater_zero_param(variable_id, False):
                raise api_exceptions.ValidationException(
                    'Variable id invalid.')

            facade.delete_variable(self.request.user, variable_id)
            data = dict()
            data['variable'] = "ok"

            return Response(data, status=status.HTTP_200_OK)

        except ObjectDoesNotExist, exception:
            log.error(exception)
            raise exceptions.VariableDoesNotExistException()
Exemplo n.º 4
0
    except EnvironmentVipNotFoundError, error:
        log.error(error)
        raise exceptions.EnvironmentVipDoesNotExistException()

    except exceptions.InvalidIdVipRequestException, exception:
        log.error(exception)
        raise exception

    except RequisicaoVips.DoesNotExist, exception:
        log.error(exception)
        raise exceptions.VipRequestDoesNotExistException()

    except (InvalidValueError, RequisicaoVipsError,
            api_exceptions.ValidationException), exception:
        log.error(exception)
        raise api_exceptions.ValidationException()

    except api_exceptions.EnvironmentEnvironmentVipNotBoundedException, exception:
        log.error(exception)
        raise exception

    except Exception, exception:
        log.error(exception)
        raise api_exceptions.NetworkAPIException()


@api_view(['GET'])
@permission_classes((IsAuthenticated, Read))
@commit_on_success
def get_by_pk(request, pk):
Exemplo n.º 5
0
def networkIPv6_deploy(request, network_id):
    """Deploy network L3 configuration in the environment routers for network ipv6

    Receives optional parameter equipments to specify what equipment should
    receive network configuration
    """

    networkipv6 = NetworkIPv6.get_by_pk(int(network_id))
    environment = networkipv6.vlan.ambiente
    equipments_id_list = None
    if request.DATA is not None:
        equipments_id_list = request.DATA.get('equipments', None)

    equipment_list = []

    if equipments_id_list is not None:
        if type(equipments_id_list) is not list:
            raise api_exceptions.ValidationException('equipments')

        for equip in equipments_id_list:
            try:
                int(equip)
            except ValueError:
                raise api_exceptions.ValidationException('equipments')

        # Check that equipments received as parameters are in correct vlan
        # environment
        equipment_list = Equipamento.objects.filter(
            equipamentoambiente__ambiente=environment,
            id__in=equipments_id_list)
        log.info('list = %s' % equipment_list)
        if len(equipment_list) != len(equipments_id_list):
            log.error(
                'Error: equipments %s are not part of network environment.' %
                equipments_id_list)
            raise exceptions.EquipmentIDNotInCorrectEnvException()
    else:
        # TODO GET network routers
        equipment_list = Equipamento.objects.filter(
            ipv6equipament__ip__networkipv6=networkipv6,
            equipamentoambiente__ambiente=networkipv6.vlan.ambiente,
            equipamentoambiente__is_router=1).distinct()
        if len(equipment_list) == 0:
            raise exceptions.NoEnvironmentRoutersFoundException()

    # Check permission to configure equipments
    for equip in equipment_list:
        # User permission
        if not has_perm(request.user, AdminPermission.EQUIPMENT_MANAGEMENT,
                        AdminPermission.WRITE_OPERATION, None, equip.id,
                        AdminPermission.EQUIP_WRITE_OPERATION):
            log.error(
                u'User does not have permission to perform the operation.')
            raise PermissionDenied(
                'No permission to configure equipment %s-%s' %
                (equip.id, equip.nome))

    if all_equipments_are_in_maintenance(equipment_list):
        raise AllEquipmentsAreInMaintenanceException()

    try:
        # deploy network configuration
        if request.method == 'POST':
            returned_data = facade.deploy_networkIPv6_configuration(
                request.user, networkipv6, equipment_list)
        elif request.method == 'DELETE':
            returned_data = facade.remove_deploy_networkIPv6_configuration(
                request.user, networkipv6, equipment_list)

        return Response(returned_data)

    except Exception, exception:
        log.error(exception)
        raise api_exceptions.NetworkAPIException()
Exemplo n.º 6
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:
                    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()

        # SYNC_VIP
        syncs.old_to_new(obj_req_vip)

        return req_vip_serializer.data