summaryrefslogtreecommitdiff
path: root/src/kv.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/kv.rs')
-rw-r--r--src/kv.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/kv.rs b/src/kv.rs
index 6c6f15a..5373ec6 100644
--- a/src/kv.rs
+++ b/src/kv.rs
@@ -1,10 +1,19 @@
+use std::collections::HashMap;
+
use anyhow::{anyhow, Result};
use bytes::Bytes;
use log::*;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
-use crate::Consul;
+use crate::{Consul, WithIndex};
+
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
+#[serde(rename_all = "PascalCase")]
+pub struct KvGetPrefixEntry {
+ pub key: String,
+ pub value: String,
+}
impl Consul {
pub async fn kv_get(&self, key: &str) -> Result<Option<Bytes>> {
@@ -37,6 +46,33 @@ impl Consul {
}
}
+ pub async fn kv_get_prefix(
+ &self,
+ key_prefix: &str,
+ last_index: Option<usize>,
+ ) -> Result<WithIndex<HashMap<String, Bytes>>> {
+ debug!("kv_get_prefix {} index={:?}", key_prefix, last_index);
+ let results: WithIndex<Vec<KvGetPrefixEntry>> = self
+ .get_with_index(
+ format!(
+ "{}/v1/kv/{}{}?recurse",
+ self.url, self.kv_prefix, key_prefix
+ ),
+ last_index,
+ )
+ .await?;
+
+ let mut res = HashMap::new();
+ for ent in results.value {
+ res.insert(ent.key, Bytes::from(base64::decode(&ent.value)?));
+ }
+
+ Ok(WithIndex {
+ value: res,
+ index: results.index,
+ })
+ }
+
pub async fn kv_put(&self, key: &str, bytes: Bytes) -> Result<()> {
debug!("kv_put {}", key);