tricot/src/consul.rs

122 lines
3.1 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use anyhow::Result;
2021-12-06 22:40:41 +00:00
use bytes::Bytes;
2021-12-07 12:50:44 +00:00
use log::*;
2021-12-06 22:40:41 +00:00
use reqwest::StatusCode;
2021-12-07 12:50:44 +00:00
use serde::{Deserialize, Serialize};
// ---- Watch and retrieve Consul catalog ----
#[derive(Serialize, Deserialize, Debug)]
pub struct ConsulServiceEntry {
#[serde(rename = "Address")]
pub address: String,
#[serde(rename = "Port")]
pub port: u16,
#[serde(rename = "Tags")]
pub tags: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ConsulNodeCatalog {
#[serde(rename = "Services")]
pub services: HashMap<String, ConsulServiceEntry>,
}
#[derive(Clone)]
pub struct Consul {
client: reqwest::Client,
url: String,
2021-12-06 22:40:41 +00:00
kv_prefix: String,
idx: Option<u64>,
}
impl Consul {
2021-12-06 22:40:41 +00:00
pub fn new(url: &str, kv_prefix: &str) -> Self {
return Self {
client: reqwest::Client::new(),
url: url.to_string(),
2021-12-06 22:40:41 +00:00
kv_prefix: kv_prefix.to_string(),
idx: None,
};
}
pub fn watch_node_reset(&mut self) -> () {
self.idx = None;
}
pub async fn watch_node(&mut self, host: &str) -> Result<ConsulNodeCatalog> {
let url = match self.idx {
Some(i) => format!("{}/v1/catalog/node/{}?index={}", self.url, host, i),
None => format!("{}/v1/catalog/node/{}", self.url, host),
};
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>()?),
None => return Err(anyhow!("X-Consul-Index header not found")),
};
let resp: ConsulNodeCatalog = http.json().await?;
return Ok(resp);
}
2021-12-06 22:40:41 +00:00
pub async fn kv_get(&self, key: &str) -> Result<Option<Bytes>> {
2021-12-07 12:50:44 +00:00
debug!("kv_get {}", key);
2021-12-06 22:40:41 +00:00
let url = format!("{}/v1/kv/{}{}?raw", self.url, self.kv_prefix, key);
let http = self.client.get(&url).send().await?;
match http.status() {
StatusCode::OK => Ok(Some(http.bytes().await?)),
StatusCode::NOT_FOUND => Ok(None),
2021-12-07 12:50:44 +00:00
_ => Err(anyhow!(
"Consul request failed: {:?}",
http.error_for_status()
)),
}
}
pub async fn kv_get_json<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Result<Option<T>> {
debug!("kv_get_json {}", key);
let url = format!("{}/v1/kv/{}{}?raw", self.url, self.kv_prefix, key);
let http = self.client.get(&url).send().await?;
match http.status() {
StatusCode::OK => Ok(Some(http.json().await?)),
StatusCode::NOT_FOUND => Ok(None),
_ => Err(anyhow!(
"Consul request failed: {:?}",
http.error_for_status()
)),
2021-12-06 22:40:41 +00:00
}
}
pub async fn kv_put(&self, key: &str, bytes: Bytes) -> Result<()> {
2021-12-07 12:50:44 +00:00
debug!("kv_put {}", key);
2021-12-06 22:40:41 +00:00
let url = format!("{}/v1/kv/{}{}", self.url, self.kv_prefix, key);
let http = self.client.put(&url).body(bytes).send().await?;
http.error_for_status()?;
Ok(())
}
2021-12-07 12:50:44 +00:00
pub async fn kv_put_json<T: Serialize>(&self, key: &str, value: &T) -> Result<()> {
debug!("kv_put_json {}", key);
let url = format!("{}/v1/kv/{}{}", self.url, self.kv_prefix, key);
let http = self.client.put(&url).json(value).send().await?;
http.error_for_status()?;
Ok(())
}
2021-12-06 22:40:41 +00:00
pub async fn kv_delete(&self, key: &str) -> Result<()> {
let url = format!("{}/v1/kv/{}{}", self.url, self.kv_prefix, key);
let http = self.client.delete(&url).send().await?;
http.error_for_status()?;
Ok(())
}
}