Search in sources :

Example 16 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

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 17 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

the class ParamFlowRuleController method apiAddParamFlowRule.

@PostMapping("/rule")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<ParamFlowRuleEntity> apiAddParamFlowRule(@RequestBody ParamFlowRuleEntity entity) {
    Result<ParamFlowRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    if (!checkIfSupported(entity.getApp(), entity.getIp(), entity.getPort())) {
        return unsupportedVersion();
    }
    entity.setId(null);
    entity.getRule().setResource(entity.getResource().trim());
    Date date = new Date();
    entity.setGmtCreate(date);
    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 adding new parameter flow rules", ex.getCause());
        if (isNotSupported(ex.getCause())) {
            return unsupportedVersion();
        } else {
            return Result.ofThrowable(-1, ex.getCause());
        }
    } catch (Throwable throwable) {
        logger.error("Error when adding new parameter flow rules", 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) PostMapping(org.springframework.web.bind.annotation.PostMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 18 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project pig by pig-mesh.

the class ParamFlowRuleController method apiDeleteRule.

@DeleteMapping("/rule/{id}")
@AuthAction(PrivilegeType.DELETE_RULE)
public Result<Long> apiDeleteRule(@PathVariable("id") Long id) {
    if (id == null) {
        return Result.ofFail(-1, "id cannot be null");
    }
    ParamFlowRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofSuccess(null);
    }
    try {
        repository.delete(id);
        publishRules(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort()).get();
        return Result.ofSuccess(id);
    } catch (ExecutionException ex) {
        logger.error("Error when deleting parameter flow rules", ex.getCause());
        if (isNotSupported(ex.getCause())) {
            return unsupportedVersion();
        } else {
            return Result.ofThrowable(-1, ex.getCause());
        }
    } catch (Throwable throwable) {
        logger.error("Error when deleting parameter flow rules", throwable);
        return Result.ofFail(-1, throwable.getMessage());
    }
}
Also used : ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) ExecutionException(java.util.concurrent.ExecutionException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 19 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project spring-boot-student by wyh-spring-ecosystem-student.

the class DegradeController method add.

@ResponseBody
@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> add(String app, String ip, Integer port, String limitApp, String resource, Double count, Integer timeWindow, Integer grade) {
    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(ip)) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (StringUtil.isBlank(limitApp)) {
        return Result.ofFail(-1, "limitApp can't be null or empty");
    }
    if (StringUtil.isBlank(resource)) {
        return Result.ofFail(-1, "resource can't be null or empty");
    }
    if (count == null) {
        return Result.ofFail(-1, "count can't be null");
    }
    if (timeWindow == null) {
        return Result.ofFail(-1, "timeWindow can't be null");
    }
    if (grade == null) {
        return Result.ofFail(-1, "grade can't be null");
    }
    if (grade < RuleConstant.DEGRADE_GRADE_RT || grade > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
        return Result.ofFail(-1, "Invalid grade: " + grade);
    }
    DegradeRuleEntity entity = new DegradeRuleEntity();
    entity.setApp(app.trim());
    entity.setIp(ip.trim());
    entity.setPort(port);
    entity.setLimitApp(limitApp.trim());
    entity.setResource(resource.trim());
    entity.setCount(count);
    entity.setTimeWindow(timeWindow);
    entity.setGrade(grade);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("add error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.info("publish degrade rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : DegradeRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity) Date(java.util.Date) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 20 with AuthAction

use of com.alibaba.csp.sentinel.dashboard.auth.AuthAction in project spring-boot-student by wyh-spring-ecosystem-student.

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());
        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) 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