org.dromara.jpom.func.assets.controller.MachineNodeController Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2019 Of Him Code Technology Studio
* Jpom is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.jpom.func.assets.controller;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Entity;
import cn.keepbx.jpom.IJsonMessage;
import cn.keepbx.jpom.model.JsonMessage;
import com.alibaba.fastjson2.JSONObject;
import org.dromara.jpom.common.forward.NodeForward;
import org.dromara.jpom.common.forward.NodeUrl;
import org.dromara.jpom.common.i18n.I18nMessageUtil;
import org.dromara.jpom.common.validator.ValidatorItem;
import org.dromara.jpom.func.BaseGroupNameController;
import org.dromara.jpom.func.assets.model.MachineNodeModel;
import org.dromara.jpom.func.assets.server.MachineNodeServer;
import org.dromara.jpom.model.PageResultDto;
import org.dromara.jpom.model.data.NodeModel;
import org.dromara.jpom.model.data.WorkspaceModel;
import org.dromara.jpom.model.node.NodeScriptCacheModel;
import org.dromara.jpom.model.node.ProjectInfoCacheModel;
import org.dromara.jpom.permission.ClassFeature;
import org.dromara.jpom.permission.Feature;
import org.dromara.jpom.permission.MethodFeature;
import org.dromara.jpom.permission.SystemPermission;
import org.dromara.jpom.service.node.NodeService;
import org.dromara.jpom.service.node.ProjectInfoCacheService;
import org.dromara.jpom.service.node.script.NodeScriptServer;
import org.dromara.jpom.service.system.WorkspaceService;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 机器节点
*
* @author bwcx_jzy
* @since 2023/2/18
*/
@RestController
@RequestMapping(value = "/system/assets/machine")
@Feature(cls = ClassFeature.SYSTEM_ASSETS_MACHINE)
@SystemPermission
public class MachineNodeController extends BaseGroupNameController {
private final WorkspaceService workspaceService;
private final ProjectInfoCacheService projectInfoCacheService;
private final NodeScriptServer nodeScriptServer;
private final NodeService nodeService;
public MachineNodeController(WorkspaceService workspaceService,
MachineNodeServer machineNodeServer,
ProjectInfoCacheService projectInfoCacheService,
NodeScriptServer nodeScriptServer,
NodeService nodeService) {
super(machineNodeServer);
this.workspaceService = workspaceService;
this.projectInfoCacheService = projectInfoCacheService;
this.nodeScriptServer = nodeScriptServer;
this.nodeService = nodeService;
}
@PostMapping(value = "list-data", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> listJson(HttpServletRequest request) {
PageResultDto pageResultDto = machineNodeServer.listPage(request);
return JsonMessage.success("", pageResultDto);
}
@GetMapping(value = "search", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> search(String name, String appendIds, int limit) {
Entity entity = new Entity();
if (StrUtil.isNotEmpty(name)) {
entity.set("name", StrUtil.format(" like '%{}%'", name));
}
limit = Math.max(limit, 1);
List appendIdList = StrUtil.splitTrim(appendIds, StrUtil.COMMA);
List machineNodeModels = machineNodeServer.queryList(entity, limit, machineNodeServer.defaultOrders());
appendIdList = appendIdList.stream()
.filter(s -> machineNodeModels.stream()
.noneMatch(machineNodeModel -> StrUtil.equals(s, machineNodeModel.getId())))
.collect(Collectors.toList());
for (String s : appendIdList) {
MachineNodeModel nodeModel = machineNodeServer.getByKey(s);
if (nodeModel == null) {
continue;
}
machineNodeModels.add(nodeModel);
}
return JsonMessage.success("", machineNodeModels);
}
@PostMapping(value = "edit", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public IJsonMessage save(HttpServletRequest request) {
machineNodeServer.update(request);
return JsonMessage.success(I18nMessageUtil.get("i18n.operation_succeeded.3313"));
}
@PostMapping(value = "delete", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public IJsonMessage delete(@ValidatorItem String id) {
long count = nodeService.countByMachine(id);
Assert.state(count <= 0, StrUtil.format(I18nMessageUtil.get("i18n.associated_nodes_warning.64d8"), count));
machineNodeServer.delByKey(id);
return JsonMessage.success(I18nMessageUtil.get("i18n.operation_succeeded.3313"));
}
/**
* 将机器分配到指定工作空间
*
* @param ids 机器id
* @param workspaceId 工作空间id
* @return json
*/
@PostMapping(value = "distribute", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public IJsonMessage distribute(@ValidatorItem String ids, @ValidatorItem String workspaceId) {
List list = StrUtil.splitTrim(ids, StrUtil.COMMA);
for (String id : list) {
MachineNodeModel machineNodeModel = machineNodeServer.getByKey(id);
Assert.notNull(machineNodeModel, I18nMessageUtil.get("i18n.no_machine.89ed"));
WorkspaceModel workspaceModel = new WorkspaceModel(workspaceId);
boolean exists = workspaceService.exists(workspaceModel);
Assert.state(exists, I18nMessageUtil.get("i18n.workspace_not_exist.a6fd"));
//
if (!nodeService.existsNode2(workspaceId, id)) {
//
machineNodeServer.insertNode(machineNodeModel, workspaceId);
}
}
return JsonMessage.success(I18nMessageUtil.get("i18n.operation_succeeded.3313"));
}
@GetMapping(value = "list-node", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> listData(@ValidatorItem String id) {
MachineNodeModel machineNodeModel = machineNodeServer.getByKey(id);
Assert.notNull(machineNodeModel, I18nMessageUtil.get("i18n.no_machine.89ed"));
NodeModel nodeModel = new NodeModel();
nodeModel.setMachineId(id);
List modelList = nodeService.listByBean(nodeModel);
modelList = Optional.ofNullable(modelList).orElseGet(ArrayList::new);
for (NodeModel model : modelList) {
model.setWorkspace(workspaceService.getByKey(model.getWorkspaceId()));
}
return JsonMessage.success("", modelList);
}
/**
* 查询模板节点
*
* @return list
*/
@GetMapping(value = "list-template-node", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> listTemplate() {
MachineNodeModel machineNodeModel = new MachineNodeModel();
machineNodeModel.setTemplateNode(true);
List modelList = machineNodeServer.listByBean(machineNodeModel);
return JsonMessage.success("", modelList);
}
/**
* 保存授权配置
*
* @return json
*/
@RequestMapping(value = "save-whitelist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(cls = ClassFeature.SYSTEM_NODE_WHITELIST, method = MethodFeature.EDIT)
public IJsonMessage
© 2015 - 2025 Weber Informatics LLC | Privacy Policy