org.apache.linkis.instance.label.restful.InstanceRestful Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.linkis.instance.label.restful;
import org.apache.linkis.common.ServiceInstance;
import org.apache.linkis.common.conf.Configuration;
import org.apache.linkis.instance.label.entity.InstanceInfo;
import org.apache.linkis.instance.label.exception.InstanceErrorException;
import org.apache.linkis.instance.label.service.impl.DefaultInsLabelService;
import org.apache.linkis.instance.label.utils.EntityParser;
import org.apache.linkis.instance.label.vo.InstanceInfoVo;
import org.apache.linkis.manager.label.builder.factory.LabelBuilderFactory;
import org.apache.linkis.manager.label.builder.factory.LabelBuilderFactoryContext;
import org.apache.linkis.manager.label.entity.Label;
import org.apache.linkis.manager.label.entity.UserModifiable;
import org.apache.linkis.manager.label.utils.LabelUtils;
import org.apache.linkis.server.Message;
import org.apache.linkis.server.utils.ModuleUserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import static org.apache.linkis.instance.label.errorcode.LinkisInstanceLabelErrorCodeSummary.*;
@Api(tags = "instance restful")
@RestController
@RequestMapping(path = "/microservice")
public class InstanceRestful {
private static final Log logger = LogFactory.getLog(InstanceRestful.class);
private LabelBuilderFactory labelBuilderFactory =
LabelBuilderFactoryContext.getLabelBuilderFactory();
@Autowired private DefaultInsLabelService insLabelService;
@ApiOperation(
value = "listAllInstanceWithLabel",
notes = "list all instance with label",
response = Message.class)
@RequestMapping(path = "/allInstance", method = RequestMethod.GET)
public Message listAllInstanceWithLabel(HttpServletRequest req) throws Exception {
String userName = ModuleUserUtils.getOperationUser(req);
if (Configuration.isNotAdmin(userName)) {
throw new InstanceErrorException(
String.format(
ONLY_ADMIN_CAN_VIEW.getErrorDesc() + "The user [%s] is not admin.", userName));
}
logger.info("start to get all instance informations.....");
List instances = insLabelService.listAllInstanceWithLabel();
insLabelService.markInstanceLabel(instances);
List instanceVos = EntityParser.parseToInstanceVo(instances);
logger.info("Done, all instance:" + instances);
return Message.ok().data("instances", instanceVos);
}
@ApiOperation(
value = "upDateInstanceLabel",
notes = "up date instance label",
response = Message.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "applicationName", dataType = "String"),
@ApiImplicitParam(name = "instance", required = false, dataType = "String", value = "instance"),
@ApiImplicitParam(name = "labels", required = false, dataType = "List", value = "labels"),
@ApiImplicitParam(name = "labelKey", dataType = "String"),
@ApiImplicitParam(name = "stringValue", dataType = "String")
})
@ApiOperationSupport(ignoreParameters = {"jsonNode"})
@RequestMapping(path = "/instanceLabel", method = RequestMethod.PUT)
public Message upDateInstanceLabel(HttpServletRequest req, @RequestBody JsonNode jsonNode)
throws Exception {
String userName = ModuleUserUtils.getOperationUser(req);
if (Configuration.isNotAdmin(userName)) {
throw new InstanceErrorException(
String.format(
ONLY_ADMIN_CAN_MODIFY.getErrorDesc() + " The user [%s] is not admin", userName));
}
String instanceName = jsonNode.get("instance").asText();
String instanceType = jsonNode.get("applicationName").asText();
if (StringUtils.isEmpty(instanceName)) {
return Message.error("instance cannot be empty(实例名不能为空");
}
if (StringUtils.isEmpty(instanceType)) {
return Message.error("instance cannot be empty(实例类型不能为空");
}
JsonNode labelsNode = jsonNode.get("labels");
Iterator labelKeyIterator = labelsNode.iterator();
ServiceInstance instance = ServiceInstance.apply(instanceType, instanceName);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy