Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
rewrite API and add tests (#66)
Browse files Browse the repository at this point in the history
* rewrite API and add tests

* add v3 api proto file, fix test code warring

Co-authored-by: 0x0177b11f <[email protected]>
  • Loading branch information
zarvd and 0x0177b11f authored Mar 9, 2022
1 parent b031bfb commit a45467f
Show file tree
Hide file tree
Showing 48 changed files with 2,158 additions and 2,017 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
[package]
name = "etcd-rs"
version = "0.6.0"
authors = ["luncj <[email protected]>"]
version = "1.0.0-alpha.0"
authors = ["lodrem <[email protected]>"]
edition = "2021"
keywords = ["etcd", "future", "async"]
repository = "https://github.com/luncj/etcd-rs"
homepage = "https://github.com/luncj/etcd-rs"
repository = "https://github.com/lodrem/etcd-rs"
homepage = "https://github.com/lodrem/etcd-rs"
description = "etcd client for rust"
documentation = "https://docs.rs/etcd-rs"
license = "MIT"

[dependencies]
tonic = { version = "0.6", features = ["tls"] }
prost = "0.9"
tokio = "1.15"
tokio = "1.17"
tokio-stream = "0.1"
async-trait = "0.1"
futures = "0.3"
thiserror = "1.0"
http = "0.2"

[dev-dependencies]
tokio = { version = "1.15", features = ["full"] }
tokio = { version = "1.17", features = ["full"] }
rand = "0.8"

[build-dependencies]
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ ETCD_CLUSTER_WITH_TLS ?= false
ETCD_NODE ?= etcd-1
ETCD_VERSION ?= v3.5.2

TEST_CASE ?= test_basic

.PHONY: build
build:
cargo build

.PHONY: test
test:
RUST_BACKTRACE=full cargo test -- --test-threads=1 --nocapture;
RUST_BACKTRACE=full cargo test -- --test-threads=1 --show-output;
cargo check --no-default-features

.PHONY: test-one
test-one:
RUST_BACKTRACE=full cargo test ${TEST_CASE} -- --test-threads=1 --show-output;

.PHONY: publish
publish:
cargo package && cargo publish
Expand Down
139 changes: 76 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
etcd for Rust
etcd client for Rust
====

[![CI Status][ci-badge]][ci-url]
[![Crates.io][crates-badge]][crates-url]
[![License][license-badge]][license-url]
[<img alt="github" height="20" src="https://img.shields.io/badge/github-lodrem/etcd--rs-8da0cb?style=for-the-badge&labelColor=555555&logo=github">](https://github.com/lodrem/etcd-rs)
[<img alt="crates.io" height="20" src="https://img.shields.io/crates/v/etcd-rs.svg?style=for-the-badge&color=fc8d62&logo=rust">](https://crates.io/crates/etcd-rs)
[<img alt="docs.rs" height="20" src="https://img.shields.io/badge/docs.rs-etcd--rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K">](https://docs.rs/etcd-rs)
[<img alt="build status" height="20" src="https://img.shields.io/github/workflow/status/lodrem/etcd-rs/CI/master?style=for-the-badge">](https://github.com/luncj/etcd-rs/actions?query%3Amaster)

[ci-badge]: https://img.shields.io/github/workflow/status/luncj/etcd-rs/CI?style=flat-square
[ci-url]: https://github.com/luncj/etcd-rs/actions
[crates-badge]: https://img.shields.io/crates/v/etcd-rs.svg?style=flat-square
[crates-url]: https://crates.io/crates/etcd-rs
[license-badge]: https://img.shields.io/github/license/luncj/etcd-rs.svg?style=flat-square
[license-url]: https://github.com/luncj/etcd-rs/blob/master/LICENSE
An [etcd](https://github.com/etcd-io/etcd) (API v3) client for Rust backed by [tokio](https://github.com/tokio-rs/tokio) and [tonic](https://github.com/hyperium/tonic).


An [etcd](https://github.com/etcd-io/etcd) (API v3) client for Rust, and it provides `async/await` APIs backed by [tokio](https://github.com/tokio-rs/tokio) and [tonic](https://github.com/hyperium/tonic).

Documentation on the library can be found at [docs.rs/etcd-rs](https://docs.rs/etcd-rs).

Features
----

- Asynchronous
- Etcd APIv3

Examples
Supported APIs
----

[./examples](./examples)
- KV
- [x] Put
- [x] Range
- [x] Delete
- [x] Transaction
- [x] Compact
- Lease
- [x] Grant
- [x] Revoke
- [x] KeepAlive
- [x] TimeToLive
- Watch
- [x] WatchCreate
- [x] WatchCancel
- Auth
- [x] Authenticate
- [ ] RoleAdd
- [ ] RoleGrantPermission
- [ ] UserAdd
- [ ] UserGrantRole
- [ ] AuthEnable
- [ ] AuthDisable
- Cluster
- [x] MemberAdd
- [x] MemberRemove
- [x] MemberUpdate
- [x] MemberList
- Maintenance
- [ ] Alarm
- [ ] Status
- [ ] Defragment
- [ ] Hash
- [ ] Snapshot
- [ ] MoveLeader

Usage
----
Expand All @@ -35,66 +53,61 @@ Add following dependencies in your project `cargo.toml`:

```toml
[dependencies]
etcd-rs = "0.6"
etcd-rs = "1.0.0-alpha.0"
```

#### Setup Client

```rust
fn foo() {
let endpoints = vec!["http://127.0.0.1:2379".to_owned()];

let client = Client::connect(ClientConfig {
endpoints,
use etcd_rs::Client;

#[tokio::main]
async fn main() {
let cli = Client::connect(ClientConfig {
endpoints: [
"http://127.0.0.1:12379",
"http://127.0.0.1:22379",
"http://127.0.0.1:32379",
],
..Default::default()
}).await;
}).await;

cli.put(("foo", "bar")).await.expect("put kv");

let kvs = cli.get("foo").await.expect("get kv").take_kvs();
assert_eq!(kvs.len(), 1);
}
```

if authenticate enabled
Development
----

```rust
fn foo() {
let endpoints = vec!["http://127.0.0.1:2379".to_owned()];

let client = Client::connect(ClientConfig {
endpoints,
auth: Some(("user".to_owned(), "password".to_owned())),
..Default::default()
}).await;
}
```
requirements:
- Makefile
- docker
- docker-compose

with tls
### Start local etcd cluster

```rust
fn foo() {
let endpoints = vec!["https://127.0.0.1:2379".to_owned()];
let tls = ClientTlsConfig::new();

let client = Client::connect(ClientConfig {
endpoints,
auth: Some(("user".to_owned(), "password".to_owned())),
tls: Some(tls),
..Default::default()
}).await;
}
```shell
make setup-etcd-cluster
```

Developemnt
----
stop cluster
```shell
make teardown-etcd-cluster
```

### Run tests

requirements:
- Makefile
- docker
- docker-compose

```shell
make test
```

for specified case:
```shell
TEST_CASE=test_put_error make test-one
```

License
----

Expand Down
14 changes: 7 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
// NOTE: compile v3lockpb before rpc for disabling generate duplicated client in v3lockpb
tonic_build::configure()
.build_server(false)
.build_client(false)
.compile(&["proto/lock.proto"], &["proto"])?;

tonic_build::configure().build_server(false).compile(
&["proto/auth.proto", "proto/kv.proto", "proto/rpc.proto"],
&[
"proto/auth.proto",
"proto/kv.proto",
"proto/rpc.proto",
"proto/v3lock.proto",
"proto/v3election.proto",
],
&["proto"],
)?;

Expand Down
Loading

0 comments on commit a45467f

Please sign in to comment.