feat: 命令片段 api.

This commit is contained in:
lijiahang
2024-01-22 18:49:03 +08:00
parent 12901bd8f8
commit ddd603c957
14 changed files with 563 additions and 2 deletions

View File

@@ -45,9 +45,11 @@ public class CodeGenerators {
// .color("blue", "gray", "red", "green", "white")
// .valueUseFields()
// .build(),
Template.create("host_connect_log", "主机连接日志", "host")
Template.create("command_snippet", "命令片段", "command")
.disableUnitTest()
.vue("asset", "host-connect-log")
.cache("command:snippet:list:{}", "命令片段列表 ${userId}")
.expire(1, TimeUnit.DAYS)
.vue("host", "command-snippet")
.build(),
};
// jdbc 配置 - 使用配置文件

View File

@@ -0,0 +1,67 @@
package com.orion.ops.module.asset.controller;
import com.orion.ops.framework.log.core.annotation.IgnoreLog;
import com.orion.ops.framework.log.core.enums.IgnoreLogMode;
import com.orion.ops.framework.web.core.annotation.RestWrapper;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import com.orion.ops.module.asset.service.CommandSnippetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 命令片段 api
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Tag(name = "asset - 命令片段服务")
@Slf4j
@Validated
@RestWrapper
@RestController
@RequestMapping("/asset/command-snippet")
@SuppressWarnings({"ELValidationInJSP", "SpringElInspection"})
public class CommandSnippetController {
@Resource
private CommandSnippetService commandSnippetService;
@PostMapping("/create")
@Operation(summary = "创建命令片段")
public Long createCommandSnippet(@Validated @RequestBody CommandSnippetCreateRequest request) {
return commandSnippetService.createCommandSnippet(request);
}
@PutMapping("/update")
@Operation(summary = "更新命令片段")
public Integer updateCommandSnippet(@Validated @RequestBody CommandSnippetUpdateRequest request) {
return commandSnippetService.updateCommandSnippetById(request);
}
@IgnoreLog(IgnoreLogMode.RET)
@PostMapping("/list")
@Operation(summary = "查询全部命令片段")
public List<CommandSnippetVO> getCommandSnippetList(@Validated @RequestBody CommandSnippetQueryRequest request) {
return commandSnippetService.getCommandSnippetList(request);
}
@DeleteMapping("/delete")
@Operation(summary = "删除命令片段")
@Parameter(name = "id", description = "id", required = true)
public Integer deleteCommandSnippet(@RequestParam("id") Long id) {
return commandSnippetService.deleteCommandSnippetById(id);
}
}

View File

@@ -0,0 +1,40 @@
package com.orion.ops.module.asset.convert;
import com.orion.ops.module.asset.entity.domain.CommandSnippetDO;
import com.orion.ops.module.asset.entity.dto.CommandSnippetCacheDTO;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 命令片段 内部对象转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Mapper
public interface CommandSnippetConvert {
CommandSnippetConvert MAPPER = Mappers.getMapper(CommandSnippetConvert.class);
CommandSnippetDO to(CommandSnippetCreateRequest request);
CommandSnippetDO to(CommandSnippetUpdateRequest request);
CommandSnippetDO to(CommandSnippetQueryRequest request);
CommandSnippetVO to(CommandSnippetDO domain);
List<CommandSnippetVO> to(List<CommandSnippetDO> list);
CommandSnippetVO to(CommandSnippetCacheDTO cache);
CommandSnippetCacheDTO toCache(CommandSnippetDO domain);
}

View File

@@ -0,0 +1,17 @@
package com.orion.ops.module.asset.dao;
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
import com.orion.ops.module.asset.entity.domain.CommandSnippetDO;
import org.apache.ibatis.annotations.Mapper;
/**
* 命令片段 Mapper 接口
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Mapper
public interface CommandSnippetDAO extends IMapper<CommandSnippetDO> {
}

View File

@@ -0,0 +1,27 @@
package com.orion.ops.module.asset.define.cache;
import com.orion.lang.define.cache.key.CacheKeyBuilder;
import com.orion.lang.define.cache.key.CacheKeyDefine;
import com.orion.lang.define.cache.key.struct.RedisCacheStruct;
import com.orion.ops.module.asset.entity.dto.CommandSnippetCacheDTO;
import java.util.concurrent.TimeUnit;
/**
* 命令片段缓存 key
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
public interface CommandSnippetCacheKeyDefine {
CacheKeyDefine COMMAND_SNIPPET = new CacheKeyBuilder()
.key("command:snippet:list:{}")
.desc("命令片段列表 ${userId}")
.type(CommandSnippetCacheDTO.class)
.struct(RedisCacheStruct.HASH)
.timeout(1, TimeUnit.DAYS)
.build();
}

View File

@@ -0,0 +1,49 @@
package com.orion.ops.module.asset.entity.domain;
import com.baomidou.mybatisplus.annotation.*;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.math.*;
/**
* 命令片段 实体对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(value = "command_snippet", autoResultMap = true)
@Schema(name = "CommandSnippetDO", description = "命令片段 实体对象")
public class CommandSnippetDO extends BaseDO {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@Schema(description = "用户id")
@TableField("user_id")
private Long userId;
@Schema(description = "名称")
@TableField("name")
private String name;
@Schema(description = "触发前缀")
@TableField("prefix")
private String prefix;
@Schema(description = "代码片段")
@TableField("command")
private String command;
}

View File

@@ -0,0 +1,41 @@
package com.orion.ops.module.asset.entity.dto;
import com.orion.lang.define.cache.key.model.LongCacheIdModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
/**
* 命令片段 缓存对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetCacheDTO", description = "命令片段 缓存对象")
public class CommandSnippetCacheDTO implements LongCacheIdModel, Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "id")
private Long id;
@Schema(description = "名称")
private String name;
@Schema(description = "触发前缀")
private String prefix;
@Schema(description = "代码片段")
private String command;
}

View File

@@ -0,0 +1,41 @@
package com.orion.ops.module.asset.entity.request.command;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 命令片段 创建请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetCreateRequest", description = "命令片段 创建请求对象")
public class CommandSnippetCreateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotBlank
@Size(max = 64)
@Schema(description = "名称")
private String name;
@Schema(description = "groupId")
private Long groupId;
@NotBlank
@Schema(description = "代码片段")
private String command;
}

View File

@@ -0,0 +1,28 @@
package com.orion.ops.module.asset.entity.request.command;
import com.orion.ops.framework.common.entity.PageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import javax.validation.constraints.Size;
/**
* 命令片段 查询请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(name = "CommandSnippetQueryRequest", description = "命令片段 查询请求对象")
public class CommandSnippetQueryRequest extends PageRequest {
@Size(max = 64)
@Schema(description = "名称")
private String name;
}

View File

@@ -0,0 +1,46 @@
package com.orion.ops.module.asset.entity.request.command;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 命令片段 更新请求对象
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "CommandSnippetUpdateRequest", description = "命令片段 更新请求对象")
public class CommandSnippetUpdateRequest implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Schema(description = "id")
private Long id;
@NotBlank
@Size(max = 64)
@Schema(description = "名称")
private String name;
@Schema(description = "groupId")
private Long groupId;
@NotBlank
@Schema(description = "代码片段")
private String command;
}

View File

@@ -0,0 +1,51 @@
package com.orion.ops.module.asset.service;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import java.util.List;
/**
* 命令片段 服务类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
public interface CommandSnippetService {
/**
* 创建命令片段
*
* @param request request
* @return id
*/
Long createCommandSnippet(CommandSnippetCreateRequest request);
/**
* 更新命令片段
*
* @param request request
* @return effect
*/
Integer updateCommandSnippetById(CommandSnippetUpdateRequest request);
/**
* 查询全部命令片段
*
* @param request request
* @return rows
*/
List<CommandSnippetVO> getCommandSnippetList(CommandSnippetQueryRequest request);
/**
* 删除命令片段
*
* @param id id
* @return effect
*/
Integer deleteCommandSnippetById(Long id);
}

View File

@@ -0,0 +1,123 @@
package com.orion.ops.module.asset.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orion.ops.framework.common.constant.ErrorMessage;
import com.orion.ops.framework.common.utils.Valid;
import com.orion.ops.framework.redis.core.utils.RedisMaps;
import com.orion.ops.module.asset.convert.CommandSnippetConvert;
import com.orion.ops.module.asset.dao.CommandSnippetDAO;
import com.orion.ops.module.asset.define.cache.CommandSnippetCacheKeyDefine;
import com.orion.ops.module.asset.entity.domain.CommandSnippetDO;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetCreateRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetQueryRequest;
import com.orion.ops.module.asset.entity.request.command.CommandSnippetUpdateRequest;
import com.orion.ops.module.asset.entity.vo.CommandSnippetVO;
import com.orion.ops.module.asset.service.CommandSnippetService;
import com.orion.ops.module.infra.api.DataGroupRelApi;
import com.orion.ops.module.infra.enums.DataGroupTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
* 命令片段 服务实现类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2024-1-22 15:28
*/
@Slf4j
@Service
public class CommandSnippetServiceImpl implements CommandSnippetService {
@Resource
private CommandSnippetDAO commandSnippetDAO;
@Resource
private DataGroupRelApi dataGroupRelApi;
@Override
public Long createCommandSnippet(CommandSnippetCreateRequest request) {
log.info("CommandSnippetService-createCommandSnippet request: {}", JSON.toJSONString(request));
// 转换
CommandSnippetDO record = CommandSnippetConvert.MAPPER.to(request);
// 插入
int effect = commandSnippetDAO.insert(record);
Long id = record.getId();
log.info("CommandSnippetService-createCommandSnippet id: {}, effect: {}", id, effect);
// 设置分组引用
if (request.getGroupId() != null) {
dataGroupRelApi.addGroupRel(request.getGroupId(), id);
}
// 删除缓存
RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET);
return id;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer updateCommandSnippetById(CommandSnippetUpdateRequest request) {
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
log.info("CommandSnippetService-updateCommandSnippetById id: {}, request: {}", id, JSON.toJSONString(request));
// 查询
CommandSnippetDO record = commandSnippetDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 转换
CommandSnippetDO updateRecord = CommandSnippetConvert.MAPPER.to(request);
// 更新
int effect = commandSnippetDAO.updateById(updateRecord);
log.info("CommandSnippetService-updateCommandSnippetById effect: {}", effect);
// 删除分组引用
dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id);
// 设置分组引用
if (request.getGroupId() != null) {
dataGroupRelApi.addGroupRel(request.getGroupId(), id);
}
// 删除缓存
RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET);
return effect;
}
@Override
public List<CommandSnippetVO> getCommandSnippetList(CommandSnippetQueryRequest request) {
// FIXME 查询缓存
// fixme 查询分组
// 条件
LambdaQueryWrapper<CommandSnippetDO> wrapper = this.buildQueryWrapper(request);
// 查询
return commandSnippetDAO.of(wrapper).list(CommandSnippetConvert.MAPPER::to);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Integer deleteCommandSnippetById(Long id) {
log.info("CommandSnippetService-deleteCommandSnippetById id: {}", id);
// 检查数据是否存在
CommandSnippetDO record = commandSnippetDAO.selectById(id);
Valid.notNull(record, ErrorMessage.DATA_ABSENT);
// 删除
int effect = commandSnippetDAO.deleteById(id);
log.info("CommandSnippetService-deleteCommandSnippetById id: {}, effect: {}", id, effect);
// 删除分组引用
dataGroupRelApi.deleteByRelId(DataGroupTypeEnum.COMMAND_SNIPPET, id);
// 删除缓存
RedisMaps.delete(CommandSnippetCacheKeyDefine.COMMAND_SNIPPET, id);
return effect;
}
/**
* 构建查询 wrapper
*
* @param request request
* @return wrapper
*/
private LambdaQueryWrapper<CommandSnippetDO> buildQueryWrapper(CommandSnippetQueryRequest request) {
return commandSnippetDAO.wrapper()
.eq(CommandSnippetDO::getName, request.getName());
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.orion.ops.module.asset.dao.CommandSnippetDAO">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.orion.ops.module.asset.entity.domain.CommandSnippetDO">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="name" property="name"/>
<result column="prefix" property="prefix"/>
<result column="command" property="command"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="creator" property="creator"/>
<result column="updater" property="updater"/>
<result column="deleted" property="deleted"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, user_id, name, prefix, command, create_time, update_time, creator, updater, deleted
</sql>
</mapper>

View File

@@ -14,6 +14,11 @@ public enum DataGroupTypeEnum {
*/
HOST,
/**
* 命令片段
*/
COMMAND_SNIPPET,
;
public static DataGroupTypeEnum of(String type) {