Skip to content

Commit

Permalink
feat: implement etcd client
Browse files Browse the repository at this point in the history
Create a new internal_etcd interface witch contains all methods
related to the ETCD client.

Signed-off-by: GridexX <[email protected]>
  • Loading branch information
GridexX committed Aug 26, 2022
1 parent ce1e1cb commit 836722a
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion controller/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
etcd-client = "0.9.2"
actix-web = "4.1.0"
serde = { version = "1.0.139", features = ["derive"] }
tonic = "0.7.2"
proto = { path = "../../proto" }
log = "0.4.0"
tokio = { version = "1.20.0", features = ["rt-multi-thread"] }
tokio = { version = "1.20.0", features = ["rt-multi-thread", "macros"] }
113 changes: 113 additions & 0 deletions controller/lib/src/etcd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use etcd_client::{Client, DeleteResponse, Error, GetOptions, PutResponse};
use log::info;

pub struct EtcdClient {
inner: Client,
}

impl EtcdClient {
pub async fn new(address: String) -> Result<Self, Error> {
let inner = Client::connect([address], None).await?;
Ok(Self { inner })
}

pub async fn get(&mut self, key: &str) -> Option<String> {
match self.inner.get(key, None).await {
Ok(response) => match response.kvs().first() {
Some(kv) => match kv.value_str() {
Ok(value) => Some(value.to_string()),
Err(_) => None,
},
None => None,
},
Err(_) => None,
}
}
pub async fn put(&mut self, key: &str, value: &str) -> Result<PutResponse, Error> {
info!(
"Inserting value in ETCD : Key \"{}\" associated with value \"{}\"",
key, value
);
self.inner.put(key, value, None).await
}
pub async fn delete(&mut self, key: &str) -> Option<DeleteResponse> {
match self.get(key).await {
Some(_) => match self.inner.delete(key, None).await {
Ok(response) => Some(response),
Err(_) => None,
},
None => None,
}
}

pub async fn get_all(&mut self) -> Result<Vec<String>, Error> {
info!("Retrieving all keys in ETCD");
let resp = self
.inner
.get("", Some(GetOptions::new().with_all_keys()))
.await?;

let mut values: Vec<String> = vec![];
for kv in resp.kvs() {
let value = kv.value_str()?;
values.push(value.to_string())
}
Ok(values)
}
}
/*
#[cfg(test)]
mod tests {
use crate::etcd::EtcdClient;
use etcd_client::Error;
#[tokio::test]
async fn test_value_insertion() -> Result<(), Error> {
let mut etcd_client = EtcdClient::new("localhost:2379".to_string()).await;
let _res = etcd_client.put("foo", "bar").await?;
let resp = etcd_client.get("foo").await?;
assert_eq!(resp, "bar");
Ok(())
}
#[tokio::test]
async fn test_value_modification() -> Result<(), Error> {
let mut etcd_client = EtcdClient::new("localhost:2379".to_string()).await;
etcd_client.put("foo", "bar").await?;
let _res = etcd_client.patch("foo", "baz").await?;
let resp = etcd_client.get("foo").await?;
assert_eq!(resp, "baz");
Ok(())
}
#[tokio::test]
async fn test_value_deletion() -> Result<(), Error> {
let mut etcd_client = EtcdClient::new("localhost:2379".to_string()).await;
let _res = etcd_client.put("foo", "bar").await?;
let _res = etcd_client.delete("foo").await?;
let err = etcd_client.get("foo").await;
assert!(err.is_err());
Ok(())
}
#[tokio::test]
async fn test_value_deletion_doesnt_exists() -> Result<(), Error> {
let mut etcd_client = EtcdClient::new("localhost:2379".to_string()).await;
let _res = etcd_client.put("foo", "bar").await?;
let err = etcd_client.delete("foo2").await;
assert!(err.is_err());
Ok(())
}
#[tokio::test]
async fn test_function_get_all() -> Result<(), Error> {
let mut etcd_client = EtcdClient::new("localhost:2379".to_string()).await;
let _res = etcd_client.put("bar", "foo").await;
let _res = etcd_client.put("hello", "world").await;
let values = etcd_client.get_all().await?;
assert_eq!(values[0], "foo");
assert_eq!(values[1], "world");
Ok(())
}
}
*/
1 change: 1 addition & 0 deletions controller/lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod etcd;
pub mod external_api;
pub mod grpc_client;
pub mod internal_api;

0 comments on commit 836722a

Please sign in to comment.