Search in sources :

Example 11 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class InstanceController method register.

/**
 * Register new instance.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception any error during register
 */
@CanDistro
@PostMapping
@Secured(action = ActionTypes.WRITE)
public String register(HttpServletRequest request) throws Exception {
    final String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    final String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    final Instance instance = HttpRequestInstanceBuilder.newBuilder().setDefaultInstanceEphemeral(switchDomain.isDefaultInstanceEphemeral()).setRequest(request).build();
    getInstanceOperator().registerInstance(namespaceId, serviceName, instance);
    return "ok";
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 12 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class InstanceController method list.

/**
 * Get all instance of input service.
 *
 * @param request http request
 * @return list of instance
 * @throws Exception any error during list
 */
@GetMapping("/list")
@Secured(action = ActionTypes.READ)
public Object list(HttpServletRequest request) throws Exception {
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    String agent = WebUtils.getUserAgent(request);
    String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY);
    String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY);
    int udpPort = Integer.parseInt(WebUtils.optional(request, "udpPort", "0"));
    boolean healthyOnly = Boolean.parseBoolean(WebUtils.optional(request, "healthyOnly", "false"));
    String app = WebUtils.optional(request, "app", StringUtils.EMPTY);
    Subscriber subscriber = new Subscriber(clientIP + ":" + udpPort, agent, app, clientIP, namespaceId, serviceName, udpPort, clusters);
    return getInstanceOperator().listInstance(namespaceId, serviceName, subscriber, clusters, healthyOnly);
}
Also used : Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 13 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class InstanceController method update.

/**
 * Update instance.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception any error during update
 */
@CanDistro
@PutMapping
@Secured(action = ActionTypes.WRITE)
public String update(HttpServletRequest request) throws Exception {
    String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    NamingUtils.checkServiceNameFormat(serviceName);
    Instance instance = HttpRequestInstanceBuilder.newBuilder().setDefaultInstanceEphemeral(switchDomain.isDefaultInstanceEphemeral()).setRequest(request).build();
    getInstanceOperator().updateInstance(namespaceId, serviceName, instance);
    return "ok";
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 14 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class InstanceControllerV2 method detail.

/**
 * Get detail information of specified instance.
 *
 * @param namespaceId service namespaceId
 * @param serviceName service serviceName
 * @param ip          instance ip
 * @param clusterName service clusterName
 * @param port        instance port
 * @return detail information of instance
 * @throws Exception any error during get
 */
@GetMapping
@Secured(action = ActionTypes.READ)
public ObjectNode detail(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String clusterName, @RequestParam Integer port) throws Exception {
    NamingUtils.checkServiceNameFormat(serviceName);
    Instance instance = instanceServiceV2.getInstance(namespaceId, serviceName, clusterName, ip, port);
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    result.put("service", serviceName);
    result.put("ip", ip);
    result.put("port", port);
    result.put("clusterName", clusterName);
    result.put("weight", instance.getWeight());
    result.put("healthy", instance.isHealthy());
    result.put("instanceId", instance.getInstanceId());
    result.set("metadata", JacksonUtils.transferToJsonNode(instance.getMetadata()));
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.api.naming.pojo.Instance) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 15 with Secured

use of com.alibaba.nacos.auth.annotation.Secured in project nacos by alibaba.

the class InstanceControllerV2 method batchDeleteInstanceMetadata.

/**
 * Batch delete instance's metadata. old key exist = delete, old key not exist = not operate
 *
 * @param namespaceId     namespace id
 * @param serviceName     service name
 * @param metadata        service metadata
 * @param consistencyType consistencyType
 * @param instances       instances info
 * @return success updated instances. such as '{"updated":["2.2.2.2:8080:unknown:xxxx-cluster:ephemeral"}'.
 * @throws Exception any error during update
 * @since 1.4.0
 */
@CanDistro
@DeleteMapping("/metadata/batch")
@Secured(action = ActionTypes.WRITE)
public ObjectNode batchDeleteInstanceMetadata(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = "") String consistencyType, @RequestParam(defaultValue = "") String instances, @RequestParam String metadata) throws Exception {
    List<Instance> targetInstances = parseBatchInstances(instances);
    Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
    InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
    List<String> operatedInstances = instanceServiceV2.batchDeleteMetadata(namespaceId, instanceOperationInfo, targetMetadata);
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    ArrayNode ipArray = JacksonUtils.createEmptyArrayNode();
    for (String ip : operatedInstances) {
        ipArray.add(ip);
    }
    result.replace("updated", ipArray);
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Aggregations

Secured (com.alibaba.nacos.auth.annotation.Secured)104 Resource (com.alibaba.nacos.plugin.auth.api.Resource)34 Test (org.junit.Test)32 GetMapping (org.springframework.web.bind.annotation.GetMapping)20 CanDistro (com.alibaba.nacos.naming.web.CanDistro)17 Instance (com.alibaba.nacos.api.naming.pojo.Instance)16 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)15 PostMapping (org.springframework.web.bind.annotation.PostMapping)13 PutMapping (org.springframework.web.bind.annotation.PutMapping)13 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)12 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)10 Timestamp (java.sql.Timestamp)10 NacosException (com.alibaba.nacos.api.exception.NacosException)8 AbstractNamingRequest (com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest)8 Request (com.alibaba.nacos.api.remote.request.Request)8 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)8 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)7 ConfigBatchListenRequest (com.alibaba.nacos.api.config.remote.request.ConfigBatchListenRequest)6 HashMap (java.util.HashMap)6 ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)5