aboutsummaryrefslogtreecommitdiff
path: root/src/consul.rs
diff options
context:
space:
mode:
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 ----