添加配置检查策略.
This commit is contained in:
@@ -26,14 +26,14 @@ public enum BooleanBit {
|
|||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
private final Integer v;
|
private final Integer value;
|
||||||
|
|
||||||
public static BooleanBit of(Integer value) {
|
public static BooleanBit of(Integer value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
for (BooleanBit e : values()) {
|
for (BooleanBit e : values()) {
|
||||||
if (e.v.equals(value)) {
|
if (e.value.equals(value)) {
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,4 +105,15 @@ public class Valid extends com.orion.lang.utils.Valid {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否更新成功
|
||||||
|
*
|
||||||
|
* @param effect effect
|
||||||
|
* @return effect
|
||||||
|
*/
|
||||||
|
public static int version(int effect) {
|
||||||
|
isTrue(effect > 0, ErrorMessage.DATA_MODIFIED);
|
||||||
|
return effect;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.orion.ops.framework.mybatis.config;
|
package com.orion.ops.framework.mybatis.config;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
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.AutoConfigureOrderConst;
|
||||||
import com.orion.ops.framework.common.constant.FilterOrderConst;
|
import com.orion.ops.framework.common.constant.FilterOrderConst;
|
||||||
import com.orion.ops.framework.common.filter.FilterCreator;
|
import com.orion.ops.framework.common.filter.FilterCreator;
|
||||||
@@ -37,6 +39,18 @@ public class OrionMybatisAutoConfiguration {
|
|||||||
return new FieldFillHandler();
|
return new FieldFillHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册 乐观锁插件
|
||||||
|
*
|
||||||
|
* @return 拦截器
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||||
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
|
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mybatis 缓存清理过滤器
|
* @return mybatis 缓存清理过滤器
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -25,12 +25,8 @@ import java.io.Serializable;
|
|||||||
public class HostConfigUpdateRequest implements Serializable {
|
public class HostConfigUpdateRequest implements Serializable {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "hostId")
|
@Schema(description = "id")
|
||||||
private Long hostId;
|
private Long id;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Schema(description = "配置类型")
|
|
||||||
private String type;
|
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Schema(description = "配置详情")
|
@Schema(description = "配置详情")
|
||||||
|
|||||||
@@ -24,15 +24,15 @@ import java.io.Serializable;
|
|||||||
public class HostConfigUpdateStatusRequest implements Serializable {
|
public class HostConfigUpdateStatusRequest implements Serializable {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "hostId")
|
@Schema(description = "id")
|
||||||
private Long hostId;
|
private Long id;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Schema(description = "配置类型")
|
|
||||||
private String type;
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "状态 0停用 1启用")
|
@Schema(description = "状态 0停用 1启用")
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Schema(description = "配置版本号")
|
||||||
|
private Integer version;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ public class HostConfigVO {
|
|||||||
@Schema(description = "version")
|
@Schema(description = "version")
|
||||||
private Integer version;
|
private Integer version;
|
||||||
|
|
||||||
|
@Schema(description = "配置类型")
|
||||||
|
private String type;
|
||||||
|
|
||||||
@Schema(description = "状态 0停用 1启用")
|
@Schema(description = "状态 0停用 1启用")
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.orion.ops.module.asset.enums;
|
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.HostConfigModel;
|
||||||
import com.orion.ops.module.asset.handler.host.config.model.HostSshConfigModel;
|
import com.orion.ops.module.asset.handler.host.config.model.HostSshConfigModel;
|
||||||
import com.orion.ops.module.asset.handler.host.config.strategy.HostConfigStrategy;
|
import com.orion.ops.module.asset.handler.host.config.strategy.HostConfigStrategy;
|
||||||
@@ -19,7 +20,7 @@ public enum HostConfigTypeEnum {
|
|||||||
/**
|
/**
|
||||||
* SSH 配置
|
* 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 HostConfigStrategy<?> strategy;
|
||||||
|
|
||||||
|
private final Integer defaultStatus;
|
||||||
|
|
||||||
public static HostConfigTypeEnum of(String type) {
|
public static HostConfigTypeEnum of(String type) {
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -48,4 +51,8 @@ public enum HostConfigTypeEnum {
|
|||||||
return (T) strategy;
|
return (T) strategy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getDefaultStatus() {
|
||||||
|
return defaultStatus;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,12 +37,20 @@ public class HostSshConfigModel implements HostConfigModel {
|
|||||||
@Schema(description = "身份id")
|
@Schema(description = "身份id")
|
||||||
private Long identityId;
|
private Long identityId;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Schema(description = "连接超时时间")
|
||||||
|
private Integer connectTimeout;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Schema(description = "编码")
|
@Schema(description = "SSH输出编码")
|
||||||
private String charset;
|
private String charset;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Schema(description = "文件名称编码")
|
@Schema(description = "文件名称编码")
|
||||||
private String filenameCharset;
|
private String fileNameCharset;
|
||||||
|
|
||||||
|
@NotBlank
|
||||||
|
@Schema(description = "文件内容编码")
|
||||||
|
private String fileContentCharset;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,6 @@ public interface HostConfigStrategy<Config extends HostConfigModel> {
|
|||||||
*/
|
*/
|
||||||
Config getDefault();
|
Config getDefault();
|
||||||
|
|
||||||
/**
|
|
||||||
* 插入填充
|
|
||||||
*
|
|
||||||
* @param config config
|
|
||||||
*/
|
|
||||||
void insertFill(Config config);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新填充
|
* 更新填充
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -24,16 +24,12 @@ public class HostSshConfigStrategy implements HostConfigStrategy<HostSshConfigMo
|
|||||||
return HostSshConfigModel.builder()
|
return HostSshConfigModel.builder()
|
||||||
.port(SSH_PORT)
|
.port(SSH_PORT)
|
||||||
.charset(Const.UTF_8)
|
.charset(Const.UTF_8)
|
||||||
.filenameCharset(Const.UTF_8)
|
.connectTimeout(Const.MS_S_10)
|
||||||
|
.fileNameCharset(Const.UTF_8)
|
||||||
|
.fileContentCharset(Const.UTF_8)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void insertFill(HostSshConfigModel config) {
|
|
||||||
// 加密密码
|
|
||||||
this.checkEncryptPassword(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateFill(HostSshConfigModel before, HostSshConfigModel after) {
|
public void updateFill(HostSshConfigModel before, HostSshConfigModel after) {
|
||||||
// 加密密码
|
// 加密密码
|
||||||
|
|||||||
@@ -59,4 +59,11 @@ public interface HostConfigService {
|
|||||||
*/
|
*/
|
||||||
Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request);
|
Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化主机配置
|
||||||
|
*
|
||||||
|
* @param hostId hostId
|
||||||
|
*/
|
||||||
|
void initHostConfig(Long hostId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.entity.vo.HostConfigVO;
|
||||||
import com.orion.ops.module.asset.enums.HostConfigTypeEnum;
|
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.model.HostConfigModel;
|
||||||
import com.orion.ops.module.asset.handler.host.config.strategy.HostConfigStrategy;
|
|
||||||
import com.orion.ops.module.asset.service.HostConfigService;
|
import com.orion.ops.module.asset.service.HostConfigService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -64,6 +64,12 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
@Override
|
@Override
|
||||||
public List<HostConfigVO> getHostConfig(Long hostId) {
|
public List<HostConfigVO> getHostConfig(Long hostId) {
|
||||||
List<HostConfigDO> configs = hostConfigDAO.getHostConfigByHostId(hostId);
|
List<HostConfigDO> configs = hostConfigDAO.getHostConfigByHostId(hostId);
|
||||||
|
if (configs.isEmpty()) {
|
||||||
|
// 初始化 兜底
|
||||||
|
this.initHostConfig(hostId);
|
||||||
|
configs = hostConfigDAO.getHostConfigByHostId(hostId);
|
||||||
|
}
|
||||||
|
// 返回
|
||||||
return configs.stream().map(s -> {
|
return configs.stream().map(s -> {
|
||||||
HostConfigVO vo = HostConfigConvert.MAPPER.to(s);
|
HostConfigVO vo = HostConfigConvert.MAPPER.to(s);
|
||||||
// 获取配置
|
// 获取配置
|
||||||
@@ -77,71 +83,60 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer updateHostConfig(HostConfigUpdateRequest request) {
|
public Integer updateHostConfig(HostConfigUpdateRequest request) {
|
||||||
String typeValue = request.getType();
|
Long id = request.getId();
|
||||||
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, typeValue);
|
|
||||||
HostConfigModel requestConfig = JSON.parseObject(request.getConfig(), type.getType());
|
|
||||||
// 查询原配置
|
// 查询原配置
|
||||||
HostConfigDO record = hostConfigDAO.getHostConfigByHostId(request.getHostId(), typeValue);
|
HostConfigDO record = hostConfigDAO.selectById(id);
|
||||||
HostConfigStrategy<HostConfigModel> strategy = type.getStrategy();
|
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
|
||||||
if (record == null) {
|
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, record.getType());
|
||||||
// 填充
|
HostConfigModel config = JSON.parseObject(request.getConfig(), type.getType());
|
||||||
strategy.insertFill(requestConfig);
|
// 检查版本
|
||||||
// 检查参数
|
Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED);
|
||||||
Valid.valid(requestConfig);
|
// 填充
|
||||||
// 新增配置
|
HostConfigModel beforeConfig = JSON.parseObject(record.getConfig(), type.getType());
|
||||||
HostConfigDO insert = HostConfigConvert.MAPPER.to(request);
|
type.getStrategy().updateFill(beforeConfig, config);
|
||||||
insert.setVersion(Const.DEFAULT_VERSION);
|
// 检查参数
|
||||||
insert.setConfig(requestConfig.serial());
|
Valid.valid(config);
|
||||||
hostConfigDAO.insert(insert);
|
// 修改配置
|
||||||
return Const.DEFAULT_VERSION;
|
HostConfigDO update = new HostConfigDO();
|
||||||
} else {
|
update.setId(id);
|
||||||
// 检查版本
|
update.setVersion(request.getVersion());
|
||||||
Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED);
|
update.setConfig(config.serial());
|
||||||
// 填充
|
int effect = hostConfigDAO.updateById(update);
|
||||||
HostConfigModel beforeConfig = JSON.parseObject(record.getConfig(), type.getType());
|
Valid.version(effect);
|
||||||
strategy.updateFill(beforeConfig, requestConfig);
|
return update.getVersion();
|
||||||
// 检查参数
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) {
|
public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) {
|
||||||
Long hostId = request.getHostId();
|
Long id = request.getId();
|
||||||
String typeValue = request.getType();
|
|
||||||
Integer status = request.getStatus();
|
Integer status = request.getStatus();
|
||||||
Valid.valid(BooleanBit::of, status);
|
Valid.valid(BooleanBit::of, status);
|
||||||
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, typeValue);
|
|
||||||
// 查询配置
|
// 查询配置
|
||||||
HostConfigDO record = hostConfigDAO.getHostConfigByHostId(hostId, typeValue);
|
HostConfigDO record = hostConfigDAO.selectById(id);
|
||||||
HostConfigStrategy<HostConfigModel> strategy = type.getStrategy();
|
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
|
||||||
if (record == null) {
|
// 修改状态
|
||||||
// 插入默认值
|
HostConfigDO update = new HostConfigDO();
|
||||||
HostConfigDO insert = new HostConfigDO();
|
update.setId(id);
|
||||||
insert.setHostId(hostId);
|
update.setStatus(status);
|
||||||
insert.setType(typeValue);
|
update.setVersion(request.getVersion());
|
||||||
insert.setStatus(status);
|
int effect = hostConfigDAO.updateById(update);
|
||||||
insert.setVersion(Const.DEFAULT_VERSION);
|
Valid.version(effect);
|
||||||
insert.setConfig(strategy.getDefault().serial());
|
return update.getVersion();
|
||||||
hostConfigDAO.insert(insert);
|
}
|
||||||
return Const.DEFAULT_VERSION;
|
|
||||||
} else {
|
@Override
|
||||||
// TODO 检查version是否改变
|
public void initHostConfig(Long hostId) {
|
||||||
// 修改状态
|
List<HostConfigDO> configs = Arrays.stream(HostConfigTypeEnum.values())
|
||||||
HostConfigDO update = new HostConfigDO();
|
.map(s -> {
|
||||||
update.setId(record.getId());
|
HostConfigDO insert = new HostConfigDO();
|
||||||
update.setStatus(status);
|
insert.setHostId(hostId);
|
||||||
hostConfigDAO.updateById(update);
|
insert.setType(s.name());
|
||||||
return update.getVersion();
|
insert.setStatus(s.getDefaultStatus());
|
||||||
}
|
insert.setConfig(s.getStrategy().getDefault().serial());
|
||||||
|
insert.setVersion(Const.DEFAULT_VERSION);
|
||||||
|
return insert;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
hostConfigDAO.insertBatch(configs);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.request.host.HostUpdateRequest;
|
||||||
import com.orion.ops.module.asset.entity.vo.HostConfigVO;
|
import com.orion.ops.module.asset.entity.vo.HostConfigVO;
|
||||||
import com.orion.ops.module.asset.entity.vo.HostVO;
|
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.asset.service.HostService;
|
||||||
import com.orion.ops.module.infra.api.FavoriteApi;
|
import com.orion.ops.module.infra.api.FavoriteApi;
|
||||||
import com.orion.ops.module.infra.api.TagRelApi;
|
import com.orion.ops.module.infra.api.TagRelApi;
|
||||||
@@ -56,6 +57,9 @@ public class HostServiceImpl implements HostService {
|
|||||||
@Resource
|
@Resource
|
||||||
private HostConfigDAO hostConfigDAO;
|
private HostConfigDAO hostConfigDAO;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private HostConfigService hostConfigService;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private TagRelApi tagRelApi;
|
private TagRelApi tagRelApi;
|
||||||
|
|
||||||
@@ -79,6 +83,8 @@ public class HostServiceImpl implements HostService {
|
|||||||
if (!Lists.isEmpty(tags)) {
|
if (!Lists.isEmpty(tags)) {
|
||||||
tagRelApi.addTagRel(TagTypeEnum.HOST, id, tags);
|
tagRelApi.addTagRel(TagTypeEnum.HOST, id, tags);
|
||||||
}
|
}
|
||||||
|
// 创建配置
|
||||||
|
hostConfigService.initHostConfig(id);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user