Search in sources :

Example 1 with ServiceOperator

use of com.alibaba.nacos.naming.core.ServiceOperator in project nacos by alibaba.

the class UpgradeOpsController method searchService.

/**
 * Search service names.
 *
 * @param namespaceId     namespace
 * @param expr            search pattern
 * @param responsibleOnly whether only search responsible service
 * @return search result
 */
@RequestMapping("/service/names")
@Secured(action = ActionTypes.READ)
public ObjectNode searchService(@RequestParam(defaultValue = "v2", required = false) String ver, @RequestParam(defaultValue = StringUtils.EMPTY) String namespaceId, @RequestParam(defaultValue = StringUtils.EMPTY) String expr, @RequestParam(required = false) boolean responsibleOnly) throws NacosException {
    Map<String, Collection<String>> serviceNameMap = new HashMap<>(16);
    int totalCount = 0;
    ServiceOperator serviceOperator = getServiceOperator(ver);
    if (StringUtils.isNotBlank(namespaceId)) {
        Collection<String> names = serviceOperator.searchServiceName(namespaceId, expr, responsibleOnly);
        serviceNameMap.put(namespaceId, names);
        totalCount = names.size();
    } else {
        for (String each : serviceOperator.listAllNamespace()) {
            Collection<String> names = serviceOperator.searchServiceName(each, expr, responsibleOnly);
            serviceNameMap.put(each, names);
            totalCount += names.size();
        }
    }
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    result.replace("services", JacksonUtils.transferToJsonNode(serviceNameMap));
    result.put("count", totalCount);
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) Collection(java.util.Collection) ServiceOperator(com.alibaba.nacos.naming.core.ServiceOperator) Secured(com.alibaba.nacos.auth.annotation.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Secured (com.alibaba.nacos.auth.annotation.Secured)1 ServiceOperator (com.alibaba.nacos.naming.core.ServiceOperator)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1