From b2a697f6ba591f87c8aea15f20bb4a7f9d1b1f21 Mon Sep 17 00:00:00 2001 From: lijiahang Date: Mon, 25 Dec 2023 12:12:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9=E4=B8=BB?= =?UTF-8?q?=E6=9C=BA=E9=85=8D=E7=BD=AE.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/host/HostConfigUpdateRequest.java | 10 ++- .../host/HostConfigUpdateStatusRequest.java | 3 - .../service/impl/HostConfigServiceImpl.java | 72 ++++++++++++------- orion-ops-ui/src/api/asset/host-config.ts | 2 +- .../components/config/host-config-drawer.vue | 9 ++- .../components/config/ssh/ssh-config-form.vue | 15 ++-- .../src/views/asset/host-list/types/const.ts | 7 +- 7 files changed, 75 insertions(+), 43 deletions(-) diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateRequest.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateRequest.java index 7964376d..d4c98581 100644 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateRequest.java +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateRequest.java @@ -8,6 +8,7 @@ import lombok.NoArgsConstructor; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; import java.io.Serializable; /** @@ -25,8 +26,13 @@ import java.io.Serializable; public class HostConfigUpdateRequest implements Serializable { @NotNull - @Schema(description = "id") - private Long id; + @Schema(description = "主机id") + private Long hostId; + + @NotNull + @Size(max = 32) + @Schema(description = "配置类型") + private String type; @NotBlank @Schema(description = "配置详情") diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateStatusRequest.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateStatusRequest.java index d3a848e1..03f75a9b 100644 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateStatusRequest.java +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/entity/request/host/HostConfigUpdateStatusRequest.java @@ -24,9 +24,6 @@ import java.io.Serializable; @Schema(name = "HostConfigUpdateRequest", description = "主机配置 更新请求对象") public class HostConfigUpdateStatusRequest implements Serializable { - @Schema(description = "id") - private Long id; - @NotNull @Schema(description = "主机id") private Long hostId; diff --git a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/HostConfigServiceImpl.java b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/HostConfigServiceImpl.java index 98e47b3a..5e52bae3 100644 --- a/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/HostConfigServiceImpl.java +++ b/orion-ops-module-asset/orion-ops-module-asset-service/src/main/java/com/orion/ops/module/asset/service/impl/HostConfigServiceImpl.java @@ -25,6 +25,7 @@ import javax.annotation.Resource; import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -44,13 +45,6 @@ public class HostConfigServiceImpl implements HostConfigService { @Resource private HostConfigDAO hostConfigDAO; - // FIXME - // T 动态初始化 - // T 改为小写 - // F 保存后重置有问题 - // F 前端逻辑 - // F 测试 - @Override public HostConfigVO getHostConfig(Long hostId, String type) { HostConfigTypeEnum configType = Valid.valid(HostConfigTypeEnum::of, type); @@ -81,22 +75,27 @@ public class HostConfigServiceImpl implements HostConfigService { // 查询 List configs = hostConfigDAO.getHostConfigByHostId(hostId); // 返回 - return configs.stream().map(s -> { - HostConfigVO vo = HostConfigConvert.MAPPER.to(s); - // 获取配置 - Map config = HostConfigTypeEnum.of(s.getType()) - .getStrategyBean() - .toView(s.getConfig()); - vo.setConfig(config); - return vo; - }).collect(Collectors.toList()); + return configs.stream() + .map(s -> { + // 获取配置 + HostConfigTypeEnum type = HostConfigTypeEnum.of(s.getType()); + if (type == null) { + return null; + } + // 转为视图 + HostConfigVO vo = HostConfigConvert.MAPPER.to(s); + Map config = type.getStrategyBean().toView(s.getConfig()); + vo.setConfig(config); + return vo; + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } @Override public Integer updateHostConfig(HostConfigUpdateRequest request) { - Long id = request.getId(); // 查询原配置 - HostConfigDO record = hostConfigDAO.selectById(id); + HostConfigDO record = this.getHostConfigByType(request.getHostId(), request.getType()); Valid.notNull(record, ErrorMessage.CONFIG_ABSENT); HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, record.getType()); GenericsDataModel newConfig = type.parse(request.getConfig()); @@ -115,7 +114,7 @@ public class HostConfigServiceImpl implements HostConfigService { strategy.doValidChain(beforeConfig, newConfig); // 修改配置 HostConfigDO update = new HostConfigDO(); - update.setId(id); + update.setId(record.getId()); update.setVersion(request.getVersion()); update.setConfig(newConfig.serial()); int effect = hostConfigDAO.updateById(update); @@ -125,33 +124,34 @@ public class HostConfigServiceImpl implements HostConfigService { @Override public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) { - Long id = request.getId(); Long hostId = request.getHostId(); + String type = request.getType(); Integer status = request.getStatus(); EnableStatus statusEnum = Valid.valid(EnableStatus::of, status); - HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, request.getType()); + HostConfigTypeEnum configType = Valid.valid(HostConfigTypeEnum::of, type); // 查询主机 HostDO host = hostDAO.selectById(hostId); Valid.notNull(host, ErrorMessage.HOST_ABSENT); + HostConfigDO config = this.getHostConfigByType(hostId, type); // 添加日志参数 OperatorLogs.add(OperatorLogs.REL_ID, host.getId()); OperatorLogs.add(OperatorLogs.NAME, host.getName()); OperatorLogs.add(OperatorLogs.STATUS_NAME, statusEnum.name()); - if (id != null) { + if (config != null) { // 修改 查询配置 - HostConfigDO record = hostConfigDAO.selectById(id); - Valid.notNull(record, ErrorMessage.CONFIG_ABSENT); + Integer version = request.getVersion(); + Valid.notNull(version); // 修改状态 HostConfigDO update = new HostConfigDO(); - update.setId(id); + update.setId(config.getId()); update.setStatus(status); - update.setVersion(request.getVersion()); + update.setVersion(version); int effect = hostConfigDAO.updateById(update); Valid.version(effect); return update.getVersion(); } else { // 新增 初始化 - HostConfigDO defaultConfig = this.getDefaultConfig(hostId, type); + HostConfigDO defaultConfig = this.getDefaultConfig(hostId, configType); defaultConfig.setStatus(status); // 插入数据 hostConfigDAO.insert(defaultConfig); @@ -159,6 +159,7 @@ public class HostConfigServiceImpl implements HostConfigService { } } + @Override public void initHostConfig(Long hostId) { List configs = Arrays.stream(HostConfigTypeEnum.values()) @@ -167,6 +168,23 @@ public class HostConfigServiceImpl implements HostConfigService { hostConfigDAO.insertBatch(configs); } + /** + * 通过类型获取配置 + * + * @param hostId hostId + * @param type type + * @return config + */ + private HostConfigDO getHostConfigByType(Long hostId, String type) { + // 查询配置 + return hostConfigDAO.of() + .createWrapper() + .eq(HostConfigDO::getHostId, hostId) + .eq(HostConfigDO::getType, type) + .then() + .getOne(); + } + /** * 获取默认配置 * diff --git a/orion-ops-ui/src/api/asset/host-config.ts b/orion-ops-ui/src/api/asset/host-config.ts index 353ef1c0..7c69ce6f 100644 --- a/orion-ops-ui/src/api/asset/host-config.ts +++ b/orion-ops-ui/src/api/asset/host-config.ts @@ -4,8 +4,8 @@ import axios from 'axios'; * 主机配置请求 */ export interface HostConfigRequest { - id?: number; hostId?: number; + type?: string; version?: number; status?: number; config?: string; diff --git a/orion-ops-ui/src/views/asset/host-list/components/config/host-config-drawer.vue b/orion-ops-ui/src/views/asset/host-list/components/config/host-config-drawer.vue index ced933e2..59621c8e 100644 --- a/orion-ops-ui/src/views/asset/host-list/components/config/host-config-drawer.vue +++ b/orion-ops-ui/src/views/asset/host-list/components/config/host-config-drawer.vue @@ -16,7 +16,9 @@ - + @@ -42,9 +44,9 @@ const { loading, setLoading } = useLoading(); const cacheStore = useCacheStore(); - const record = ref(); + const record = ref({} as any); const config = ref({ - SSH: undefined + ssh: undefined }); // 打开 @@ -61,6 +63,7 @@ data.forEach(s => { config.value[s.type] = s; }); + console.log(config.value); } catch ({ message }) { Message.error(`配置加载失败 ${message}`); setVisible(false); diff --git a/orion-ops-ui/src/views/asset/host-list/components/config/ssh/ssh-config-form.vue b/orion-ops-ui/src/views/asset/host-list/components/config/ssh/ssh-config-form.vue index 151cd676..16071d90 100644 --- a/orion-ops-ui/src/views/asset/host-list/components/config/ssh/ssh-config-form.vue +++ b/orion-ops-ui/src/views/asset/host-list/components/config/ssh/ssh-config-form.vue @@ -107,7 +107,6 @@ @@ -115,7 +114,6 @@ @@ -150,12 +148,14 @@ import HostKeySelector from '@/components/asset/host-key/host-key-selector.vue'; import HostIdentitySelector from '@/components/asset/host-identity/host-identity-selector.vue'; import { EnabledStatus } from '@/types/const'; + import { HostConfigType } from '../../../types/const'; const { loading, setLoading } = useLoading(); const { toOptions } = useDictStore(); const props = defineProps({ - content: Object + content: Object, + hostId: Number, }); const emits = defineEmits(['submitted']); @@ -220,7 +220,8 @@ const updateStatus = (e: number) => { setLoading(true); return updateHostConfigStatus({ - id: props?.content?.id, + hostId: props?.hostId, + type: HostConfigType.SSH, status: e, version: config.version }).then(({ data }) => { @@ -249,8 +250,10 @@ return false; } setLoading(true); + // 更新 const { data } = await updateHostConfig({ - id: props?.content?.id, + hostId: props?.hostId, + type: HostConfigType.SSH, version: config.version, config: JSON.stringify(formModel.value) }); @@ -258,7 +261,7 @@ setLoading(false); Message.success('修改成功'); // 回调 props - emits('submitted', { ...formModel }); + emits('submitted', { ...formModel.value }); } catch (e) { } finally { setLoading(false); diff --git a/orion-ops-ui/src/views/asset/host-list/types/const.ts b/orion-ops-ui/src/views/asset/host-list/types/const.ts index fb28a249..c24a4493 100644 --- a/orion-ops-ui/src/views/asset/host-list/types/const.ts +++ b/orion-ops-ui/src/views/asset/host-list/types/const.ts @@ -2,11 +2,16 @@ import type { HostSshConfig } from '../components/config/ssh/types/const'; // 主机所有配置 export interface HostConfigWrapper { - SSH: HostSshConfig | unknown; + ssh: HostSshConfig | unknown; [key: string]: unknown; } +// 主机配置类型 +export const HostConfigType = { + SSH: 'ssh' +}; + // tag 颜色 export const tagColor = [ 'arcoblue',