refactor: 修改主机配置.

This commit is contained in:
lijiahang
2023-12-25 12:12:05 +08:00
parent 5cf70595c0
commit b2a697f6ba
7 changed files with 75 additions and 43 deletions

View File

@@ -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 = "配置详情")

View File

@@ -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;

View File

@@ -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<HostConfigDO> configs = hostConfigDAO.getHostConfigByHostId(hostId);
// 返回
return configs.stream().map(s -> {
HostConfigVO vo = HostConfigConvert.MAPPER.to(s);
// 获取配置
Map<String, Object> 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<String, Object> 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<HostConfigDO> 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();
}
/**
* 获取默认配置
*

View File

@@ -4,8 +4,8 @@ import axios from 'axios';
* 主机配置请求
*/
export interface HostConfigRequest {
id?: number;
hostId?: number;
type?: string;
version?: number;
status?: number;
config?: string;

View File

@@ -16,7 +16,9 @@
</template>
<a-spin :loading="loading" class="host-config-container">
<!-- SSH 配置 -->
<ssh-config-form :content="config.SSH" @submitted="(e) => config.SSH.config = e" />
<ssh-config-form :host-id="record.id"
:content="config.ssh"
@submitted="(e) => config.ssh.config = e" />
</a-spin>
</a-drawer>
</template>
@@ -42,9 +44,9 @@
const { loading, setLoading } = useLoading();
const cacheStore = useCacheStore();
const record = ref();
const record = ref({} as any);
const config = ref<HostConfigWrapper>({
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);

View File

@@ -107,7 +107,6 @@
<a-form-item field="fileNameCharset"
label="文件名称编码"
:hide-asterisk="true"
label-col-flex="86px">
<a-input v-model="formModel.fileNameCharset" placeholder="请输入 SFTP 文件名称编码" />
</a-form-item>
@@ -115,7 +114,6 @@
<a-form-item field="fileContentCharset"
label="文件内容编码"
:hide-asterisk="true"
label-col-flex="86px">
<a-input v-model="formModel.fileContentCharset" placeholder="请输入 SFTP 文件内容编码" />
</a-form-item>
@@ -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);

View File

@@ -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',