2021-09-20 13:00:31 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-05-21 21:04:21 +00:00
|
|
|
|
2021-12-30 19:42:56 +00:00
|
|
|
use crate::config::RuntimeConfigConsul;
|
|
|
|
|
2020-05-21 21:04:21 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct ServiceEntry {
|
2023-04-04 16:46:14 +00:00
|
|
|
#[serde(rename = "Tags")]
|
|
|
|
pub tags: Vec<String>,
|
2020-05-21 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 16:46:14 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Default)]
|
2020-05-21 21:04:21 +00:00
|
|
|
pub struct CatalogNode {
|
2023-04-04 16:46:14 +00:00
|
|
|
#[serde(rename = "Services")]
|
|
|
|
pub services: HashMap<String, ServiceEntry>,
|
2020-05-21 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Consul {
|
|
|
|
client: reqwest::Client,
|
2020-05-22 08:33:09 +00:00
|
|
|
url: String,
|
2021-09-11 14:34:03 +00:00
|
|
|
idx: Option<u64>,
|
2020-05-21 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Consul {
|
2021-12-30 19:42:56 +00:00
|
|
|
pub fn new(config: &RuntimeConfigConsul) -> Self {
|
2022-08-24 16:22:00 +00:00
|
|
|
let client = if let Some((ca, skip_verify, ident)) = config.tls.clone() {
|
|
|
|
if skip_verify {
|
|
|
|
reqwest::Client::builder()
|
|
|
|
.use_rustls_tls()
|
|
|
|
.danger_accept_invalid_certs(true)
|
|
|
|
.identity(ident)
|
|
|
|
.build()
|
|
|
|
.expect("Unable to build reqwest client")
|
|
|
|
} else if let Some(ca) = ca {
|
|
|
|
reqwest::Client::builder()
|
|
|
|
.use_rustls_tls()
|
|
|
|
.add_root_certificate(ca)
|
|
|
|
.identity(ident)
|
|
|
|
.build()
|
|
|
|
.expect("Unable to build reqwest client")
|
|
|
|
} else {
|
|
|
|
reqwest::Client::builder()
|
|
|
|
.use_rustls_tls()
|
|
|
|
.identity(ident)
|
|
|
|
.build()
|
|
|
|
.expect("Unable to build reqwest client")
|
|
|
|
}
|
2021-12-30 19:42:56 +00:00
|
|
|
} else {
|
|
|
|
reqwest::Client::new()
|
|
|
|
};
|
2020-05-21 21:04:21 +00:00
|
|
|
return Self {
|
2021-12-30 19:42:56 +00:00
|
|
|
client,
|
|
|
|
url: config.url.clone(),
|
2021-09-11 14:34:03 +00:00
|
|
|
idx: None,
|
2021-12-25 18:19:19 +00:00
|
|
|
};
|
2020-05-21 21:04:21 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 18:19:19 +00:00
|
|
|
pub fn watch_node_reset(&mut self) -> () {
|
|
|
|
self.idx = None;
|
|
|
|
}
|
2020-05-22 13:19:49 +00:00
|
|
|
|
2020-05-22 08:33:09 +00:00
|
|
|
pub async fn watch_node(&mut self, host: &str) -> Result<CatalogNode> {
|
|
|
|
let url = match self.idx {
|
|
|
|
Some(i) => format!("{}/v1/catalog/node/{}?index={}", self.url, host, i),
|
2021-09-11 14:34:03 +00:00
|
|
|
None => format!("{}/v1/catalog/node/{}", self.url, host),
|
2020-05-22 08:33:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let http = self.client.get(&url).send().await?;
|
|
|
|
self.idx = match http.headers().get("X-Consul-Index") {
|
|
|
|
Some(v) => Some(v.to_str()?.parse::<u64>()?),
|
2021-09-11 14:34:03 +00:00
|
|
|
None => return Err(anyhow!("X-Consul-Index header not found")),
|
2020-05-22 08:33:09 +00:00
|
|
|
};
|
|
|
|
|
2023-04-04 16:46:14 +00:00
|
|
|
let resp: Option<CatalogNode> = http.json().await?;
|
|
|
|
return Ok(resp.unwrap_or_default());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn kv_put(&self, key: &str, bytes: Vec<u8>) -> Result<()> {
|
|
|
|
let url = format!("{}/v1/kv/{}", self.url, key);
|
|
|
|
let http = self.client.put(&url).body(bytes).send().await?;
|
|
|
|
http.error_for_status()?;
|
|
|
|
Ok(())
|
2020-05-21 21:04:21 +00:00
|
|
|
}
|
|
|
|
}
|