Search in sources :

Example 16 with Secured

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

the class InstanceControllerV2 method beat.

/**
 * Create a beat for instance.
 *
 * @param namespaceId service namespaceId
 * @param serviceName service serviceName
 * @param ip          instance ip
 * @param clusterName service clusterName
 * @param port        instance port
 * @param beat        instance beat info
 * @return detail information of instance
 * @throws Exception any error during handle
 */
@CanDistro
@PutMapping("/beat")
@Secured(action = ActionTypes.WRITE)
public ObjectNode beat(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = StringUtils.EMPTY) String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String clusterName, @RequestParam(defaultValue = "0") Integer port, @RequestParam(defaultValue = StringUtils.EMPTY) String beat) throws Exception {
    ObjectNode result = JacksonUtils.createEmptyJsonNode();
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
    RsInfo clientBeat = null;
    if (StringUtils.isNotBlank(beat)) {
        clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
    }
    if (clientBeat != null) {
        if (StringUtils.isNotBlank(clientBeat.getCluster())) {
            clusterName = clientBeat.getCluster();
        } else {
            // fix #2533
            clientBeat.setCluster(clusterName);
        }
        ip = clientBeat.getIp();
        port = clientBeat.getPort();
    }
    NamingUtils.checkServiceNameFormat(serviceName);
    Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat, serviceName, namespaceId);
    BeatInfoInstanceBuilder builder = BeatInfoInstanceBuilder.newBuilder();
    int resultCode = instanceServiceV2.handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, builder);
    result.put(CommonParams.CODE, resultCode);
    result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, instanceServiceV2.getHeartBeatInterval(namespaceId, serviceName, ip, port, clusterName));
    result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled());
    return result;
}
Also used : BeatInfoInstanceBuilder(com.alibaba.nacos.naming.pojo.instance.BeatInfoInstanceBuilder) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RsInfo(com.alibaba.nacos.naming.healthcheck.RsInfo) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 17 with Secured

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

the class InstanceControllerV2 method register.

/**
 * Register new instance.
 *
 * @param namespaceId namespace id
 * @param serviceName service name
 * @param metadata    service metadata
 * @param cluster     service cluster
 * @param ip          instance ip
 * @param port        instance port
 * @param healthy     instance healthy
 * @param weight      instance weight
 * @param enabled     instance enabled
 * @param ephemeral   instance ephemeral
 * @return 'ok' if success
 * @throws Exception any error during register
 */
@CanDistro
@PostMapping
@Secured(action = ActionTypes.WRITE)
public String register(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String cluster, @RequestParam Integer port, @RequestParam(defaultValue = "true") Boolean healthy, @RequestParam(defaultValue = "1") Double weight, @RequestParam(defaultValue = "true") Boolean enabled, @RequestParam String metadata, @RequestParam Boolean ephemeral) throws Exception {
    NamingUtils.checkServiceNameFormat(serviceName);
    checkWeight(weight);
    final Instance instance = InstanceBuilder.newBuilder().setServiceName(serviceName).setIp(ip).setClusterName(cluster).setPort(port).setHealthy(healthy).setWeight(weight).setEnabled(enabled).setMetadata(UtilsAndCommons.parseMetadata(metadata)).setEphemeral(ephemeral).build();
    if (ephemeral == null) {
        instance.setEphemeral((switchDomain.isDefaultInstanceEphemeral()));
    }
    instanceServiceV2.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 18 with Secured

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

the class InstanceControllerV2 method patch.

/**
 * Patch instance.
 *
 * @param namespaceId namespace id
 * @param serviceName service name
 * @param metadata    service metadata
 * @param cluster     service cluster
 * @param ip          instance ip
 * @param port        instance port
 * @param weight      instance weight
 * @param enabled     instance enabled
 * @return 'ok' if success
 * @throws Exception any error during patch
 */
@CanDistro
@PatchMapping
@Secured(action = ActionTypes.WRITE)
public String patch(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String cluster, @RequestParam Integer port, @RequestParam Double weight, @RequestParam Boolean enabled, @RequestParam String metadata) throws Exception {
    NamingUtils.checkServiceNameFormat(serviceName);
    InstancePatchObject patchObject = new InstancePatchObject(cluster, ip, port);
    if (StringUtils.isNotBlank(metadata)) {
        patchObject.setMetadata(UtilsAndCommons.parseMetadata(metadata));
    }
    if (weight != null) {
        checkWeight(weight);
        patchObject.setWeight(weight);
    }
    if (enabled != null) {
        patchObject.setEnabled(enabled);
    }
    instanceServiceV2.patchInstance(namespaceId, serviceName, patchObject);
    return "ok";
}
Also used : InstancePatchObject(com.alibaba.nacos.naming.core.InstancePatchObject) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 19 with Secured

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

the class InstanceControllerV2 method deregister.

/**
 * Deregister instances.
 *
 * @param namespaceId namespace id
 * @param serviceName service name
 * @param metadata    service metadata
 * @param cluster     service cluster
 * @param ip          instance ip
 * @param port        instance port
 * @param healthy     instance healthy
 * @param weight      instance weight
 * @param enabled     instance enabled
 * @param ephemeral   instance ephemeral
 * @return 'ok' if success
 * @throws Exception any error during deregister
 */
@CanDistro
@DeleteMapping
@Secured(action = ActionTypes.WRITE)
public String deregister(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam String ip, @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String cluster, @RequestParam Integer port, @RequestParam(defaultValue = "true") Boolean healthy, @RequestParam(defaultValue = "1") Double weight, @RequestParam(defaultValue = "true") Boolean enabled, @RequestParam String metadata, @RequestParam Boolean ephemeral) throws Exception {
    NamingUtils.checkServiceNameFormat(serviceName);
    checkWeight(weight);
    final Instance instance = InstanceBuilder.newBuilder().setServiceName(serviceName).setIp(ip).setClusterName(cluster).setPort(port).setHealthy(healthy).setWeight(weight).setEnabled(enabled).setMetadata(UtilsAndCommons.parseMetadata(metadata)).setEphemeral(ephemeral).build();
    if (ephemeral == null) {
        instance.setEphemeral((switchDomain.isDefaultInstanceEphemeral()));
    }
    instanceServiceV2.removeInstance(namespaceId, serviceName, instance);
    return "ok";
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 20 with Secured

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

the class ServiceController method create.

/**
 * Create a new service. This API will create a persistence service.
 *
 * @param namespaceId      namespace id
 * @param serviceName      service name
 * @param protectThreshold protect threshold
 * @param metadata         service metadata
 * @param selector         selector
 * @return 'ok' if success
 * @throws Exception exception
 */
@PostMapping
@Secured(action = ActionTypes.WRITE)
public String create(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(required = false, defaultValue = "0.0F") float protectThreshold, @RequestParam(defaultValue = StringUtils.EMPTY) String metadata, @RequestParam(defaultValue = StringUtils.EMPTY) String selector) throws Exception {
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    serviceMetadata.setProtectThreshold(protectThreshold);
    serviceMetadata.setSelector(parseSelector(selector));
    serviceMetadata.setExtendData(UtilsAndCommons.parseMetadata(metadata));
    serviceMetadata.setEphemeral(false);
    getServiceOperator().create(namespaceId, serviceName, serviceMetadata);
    return "ok";
}
Also used : ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

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