Can define bridge name

This commit is contained in:
Pierre HUBERT 2023-12-19 18:11:53 +01:00
parent afebe97395
commit e969455c58
5 changed files with 46 additions and 4 deletions

View File

@ -71,7 +71,15 @@ pub async fn update(
path: web::Path<NetworkID>,
body: web::Json<NetworkInfo>,
) -> HttpResult {
let mut network = body.0.to_virt_network()?;
let mut network = match body.0.to_virt_network() {
Ok(n) => n,
Err(e) => {
log::error!("Failed to extract network info! {e}");
return Ok(
HttpResponse::BadRequest().json(format!("Failed to extract network info!\n${e}"))
);
}
};
network.uuid = Some(path.uid);
if let Err(e) = client.update_network(network).await {

View File

@ -378,6 +378,14 @@ pub struct NetworkForwardXML {
pub dev: String,
}
/// Network bridge information
#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[serde(rename = "bridge")]
pub struct NetworkBridgeXML {
#[serde(rename(serialize = "@name"))]
pub name: String,
}
/// Network DNS information
#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[serde(rename = "dns")]
@ -500,6 +508,8 @@ pub struct NetworkXML {
#[serde(skip_serializing_if = "Option::is_none")]
pub forward: Option<NetworkForwardXML>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bridge: Option<NetworkBridgeXML>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dns: Option<NetworkDNSXML>,
#[serde(skip_serializing_if = "Option::is_none")]
pub domain: Option<NetworkDomainXML>,

View File

@ -4,9 +4,10 @@ use crate::libvirt_lib_structures::{
DevicesXML, DiskBootXML, DiskDriverXML, DiskReadOnlyXML, DiskSourceXML, DiskTargetXML, DiskXML,
DomainCPUTopology, DomainCPUXML, DomainInputXML, DomainMemoryXML, DomainNetInterfaceXML,
DomainVCPUXML, DomainXML, FeaturesXML, GraphicsXML, NetIntSourceXML, NetMacAddress,
NetworkDHCPHostXML, NetworkDHCPRangeXML, NetworkDHCPXML, NetworkDNSForwarderXML, NetworkDNSXML,
NetworkDomainXML, NetworkForwardXML, NetworkIPXML, NetworkXML, OSLoaderXML, OSTypeXML,
TPMBackendXML, TPMDeviceXML, VideoModelXML, VideoXML, XMLUuid, ACPIXML, OSXML,
NetworkBridgeXML, NetworkDHCPHostXML, NetworkDHCPRangeXML, NetworkDHCPXML,
NetworkDNSForwarderXML, NetworkDNSXML, NetworkDomainXML, NetworkForwardXML, NetworkIPXML,
NetworkXML, OSLoaderXML, OSTypeXML, TPMBackendXML, TPMDeviceXML, VideoModelXML, VideoXML,
XMLUuid, ACPIXML, OSXML,
};
use crate::libvirt_rest_structures::LibVirtStructError::StructureExtraction;
use crate::utils::disks_utils::Disk;
@ -492,6 +493,7 @@ pub struct NetworkInfo {
description: Option<String>,
forward_mode: NetworkForwardMode,
device: Option<String>,
bridge_name: Option<String>,
dns_server: Option<Ipv4Addr>,
domain: Option<String>,
ip_v4: Option<IPV4Config>,
@ -516,6 +518,12 @@ impl NetworkInfo {
}
}
if let Some(bridge) = &self.bridge_name {
if !regex!("^[a-zA-Z0-9]+$").is_match(bridge) {
return Err(StructureExtraction("Network bridge name is invalid!").into());
}
}
if let Some(domain) = &self.domain {
if !regex!("^[a-zA-Z0-9.]+$").is_match(domain) {
return Err(StructureExtraction("Domain name is invalid!").into());
@ -594,6 +602,9 @@ impl NetworkInfo {
}),
NetworkForwardMode::Isolated => None,
},
bridge: self.bridge_name.map(|b| NetworkBridgeXML {
name: b.to_string(),
}),
dns: self.dns_server.map(|addr| NetworkDNSXML {
forwarder: NetworkDNSForwarderXML { addr },
}),
@ -619,6 +630,7 @@ impl NetworkInfo {
false => Some(f.dev),
})
.unwrap_or(None),
bridge_name: xml.bridge.map(|b| b.name),
dns_server: xml.dns.map(|d| d.forwarder.addr),
domain: xml.domain.map(|d| d.name),
ip_v4: xml

View File

@ -26,6 +26,7 @@ export interface NetworkInfo {
description?: string;
forward_mode: "NAT" | "Isolated";
device?: string;
bridge_name?: string;
dns_server?: string;
domain?: string;
ip_v4?: IpConfig;

View File

@ -128,6 +128,17 @@ function NetworkDetailsInner(
/>
)}
<TextInput
editable={p.editable}
label="Network bridge name"
onValueChange={(v) => {
p.net.bridge_name = v;
p.onChange?.();
}}
value={p.net.bridge_name}
checkValue={(v) => v === "" || /^[a-zA-Z0-9]+$/.test(v)}
/>
<IPInput
editable={p.editable}
label="DNS server to use"