fix:网络配置

This commit is contained in:
xydkj 2026-04-28 09:24:00 +08:00
parent 392428d2f0
commit a4da5e65b5
7 changed files with 81 additions and 21 deletions

View File

@ -76,6 +76,12 @@ public class NetworkConfigController {
} }
@Operation(summary = "获取所有Bond名称", description = "获取系统中所有Bond连接名称")
@GetMapping("/bond/names")
public ApiResponse<List<String>> getBondNames() {
return ApiResponse.success(networkConfigService.getBondNames());
}
@Operation(summary = "创建Bond", description = "创建一个新的Bond聚合接口需指定名称、模式和从属接口") @Operation(summary = "创建Bond", description = "创建一个新的Bond聚合接口需指定名称、模式和从属接口")
@PostMapping("/bond/create") @PostMapping("/bond/create")
@AuditedOperation(module = ModuleCode.NETWORK, action = ActionType.CREATE, summary = "创建Bond") @AuditedOperation(module = ModuleCode.NETWORK, action = ActionType.CREATE, summary = "创建Bond")

View File

@ -27,7 +27,7 @@ public class BondConfigRequest {
* 4: 802.3ad * 4: 802.3ad
*/ */
@Schema( @Schema(
description = "Bond 模式0=balance-rr平衡轮询1=active-backup主备2=balance-xor平衡异或4=802.3ad动态链路聚合", description = "Bond 模式0=balance-rr1=active-backup2=balance-xor4=802.3ad",
example = "1", example = "1",
requiredMode = Schema.RequiredMode.REQUIRED requiredMode = Schema.RequiredMode.REQUIRED
) )

View File

@ -31,7 +31,7 @@ public class BondCreateRequest {
* 4: 802.3ad * 4: 802.3ad
*/ */
@Schema( @Schema(
description = "Bond模式0=balance-rr平衡轮询1=active-backup主备2=balance-xor平衡异或4=802.3ad动态链路聚合", description = "Bond模式0=balance-rr1=active-backup2=balance-xor4=802.3ad",
example = "1", example = "1",
allowableValues = {"0", "1", "2", "4"}, allowableValues = {"0", "1", "2", "4"},
requiredMode = Schema.RequiredMode.REQUIRED requiredMode = Schema.RequiredMode.REQUIRED

View File

@ -18,7 +18,7 @@ public class BondTableResponse {
*/ */
@Schema( @Schema(
description = "Bond模式", description = "Bond模式",
example = "bond1-主备" example = "balance-rr"
) )
private String bondMode; private String bondMode;
@ -28,7 +28,7 @@ public class BondTableResponse {
@Schema( @Schema(
description = "地址类型IPV4 或 IPV6", description = "地址类型IPV4 或 IPV6",
example = "IPV4", example = "IPV4",
allowableValues = {"IPV4", "IPV6", "-"} allowableValues = {"IPV4", "IPV6"}
) )
private String addressType; private String addressType;
@ -36,7 +36,7 @@ public class BondTableResponse {
* 地址 * 地址
*/ */
@Schema(description = "IP地址", example = "192.168.10.100") @Schema(description = "IP地址", example = "192.168.10.100")
private String address; private String ipAddress;
/** /**
* 前缀长度 * 前缀长度

View File

@ -17,9 +17,9 @@ public class NetworkTableResponse {
* 地址类型 IPV4IPV6- * 地址类型 IPV4IPV6-
*/ */
@Schema( @Schema(
description = "地址类型IPV4、IPV6 或 -", description = "地址类型IPV4、IPV6",
example = "IPV4", example = "IPV4",
allowableValues = {"IPV4", "IPV6", "-"} allowableValues = {"IPV4", "IPV6"}
) )
private String addressType; private String addressType;
@ -27,7 +27,7 @@ public class NetworkTableResponse {
* 地址 172.16.18.1 * 地址 172.16.18.1
*/ */
@Schema(description = "IP地址", example = "172.16.18.1") @Schema(description = "IP地址", example = "172.16.18.1")
private String address; private String ipAddress;
/** /**
* 前缀长度 2464 * 前缀长度 2464
@ -46,8 +46,7 @@ public class NetworkTableResponse {
*/ */
@Schema( @Schema(
description = "网卡状态Active=激活Inactive=未激活", description = "网卡状态Active=激活Inactive=未激活",
example = "Active", example = "Active"
allowableValues = {"Active", "Inactive"}
) )
private String status; private String status;
} }

View File

@ -45,16 +45,14 @@ public class RouteTableResponse {
* 状态Active / Inactive * 状态Active / Inactive
*/ */
@Schema( @Schema(
description = "网口状态Active=激活Inactive=未激活", description = "网卡状态Active=激活Inactive=未激活",
example = "Active", example = "Active"
allowableValues = {"Active", "Inactive"}
) )
private String status; private String status;
@Schema( @Schema(
description = "网口状态Active=激活Inactive=未激活", description = "地址类型IPv4 或 IPv6",
example = "Active", example = "IPv4"
allowableValues = {"Active", "Inactive"}
) )
private String addressType; private String addressType;

View File

@ -160,6 +160,63 @@ public class NetworkConfigService {
} }
public List<String> getBondNames() {
List<String> result = new ArrayList<>();
List<String> lines = executeCommand(
"nmcli",
"-t",
"-f",
"NAME,TYPE",
"con",
"show"
);
for (String line : lines) {
if (line == null || line.trim().isEmpty()) {
continue;
}
String[] parts = line.split("(?<!\\\\):", -1);
if (parts.length < 2) {
continue;
}
String name = parts[0].replace("\\:", ":");
String type = parts[1];
if (!"bond".equals(type)) {
continue;
}
result.add(name);
}
result.sort(Comparator.comparingInt(this::getBondIndex));
return result;
}
private int getBondIndex(String bondName) {
try {
if (bondName == null) {
return Integer.MAX_VALUE;
}
String name = bondName.trim();
if (!name.matches("^bond\\d+$")) {
return Integer.MAX_VALUE;
}
return Integer.parseInt(name.substring(4));
} catch (Exception e) {
return Integer.MAX_VALUE;
}
}
public PageResult<NetworkTableResponse> getNetworkTablePage(NetworkTableQueryRequest req) { public PageResult<NetworkTableResponse> getNetworkTablePage(NetworkTableQueryRequest req) {
List<NetworkTableResponse> resultList = new ArrayList<>(); List<NetworkTableResponse> resultList = new ArrayList<>();
String targetDeviceName = trim(req.getDeviceName()); String targetDeviceName = trim(req.getDeviceName());
@ -317,7 +374,7 @@ public class NetworkConfigService {
NetworkTableResponse emptyItem = new NetworkTableResponse(); NetworkTableResponse emptyItem = new NetworkTableResponse();
emptyItem.setDeviceName(device); emptyItem.setDeviceName(device);
// emptyItem.setAddressType("-"); // emptyItem.setAddressType("-");
// emptyItem.setAddress("-"); // emptyItem.setIpAddress("-");
// emptyItem.setPrefixLength("-"); // emptyItem.setPrefixLength("-");
// emptyItem.setGateway("-"); // emptyItem.setGateway("-");
emptyItem.setStatus(mappedStatus); emptyItem.setStatus(mappedStatus);
@ -554,10 +611,10 @@ public class NetworkConfigService {
if (ipWithPrefix.contains("/")) { if (ipWithPrefix.contains("/")) {
String[] parts = ipWithPrefix.split("/"); String[] parts = ipWithPrefix.split("/");
item.setAddress(parts[0]); item.setIpAddress(parts[0]);
item.setPrefixLength(parts[1]); item.setPrefixLength(parts[1]);
} else { } else {
item.setAddress(ipWithPrefix); item.setIpAddress(ipWithPrefix);
item.setPrefixLength(""); item.setPrefixLength("");
} }
return item; return item;
@ -1786,10 +1843,10 @@ public class NetworkConfigService {
if (normalizedIp.contains("/")) { if (normalizedIp.contains("/")) {
String[] parts = normalizedIp.split("/", 2); String[] parts = normalizedIp.split("/", 2);
item.setAddress(parts[0]); item.setIpAddress(parts[0]);
item.setPrefixLength(parts[1]); item.setPrefixLength(parts[1]);
} else { } else {
item.setAddress(normalizedIp); item.setIpAddress(normalizedIp);
item.setPrefixLength(""); item.setPrefixLength("");
} }