feat: 主机配置动态初始化.

This commit is contained in:
lijiahangmax
2023-12-24 22:53:23 +08:00
parent 03c87b28d4
commit 5cf70595c0
11 changed files with 82 additions and 41 deletions

View File

@@ -19,7 +19,7 @@ public interface GenericsDataDefinition {
* *
* @return class * @return class
*/ */
Class<? extends GenericsDataModel> getType(); Class<? extends GenericsDataModel> getModel();
/** /**
* 获取数据处理策略 * 获取数据处理策略
@@ -49,7 +49,7 @@ public interface GenericsDataDefinition {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default <Model extends GenericsDataModel> Model parse(String json) { default <Model extends GenericsDataModel> Model parse(String json) {
return (Model) JSON.parseObject(json, this.getType()); return (Model) JSON.parseObject(json, this.getModel());
} }

View File

@@ -30,7 +30,7 @@ public @interface OperatorLog {
* - {@link org.springframework.web.bind.annotation.PathVariable} * - {@link org.springframework.web.bind.annotation.PathVariable}
* <p> * <p>
* 使用 @IgnoreParameter 可以忽略参数记录 {@link IgnoreParameter} * 使用 @IgnoreParameter 可以忽略参数记录 {@link IgnoreParameter}
* 如果只需要忽略某个字段可以使用 {@code @Desensitize(toEmpty = true)} 标注 * 如果只需要忽略对象中的某个字段可以使用 {@code @Desensitize(toEmpty = true)} 标注
*/ */
boolean parameter() default true; boolean parameter() default true;

View File

@@ -10,7 +10,11 @@ import java.lang.annotation.*;
* FastJson / Jackson 脱敏配置元注解 * FastJson / Jackson 脱敏配置元注解
* <p> * <p>
* Jackson 标注在字段上则标记该字段执行 http 序列化 (返回 VO)时脱敏 (http-message-converts 用的是 jackson) * Jackson 标注在字段上则标记该字段执行 http 序列化 (返回 VO)时脱敏 (http-message-converts 用的是 jackson)
* <p>
* FastJson 需要组合 {@link DesensitizeObject} 一起使用 * FastJson 需要组合 {@link DesensitizeObject} 一起使用
* JSON.toJSONString 时需要使用过滤器 {@link com.orion.ops.framework.desensitize.core.filter.DesensitizeValueFilter}
* - 全局日志打印 {@see LogPrinterInterceptor}
* - 操作日志切面 {@see OperatorLogAspect}
* *
* @author Jiahang Li * @author Jiahang Li
* @version 1.0.0 * @version 1.0.0

View File

@@ -69,7 +69,7 @@ public class HostConfigController {
@OperatorLog(HostOperatorType.UPDATE_CONFIG_STATUS) @OperatorLog(HostOperatorType.UPDATE_CONFIG_STATUS)
@PutMapping("/update-status") @PutMapping("/update-status")
@Operation(summary = "更新主机配置状态") @Operation(summary = "更新主机配置状态/动态初始化配置")
@PreAuthorize("@ss.hasPermission('asset:host:update-config')") @PreAuthorize("@ss.hasPermission('asset:host:update-config')")
public Integer updateHostConfigStatus(@Validated @RequestBody HostConfigUpdateStatusRequest request) { public Integer updateHostConfigStatus(@Validated @RequestBody HostConfigUpdateStatusRequest request) {
return hostConfigService.updateHostConfigStatus(request); return hostConfigService.updateHostConfigStatus(request);

View File

@@ -31,7 +31,7 @@ public class HostConfigDO extends BaseDO {
@TableField("host_id") @TableField("host_id")
private Long hostId; private Long hostId;
@Schema(description = "连接类型") @Schema(description = "配置类型")
@TableField("type") @TableField("type")
private String type; private String type;

View File

@@ -24,8 +24,8 @@ public class HostConfigQueryRequest extends PageRequest {
@Schema(description = "主机id") @Schema(description = "主机id")
private Long hostId; private Long hostId;
@Size(max = 255) @Size(max = 32)
@Schema(description = "连接类型") @Schema(description = "配置类型")
private String type; private String type;
} }

View File

@@ -7,6 +7,7 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable; import java.io.Serializable;
/** /**
@@ -23,15 +24,22 @@ import java.io.Serializable;
@Schema(name = "HostConfigUpdateRequest", description = "主机配置 更新请求对象") @Schema(name = "HostConfigUpdateRequest", description = "主机配置 更新请求对象")
public class HostConfigUpdateStatusRequest implements Serializable { public class HostConfigUpdateStatusRequest implements Serializable {
@NotNull
@Schema(description = "id") @Schema(description = "id")
private Long id; private Long id;
@NotNull
@Schema(description = "主机id")
private Long hostId;
@NotNull
@Size(max = 32)
@Schema(description = "配置类型")
private String type;
@NotNull @NotNull
@Schema(description = "状态 0停用 1启用") @Schema(description = "状态 0停用 1启用")
private Integer status; private Integer status;
@NotNull
@Schema(description = "配置版本号") @Schema(description = "配置版本号")
private Integer version; private Integer version;

View File

@@ -23,11 +23,16 @@ public enum HostConfigTypeEnum implements GenericsDataDefinition {
/** /**
* SSH 配置 * SSH 配置
*/ */
SSH(HostSshConfigModel.class, HostSshConfigStrategy.class, EnableStatus.ENABLED.getValue()), SSH("ssh",
HostSshConfigModel.class,
HostSshConfigStrategy.class,
EnableStatus.ENABLED.getValue()),
; ;
private final Class<? extends GenericsDataModel> type; private final String type;
private final Class<? extends GenericsDataModel> model;
private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy; private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy;
@@ -38,7 +43,7 @@ public enum HostConfigTypeEnum implements GenericsDataDefinition {
return null; return null;
} }
for (HostConfigTypeEnum value : values()) { for (HostConfigTypeEnum value : values()) {
if (value.name().equals(type)) { if (value.type.equals(type)) {
return value; return value;
} }
} }

View File

@@ -28,7 +28,7 @@ public enum HostExtraItemEnum implements GenericsDataDefinition {
private final String item; private final String item;
private final Class<? extends GenericsDataModel> type; private final Class<? extends GenericsDataModel> model;
private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy; private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy;

View File

@@ -44,8 +44,12 @@ public class HostConfigServiceImpl implements HostConfigService {
@Resource @Resource
private HostConfigDAO hostConfigDAO; private HostConfigDAO hostConfigDAO;
// FIXME 动态初始化 // FIXME
// 改为小写 // T 动态初始化
// T 改为小写
// F 保存后重置有问题
// F 前端逻辑
// F 测试
@Override @Override
public HostConfigVO getHostConfig(Long hostId, String type) { public HostConfigVO getHostConfig(Long hostId, String type) {
@@ -65,11 +69,11 @@ public class HostConfigServiceImpl implements HostConfigService {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends GenericsDataModel> T getHostConfig(Long hostId, HostConfigTypeEnum type) { public <T extends GenericsDataModel> T getHostConfig(Long hostId, HostConfigTypeEnum type) {
// 查询配置 // 查询配置
HostConfigDO config = hostConfigDAO.getHostConfigByHostId(hostId, type.name()); HostConfigDO config = hostConfigDAO.getHostConfigByHostId(hostId, type.getType());
if (config == null) { if (config == null) {
return null; return null;
} }
return (T) JSON.parseObject(config.getConfig(), type.getType()); return (T) JSON.parseObject(config.getConfig(), type.getModel());
} }
@Override @Override
@@ -102,7 +106,7 @@ public class HostConfigServiceImpl implements HostConfigService {
// 添加日志参数 // 添加日志参数
OperatorLogs.add(OperatorLogs.REL_ID, host.getId()); OperatorLogs.add(OperatorLogs.REL_ID, host.getId());
OperatorLogs.add(OperatorLogs.NAME, host.getName()); OperatorLogs.add(OperatorLogs.NAME, host.getName());
OperatorLogs.add(OperatorLogs.TYPE, type.name()); OperatorLogs.add(OperatorLogs.TYPE, type.getType());
// 检查版本 // 检查版本
Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED); Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED);
MapDataStrategy<GenericsDataModel> strategy = type.getStrategyBean(); MapDataStrategy<GenericsDataModel> strategy = type.getStrategyBean();
@@ -122,42 +126,62 @@ public class HostConfigServiceImpl implements HostConfigService {
@Override @Override
public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) { public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) {
Long id = request.getId(); Long id = request.getId();
Long hostId = request.getHostId();
Integer status = request.getStatus(); Integer status = request.getStatus();
EnableStatus statusEnum = Valid.valid(EnableStatus::of, status); EnableStatus statusEnum = Valid.valid(EnableStatus::of, status);
// 查询配置 HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, request.getType());
HostConfigDO record = hostConfigDAO.selectById(id);
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
// 查询主机 // 查询主机
HostDO host = hostDAO.selectById(record.getHostId()); HostDO host = hostDAO.selectById(hostId);
Valid.notNull(host, ErrorMessage.HOST_ABSENT); Valid.notNull(host, ErrorMessage.HOST_ABSENT);
// 添加日志参数 // 添加日志参数
OperatorLogs.add(OperatorLogs.REL_ID, host.getId()); OperatorLogs.add(OperatorLogs.REL_ID, host.getId());
OperatorLogs.add(OperatorLogs.NAME, host.getName()); OperatorLogs.add(OperatorLogs.NAME, host.getName());
OperatorLogs.add(OperatorLogs.TYPE, HostConfigTypeEnum.of(record.getType()).name());
OperatorLogs.add(OperatorLogs.STATUS_NAME, statusEnum.name()); OperatorLogs.add(OperatorLogs.STATUS_NAME, statusEnum.name());
// 修改状态 if (id != null) {
HostConfigDO update = new HostConfigDO(); // 修改 查询配置
update.setId(id); HostConfigDO record = hostConfigDAO.selectById(id);
update.setStatus(status); Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
update.setVersion(request.getVersion()); // 修改状态
int effect = hostConfigDAO.updateById(update); HostConfigDO update = new HostConfigDO();
Valid.version(effect); update.setId(id);
return update.getVersion(); update.setStatus(status);
update.setVersion(request.getVersion());
int effect = hostConfigDAO.updateById(update);
Valid.version(effect);
return update.getVersion();
} else {
// 新增 初始化
HostConfigDO defaultConfig = this.getDefaultConfig(hostId, type);
defaultConfig.setStatus(status);
// 插入数据
hostConfigDAO.insert(defaultConfig);
return defaultConfig.getVersion();
}
} }
@Override @Override
public void initHostConfig(Long hostId) { public void initHostConfig(Long hostId) {
List<HostConfigDO> configs = Arrays.stream(HostConfigTypeEnum.values()) List<HostConfigDO> configs = Arrays.stream(HostConfigTypeEnum.values())
.map(s -> { .map(s -> this.getDefaultConfig(hostId, s))
HostConfigDO insert = new HostConfigDO(); .collect(Collectors.toList());
insert.setHostId(hostId);
insert.setType(s.name());
insert.setStatus(s.getDefaultStatus());
insert.setConfig(s.getStrategyBean().getDefault().serial());
insert.setVersion(Const.DEFAULT_VERSION);
return insert;
}).collect(Collectors.toList());
hostConfigDAO.insertBatch(configs); hostConfigDAO.insertBatch(configs);
} }
/**
* 获取默认配置
*
* @param hostId hostId
* @param type type
* @return config
*/
private HostConfigDO getDefaultConfig(Long hostId, HostConfigTypeEnum type) {
HostConfigDO insert = new HostConfigDO();
insert.setHostId(hostId);
insert.setType(type.getType());
insert.setStatus(type.getDefaultStatus());
insert.setConfig(type.getStrategyBean().getDefault().serial());
insert.setVersion(Const.DEFAULT_VERSION);
return insert;
}
} }

View File

@@ -292,7 +292,7 @@ CREATE TABLE `host_config`
( (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT 'id', `id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT 'id',
`host_id` bigint(0) NULL DEFAULT NULL COMMENT '主机id', `host_id` bigint(0) NULL DEFAULT NULL COMMENT '主机id',
`type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '连接类型', `type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '配置类型',
`status` tinyint(0) NULL DEFAULT 1 COMMENT '状态 0停用 1启用', `status` tinyint(0) NULL DEFAULT 1 COMMENT '状态 0停用 1启用',
`config` json NULL COMMENT '配置详情', `config` json NULL COMMENT '配置详情',
`version` int(0) NULL DEFAULT 0 COMMENT '配置版本号', `version` int(0) NULL DEFAULT 0 COMMENT '配置版本号',