feat: 主机配置动态初始化.
This commit is contained in:
@@ -19,7 +19,7 @@ public interface GenericsDataDefinition {
|
||||
*
|
||||
* @return class
|
||||
*/
|
||||
Class<? extends GenericsDataModel> getType();
|
||||
Class<? extends GenericsDataModel> getModel();
|
||||
|
||||
/**
|
||||
* 获取数据处理策略
|
||||
@@ -49,7 +49,7 @@ public interface GenericsDataDefinition {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
default <Model extends GenericsDataModel> Model parse(String json) {
|
||||
return (Model) JSON.parseObject(json, this.getType());
|
||||
return (Model) JSON.parseObject(json, this.getModel());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public @interface OperatorLog {
|
||||
* - {@link org.springframework.web.bind.annotation.PathVariable}
|
||||
* <p>
|
||||
* 使用 @IgnoreParameter 可以忽略参数记录 {@link IgnoreParameter}
|
||||
* 如果只需要忽略某个字段可以使用 {@code @Desensitize(toEmpty = true)} 标注
|
||||
* 如果只需要忽略对象中的某个字段可以使用 {@code @Desensitize(toEmpty = true)} 标注
|
||||
*/
|
||||
boolean parameter() default true;
|
||||
|
||||
|
||||
@@ -10,7 +10,11 @@ import java.lang.annotation.*;
|
||||
* FastJson / Jackson 脱敏配置元注解
|
||||
* <p>
|
||||
* Jackson 标注在字段上则标记该字段执行 http 序列化 (返回 VO)时脱敏 (http-message-converts 用的是 jackson)
|
||||
* <p>
|
||||
* FastJson 需要组合 {@link DesensitizeObject} 一起使用
|
||||
* JSON.toJSONString 时需要使用过滤器 {@link com.orion.ops.framework.desensitize.core.filter.DesensitizeValueFilter}
|
||||
* - 全局日志打印 {@see LogPrinterInterceptor}
|
||||
* - 操作日志切面 {@see OperatorLogAspect}
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
|
||||
@@ -69,7 +69,7 @@ public class HostConfigController {
|
||||
|
||||
@OperatorLog(HostOperatorType.UPDATE_CONFIG_STATUS)
|
||||
@PutMapping("/update-status")
|
||||
@Operation(summary = "更新主机配置状态")
|
||||
@Operation(summary = "更新主机配置状态/动态初始化配置")
|
||||
@PreAuthorize("@ss.hasPermission('asset:host:update-config')")
|
||||
public Integer updateHostConfigStatus(@Validated @RequestBody HostConfigUpdateStatusRequest request) {
|
||||
return hostConfigService.updateHostConfigStatus(request);
|
||||
|
||||
@@ -31,7 +31,7 @@ public class HostConfigDO extends BaseDO {
|
||||
@TableField("host_id")
|
||||
private Long hostId;
|
||||
|
||||
@Schema(description = "连接类型")
|
||||
@Schema(description = "配置类型")
|
||||
@TableField("type")
|
||||
private String type;
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ public class HostConfigQueryRequest extends PageRequest {
|
||||
@Schema(description = "主机id")
|
||||
private Long hostId;
|
||||
|
||||
@Size(max = 255)
|
||||
@Schema(description = "连接类型")
|
||||
@Size(max = 32)
|
||||
@Schema(description = "配置类型")
|
||||
private String type;
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@@ -23,15 +24,22 @@ import java.io.Serializable;
|
||||
@Schema(name = "HostConfigUpdateRequest", description = "主机配置 更新请求对象")
|
||||
public class HostConfigUpdateStatusRequest implements Serializable {
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "id")
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "主机id")
|
||||
private Long hostId;
|
||||
|
||||
@NotNull
|
||||
@Size(max = 32)
|
||||
@Schema(description = "配置类型")
|
||||
private String type;
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "状态 0停用 1启用")
|
||||
private Integer status;
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "配置版本号")
|
||||
private Integer version;
|
||||
|
||||
|
||||
@@ -23,11 +23,16 @@ public enum HostConfigTypeEnum implements GenericsDataDefinition {
|
||||
/**
|
||||
* 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;
|
||||
|
||||
@@ -38,7 +43,7 @@ public enum HostConfigTypeEnum implements GenericsDataDefinition {
|
||||
return null;
|
||||
}
|
||||
for (HostConfigTypeEnum value : values()) {
|
||||
if (value.name().equals(type)) {
|
||||
if (value.type.equals(type)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public enum HostExtraItemEnum implements GenericsDataDefinition {
|
||||
|
||||
private final String item;
|
||||
|
||||
private final Class<? extends GenericsDataModel> type;
|
||||
private final Class<? extends GenericsDataModel> model;
|
||||
|
||||
private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy;
|
||||
|
||||
|
||||
@@ -44,8 +44,12 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
@Resource
|
||||
private HostConfigDAO hostConfigDAO;
|
||||
|
||||
// FIXME 动态初始化
|
||||
// 改为小写
|
||||
// FIXME
|
||||
// T 动态初始化
|
||||
// T 改为小写
|
||||
// F 保存后重置有问题
|
||||
// F 前端逻辑
|
||||
// F 测试
|
||||
|
||||
@Override
|
||||
public HostConfigVO getHostConfig(Long hostId, String type) {
|
||||
@@ -65,11 +69,11 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
@SuppressWarnings("unchecked")
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
return (T) JSON.parseObject(config.getConfig(), type.getType());
|
||||
return (T) JSON.parseObject(config.getConfig(), type.getModel());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,7 +106,7 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
// 添加日志参数
|
||||
OperatorLogs.add(OperatorLogs.REL_ID, host.getId());
|
||||
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);
|
||||
MapDataStrategy<GenericsDataModel> strategy = type.getStrategyBean();
|
||||
@@ -122,19 +126,21 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
@Override
|
||||
public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) {
|
||||
Long id = request.getId();
|
||||
Long hostId = request.getHostId();
|
||||
Integer status = request.getStatus();
|
||||
EnableStatus statusEnum = Valid.valid(EnableStatus::of, status);
|
||||
// 查询配置
|
||||
HostConfigDO record = hostConfigDAO.selectById(id);
|
||||
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
|
||||
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, request.getType());
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(record.getHostId());
|
||||
HostDO host = hostDAO.selectById(hostId);
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
// 添加日志参数
|
||||
OperatorLogs.add(OperatorLogs.REL_ID, host.getId());
|
||||
OperatorLogs.add(OperatorLogs.NAME, host.getName());
|
||||
OperatorLogs.add(OperatorLogs.TYPE, HostConfigTypeEnum.of(record.getType()).name());
|
||||
OperatorLogs.add(OperatorLogs.STATUS_NAME, statusEnum.name());
|
||||
if (id != null) {
|
||||
// 修改 查询配置
|
||||
HostConfigDO record = hostConfigDAO.selectById(id);
|
||||
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
|
||||
// 修改状态
|
||||
HostConfigDO update = new HostConfigDO();
|
||||
update.setId(id);
|
||||
@@ -143,21 +149,39 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
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
|
||||
public void initHostConfig(Long hostId) {
|
||||
List<HostConfigDO> configs = Arrays.stream(HostConfigTypeEnum.values())
|
||||
.map(s -> {
|
||||
HostConfigDO insert = new HostConfigDO();
|
||||
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());
|
||||
.map(s -> this.getDefaultConfig(hostId, s))
|
||||
.collect(Collectors.toList());
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -292,7 +292,7 @@ CREATE TABLE `host_config`
|
||||
(
|
||||
`id` bigint(0) NOT NULL AUTO_INCREMENT 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启用',
|
||||
`config` json NULL COMMENT '配置详情',
|
||||
`version` int(0) NULL DEFAULT 0 COMMENT '配置版本号',
|
||||
|
||||
Reference in New Issue
Block a user