refactor: 修改主机配置.
This commit is contained in:
@@ -8,6 +8,7 @@ import lombok.NoArgsConstructor;
|
|||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,8 +26,13 @@ import java.io.Serializable;
|
|||||||
public class HostConfigUpdateRequest implements Serializable {
|
public class HostConfigUpdateRequest implements Serializable {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "id")
|
@Schema(description = "主机id")
|
||||||
private Long id;
|
private Long hostId;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Size(max = 32)
|
||||||
|
@Schema(description = "配置类型")
|
||||||
|
private String type;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Schema(description = "配置详情")
|
@Schema(description = "配置详情")
|
||||||
|
|||||||
@@ -24,9 +24,6 @@ import java.io.Serializable;
|
|||||||
@Schema(name = "HostConfigUpdateRequest", description = "主机配置 更新请求对象")
|
@Schema(name = "HostConfigUpdateRequest", description = "主机配置 更新请求对象")
|
||||||
public class HostConfigUpdateStatusRequest implements Serializable {
|
public class HostConfigUpdateStatusRequest implements Serializable {
|
||||||
|
|
||||||
@Schema(description = "id")
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Schema(description = "主机id")
|
@Schema(description = "主机id")
|
||||||
private Long hostId;
|
private Long hostId;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import javax.annotation.Resource;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,13 +45,6 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
@Resource
|
@Resource
|
||||||
private HostConfigDAO hostConfigDAO;
|
private HostConfigDAO hostConfigDAO;
|
||||||
|
|
||||||
// FIXME
|
|
||||||
// T 动态初始化
|
|
||||||
// T 改为小写
|
|
||||||
// F 保存后重置有问题
|
|
||||||
// F 前端逻辑
|
|
||||||
// F 测试
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HostConfigVO getHostConfig(Long hostId, String type) {
|
public HostConfigVO getHostConfig(Long hostId, String type) {
|
||||||
HostConfigTypeEnum configType = Valid.valid(HostConfigTypeEnum::of, type);
|
HostConfigTypeEnum configType = Valid.valid(HostConfigTypeEnum::of, type);
|
||||||
@@ -81,22 +75,27 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
// 查询
|
// 查询
|
||||||
List<HostConfigDO> configs = hostConfigDAO.getHostConfigByHostId(hostId);
|
List<HostConfigDO> configs = hostConfigDAO.getHostConfigByHostId(hostId);
|
||||||
// 返回
|
// 返回
|
||||||
return configs.stream().map(s -> {
|
return configs.stream()
|
||||||
HostConfigVO vo = HostConfigConvert.MAPPER.to(s);
|
.map(s -> {
|
||||||
// 获取配置
|
// 获取配置
|
||||||
Map<String, Object> config = HostConfigTypeEnum.of(s.getType())
|
HostConfigTypeEnum type = HostConfigTypeEnum.of(s.getType());
|
||||||
.getStrategyBean()
|
if (type == null) {
|
||||||
.toView(s.getConfig());
|
return null;
|
||||||
vo.setConfig(config);
|
}
|
||||||
return vo;
|
// 转为视图
|
||||||
}).collect(Collectors.toList());
|
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
|
@Override
|
||||||
public Integer updateHostConfig(HostConfigUpdateRequest request) {
|
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);
|
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
|
||||||
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, record.getType());
|
HostConfigTypeEnum type = Valid.valid(HostConfigTypeEnum::of, record.getType());
|
||||||
GenericsDataModel newConfig = type.parse(request.getConfig());
|
GenericsDataModel newConfig = type.parse(request.getConfig());
|
||||||
@@ -115,7 +114,7 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
strategy.doValidChain(beforeConfig, newConfig);
|
strategy.doValidChain(beforeConfig, newConfig);
|
||||||
// 修改配置
|
// 修改配置
|
||||||
HostConfigDO update = new HostConfigDO();
|
HostConfigDO update = new HostConfigDO();
|
||||||
update.setId(id);
|
update.setId(record.getId());
|
||||||
update.setVersion(request.getVersion());
|
update.setVersion(request.getVersion());
|
||||||
update.setConfig(newConfig.serial());
|
update.setConfig(newConfig.serial());
|
||||||
int effect = hostConfigDAO.updateById(update);
|
int effect = hostConfigDAO.updateById(update);
|
||||||
@@ -125,33 +124,34 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) {
|
public Integer updateHostConfigStatus(HostConfigUpdateStatusRequest request) {
|
||||||
Long id = request.getId();
|
|
||||||
Long hostId = request.getHostId();
|
Long hostId = request.getHostId();
|
||||||
|
String type = request.getType();
|
||||||
Integer status = request.getStatus();
|
Integer status = request.getStatus();
|
||||||
EnableStatus statusEnum = Valid.valid(EnableStatus::of, status);
|
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);
|
HostDO host = hostDAO.selectById(hostId);
|
||||||
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
|
||||||
|
HostConfigDO config = this.getHostConfigByType(hostId, type);
|
||||||
// 添加日志参数
|
// 添加日志参数
|
||||||
OperatorLogs.add(OperatorLogs.REL_ID, host.getId());
|
OperatorLogs.add(OperatorLogs.REL_ID, host.getId());
|
||||||
OperatorLogs.add(OperatorLogs.NAME, host.getName());
|
OperatorLogs.add(OperatorLogs.NAME, host.getName());
|
||||||
OperatorLogs.add(OperatorLogs.STATUS_NAME, statusEnum.name());
|
OperatorLogs.add(OperatorLogs.STATUS_NAME, statusEnum.name());
|
||||||
if (id != null) {
|
if (config != null) {
|
||||||
// 修改 查询配置
|
// 修改 查询配置
|
||||||
HostConfigDO record = hostConfigDAO.selectById(id);
|
Integer version = request.getVersion();
|
||||||
Valid.notNull(record, ErrorMessage.CONFIG_ABSENT);
|
Valid.notNull(version);
|
||||||
// 修改状态
|
// 修改状态
|
||||||
HostConfigDO update = new HostConfigDO();
|
HostConfigDO update = new HostConfigDO();
|
||||||
update.setId(id);
|
update.setId(config.getId());
|
||||||
update.setStatus(status);
|
update.setStatus(status);
|
||||||
update.setVersion(request.getVersion());
|
update.setVersion(version);
|
||||||
int effect = hostConfigDAO.updateById(update);
|
int effect = hostConfigDAO.updateById(update);
|
||||||
Valid.version(effect);
|
Valid.version(effect);
|
||||||
return update.getVersion();
|
return update.getVersion();
|
||||||
} else {
|
} else {
|
||||||
// 新增 初始化
|
// 新增 初始化
|
||||||
HostConfigDO defaultConfig = this.getDefaultConfig(hostId, type);
|
HostConfigDO defaultConfig = this.getDefaultConfig(hostId, configType);
|
||||||
defaultConfig.setStatus(status);
|
defaultConfig.setStatus(status);
|
||||||
// 插入数据
|
// 插入数据
|
||||||
hostConfigDAO.insert(defaultConfig);
|
hostConfigDAO.insert(defaultConfig);
|
||||||
@@ -159,6 +159,7 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initHostConfig(Long hostId) {
|
public void initHostConfig(Long hostId) {
|
||||||
List<HostConfigDO> configs = Arrays.stream(HostConfigTypeEnum.values())
|
List<HostConfigDO> configs = Arrays.stream(HostConfigTypeEnum.values())
|
||||||
@@ -167,6 +168,23 @@ public class HostConfigServiceImpl implements HostConfigService {
|
|||||||
hostConfigDAO.insertBatch(configs);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取默认配置
|
* 获取默认配置
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import axios from 'axios';
|
|||||||
* 主机配置请求
|
* 主机配置请求
|
||||||
*/
|
*/
|
||||||
export interface HostConfigRequest {
|
export interface HostConfigRequest {
|
||||||
id?: number;
|
|
||||||
hostId?: number;
|
hostId?: number;
|
||||||
|
type?: string;
|
||||||
version?: number;
|
version?: number;
|
||||||
status?: number;
|
status?: number;
|
||||||
config?: string;
|
config?: string;
|
||||||
|
|||||||
@@ -16,7 +16,9 @@
|
|||||||
</template>
|
</template>
|
||||||
<a-spin :loading="loading" class="host-config-container">
|
<a-spin :loading="loading" class="host-config-container">
|
||||||
<!-- SSH 配置 -->
|
<!-- 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-spin>
|
||||||
</a-drawer>
|
</a-drawer>
|
||||||
</template>
|
</template>
|
||||||
@@ -42,9 +44,9 @@
|
|||||||
const { loading, setLoading } = useLoading();
|
const { loading, setLoading } = useLoading();
|
||||||
const cacheStore = useCacheStore();
|
const cacheStore = useCacheStore();
|
||||||
|
|
||||||
const record = ref();
|
const record = ref({} as any);
|
||||||
const config = ref<HostConfigWrapper>({
|
const config = ref<HostConfigWrapper>({
|
||||||
SSH: undefined
|
ssh: undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
// 打开
|
// 打开
|
||||||
@@ -61,6 +63,7 @@
|
|||||||
data.forEach(s => {
|
data.forEach(s => {
|
||||||
config.value[s.type] = s;
|
config.value[s.type] = s;
|
||||||
});
|
});
|
||||||
|
console.log(config.value);
|
||||||
} catch ({ message }) {
|
} catch ({ message }) {
|
||||||
Message.error(`配置加载失败 ${message}`);
|
Message.error(`配置加载失败 ${message}`);
|
||||||
setVisible(false);
|
setVisible(false);
|
||||||
|
|||||||
@@ -107,7 +107,6 @@
|
|||||||
<a-form-item field="fileNameCharset"
|
<a-form-item field="fileNameCharset"
|
||||||
label="文件名称编码"
|
label="文件名称编码"
|
||||||
:hide-asterisk="true"
|
:hide-asterisk="true"
|
||||||
|
|
||||||
label-col-flex="86px">
|
label-col-flex="86px">
|
||||||
<a-input v-model="formModel.fileNameCharset" placeholder="请输入 SFTP 文件名称编码" />
|
<a-input v-model="formModel.fileNameCharset" placeholder="请输入 SFTP 文件名称编码" />
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
@@ -115,7 +114,6 @@
|
|||||||
<a-form-item field="fileContentCharset"
|
<a-form-item field="fileContentCharset"
|
||||||
label="文件内容编码"
|
label="文件内容编码"
|
||||||
:hide-asterisk="true"
|
:hide-asterisk="true"
|
||||||
|
|
||||||
label-col-flex="86px">
|
label-col-flex="86px">
|
||||||
<a-input v-model="formModel.fileContentCharset" placeholder="请输入 SFTP 文件内容编码" />
|
<a-input v-model="formModel.fileContentCharset" placeholder="请输入 SFTP 文件内容编码" />
|
||||||
</a-form-item>
|
</a-form-item>
|
||||||
@@ -150,12 +148,14 @@
|
|||||||
import HostKeySelector from '@/components/asset/host-key/host-key-selector.vue';
|
import HostKeySelector from '@/components/asset/host-key/host-key-selector.vue';
|
||||||
import HostIdentitySelector from '@/components/asset/host-identity/host-identity-selector.vue';
|
import HostIdentitySelector from '@/components/asset/host-identity/host-identity-selector.vue';
|
||||||
import { EnabledStatus } from '@/types/const';
|
import { EnabledStatus } from '@/types/const';
|
||||||
|
import { HostConfigType } from '../../../types/const';
|
||||||
|
|
||||||
const { loading, setLoading } = useLoading();
|
const { loading, setLoading } = useLoading();
|
||||||
const { toOptions } = useDictStore();
|
const { toOptions } = useDictStore();
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
content: Object
|
content: Object,
|
||||||
|
hostId: Number,
|
||||||
});
|
});
|
||||||
|
|
||||||
const emits = defineEmits(['submitted']);
|
const emits = defineEmits(['submitted']);
|
||||||
@@ -220,7 +220,8 @@
|
|||||||
const updateStatus = (e: number) => {
|
const updateStatus = (e: number) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
return updateHostConfigStatus({
|
return updateHostConfigStatus({
|
||||||
id: props?.content?.id,
|
hostId: props?.hostId,
|
||||||
|
type: HostConfigType.SSH,
|
||||||
status: e,
|
status: e,
|
||||||
version: config.version
|
version: config.version
|
||||||
}).then(({ data }) => {
|
}).then(({ data }) => {
|
||||||
@@ -249,8 +250,10 @@
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
// 更新
|
||||||
const { data } = await updateHostConfig({
|
const { data } = await updateHostConfig({
|
||||||
id: props?.content?.id,
|
hostId: props?.hostId,
|
||||||
|
type: HostConfigType.SSH,
|
||||||
version: config.version,
|
version: config.version,
|
||||||
config: JSON.stringify(formModel.value)
|
config: JSON.stringify(formModel.value)
|
||||||
});
|
});
|
||||||
@@ -258,7 +261,7 @@
|
|||||||
setLoading(false);
|
setLoading(false);
|
||||||
Message.success('修改成功');
|
Message.success('修改成功');
|
||||||
// 回调 props
|
// 回调 props
|
||||||
emits('submitted', { ...formModel });
|
emits('submitted', { ...formModel.value });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
@@ -2,11 +2,16 @@ import type { HostSshConfig } from '../components/config/ssh/types/const';
|
|||||||
|
|
||||||
// 主机所有配置
|
// 主机所有配置
|
||||||
export interface HostConfigWrapper {
|
export interface HostConfigWrapper {
|
||||||
SSH: HostSshConfig | unknown;
|
ssh: HostSshConfig | unknown;
|
||||||
|
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 主机配置类型
|
||||||
|
export const HostConfigType = {
|
||||||
|
SSH: 'ssh'
|
||||||
|
};
|
||||||
|
|
||||||
// tag 颜色
|
// tag 颜色
|
||||||
export const tagColor = [
|
export const tagColor = [
|
||||||
'arcoblue',
|
'arcoblue',
|
||||||
|
|||||||
Reference in New Issue
Block a user