Search in sources :

Example 11 with ParamFlowRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity in project pig by pig-mesh.

the class ParamFlowRuleController method apiUpdateParamFlowRule.

@PutMapping("/rule/{id}")
@AuthAction(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 12 with ParamFlowRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity in project spring-boot-student by wyh-spring-ecosystem-student.

the class JsonSerializeTest method paramFlowRuleSerializeTest.

@Test
public void paramFlowRuleSerializeTest() {
    ParamFlowRuleEntity emptyRule = new ParamFlowRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    ParamFlowRuleEntity paramFlowRule = new ParamFlowRuleEntity();
    ParamFlowRule rule = new ParamFlowRule();
    rule.setClusterConfig(new ParamFlowClusterConfig());
    rule.setResource("rs").setLimitApp("default");
    paramFlowRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"burstCount\":0,\"clusterConfig\":{\"fallbackToLocalWhenFail\":false,\"sampleCount\":10,\"thresholdType\":0,\"windowIntervalMs\":1000},\"clusterMode\":false,\"controlBehavior\":0,\"count\":0.0,\"durationInSec\":1,\"grade\":1,\"limitApp\":\"default\",\"maxQueueingTimeMs\":0,\"paramFlowItemList\":[],\"resource\":\"rs\"}}".equals(JSON.toJSONString(paramFlowRule)));
}
Also used : ParamFlowRule(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule) ParamFlowClusterConfig(com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig) ParamFlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity) Test(org.junit.Test)

Example 13 with ParamFlowRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity in project spring-boot-student by wyh-spring-ecosystem-student.

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 14 with ParamFlowRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity in project spring-boot-student by wyh-spring-ecosystem-student.

the class ParamFlowRuleController method apiUpdateParamFlowRule.

@PutMapping("/rule/{id}")
@AuthAction(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 15 with ParamFlowRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity in project XHuiCloud by sindaZeng.

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)

Aggregations

ParamFlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity)17 AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)15 ExecutionException (java.util.concurrent.ExecutionException)15 Date (java.util.Date)10 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 PutMapping (org.springframework.web.bind.annotation.PutMapping)4 ParamFlowClusterConfig (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig)2 ParamFlowRule (com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule)2 Test (org.junit.Test)2