Search in sources :

Example 1 with AuthorityRuleEntity

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

the class JsonSerializeTest method authorityRuleJsonSerializeTest.

@Test
public void authorityRuleJsonSerializeTest() {
    AuthorityRuleEntity emptyRule = new AuthorityRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    AuthorityRuleEntity authorityRule = new AuthorityRuleEntity();
    AuthorityRule rule = new AuthorityRule();
    rule.setStrategy(0).setLimitApp("default").setResource("rs");
    authorityRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"limitApp\":\"default\",\"resource\":\"rs\",\"strategy\":0}}".equals(JSON.toJSONString(authorityRule)));
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule) AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Test(org.junit.Test)

Example 2 with AuthorityRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity in project RuoYi-Cloud-Plus by JavaLionLi.

the class AuthorityRuleController method apiAddAuthorityRule.

@PostMapping("/rule")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<AuthorityRuleEntity> apiAddAuthorityRule(@RequestBody AuthorityRuleEntity entity) {
    Result<AuthorityRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(null);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("Failed to add authority rule", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("Publish authority rules failed after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 3 with AuthorityRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity in project Sentinel by alibaba.

the class AuthorityRuleController method apiUpdateParamFlowRule.

@PutMapping("/rule/{id}")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<AuthorityRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Long id, @RequestBody AuthorityRuleEntity entity) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "Invalid id");
    }
    Result<AuthorityRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(id);
    Date date = new Date();
    entity.setGmtCreate(null);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
        if (entity == null) {
            return Result.ofFail(-1, "Failed to save authority rule");
        }
    } catch (Throwable throwable) {
        logger.error("Failed to save authority rule", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("Publish authority rules failed after rule update");
    }
    return Result.ofSuccess(entity);
}
Also used : AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Date(java.util.Date) PutMapping(org.springframework.web.bind.annotation.PutMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 4 with AuthorityRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity in project Sentinel by alibaba.

the class AuthorityRuleController method apiAddAuthorityRule.

@PostMapping("/rule")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<AuthorityRuleEntity> apiAddAuthorityRule(@RequestBody AuthorityRuleEntity entity) {
    Result<AuthorityRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(null);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("Failed to add authority rule", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("Publish authority rules failed after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 5 with AuthorityRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity in project Sentinel by alibaba.

the class JsonSerializeTest method authorityRuleJsonSerializeTest.

@Test
public void authorityRuleJsonSerializeTest() {
    AuthorityRuleEntity emptyRule = new AuthorityRuleEntity();
    Assert.assertTrue("{}".equals(JSON.toJSONString(emptyRule)));
    AuthorityRuleEntity authorityRule = new AuthorityRuleEntity();
    AuthorityRule rule = new AuthorityRule();
    rule.setStrategy(0).setLimitApp("default").setResource("rs");
    authorityRule.setRule(rule);
    Assert.assertTrue("{\"rule\":{\"limitApp\":\"default\",\"resource\":\"rs\",\"strategy\":0}}".equals(JSON.toJSONString(authorityRule)));
}
Also used : AuthorityRule(com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule) AuthorityRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity) Test(org.junit.Test)

Aggregations

AuthorityRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity)12 AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)10 Date (java.util.Date)10 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 PutMapping (org.springframework.web.bind.annotation.PutMapping)5 AuthorityRule (com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule)2 Test (org.junit.Test)2