hacky hacks

This commit is contained in:
Jan Christian Grünhage 2023-03-28 10:23:04 +02:00
parent a48db9c4c8
commit 54f109f48a
2 changed files with 21 additions and 8 deletions

View file

@ -14,3 +14,4 @@ actix-web-httpauth = "0.8.0"
actix-web = "4.2.1" actix-web = "4.2.1"
tokio = "1.21.2" tokio = "1.21.2"
rsvici = "0.1" rsvici = "0.1"
anyhow = "1.0.70"

View file

@ -3,9 +3,11 @@
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use prometheus_client::{ use futures_util:: stream::StreamExt;
encoding::text::Encode,
}; use prometheus_client::encoding::text::Encode;
use anyhow::Result;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct VICIState { pub struct VICIState {
@ -19,21 +21,29 @@ pub struct VICIState {
pub pools: Pools, pub pools: Pools,
} }
impl<E> VICIState { impl VICIState {
async fn update(client: rsvici::Client) -> Result<VICIState, E> { async fn update(client: &mut rsvici::Client) -> Result<VICIState> {
VICIState { Ok(VICIState {
version: client.request("version", ()).await?, version: client.request("version", ()).await?,
statistics: client.request("statistics", ()).await?, statistics: client.request("statistics", ()).await?,
policies: client.stream_request::<(), Policies>("list-policies", "list-policy", ()).await?, policies: collected_stream::<NamedPolicy, Policies>(client, "list-policies", "list-policy").await,
connections: client.stream_request::<(), Connections>("list-connections", "list-conn", ()).await?, connections: client.stream_request::<(), Connections>("list-connections", "list-conn", ()).await?,
security_associations: client.stream_request::<(), SecurityAssociations>("list-sas", "list-sa", ()).await?, security_associations: client.stream_request::<(), SecurityAssociations>("list-sas", "list-sa", ()).await?,
certificates: client.stream_request::<(), Certificates>("list-certs", "list-cert", ()).await?, certificates: client.stream_request::<(), Certificates>("list-certs", "list-cert", ()).await?,
authorities: client.stream_request::<(), Authorities>("list-authoroties", "list-authoroty", ()).await?, authorities: client.stream_request::<(), Authorities>("list-authoroties", "list-authoroty", ()).await?,
pools: client.stream_request::<(), Pools>("list-pools", "list-pool", ()).await?, pools: client.stream_request::<(), Pools>("list-pools", "list-pool", ()).await?,
} })
} }
} }
async fn collected_stream<N,C>(client: &mut rsvici::Client, command: &str, event: &str) -> C
where
N: for<'de> serde::Deserialize<'de>,
C: std::iter::Extend<N> + Default,
{
client.stream_request::<(), N>(command, event, ()).filter_map(|event| async move {event.ok()}).collect::<C>().await
}
// Structs for parsing the control interface // Structs for parsing the control interface
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -99,6 +109,8 @@ pub struct StatisticsMallinfo {
pub type Policies = HashMap<String, Policy>; pub type Policies = HashMap<String, Policy>;
pub type NamedPolicy = (String, Policy);
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Policy { pub struct Policy {
pub child: String, pub child: String,