refactor: 重构数据额外配置.
This commit is contained in:
@@ -8,4 +8,31 @@ Authorization: {{token}}
|
||||
"name": "alias"
|
||||
}
|
||||
|
||||
### 获取主机拓展信息
|
||||
GET {{baseUrl}}/asset/host-extra/get?hostId=1&item=ssh
|
||||
Authorization: {{token}}
|
||||
|
||||
### 获取多个主机拓展信息
|
||||
POST {{baseUrl}}/asset/host-extra/list
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"hostId": 1,
|
||||
"items": [
|
||||
"ssh"
|
||||
]
|
||||
}
|
||||
|
||||
### 修改主机拓展信息
|
||||
PUT {{baseUrl}}/asset/host-extra/update
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"hostId": 1,
|
||||
"item": "ssh",
|
||||
"extra": "{\"authType\":\"DEFAULT\"}"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
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.host.HostAliasUpdateRequest;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostExtraQueryRequest;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostExtraUpdateRequest;
|
||||
import com.orion.ops.module.asset.service.HostExtraService;
|
||||
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.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 主机拓展信息 api
|
||||
@@ -39,5 +42,27 @@ public class HostExtraController {
|
||||
return hostExtraService.updateHostAlias(request);
|
||||
}
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获取主机拓展信息")
|
||||
@Parameter(name = "hostId", description = "hostId", required = true)
|
||||
@Parameter(name = "item", description = "item", required = true)
|
||||
public Map<String, Object> getHostExtra(@RequestParam("hostId") Long hostId, @RequestParam("item") String item) {
|
||||
return hostExtraService.getHostExtra(hostId, item);
|
||||
}
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@PostMapping("/list")
|
||||
@Operation(summary = "获取多个主机拓展信息")
|
||||
public Map<String, Map<String, Object>> getHostExtraList(@Validated @RequestBody HostExtraQueryRequest request) {
|
||||
return hostExtraService.getHostExtraList(request);
|
||||
}
|
||||
|
||||
@GetMapping("/update")
|
||||
@Operation(summary = "修改主机拓展信息")
|
||||
public Integer updateHostExtra(@Validated @RequestBody HostExtraUpdateRequest request) {
|
||||
return hostExtraService.updateHostExtra(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.orion.ops.module.asset.entity.request.host;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 主机拓展信息查询请求
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/12/20 21:36
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "HostExtraQueryRequest", description = "主机拓展信息查询请求")
|
||||
public class HostExtraQueryRequest {
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "主机id")
|
||||
private Long hostId;
|
||||
|
||||
@NotEmpty
|
||||
@Schema(description = "配置项")
|
||||
private List<String> items;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.orion.ops.module.asset.entity.request.host;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 主机拓展信息更新请求
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/12/20 21:36
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "HostExtraUpdateRequest", description = "主机拓展信息更新请求")
|
||||
public class HostExtraUpdateRequest {
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "主机id")
|
||||
private Long hostId;
|
||||
|
||||
@NotNull
|
||||
@Schema(description = "配置项")
|
||||
private String item;
|
||||
|
||||
@NotBlank
|
||||
@Schema(description = "拓展信息")
|
||||
private String extra;
|
||||
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.orion.ops.module.asset.enums;
|
||||
|
||||
/**
|
||||
* 主机验证类型
|
||||
* 主机验证类型 - ssh
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/9/21 19:01
|
||||
*/
|
||||
public enum HostAuthTypeEnum {
|
||||
public enum HostConfigSshAuthTypeEnum {
|
||||
|
||||
/**
|
||||
* 密码验证
|
||||
@@ -26,11 +26,11 @@ public enum HostAuthTypeEnum {
|
||||
|
||||
;
|
||||
|
||||
public static HostAuthTypeEnum of(String type) {
|
||||
public static HostConfigSshAuthTypeEnum of(String type) {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
for (HostAuthTypeEnum value : values()) {
|
||||
for (HostConfigSshAuthTypeEnum value : values()) {
|
||||
if (value.name().equals(type)) {
|
||||
return value;
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.orion.ops.module.asset.enums;
|
||||
|
||||
import com.orion.ops.framework.common.enums.EnableStatus;
|
||||
import com.orion.ops.framework.common.handler.data.GenericsDataDefinition;
|
||||
import com.orion.ops.framework.common.handler.data.model.GenericsDataModel;
|
||||
import com.orion.ops.framework.common.handler.data.strategy.MapDataStrategy;
|
||||
import com.orion.ops.module.asset.handler.host.config.model.HostSshConfigModel;
|
||||
import com.orion.ops.module.asset.handler.host.config.strategy.HostSshConfigStrategy;
|
||||
import com.orion.spring.SpringHolder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@@ -16,8 +16,9 @@ import lombok.Getter;
|
||||
* @version 1.0.0
|
||||
* @since 2023/9/11 14:37
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum HostConfigTypeEnum {
|
||||
public enum HostConfigTypeEnum implements GenericsDataDefinition {
|
||||
|
||||
/**
|
||||
* SSH 配置
|
||||
@@ -26,12 +27,10 @@ public enum HostConfigTypeEnum {
|
||||
|
||||
;
|
||||
|
||||
@Getter
|
||||
private final Class<? extends GenericsDataModel> type;
|
||||
|
||||
private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy;
|
||||
|
||||
@Getter
|
||||
private final Integer defaultStatus;
|
||||
|
||||
public static HostConfigTypeEnum of(String type) {
|
||||
@@ -46,9 +45,4 @@ public enum HostConfigTypeEnum {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <Config extends GenericsDataModel, T extends MapDataStrategy<Config>> T getStrategy() {
|
||||
return (T) SpringHolder.getBean(strategy);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.orion.ops.module.asset.enums;
|
||||
|
||||
import com.orion.ops.framework.common.handler.data.GenericsDataDefinition;
|
||||
import com.orion.ops.framework.common.handler.data.model.GenericsDataModel;
|
||||
import com.orion.ops.framework.common.handler.data.strategy.MapDataStrategy;
|
||||
import com.orion.ops.module.asset.handler.host.extra.model.HostSshExtraModel;
|
||||
import com.orion.ops.module.asset.handler.host.extra.strategy.HostSshExtraStrategy;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 主机额外配置项枚举
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/12/20 22:48
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum HostExtraItemEnum implements GenericsDataDefinition {
|
||||
|
||||
/**
|
||||
* SSH 额外配置
|
||||
*/
|
||||
SSH("ssh", HostSshExtraModel.class, HostSshExtraStrategy.class),
|
||||
|
||||
;
|
||||
|
||||
private final String item;
|
||||
|
||||
private final Class<? extends GenericsDataModel> type;
|
||||
|
||||
private final Class<? extends MapDataStrategy<? extends GenericsDataModel>> strategy;
|
||||
|
||||
public static HostExtraItemEnum of(String type) {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
for (HostExtraItemEnum value : values()) {
|
||||
if (value.item.equals(type)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.orion.ops.module.asset.enums;
|
||||
|
||||
/**
|
||||
* 主机认证类型
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/12/20 21:41
|
||||
*/
|
||||
public enum HostExtraSshAuthTypeEnum {
|
||||
|
||||
/**
|
||||
* 默认验证方式
|
||||
*/
|
||||
DEFAULT,
|
||||
|
||||
/**
|
||||
* 秘钥验证
|
||||
*/
|
||||
KEY,
|
||||
|
||||
/**
|
||||
* 身份验证
|
||||
*/
|
||||
IDENTITY,
|
||||
|
||||
;
|
||||
|
||||
public static HostExtraSshAuthTypeEnum of(String type) {
|
||||
if (type == null) {
|
||||
return DEFAULT;
|
||||
}
|
||||
for (HostExtraSshAuthTypeEnum value : values()) {
|
||||
if (value.name().equals(type)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return DEFAULT;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import com.orion.ops.framework.common.security.PasswordModifier;
|
||||
import com.orion.ops.framework.common.utils.Valid;
|
||||
import com.orion.ops.module.asset.dao.HostIdentityDAO;
|
||||
import com.orion.ops.module.asset.dao.HostKeyDAO;
|
||||
import com.orion.ops.module.asset.enums.HostAuthTypeEnum;
|
||||
import com.orion.ops.module.asset.enums.HostConfigSshAuthTypeEnum;
|
||||
import com.orion.ops.module.asset.handler.host.config.model.HostSshConfigModel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -44,6 +44,7 @@ public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel
|
||||
return HostSshConfigModel.builder()
|
||||
.port(SSH_PORT)
|
||||
.username(USERNAME)
|
||||
.authType(HostConfigSshAuthTypeEnum.PASSWORD.name())
|
||||
.charset(Const.UTF_8)
|
||||
.connectTimeout(Const.MS_S_10)
|
||||
.fileNameCharset(Const.UTF_8)
|
||||
@@ -52,37 +53,37 @@ public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preValidConfig(HostSshConfigModel config) {
|
||||
public void preValid(HostSshConfigModel model) {
|
||||
// 验证认证类型
|
||||
Valid.valid(HostAuthTypeEnum::of, config.getAuthType());
|
||||
Valid.valid(HostConfigSshAuthTypeEnum::of, model.getAuthType());
|
||||
// 验证编码格式
|
||||
this.validCharset(config.getCharset());
|
||||
this.validCharset(config.getFileNameCharset());
|
||||
this.validCharset(config.getFileContentCharset());
|
||||
this.validCharset(model.getCharset());
|
||||
this.validCharset(model.getFileNameCharset());
|
||||
this.validCharset(model.getFileContentCharset());
|
||||
// 检查主机秘钥是否存在
|
||||
Long keyId = config.getKeyId();
|
||||
Long keyId = model.getKeyId();
|
||||
if (keyId != null) {
|
||||
Valid.notNull(hostKeyDAO.selectById(keyId), ErrorMessage.KEY_ABSENT);
|
||||
}
|
||||
// 检查主机身份是否存在
|
||||
Long identityId = config.getIdentityId();
|
||||
Long identityId = model.getIdentityId();
|
||||
if (identityId != null) {
|
||||
Valid.notNull(hostIdentityDAO.selectById(identityId), ErrorMessage.IDENTITY_ABSENT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validConfig(HostSshConfigModel config) {
|
||||
public void valid(HostSshConfigModel model) {
|
||||
// 验证填充后的参数
|
||||
Valid.valid(config);
|
||||
Valid.valid(model);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(HostSshConfigModel before, HostSshConfigModel after) {
|
||||
public void updateFill(HostSshConfigModel beforeModel, HostSshConfigModel afterModel) {
|
||||
// 加密密码
|
||||
this.checkEncryptPassword(before, after);
|
||||
after.setHasPassword(null);
|
||||
after.setUseNewPassword(null);
|
||||
this.checkEncryptPassword(beforeModel, afterModel);
|
||||
afterModel.setHasPassword(null);
|
||||
afterModel.setUseNewPassword(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +105,7 @@ public class HostSshConfigStrategy implements MapDataStrategy<HostSshConfigModel
|
||||
*/
|
||||
private void checkEncryptPassword(HostSshConfigModel before, HostSshConfigModel after) {
|
||||
// 非密码认证则直接赋值
|
||||
if (!HostAuthTypeEnum.PASSWORD.name().equals(after.getAuthType())) {
|
||||
if (!HostConfigSshAuthTypeEnum.PASSWORD.name().equals(after.getAuthType())) {
|
||||
after.setPassword(before.getPassword());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.orion.ops.module.asset.handler.host.extra.model;
|
||||
|
||||
import com.orion.ops.framework.common.handler.data.model.GenericsDataModel;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 主机拓展信息 - ssh 模型
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/12/20 21:36
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "HostExtraSshModel", description = "主机拓展信息 - ssh 模型")
|
||||
public class HostSshExtraModel implements GenericsDataModel {
|
||||
|
||||
@Schema(description = "认证方式")
|
||||
private String authType;
|
||||
|
||||
@Schema(description = "认证方式")
|
||||
private String username;
|
||||
|
||||
@Schema(description = "主机秘钥")
|
||||
private Long keyId;
|
||||
|
||||
@Schema(description = "主机身份")
|
||||
private Long identityId;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.orion.ops.module.asset.handler.host.extra.strategy;
|
||||
|
||||
import com.orion.ops.framework.common.handler.data.strategy.MapDataStrategy;
|
||||
import com.orion.ops.module.asset.enums.HostExtraSshAuthTypeEnum;
|
||||
import com.orion.ops.module.asset.handler.host.extra.model.HostSshExtraModel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 主机拓展信息 - ssh 模型处理策略
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/12/20 22:17
|
||||
*/
|
||||
@Component
|
||||
public class HostSshExtraStrategy implements MapDataStrategy<HostSshExtraModel> {
|
||||
|
||||
@Override
|
||||
public HostSshExtraModel getDefault() {
|
||||
return HostSshExtraModel.builder()
|
||||
.authType(HostExtraSshAuthTypeEnum.DEFAULT.name())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(HostSshExtraModel beforeModel, HostSshExtraModel afterModel) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preValid(HostSshExtraModel model) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void valid(HostSshExtraModel model) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.orion.ops.module.asset.service;
|
||||
|
||||
import com.orion.ops.module.asset.entity.request.host.HostAliasUpdateRequest;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostExtraQueryRequest;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostExtraUpdateRequest;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 主机拓展信息 服务
|
||||
@@ -19,6 +21,45 @@ public interface HostExtraService {
|
||||
* @param request request
|
||||
* @return effect
|
||||
*/
|
||||
Integer updateHostAlias(@Validated @RequestBody HostAliasUpdateRequest request);
|
||||
Integer updateHostAlias(HostAliasUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 获取主机额外配置
|
||||
*
|
||||
* @param hostId hostId
|
||||
* @param item item
|
||||
* @return extra
|
||||
*/
|
||||
Map<String, Object> getHostExtra(Long hostId, String item);
|
||||
|
||||
/**
|
||||
* 获取多个主机拓展信息
|
||||
*
|
||||
* @param request request
|
||||
* @return type:extra
|
||||
*/
|
||||
Map<String, Map<String, Object>> getHostExtraList(HostExtraQueryRequest request);
|
||||
|
||||
/**
|
||||
* 修改主机拓展信息
|
||||
*
|
||||
* @param request request
|
||||
* @return effect
|
||||
*/
|
||||
Integer updateHostExtra(HostExtraUpdateRequest request);
|
||||
|
||||
/**
|
||||
* 删除主机秘钥回调
|
||||
*
|
||||
* @param id id
|
||||
*/
|
||||
void deleteHostKeyCallback(Long id);
|
||||
|
||||
/**
|
||||
* 删除主机身份回调
|
||||
*
|
||||
* @param id id
|
||||
*/
|
||||
void deleteHostIdentityCallback(Long id);
|
||||
|
||||
}
|
||||
|
||||
@@ -44,6 +44,8 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
@Resource
|
||||
private HostConfigDAO hostConfigDAO;
|
||||
|
||||
// FIXME 动态初始化
|
||||
|
||||
@Override
|
||||
public HostConfigVO getHostConfig(Long hostId, String type) {
|
||||
HostConfigTypeEnum configType = Valid.valid(HostConfigTypeEnum::of, type);
|
||||
@@ -53,7 +55,7 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
// 转换
|
||||
HostConfigVO vo = HostConfigConvert.MAPPER.to(config);
|
||||
// 获取配置
|
||||
Map<String, Object> configMap = configType.getStrategy().toView(config.getConfig());
|
||||
Map<String, Object> configMap = configType.getStrategyBean().toView(config.getConfig());
|
||||
vo.setConfig(configMap);
|
||||
return vo;
|
||||
}
|
||||
@@ -82,7 +84,7 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
HostConfigVO vo = HostConfigConvert.MAPPER.to(s);
|
||||
// 获取配置
|
||||
Map<String, Object> config = HostConfigTypeEnum.of(s.getType())
|
||||
.getStrategy()
|
||||
.getStrategyBean()
|
||||
.toView(s.getConfig());
|
||||
vo.setConfig(config);
|
||||
return vo;
|
||||
@@ -96,7 +98,7 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
HostConfigDO record = hostConfigDAO.selectById(id);
|
||||
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
|
||||
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, record.getType());
|
||||
GenericsDataModel config = JSON.parseObject(request.getConfig(), type.getType());
|
||||
GenericsDataModel newConfig = type.parse(request.getConfig());
|
||||
// 查询主机
|
||||
HostDO host = hostDAO.selectById(record.getHostId());
|
||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||
@@ -106,19 +108,15 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
OperatorLogs.add(OperatorLogs.TYPE, type.name());
|
||||
// 检查版本
|
||||
Valid.eq(record.getVersion(), request.getVersion(), ErrorMessage.DATA_MODIFIED);
|
||||
MapDataStrategy<GenericsDataModel> strategy = type.getStrategy();
|
||||
// 预校验参数
|
||||
strategy.preValidConfig(config);
|
||||
// 更新填充
|
||||
GenericsDataModel beforeConfig = JSON.parseObject(record.getConfig(), type.getType());
|
||||
strategy.updateFill(beforeConfig, config);
|
||||
// 检查参数
|
||||
strategy.validConfig(config);
|
||||
MapDataStrategy<GenericsDataModel> strategy = type.getStrategyBean();
|
||||
GenericsDataModel beforeConfig = type.parse(record.getConfig());
|
||||
// 更新前校验
|
||||
strategy.doValidChain(beforeConfig, newConfig);
|
||||
// 修改配置
|
||||
HostConfigDO update = new HostConfigDO();
|
||||
update.setId(id);
|
||||
update.setVersion(request.getVersion());
|
||||
update.setConfig(config.serial());
|
||||
update.setConfig(newConfig.serial());
|
||||
int effect = hostConfigDAO.updateById(update);
|
||||
Valid.version(effect);
|
||||
return update.getVersion();
|
||||
@@ -158,7 +156,7 @@ public class HostConfigServiceImpl implements HostConfigService {
|
||||
insert.setHostId(hostId);
|
||||
insert.setType(s.name());
|
||||
insert.setStatus(s.getDefaultStatus());
|
||||
insert.setConfig(s.getStrategy().getDefault().serial());
|
||||
insert.setConfig(s.getStrategyBean().getDefault().serial());
|
||||
insert.setVersion(Const.DEFAULT_VERSION);
|
||||
return insert;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
package com.orion.ops.module.asset.service.impl;
|
||||
|
||||
import com.orion.lang.function.Functions;
|
||||
import com.orion.lang.utils.collect.Maps;
|
||||
import com.orion.ops.framework.common.constant.ErrorMessage;
|
||||
import com.orion.ops.framework.common.handler.data.model.GenericsDataModel;
|
||||
import com.orion.ops.framework.common.handler.data.strategy.MapDataStrategy;
|
||||
import com.orion.ops.framework.common.utils.Valid;
|
||||
import com.orion.ops.framework.security.core.utils.SecurityUtils;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostAliasUpdateRequest;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostExtraQueryRequest;
|
||||
import com.orion.ops.module.asset.entity.request.host.HostExtraUpdateRequest;
|
||||
import com.orion.ops.module.asset.enums.HostExtraItemEnum;
|
||||
import com.orion.ops.module.asset.service.HostExtraService;
|
||||
import com.orion.ops.module.infra.api.DataAliasApi;
|
||||
import com.orion.ops.module.infra.api.DataExtraApi;
|
||||
import com.orion.ops.module.infra.entity.dto.data.DataAliasUpdateDTO;
|
||||
import com.orion.ops.module.infra.entity.dto.data.DataExtraDTO;
|
||||
import com.orion.ops.module.infra.entity.dto.data.DataExtraQueryDTO;
|
||||
import com.orion.ops.module.infra.entity.dto.data.DataExtraSetDTO;
|
||||
import com.orion.ops.module.infra.enums.DataExtraTypeEnum;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 主机拓展信息 服务实现类
|
||||
@@ -23,6 +39,9 @@ public class HostExtraServiceImpl implements HostExtraService {
|
||||
@Resource
|
||||
private DataAliasApi dataAliasApi;
|
||||
|
||||
@Resource
|
||||
private DataExtraApi dataExtraApi;
|
||||
|
||||
@Override
|
||||
public Integer updateHostAlias(HostAliasUpdateRequest request) {
|
||||
DataAliasUpdateDTO update = DataAliasUpdateDTO.builder()
|
||||
@@ -33,4 +52,112 @@ public class HostExtraServiceImpl implements HostExtraService {
|
||||
return dataAliasApi.updateDataAlias(update, DataExtraTypeEnum.HOST);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getHostExtra(Long hostId, String item) {
|
||||
HostExtraItemEnum extraItem = Valid.valid(HostExtraItemEnum::of, item);
|
||||
// 查询配置项
|
||||
Long userId = SecurityUtils.getLoginUserId();
|
||||
DataExtraQueryDTO query = DataExtraQueryDTO.builder()
|
||||
.userId(userId)
|
||||
.relId(hostId)
|
||||
.item(item)
|
||||
.build();
|
||||
String extraValue = dataExtraApi.getExtraValue(query, DataExtraTypeEnum.HOST);
|
||||
// 检查初始化并转为视图
|
||||
return this.checkItemAndToView(extraItem, extraValue, userId, hostId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Map<String, Object>> getHostExtraList(HostExtraQueryRequest request) {
|
||||
Long hostId = request.getHostId();
|
||||
List<String> items = request.getItems();
|
||||
List<HostExtraItemEnum> extraItems = items.stream()
|
||||
.map(s -> Valid.valid(HostExtraItemEnum::of, s))
|
||||
.collect(Collectors.toList());
|
||||
// 查询配置项
|
||||
Long userId = SecurityUtils.getLoginUserId();
|
||||
DataExtraQueryDTO query = DataExtraQueryDTO.builder()
|
||||
.userId(userId)
|
||||
.relId(hostId)
|
||||
.items(items)
|
||||
.build();
|
||||
Map<String, String> extraValues = dataExtraApi.getExtraItems(query, DataExtraTypeEnum.HOST)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(
|
||||
DataExtraDTO::getItem,
|
||||
DataExtraDTO::getValue,
|
||||
Functions.right())
|
||||
);
|
||||
// 检查初始化
|
||||
Map<String, Map<String, Object>> result = Maps.newMap();
|
||||
for (HostExtraItemEnum extraItem : extraItems) {
|
||||
String item = extraItem.getItem();
|
||||
// 检查初始化并转为视图
|
||||
Map<String, Object> extraValue = this.checkItemAndToView(extraItem, extraValues.get(item), userId, hostId);
|
||||
result.put(item, extraValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer updateHostExtra(HostExtraUpdateRequest request) {
|
||||
String item = request.getItem();
|
||||
Long hostId = request.getHostId();
|
||||
Long userId = SecurityUtils.getLoginUserId();
|
||||
HostExtraItemEnum extraItem = Valid.valid(HostExtraItemEnum::of, item);
|
||||
MapDataStrategy<GenericsDataModel> strategy = extraItem.getStrategyBean();
|
||||
// 查询原始配置
|
||||
DataExtraQueryDTO query = DataExtraQueryDTO.builder()
|
||||
.userId(userId)
|
||||
.relId(hostId)
|
||||
.item(item)
|
||||
.build();
|
||||
DataExtraDTO beforeExtraItem = dataExtraApi.getExtraItem(query, DataExtraTypeEnum.HOST);
|
||||
Valid.notNull(beforeExtraItem, ErrorMessage.CONFIG_ABSENT);
|
||||
GenericsDataModel newExtra = extraItem.parse(request.getExtra());
|
||||
GenericsDataModel beforeExtra = extraItem.parse(beforeExtraItem.getValue());
|
||||
// 更新验证
|
||||
strategy.doValidChain(beforeExtra, newExtra);
|
||||
// 更新配置
|
||||
return dataExtraApi.updateExtraValue(beforeExtraItem.getId(), newExtra.serial());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void deleteHostKeyCallback(Long id) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteHostIdentityCallback(Long id) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查配置项并且转为视图 (不存在则初始化默认值)
|
||||
*
|
||||
* @param extraItem extraItem
|
||||
* @param extraValue extraValue
|
||||
* @param userId userId
|
||||
* @param hostId hostId
|
||||
* @return viewMap
|
||||
*/
|
||||
private Map<String, Object> checkItemAndToView(HostExtraItemEnum extraItem, String extraValue, Long userId, Long hostId) {
|
||||
String item = extraItem.getItem();
|
||||
MapDataStrategy<GenericsDataModel> strategy = extraItem.getStrategyBean();
|
||||
if (extraValue == null) {
|
||||
// 初始化默认数据
|
||||
extraValue = strategy.getDefault().serial();
|
||||
// 插入默认值
|
||||
DataExtraSetDTO set = DataExtraSetDTO.builder()
|
||||
.userId(userId)
|
||||
.relId(hostId)
|
||||
.item(item)
|
||||
.value(extraValue)
|
||||
.build();
|
||||
dataExtraApi.addExtraItem(set, DataExtraTypeEnum.HOST);
|
||||
}
|
||||
return strategy.toView(extraValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user