添加配置检查策略.

This commit is contained in:
lijiahang
2023-09-19 19:31:25 +08:00
parent 39f975a93a
commit f2c4190a5c
13 changed files with 125 additions and 89 deletions

View File

@@ -26,14 +26,14 @@ public enum BooleanBit {
;
private final Integer v;
private final Integer value;
public static BooleanBit of(Integer value) {
if (value == null) {
return null;
}
for (BooleanBit e : values()) {
if (e.v.equals(value)) {
if (e.value.equals(value)) {
return e;
}
}

View File

@@ -105,4 +105,15 @@ public class Valid extends com.orion.lang.utils.Valid {
return obj;
}
/**
* 检查是否更新成功
*
* @param effect effect
* @return effect
*/
public static int version(int effect) {
isTrue(effect > 0, ErrorMessage.DATA_MODIFIED);
return effect;
}
}

View File

@@ -1,6 +1,8 @@
package com.orion.ops.framework.mybatis.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.orion.ops.framework.common.constant.AutoConfigureOrderConst;
import com.orion.ops.framework.common.constant.FilterOrderConst;
import com.orion.ops.framework.common.filter.FilterCreator;
@@ -37,6 +39,18 @@ public class OrionMybatisAutoConfiguration {
return new FieldFillHandler();
}
/**
* 注册 乐观锁插件
*
* @return 拦截器
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
/**
* @return mybatis 缓存清理过滤器
*/

View File

@@ -25,12 +25,8 @@ import java.io.Serializable;
public class HostConfigUpdateRequest implements Serializable {
@NotNull
@Schema(description = "hostId")
private Long hostId;
@NotNull
@Schema(description = "配置类型")
private String type;
@Schema(description = "id")
private Long id;
@NotBlank
@Schema(description = "配置详情")

View File

@@ -24,15 +24,15 @@ import java.io.Serializable;
public class HostConfigUpdateStatusRequest implements Serializable {
@NotNull
@Schema(description = "hostId")
private Long hostId;
@NotNull
@Schema(description = "配置类型")
private String type;
@Schema(description = "id")
private Long id;
@NotNull
@Schema(description = "状态 0停用 1启用")
private Integer status;
@NotNull
@Schema(description = "配置版本号")
private Integer version;
}

View File

@@ -28,6 +28,9 @@ public class HostConfigVO {
@Schema(description = "version")
private Integer version;
@Schema(description = "配置类型")
private String type;
@Schema(description = "状态 0停用 1启用")
private Integer status;

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.asset.enums;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.config.model.HostConfigModel;
import com.orion.ops.module.asset.handler.host.config.model.HostSshConfigModel;
import com.orion.ops.module.asset.handler.host.config.strategy.HostConfigStrategy;
@@ -19,7 +20,7 @@ public enum HostConfigTypeEnum {
/**
* SSH 配置
*/
SSH(HostSshConfigModel.class, new HostSshConfigStrategy()),
SSH(HostSshConfigModel.class, new HostSshConfigStrategy(), BooleanBit.TRUE.getValue()),
;
@@ -27,6 +28,8 @@ public enum HostConfigTypeEnum {
private final HostConfigStrategy<?> strategy;
private final Integer defaultStatus;
public static HostConfigTypeEnum of(String type) {
if (type == null) {
return null;
@@ -48,4 +51,8 @@ public enum HostConfigTypeEnum {
return (T) strategy;
}
public Integer getDefaultStatus() {
return defaultStatus;
}
}

View File

@@ -37,12 +37,20 @@ public class HostSshConfigModel implements HostConfigModel {
@Schema(description = "身份id")
private Long identityId;
@NotNull
@Schema(description = "连接超时时间")
private Integer connectTimeout;
@NotBlank
@Schema(description = "编码")
@Schema(description = "SSH输出编码")
private String charset;
@NotBlank
@Schema(description = "文件名称编码")
private String filenameCharset;
private String fileNameCharset;
@NotBlank
@Schema(description = "文件内容编码")
private String fileContentCharset;
}

View File

@@ -20,13 +20,6 @@ public interface HostConfigStrategy<Config extends HostConfigModel> {
*/
Config getDefault();
/**
* 插入填充
*
* @param config config
*/
void insertFill(Config config);
/**
* 更新填充
*

View File

@@ -24,16 +24,12 @@ public class HostSshConfigStrategy implements HostConfigStrategy<HostSshConfigMo
return HostSshConfigModel.builder()
.port(SSH_PORT)
.charset(Const.UTF_8)
.filenameCharset(Const.UTF_8)
.connectTimeout(Const.MS_S_10)
.fileNameCharset(Const.UTF_8)
.fileContentCharset(Const.UTF_8)
.build();
}
@Override
public void insertFill(HostSshConfigModel config) {
// 加密密码
this.checkEncryptPassword(config);
}
@Override
public void updateFill(HostSshConfigModel before, HostSshConfigModel after) {
// 加密密码

View File

@@ -59,4 +59,11 @@ public interface HostConfigService {
*/
Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request);
/**
* 初始化主机配置
*
* @param hostId hostId
*/
void initHostConfig(Long hostId);
}

View File

@@ -13,12 +13,12 @@ import com.orion.ops.module.asset.entity.request.host.HostConfigUpdateStatusRequ
import com.orion.ops.module.asset.entity.vo.HostConfigVO;
import com.orion.ops.module.asset.enums.HostConfigTypeEnum;
import com.orion.ops.module.asset.handler.host.config.model.HostConfigModel;
import com.orion.ops.module.asset.handler.host.config.strategy.HostConfigStrategy;
import com.orion.ops.module.asset.service.HostConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -64,6 +64,12 @@ public class HostConfigServiceImpl implements HostConfigService {
@Override
public List<HostConfigVO> getHostConfig(Long hostId) {
List<HostConfigDO> configs = hostConfigDAO.getHostConfigByHostId(hostId);
if (configs.isEmpty()) {
// 初始化 兜底
this.initHostConfig(hostId);
configs = hostConfigDAO.getHostConfigByHostId(hostId);
}
// 返回
return configs.stream().map(s -> {
HostConfigVO vo = HostConfigConvert.MAPPER.to(s);
// 获取配置
@@ -77,71 +83,60 @@ public class HostConfigServiceImpl implements HostConfigService {
@Override
public Integer updateHostConfig(HostConfigUpdateRequest request) {
String typeValue = request.getType();
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, typeValue);
HostConfigModel requestConfig = JSON.parseObject(request.getConfig(), type.getType());
Long id = request.getId();
// 查询原配置
HostConfigDO record = hostConfigDAO.getHostConfigByHostId(request.getHostId(), typeValue);
HostConfigStrategy<HostConfigModel> strategy = type.getStrategy();
if (record == null) {
// 填充
strategy.insertFill(requestConfig);
// 检查参数
Valid.valid(requestConfig);
// 新增配置
HostConfigDO insert = HostConfigConvert.MAPPER.to(request);
insert.setVersion(Const.DEFAULT_VERSION);
insert.setConfig(requestConfig.serial());
hostConfigDAO.insert(insert);
return Const.DEFAULT_VERSION;
} else {
// 检查版本
Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED);
// 填充
HostConfigModel beforeConfig = JSON.parseObject(record.getConfig(), type.getType());
strategy.updateFill(beforeConfig, requestConfig);
// 检查参数
Valid.valid(requestConfig);
// 修改配置
// TODO 检查version是否改变
HostConfigDO update = new HostConfigDO();
update.setId(record.getId());
update.setVersion(request.getVersion());
update.setConfig(requestConfig.serial());
hostConfigDAO.updateById(update);
return update.getVersion();
}
HostConfigDO record = hostConfigDAO.selectById(id);
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, record.getType());
HostConfigModel config = JSON.parseObject(request.getConfig(), type.getType());
// 检查版本
Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED);
// 填充
HostConfigModel beforeConfig = JSON.parseObject(record.getConfig(), type.getType());
type.getStrategy().updateFill(beforeConfig, config);
// 检查参数
Valid.valid(config);
// 修改配置
HostConfigDO update = new HostConfigDO();
update.setId(id);
update.setVersion(request.getVersion());
update.setConfig(config.serial());
int effect = hostConfigDAO.updateById(update);
Valid.version(effect);
return update.getVersion();
}
@Override
public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) {
Long hostId = request.getHostId();
String typeValue = request.getType();
Long id = request.getId();
Integer status = request.getStatus();
Valid.valid(BooleanBit::of, status);
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, typeValue);
// 查询配置
HostConfigDO record = hostConfigDAO.getHostConfigByHostId(hostId, typeValue);
HostConfigStrategy<HostConfigModel> strategy = type.getStrategy();
if (record == null) {
// 插入默认值
HostConfigDO insert = new HostConfigDO();
insert.setHostId(hostId);
insert.setType(typeValue);
insert.setStatus(status);
insert.setVersion(Const.DEFAULT_VERSION);
insert.setConfig(strategy.getDefault().serial());
hostConfigDAO.insert(insert);
return Const.DEFAULT_VERSION;
} else {
// TODO 检查version是否改变
// 修改状态
HostConfigDO update = new HostConfigDO();
update.setId(record.getId());
update.setStatus(status);
hostConfigDAO.updateById(update);
return update.getVersion();
}
HostConfigDO record = hostConfigDAO.selectById(id);
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
// 修改状态
HostConfigDO update = new HostConfigDO();
update.setId(id);
update.setStatus(status);
update.setVersion(request.getVersion());
int effect = hostConfigDAO.updateById(update);
Valid.version(effect);
return update.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.getStrategy().getDefault().serial());
insert.setVersion(Const.DEFAULT_VERSION);
return insert;
}).collect(Collectors.toList());
hostConfigDAO.insertBatch(configs);
}
}

View File

@@ -19,6 +19,7 @@ import com.orion.ops.module.asset.entity.request.host.HostQueryRequest;
import com.orion.ops.module.asset.entity.request.host.HostUpdateRequest;
import com.orion.ops.module.asset.entity.vo.HostConfigVO;
import com.orion.ops.module.asset.entity.vo.HostVO;
import com.orion.ops.module.asset.service.HostConfigService;
import com.orion.ops.module.asset.service.HostService;
import com.orion.ops.module.infra.api.FavoriteApi;
import com.orion.ops.module.infra.api.TagRelApi;
@@ -56,6 +57,9 @@ public class HostServiceImpl implements HostService {
@Resource
private HostConfigDAO hostConfigDAO;
@Resource
private HostConfigService hostConfigService;
@Resource
private TagRelApi tagRelApi;
@@ -79,6 +83,8 @@ public class HostServiceImpl implements HostService {
if (!Lists.isEmpty(tags)) {
tagRelApi.addTagRel(TagTypeEnum.HOST, id, tags);
}
// 创建配置
hostConfigService.initHostConfig(id);
return id;
}