Can get XML network definition

This commit is contained in:
Pierre HUBERT 2023-12-11 18:41:59 +01:00
parent 2d5dc9237e
commit 375bdb1a46
11 changed files with 135 additions and 18 deletions

View File

@ -452,6 +452,20 @@ impl Handler<GetNetworkXMLReq> for LibVirtActor {
} }
} }
#[derive(Message)]
#[rtype(result = "anyhow::Result<String>")]
pub struct GetSourceNetworkXMLReq(pub XMLUuid);
impl Handler<GetSourceNetworkXMLReq> for LibVirtActor {
type Result = anyhow::Result<String>;
fn handle(&mut self, msg: GetSourceNetworkXMLReq, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Get network XML:\n{}", msg.0.as_string());
let network = Network::lookup_by_uuid_string(&self.m, &msg.0.as_string())?;
Ok(network.get_xml_desc(0)?)
}
}
#[derive(Message)] #[derive(Message)]
#[rtype(result = "anyhow::Result<()>")] #[rtype(result = "anyhow::Result<()>")]
pub struct DeleteNetwork(pub XMLUuid); pub struct DeleteNetwork(pub XMLUuid);

View File

@ -42,6 +42,12 @@ pub async fn get_single(client: LibVirtReq, req: web::Path<NetworkID>) -> HttpRe
Ok(HttpResponse::Ok().json(network)) Ok(HttpResponse::Ok().json(network))
} }
/// Get the XML source description of a single network
pub async fn single_src(client: LibVirtReq, req: web::Path<NetworkID>) -> HttpResult {
let xml = client.get_single_network_xml(req.uid).await?;
Ok(HttpResponse::Ok().content_type("application/xml").body(xml))
}
/// Update the information about a single network /// Update the information about a single network
pub async fn update( pub async fn update(
client: LibVirtReq, client: LibVirtReq,

View File

@ -119,6 +119,13 @@ impl LibVirtClient {
self.0.send(libvirt_actor::GetNetworkXMLReq(id)).await? self.0.send(libvirt_actor::GetNetworkXMLReq(id)).await?
} }
/// Get the source XML configuration of a single network
pub async fn get_single_network_xml(&self, id: XMLUuid) -> anyhow::Result<String> {
self.0
.send(libvirt_actor::GetSourceNetworkXMLReq(id))
.await?
}
/// Delete a network /// Delete a network
pub async fn delete_network(&self, id: XMLUuid) -> anyhow::Result<()> { pub async fn delete_network(&self, id: XMLUuid) -> anyhow::Result<()> {
self.0.send(libvirt_actor::DeleteNetwork(id)).await? self.0.send(libvirt_actor::DeleteNetwork(id)).await?

View File

@ -199,6 +199,10 @@ async fn main() -> std::io::Result<()> {
"/api/network/{uid}", "/api/network/{uid}",
web::get().to(network_controller::get_single), web::get().to(network_controller::get_single),
) )
.route(
"/api/network/{uid}/src",
web::get().to(network_controller::single_src),
)
.route( .route(
"/api/network/{uid}", "/api/network/{uid}",
web::put().to(network_controller::update), web::put().to(network_controller::update),

View File

@ -26,6 +26,7 @@ import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage";
import { BaseLoginPage } from "./widgets/BaseLoginPage"; import { BaseLoginPage } from "./widgets/BaseLoginPage";
import { ViewNetworkRoute } from "./routes/ViewNetworkRoute"; import { ViewNetworkRoute } from "./routes/ViewNetworkRoute";
import { VMXMLRoute } from "./routes/VMXMLRoute"; import { VMXMLRoute } from "./routes/VMXMLRoute";
import { NetXMLRoute } from "./routes/NetXMLRoute";
interface AuthContext { interface AuthContext {
signedIn: boolean; signedIn: boolean;
@ -59,6 +60,7 @@ export function App() {
<Route path="net/new" element={<CreateNetworkRoute />} /> <Route path="net/new" element={<CreateNetworkRoute />} />
<Route path="net/:uuid" element={<ViewNetworkRoute />} /> <Route path="net/:uuid" element={<ViewNetworkRoute />} />
<Route path="net/:uuid/edit" element={<EditNetworkRoute />} /> <Route path="net/:uuid/edit" element={<EditNetworkRoute />} />
<Route path="net/:uuid/xml" element={<NetXMLRoute />} />
<Route path="sysinfo" element={<SysInfoRoute />} /> <Route path="sysinfo" element={<SysInfoRoute />} />
<Route path="*" element={<NotFoundRoute />} /> <Route path="*" element={<NotFoundRoute />} />

View File

@ -25,6 +25,10 @@ export function NetworkURL(n: NetworkInfo, edit: boolean = false): string {
return `/net/${n.uuid}${edit ? "/edit" : ""}`; return `/net/${n.uuid}${edit ? "/edit" : ""}`;
} }
export function NetworkXMLURL(n: NetworkInfo): string {
return `/net/${n.uuid}/xml`;
}
export class NetworkApi { export class NetworkApi {
/** /**
* Create a new network * Create a new network
@ -63,6 +67,18 @@ export class NetworkApi {
).data; ).data;
} }
/**
* Get the source XML configuration of a network for debugging purposes
*/
static async GetSingleXML(uuid: string): Promise<string> {
return (
await APIClient.exec({
uri: `/network/${uuid}/src`,
method: "GET",
})
).data;
}
/** /**
* Get the status of network * Get the status of network
*/ */

View File

@ -0,0 +1,50 @@
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { IconButton } from "@mui/material";
import React from "react";
import { useParams } from "react-router-dom";
import { NetworkApi, NetworkInfo, NetworkURL } from "../api/NetworksApi";
import { AsyncWidget } from "../widgets/AsyncWidget";
import { RouterLink } from "../widgets/RouterLink";
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
import { XMLWidget } from "../widgets/XMLWidget";
export function NetXMLRoute(): React.ReactElement {
const { uuid } = useParams();
const [net, setNet] = React.useState<NetworkInfo | undefined>();
const [src, setSrc] = React.useState<string | undefined>();
const load = async () => {
setNet(await NetworkApi.GetSingle(uuid!));
setSrc(await NetworkApi.GetSingleXML(uuid!));
};
return (
<AsyncWidget
loadKey={uuid}
load={load}
errMsg="Failed to load network information!"
build={() => <XMLRouteInner net={net!} src={src!} />}
/>
);
}
function XMLRouteInner(p: {
net: NetworkInfo;
src: string;
}): React.ReactElement {
return (
<VirtWebRouteContainer
label={`XML definition of ${p.net.name}`}
actions={
<RouterLink to={NetworkURL(p.net)}>
<IconButton>
<ArrowBackIcon />
</IconButton>
</RouterLink>
}
>
<XMLWidget src={p.src} />
</VirtWebRouteContainer>
);
}

View File

@ -43,7 +43,7 @@ function VMRouteBody(p: { vm: VMInfo }): React.ReactElement {
<RouterLink to={p.vm.XMLURL}> <RouterLink to={p.vm.XMLURL}>
<IconButton size="small"> <IconButton size="small">
<Icon path={mdiXml} style={{ width: "1rem" }} /> <Icon path={mdiXml} style={{ width: "1em" }} />
</IconButton> </IconButton>
</RouterLink> </RouterLink>

View File

@ -1,17 +1,12 @@
import React from "react"; import React from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import xml from "react-syntax-highlighter/dist/esm/languages/hljs/xml";
import { dracula } from "react-syntax-highlighter/dist/esm/styles/hljs";
import xmlFormat from "xml-formatter";
import { VMApi, VMInfo } from "../api/VMApi"; import { VMApi, VMInfo } from "../api/VMApi";
import { AsyncWidget } from "../widgets/AsyncWidget"; import { AsyncWidget } from "../widgets/AsyncWidget";
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer"; import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
import { IconButton } from "@mui/material"; import { IconButton } from "@mui/material";
import { RouterLink } from "../widgets/RouterLink"; import { RouterLink } from "../widgets/RouterLink";
import ArrowBackIcon from "@mui/icons-material/ArrowBack"; import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { XMLWidget } from "../widgets/XMLWidget";
SyntaxHighlighter.registerLanguage("xml", xml);
export function VMXMLRoute(): React.ReactElement { export function VMXMLRoute(): React.ReactElement {
const { uuid } = useParams(); const { uuid } = useParams();
@ -35,8 +30,6 @@ export function VMXMLRoute(): React.ReactElement {
} }
function XMLRouteInner(p: { vm: VMInfo; src: string }): React.ReactElement { function XMLRouteInner(p: { vm: VMInfo; src: string }): React.ReactElement {
const xml = xmlFormat(p.src);
return ( return (
<VirtWebRouteContainer <VirtWebRouteContainer
label={`XML definition of ${p.vm.name}`} label={`XML definition of ${p.vm.name}`}
@ -48,13 +41,7 @@ function XMLRouteInner(p: { vm: VMInfo; src: string }): React.ReactElement {
</RouterLink> </RouterLink>
} }
> >
<SyntaxHighlighter <XMLWidget src={p.src} />
language="xml"
style={dracula}
customStyle={{ fontSize: "120%" }}
>
{xml}
</SyntaxHighlighter>
</VirtWebRouteContainer> </VirtWebRouteContainer>
); );
} }

View File

@ -1,14 +1,18 @@
import { mdiXml } from "@mdi/js";
import Icon from "@mdi/react";
import { Button, IconButton } from "@mui/material";
import React from "react"; import React from "react";
import { useNavigate, useParams } from "react-router-dom";
import { import {
NetworkApi, NetworkApi,
NetworkInfo, NetworkInfo,
NetworkStatus, NetworkStatus,
NetworkURL, NetworkURL,
NetworkXMLURL,
} from "../api/NetworksApi"; } from "../api/NetworksApi";
import { AsyncWidget } from "../widgets/AsyncWidget"; import { AsyncWidget } from "../widgets/AsyncWidget";
import { useNavigate, useParams } from "react-router-dom"; import { RouterLink } from "../widgets/RouterLink";
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer"; import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
import { Button } from "@mui/material";
import { NetworkDetails } from "../widgets/net/NetworkDetails"; import { NetworkDetails } from "../widgets/net/NetworkDetails";
import { NetworkStatusWidget } from "../widgets/net/NetworkStatusWidget"; import { NetworkStatusWidget } from "../widgets/net/NetworkStatusWidget";
@ -46,6 +50,12 @@ function ViewNetworkRouteInner(p: {
<span> <span>
<NetworkStatusWidget net={p.network} onChange={setNetStatus} /> <NetworkStatusWidget net={p.network} onChange={setNetStatus} />
<RouterLink to={NetworkXMLURL(p.network)}>
<IconButton size="small">
<Icon path={mdiXml} style={{ width: "1em" }} />
</IconButton>
</RouterLink>
{netStatus === "Stopped" && ( {netStatus === "Stopped" && (
<Button <Button
variant="contained" variant="contained"

View File

@ -0,0 +1,21 @@
import xml from "react-syntax-highlighter/dist/esm/languages/hljs/xml";
import { dracula } from "react-syntax-highlighter/dist/esm/styles/hljs";
import xmlFormat from "xml-formatter";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
SyntaxHighlighter.registerLanguage("xml", xml);
export function XMLWidget(p: { src: string }): React.ReactElement {
const xml = xmlFormat(p.src);
return (
<SyntaxHighlighter
language="xml"
style={dracula}
customStyle={{ fontSize: "120%" }}
>
{xml}
</SyntaxHighlighter>
);
}