feat(device): 添加设备创建时关联DNS服务器功能
- 在DeviceServiceImpl中注入DnsServerService依赖 - 设备创建流程中增加DNS服务器名称列表的处理逻辑 - 调用DnsServerService根据DNS名称列表获取对应ID列表 - 新增DnsServerService接口及实现类DnsServerServiceImpl - 实现根据单个或多个DNS名称查询对应服务器ID的方法 - 在ErrorCode中新增DNS相关错误码定义
This commit is contained in:
@@ -61,7 +61,19 @@ public enum ErrorCode {
|
||||
DEVICE_DELETE_FAILED(500, "DEVICE-303", "设备删除失败"),
|
||||
DEVICE_TYPE_DELETE_FAILED(500, "DEVICE-304", "设备类型删除失败"),
|
||||
DEVICE_TYPE_NOT_EMPTY(400, "DEVICE-305", "设备类型不为空"),
|
||||
DEVICE_DISABLED(403, "DEVICE-306", "设备已禁用");
|
||||
DEVICE_DISABLED(403, "DEVICE-306", "设备已禁用"),
|
||||
|
||||
// DNS
|
||||
DNS_NOT_DEFINED( 400, "DNS-401" , "未定义的DNS服务器" ),
|
||||
DNS_NOT_FOUND(404, "DNS-402", "DNS服务器不存在"),
|
||||
DNS_DELETE_FAILED(500, "DNS-403", "DNS服务器删除失败"),
|
||||
DNS_NOT_EMPTY(400, "DNS-404", "DNS服务器不为空"),
|
||||
DNS_DISABLED(403, "DNS-405", "DNS服务器已禁用"),
|
||||
DNS_NOT_EXISTS(400, "DNS-406", "DNS服务器不存在"),
|
||||
DNS_NOT_EXISTS_OR_PASSWORD_WRONG(401, "DNS-407", "DNS服务器不存在或密码错误"),
|
||||
DNS_UPDATE_FAILED(400, "DNS-408", "DNS服务器更新失败"),
|
||||
|
||||
;
|
||||
|
||||
private final int httpStatus;
|
||||
private final String code; // 业务错误码(领域-编号)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.nopj.chaos_api.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DnsServerService {
|
||||
|
||||
/**
|
||||
* 根据域名获取DNS服务器ID
|
||||
* @param dnsAddress 域名
|
||||
* @return DNS服务器ID
|
||||
*/
|
||||
Long getDnsServerIdByName(String dnsAddress);
|
||||
|
||||
/**
|
||||
* 根据域名列表获取DNS服务器ID列表
|
||||
* @param nameList 域名列表
|
||||
* @return DNS服务器ID列表
|
||||
*/
|
||||
List<Long> getDnsServerIdListByNameList(List<String> nameList);
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import cn.nopj.chaos_api.mapper.DeviceMapper;
|
||||
import cn.nopj.chaos_api.mapper.InterfaceAddressConfigMapper;
|
||||
import cn.nopj.chaos_api.mapper.NetworkInterfaceMapper;
|
||||
import cn.nopj.chaos_api.service.DeviceService;
|
||||
import cn.nopj.chaos_api.service.DnsServerService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
@@ -33,6 +34,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
NetworkInterfaceMapper networkInterfaceMapper;
|
||||
@Autowired
|
||||
InterfaceAddressConfigMapper interfaceAddressConfigMapper;
|
||||
@Autowired
|
||||
DnsServerService dnsServerService;
|
||||
@Override
|
||||
@Transactional
|
||||
public DeviceResponse createDevice(CreateDeviceRequest createDeviceRequest) {
|
||||
@@ -71,6 +74,10 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
||||
iac.setIsDhcp(a.getIsDhcp());
|
||||
iac.setMtu(a.getMtu());
|
||||
interfaceAddressConfigMapper.insert(iac);
|
||||
|
||||
List<String> dnsServers = a.getDnsServers();
|
||||
List<Long> dnsServerIdListByNameList = dnsServerService.getDnsServerIdListByNameList(dnsServers);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.nopj.chaos_api.service.impl;
|
||||
|
||||
import cn.nopj.chaos_api.common.constants.ErrorCode;
|
||||
import cn.nopj.chaos_api.common.exceotion.BizException;
|
||||
import cn.nopj.chaos_api.domain.entity.DnsServer;
|
||||
import cn.nopj.chaos_api.mapper.DnsServerMapper;
|
||||
import cn.nopj.chaos_api.service.DnsServerService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class DnsServerServiceImpl extends ServiceImpl<DnsServerMapper, DnsServer> implements DnsServerService {
|
||||
|
||||
@Override
|
||||
public Long getDnsServerIdByName(String dnsAddress) {
|
||||
|
||||
if (dnsAddress == null || dnsAddress.isEmpty()){
|
||||
throw new BizException(ErrorCode.DNS_NOT_FOUND);
|
||||
}
|
||||
DnsServer dnsServer = this.baseMapper.selectOne(new LambdaQueryWrapper<DnsServer>().eq(DnsServer::getName, dnsAddress));
|
||||
|
||||
if (dnsServer == null ){
|
||||
throw new BizException(ErrorCode.DNS_NOT_DEFINED);
|
||||
}
|
||||
|
||||
return dnsServer.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> getDnsServerIdListByNameList(List<String> nameList) {
|
||||
return nameList.stream().map(this::getDnsServerIdByName).toList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user