Search in sources :

Example 51 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project Sentinel by alibaba.

the class FlowControllerV1 method apiAddFlowRule.

@PostMapping("/rule")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<FlowRuleEntity> apiAddFlowRule(@RequestBody FlowRuleEntity entity) {
    Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(null);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    entity.setLimitApp(entity.getLimitApp().trim());
    entity.setResource(entity.getResource().trim());
    try {
        entity = repository.save(entity);
        publishRules(entity.getApp(), entity.getIp(), entity.getPort()).get(5000, TimeUnit.MILLISECONDS);
        return Result.ofSuccess(entity);
    } catch (Throwable t) {
        Throwable e = t instanceof ExecutionException ? t.getCause() : t;
        logger.error("Failed to add new flow rule, app={}, ip={}", entity.getApp(), entity.getIp(), e);
        return Result.ofFail(-1, e.getMessage());
    }
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) ExecutionException(java.util.concurrent.ExecutionException) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 52 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project Sentinel by alibaba.

the class FlowControllerV1 method apiUpdateFlowRule.

@PutMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<FlowRuleEntity> apiUpdateFlowRule(Long id, String app, String limitApp, String resource, Integer grade, Double count, Integer strategy, String refResource, Integer controlBehavior, Integer warmUpPeriodSec, Integer maxQueueingTimeMs) {
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    FlowRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }
    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }
    if (StringUtil.isNotBlank(limitApp)) {
        entity.setLimitApp(limitApp.trim());
    }
    if (StringUtil.isNotBlank(resource)) {
        entity.setResource(resource.trim());
    }
    if (grade != null) {
        if (grade != 0 && grade != 1) {
            return Result.ofFail(-1, "grade must be 0 or 1, but " + grade + " got");
        }
        entity.setGrade(grade);
    }
    if (count != null) {
        entity.setCount(count);
    }
    if (strategy != null) {
        if (strategy != 0 && strategy != 1 && strategy != 2) {
            return Result.ofFail(-1, "strategy must be in [0, 1, 2], but " + strategy + " got");
        }
        entity.setStrategy(strategy);
        if (strategy != 0) {
            if (StringUtil.isBlank(refResource)) {
                return Result.ofFail(-1, "refResource can't be null or empty when strategy!=0");
            }
            entity.setRefResource(refResource.trim());
        }
    }
    if (controlBehavior != null) {
        if (controlBehavior != 0 && controlBehavior != 1 && controlBehavior != 2) {
            return Result.ofFail(-1, "controlBehavior must be in [0, 1, 2], but " + controlBehavior + " got");
        }
        if (controlBehavior == 1 && warmUpPeriodSec == null) {
            return Result.ofFail(-1, "warmUpPeriodSec can't be null when controlBehavior==1");
        }
        if (controlBehavior == 2 && maxQueueingTimeMs == null) {
            return Result.ofFail(-1, "maxQueueingTimeMs can't be null when controlBehavior==2");
        }
        entity.setControlBehavior(controlBehavior);
        if (warmUpPeriodSec != null) {
            entity.setWarmUpPeriodSec(warmUpPeriodSec);
        }
        if (maxQueueingTimeMs != null) {
            entity.setMaxQueueingTimeMs(maxQueueingTimeMs);
        }
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
        if (entity == null) {
            return Result.ofFail(-1, "save entity fail: null");
        }
        publishRules(entity.getApp(), entity.getIp(), entity.getPort()).get(5000, TimeUnit.MILLISECONDS);
        return Result.ofSuccess(entity);
    } catch (Throwable t) {
        Throwable e = t instanceof ExecutionException ? t.getCause() : t;
        logger.error("Error when updating flow rules, app={}, ip={}, ruleId={}", entity.getApp(), entity.getIp(), id, e);
        return Result.ofFail(-1, e.getMessage());
    }
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) ExecutionException(java.util.concurrent.ExecutionException) Date(java.util.Date) PutMapping(org.springframework.web.bind.annotation.PutMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 53 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project Sentinel by alibaba.

the class ParamFlowRuleController method apiUpdateParamFlowRule.

@PutMapping("/rule/{id}")
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
public Result<ParamFlowRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Long id, @RequestBody ParamFlowRuleEntity entity) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "Invalid id");
    }
    ParamFlowRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofFail(-1, "id " + id + " does not exist");
    }
    Result<ParamFlowRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    if (!checkIfSupported(entity.getApp(), entity.getIp(), entity.getPort())) {
        return unsupportedVersion();
    }
    entity.setId(id);
    Date date = new Date();
    entity.setGmtCreate(oldEntity.getGmtCreate());
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
        publishRules(entity.getApp(), entity.getIp(), entity.getPort()).get();
        return Result.ofSuccess(entity);
    } catch (ExecutionException ex) {
        logger.error("Error when updating parameter flow rules, id=" + id, ex.getCause());
        if (isNotSupported(ex.getCause())) {
            return unsupportedVersion();
        } else {
            return Result.ofThrowable(-1, ex.getCause());
        }
    } catch (Throwable throwable) {
        logger.error("Error when updating parameter flow rules, id=" + id, throwable);
        return Result.ofFail(-1, throwable.getMessage());
    }
}
Also used : ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) ExecutionException(java.util.concurrent.ExecutionException) Date(java.util.Date) PutMapping(org.springframework.web.bind.annotation.PutMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 54 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project Sentinel by alibaba.

the class SystemController method apiUpdateIfNotNull.

@GetMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiUpdateIfNotNull(Long id, String app, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    SystemRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }
    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }
    if (highestSystemLoad != null) {
        if (highestSystemLoad < 0) {
            return Result.ofFail(-1, "highestSystemLoad must >= 0");
        }
        entity.setHighestSystemLoad(highestSystemLoad);
    }
    if (highestCpuUsage != null) {
        if (highestCpuUsage < 0) {
            return Result.ofFail(-1, "highestCpuUsage must >= 0");
        }
        if (highestCpuUsage > 1) {
            return Result.ofFail(-1, "highestCpuUsage must <= 1");
        }
        entity.setHighestCpuUsage(highestCpuUsage);
    }
    if (avgRt != null) {
        if (avgRt < 0) {
            return Result.ofFail(-1, "avgRt must >= 0");
        }
        entity.setAvgRt(avgRt);
    }
    if (maxThread != null) {
        if (maxThread < 0) {
            return Result.ofFail(-1, "maxThread must >= 0");
        }
        entity.setMaxThread(maxThread);
    }
    if (qps != null) {
        if (qps < 0) {
            return Result.ofFail(-1, "qps must >= 0");
        }
        entity.setQps(qps);
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("save error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("publish system rules fail after rule update");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) GetMapping(org.springframework.web.bind.annotation.GetMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 55 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project Sentinel by alibaba.

the class SystemController method apiAdd.

@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<SystemRuleEntity> apiAdd(String app, String ip, Integer port, Double highestSystemLoad, Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
    Result<SystemRuleEntity> checkResult = checkBasicParams(app, ip, port);
    if (checkResult != null) {
        return checkResult;
    }
    int notNullCount = countNotNullAndNotNegative(highestSystemLoad, avgRt, maxThread, qps, highestCpuUsage);
    if (notNullCount != 1) {
        return Result.ofFail(-1, "only one of [highestSystemLoad, avgRt, maxThread, qps,highestCpuUsage] " + "value must be set > 0, but " + notNullCount + " values get");
    }
    if (null != highestCpuUsage && highestCpuUsage > 1) {
        return Result.ofFail(-1, "highestCpuUsage must between [0.0, 1.0]");
    }
    SystemRuleEntity entity = new SystemRuleEntity();
    entity.setApp(app.trim());
    entity.setIp(ip.trim());
    entity.setPort(port);
    // -1 is a fake value
    if (null != highestSystemLoad) {
        entity.setHighestSystemLoad(highestSystemLoad);
    } else {
        entity.setHighestSystemLoad(-1D);
    }
    if (null != highestCpuUsage) {
        entity.setHighestCpuUsage(highestCpuUsage);
    } else {
        entity.setHighestCpuUsage(-1D);
    }
    if (avgRt != null) {
        entity.setAvgRt(avgRt);
    } else {
        entity.setAvgRt(-1L);
    }
    if (maxThread != null) {
        entity.setMaxThread(maxThread);
    } else {
        entity.setMaxThread(-1L);
    }
    if (qps != null) {
        entity.setQps(qps);
    } else {
        entity.setQps(-1D);
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("Add SystemRule error", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.warn("Publish system rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : SystemRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Aggregations

AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)96 Date (java.util.Date)70 FlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity)31 ExecutionException (java.util.concurrent.ExecutionException)25 PostMapping (org.springframework.web.bind.annotation.PostMapping)20 PutMapping (org.springframework.web.bind.annotation.PutMapping)20 ParamFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity)15 ApiDefinitionEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity)10 ApiPredicateItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity)10 GatewayFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity)10 GatewayParamFlowItemEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity)10 AuthorityRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity)10 DegradeRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity)10 SystemRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity)10 ApiPredicateItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo)10 GatewayParamFlowItemVo (com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)8 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)5