aboutsummaryrefslogtreecommitdiff
path: root/src/consul.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-12-07 17:56:15 +0100
committerAlex Auvolat <alex@adnab.me>2021-12-07 17:56:15 +0100
commit0682c74e9d5083b43b3f83f8bb1ca747658d1455 (patch)
tree6532eba8bf707c9d32bdbc6535ccc082e2229096 /src/consul.rs
parentbb77e7c459a624bb2b4ea043145fd6ea75771105 (diff)
downloadtricot-0682c74e9d5083b43b3f83f8bb1ca747658d1455.tar.gz
tricot-0682c74e9d5083b43b3f83f8bb1ca747658d1455.zip
Watch multiple consul nodes
Diffstat (limited to 'src/consul.rs')
-rw-r--r--src/consul.rs33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/consul.rs b/src/consul.rs
index 6fc031c..240c177 100644
--- a/src/consul.rs
+++ b/src/consul.rs
@@ -7,6 +7,12 @@ use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
// ---- Watch and retrieve Consul catalog ----
+//
+#[derive(Serialize, Deserialize, Debug)]
+pub struct ConsulNodeListNode {
+ #[serde(rename = "Node")]
+ pub node: String,
+}
#[derive(Serialize, Deserialize, Debug)]
pub struct ConsulServiceEntry {
@@ -59,8 +65,6 @@ pub struct Consul {
url: String,
kv_prefix: String,
- idx: Option<u64>,
-
pub local_node: String,
}
@@ -70,29 +74,36 @@ impl Consul {
client: reqwest::Client::new(),
url: url.to_string(),
kv_prefix: kv_prefix.to_string(),
- idx: None,
local_node: local_node.into(),
};
}
- pub fn watch_node_reset(&mut self) -> () {
- self.idx = None;
+ pub async fn list_nodes(&self) -> Result<Vec<String>> {
+ debug!("list_nodes");
+
+ let url = format!("{}/v1/catalog/nodes", self.url);
+
+ let http = self.client.get(&url).send().await?;
+ let resp: Vec<ConsulNodeListNode> = http.json().await?;
+ Ok(resp.into_iter().map(|n| n.node).collect::<Vec<_>>())
}
- pub async fn watch_node(&mut self, host: &str) -> Result<ConsulNodeCatalog> {
- let url = match self.idx {
+ pub async fn watch_node(&self, host: &str, idx: Option<usize>) -> Result<(ConsulNodeCatalog, usize)> {
+ debug!("watch_node {} {:?}", host, idx);
+
+ let url = match 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 new_idx = match http.headers().get("X-Consul-Index") {
+ Some(v) => v.to_str()?.parse::<usize>()?,
+ None => bail!("X-Consul-Index header not found"),
};
let resp: ConsulNodeCatalog = http.json().await?;
- return Ok(resp);
+ return Ok((resp, new_idx));
}
// ---- KV get and put ----