Search in sources :

Example 1 with FlowRuleEntity

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

the class FlowControllerV2 method apiAddFlowRule.

@PostMapping("/rule")
@AuthAction(value = AuthService.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());
    } catch (Throwable throwable) {
        logger.error("Failed to add flow rule", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    return Result.ofSuccess(entity);
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 2 with FlowRuleEntity

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

the class FlowControllerV2 method apiDeleteRule.

@DeleteMapping("/rule/{id}")
@AuthAction(PrivilegeType.DELETE_RULE)
public Result<Long> apiDeleteRule(@PathVariable("id") Long id) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "Invalid id");
    }
    FlowRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofSuccess(null);
    }
    try {
        repository.delete(id);
        publishRules(oldEntity.getApp());
    } catch (Exception e) {
        return Result.ofFail(-1, e.getMessage());
    }
    return Result.ofSuccess(id);
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 3 with FlowRuleEntity

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

the class FlowControllerV2 method apiQueryMachineRules.

@GetMapping("/rules")
@AuthAction(PrivilegeType.READ_RULE)
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app) {
    if (StringUtil.isEmpty(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    try {
        List<FlowRuleEntity> rules = ruleProvider.getRules(app);
        if (rules != null && !rules.isEmpty()) {
            for (FlowRuleEntity entity : rules) {
                entity.setApp(app);
                if (entity.getClusterConfig() != null && entity.getClusterConfig().getFlowId() != null) {
                    entity.setId(entity.getClusterConfig().getFlowId());
                }
            }
        }
        rules = repository.saveAll(rules);
        return Result.ofSuccess(rules);
    } catch (Throwable throwable) {
        logger.error("Error when querying flow rules", throwable);
        return Result.ofThrowable(-1, throwable);
    }
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 4 with FlowRuleEntity

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

the class FlowRuleApolloProvider method getRules.

@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
    String appId = "appId";
    String flowDataId = ApolloConfigUtil.getFlowDataId(appName);
    OpenNamespaceDTO openNamespaceDTO = apolloOpenApiClient.getNamespace(appId, "DEV", "default", "application");
    String rules = openNamespaceDTO.getItems().stream().filter(p -> p.getKey().equals(flowDataId)).map(OpenItemDTO::getValue).findFirst().orElse("");
    if (StringUtil.isEmpty(rules)) {
        return new ArrayList<>();
    }
    return converter.convert(rules);
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) StringUtil(com.alibaba.csp.sentinel.util.StringUtil) List(java.util.List) Component(org.springframework.stereotype.Component) ApolloOpenApiClient(com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient) OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) OpenNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO) Autowired(org.springframework.beans.factory.annotation.Autowired) Converter(com.alibaba.csp.sentinel.datasource.Converter) DynamicRuleProvider(com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider) ArrayList(java.util.ArrayList) OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) ArrayList(java.util.ArrayList) OpenNamespaceDTO(com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO)

Example 5 with FlowRuleEntity

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

the class FlowControllerV2 method apiQueryMachineRules.

@GetMapping("/rules")
@AuthAction(PrivilegeType.READ_RULE)
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app) {
    if (StringUtil.isEmpty(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    try {
        List<FlowRuleEntity> rules = ruleProvider.getRules(app);
        if (rules != null && !rules.isEmpty()) {
            for (FlowRuleEntity entity : rules) {
                entity.setApp(app);
                if (entity.getClusterConfig() != null && entity.getClusterConfig().getFlowId() != null) {
                    entity.setId(entity.getClusterConfig().getFlowId());
                }
            }
        }
        rules = repository.saveAll(rules);
        return Result.ofSuccess(rules);
    } catch (Throwable throwable) {
        logger.error("Error when querying flow rules", throwable);
        return Result.ofThrowable(-1, throwable);
    }
}
Also used : FlowRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity) GetMapping(org.springframework.web.bind.annotation.GetMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Aggregations

FlowRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity)33 AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)31 Date (java.util.Date)20 ExecutionException (java.util.concurrent.ExecutionException)10 PostMapping (org.springframework.web.bind.annotation.PostMapping)8 PutMapping (org.springframework.web.bind.annotation.PutMapping)8 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 DynamicRuleProvider (com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider)2 Converter (com.alibaba.csp.sentinel.datasource.Converter)2 StringUtil (com.alibaba.csp.sentinel.util.StringUtil)2 ApolloOpenApiClient (com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient)2 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)2 OpenNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2