From 3917f9f997cb894ba660cf4fb7fe1b697dbe94b1 Mon Sep 17 00:00:00 2001 From: GridexX Date: Sat, 20 Aug 2022 12:56:03 +0200 Subject: [PATCH] docs: add logger export for ETCD methods Signed-off-by: GridexX --- controller/lib/src/internal_etcd/interface.rs | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/controller/lib/src/internal_etcd/interface.rs b/controller/lib/src/internal_etcd/interface.rs index cbc254c7..de07de59 100644 --- a/controller/lib/src/internal_etcd/interface.rs +++ b/controller/lib/src/internal_etcd/interface.rs @@ -1,5 +1,5 @@ use etcd_client::{Client, Error, PutResponse, DeleteResponse, GetOptions }; -use log::info; +use log::{info,error}; pub struct PaginateRequest { limit: usize, @@ -21,16 +21,15 @@ pub struct PaginateResult { } impl PaginateResult { - pub fn new() -> Self { + pub fn new(count: usize) -> Self { PaginateResult { - count: 0, + count: count, values: vec![] } } pub fn push(&mut self, value: String) { self.values.push(value); - self.count+= 1; } pub fn get_values(& self) -> &Vec { @@ -49,6 +48,7 @@ pub struct EtcdInterface { impl EtcdInterface { pub async fn new(address: String) -> Self { + info!("Starting ETCD client on {}", address); EtcdInterface { client: Client::connect([address], None).await.unwrap(), } @@ -57,26 +57,36 @@ impl EtcdInterface { pub async fn get(&mut self, key: &str) -> Result { if let Some(kv) = self.client.get(key, None).await?.kvs().first() { let res = kv.value_str(); + info!("Retrieving value in ETCD : Key \"{}\" is associated with value \"{}\"", key, res.as_ref().unwrap()); res.map(|s| s.to_string()) } else { + error!("Error while retrieving value in ETCD : Key \"{}\" not found", key); Err(Error::from(std::io::Error::new(std::io::ErrorKind::NotFound, "Key not found"))) } } - + pub async fn put(&mut self, key: &str, value: &str) -> Result { + info!("Inserting value in ETCD : Key \"{}\" associated with value \"{}\"", key, value); self.client.put(key, value, None).await } - + pub async fn patch(&mut self, key: &str, value: &str) -> Result { + info!("Updating value in ETCD : Key \"{}\" associated with new value \"{}\"", key, value); self.client.put(key, value, None).await } - + pub async fn delete(&mut self, key: &str) -> Result { - self.client.delete(key, None).await + if let Some(kv) = self.client.get(key, None).await?.kvs().first() { + info!("Deleting value in ETCD : Key \"{}\" ", key); + self.client.delete(key, None).await + } else { + error!("Error while deleting value in ETCD : Key \"{}\" not found", key); + Err(Error::from(std::io::Error::new(std::io::ErrorKind::NotFound, "Key not found"))) + } } pub async fn get_all(&mut self) -> Result,Error> { - + info!("Retrieving all keys in ETCD"); let resp = self.client .get("", Some(GetOptions::new().with_all_keys())) .await?; @@ -107,13 +117,13 @@ impl EtcdInterface { let max_index_in_request:usize = first_index + paginate_request.limit; let max_index_in_keys = if length < max_index_in_request {length} else {max_index_in_request}; - let mut paginate_result: PaginateResult = PaginateResult::new(); + let mut paginate_result: PaginateResult = PaginateResult::new(length); for i in first_index..max_index_in_keys { let value = resp.kvs()[i].value_str()?; paginate_result.push(value.to_string()); } - + info!("Retrieving all keys with pagination in ETCD ({} showed/ {} total)", paginate_result.values.len(), length); Ok(paginate_result) }