1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::sync::Arc;
use tonic::{metadata::MetadataValue, transport::Channel, Interceptor, Request};
use crate::proto::etcdserverpb::{
auth_client::AuthClient, kv_client::KvClient, lease_client::LeaseClient,
watch_client::WatchClient,
};
use crate::{Auth, Kv, Lease, Result, Watch};
pub struct ClientConfig {
pub endpoints: Vec<String>,
pub auth: Option<(String, String)>,
}
#[derive(Clone)]
pub struct Client {
inner: Arc<Inner>,
}
#[allow(dead_code)]
pub(crate) struct Inner {
channel: Channel,
auth_client: Auth,
kv_client: Kv,
watch_client: Watch,
lease_client: Lease,
}
impl Client {
async fn generate_auth_token(endpoints: Vec<String>, auth: (String, String)) -> Result<String> {
use crate::AuthenticateRequest;
let channel = {
let endpoints = endpoints
.into_iter()
.map(|e| Channel::from_shared(e).expect("parse endpoint URI"));
Channel::balance_list(endpoints)
};
let mut auth_client = Auth::new(AuthClient::new(channel));
let (name, password) = auth;
let resp = auth_client
.authenticate(AuthenticateRequest::new(name, password))
.await?;
Ok(resp.token().to_owned())
}
pub async fn connect(cfg: ClientConfig) -> Result<Self> {
let token = match cfg.auth {
Some(auth) => {
Some(Self::generate_auth_token(cfg.endpoints.clone(), auth.clone()).await?)
}
None => None,
};
let auth_interceptor = if let Some(token) = token {
let token = MetadataValue::from_str(&token).unwrap();
Some(Interceptor::new(move |mut req: Request<()>| {
req.metadata_mut().insert("authorization", token.clone());
Ok(req)
}))
} else {
None
};
let channel = {
let endpoints = cfg
.endpoints
.into_iter()
.map(|e| Channel::from_shared(e).expect("parse endpoint URI"));
Channel::balance_list(endpoints)
};
let inner = {
let (auth_client, kv_client, watch_client, lease_client) =
if let Some(auth_interceptor) = auth_interceptor {
(
Auth::new(AuthClient::with_interceptor(
channel.clone(),
auth_interceptor.clone(),
)),
Kv::new(KvClient::with_interceptor(
channel.clone(),
auth_interceptor.clone(),
)),
Watch::new(WatchClient::with_interceptor(
channel.clone(),
auth_interceptor.clone(),
)),
Lease::new(LeaseClient::with_interceptor(
channel.clone(),
auth_interceptor,
)),
)
} else {
(
Auth::new(AuthClient::new(channel.clone())),
Kv::new(KvClient::new(channel.clone())),
Watch::new(WatchClient::new(channel.clone())),
Lease::new(LeaseClient::new(channel.clone())),
)
};
Inner {
channel,
auth_client,
kv_client,
watch_client,
lease_client,
}
};
Ok(Self {
inner: Arc::new(inner),
})
}
pub fn auth(&self) -> Auth {
self.inner.auth_client.clone()
}
pub fn kv(&self) -> Kv {
self.inner.kv_client.clone()
}
pub fn watch(&self) -> Watch {
self.inner.watch_client.clone()
}
pub fn lease(&self) -> Lease {
self.inner.lease_client.clone()
}
}