Search in sources :

Example 6 with InstanceOperationInfo

use of com.alibaba.nacos.naming.pojo.InstanceOperationInfo in project nacos by alibaba.

the class InstanceController method batchDeleteInstanceMetadata.

/**
 * Batch delete instance's metadata. old key exist = delete, old key not exist = not operate
 *
 * @param request http request
 * @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(HttpServletRequest request) throws Exception {
    final String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    String consistencyType = WebUtils.optional(request, "consistencyType", StringUtils.EMPTY);
    String instances = WebUtils.optional(request, "instances", StringUtils.EMPTY);
    List<Instance> targetInstances = parseBatchInstances(instances);
    String metadata = WebUtils.required(request, METADATA);
    Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
    InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
    List<String> operatedInstances = getInstanceOperator().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)

Example 7 with InstanceOperationInfo

use of com.alibaba.nacos.naming.pojo.InstanceOperationInfo in project nacos by alibaba.

the class InstanceControllerV2 method batchUpdateInstanceMetadata.

/**
 * Batch update instance's metadata. old key exist = update, old key not exist = add.
 *
 * @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
@PutMapping(value = "/metadata/batch")
@Secured(action = ActionTypes.WRITE)
public ObjectNode batchUpdateInstanceMetadata(@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.batchUpdateMetadata(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) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Aggregations

InstanceOperationInfo (com.alibaba.nacos.naming.pojo.InstanceOperationInfo)7 Instance (com.alibaba.nacos.api.naming.pojo.Instance)6 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)5 Secured (com.alibaba.nacos.auth.annotation.Secured)4 CanDistro (com.alibaba.nacos.naming.web.CanDistro)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 PutMapping (org.springframework.web.bind.annotation.PutMapping)2 Constants (com.alibaba.nacos.api.common.Constants)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 NamingUtils (com.alibaba.nacos.api.naming.utils.NamingUtils)1 CollectionUtils (com.alibaba.nacos.common.utils.CollectionUtils)1 InternetAddressUtil (com.alibaba.nacos.common.utils.InternetAddressUtil)1 JacksonUtils (com.alibaba.nacos.common.utils.JacksonUtils)1 StringUtils (com.alibaba.nacos.common.utils.StringUtils)1 Member (com.alibaba.nacos.core.cluster.Member)1 ServerMemberManager (com.alibaba.nacos.core.cluster.ServerMemberManager)1 ConsistencyService (com.alibaba.nacos.naming.consistency.ConsistencyService)1 Datum (com.alibaba.nacos.naming.consistency.Datum)1 KeyBuilder (com.alibaba.nacos.naming.consistency.KeyBuilder)1