pipebuilder_common 0.2.2

lib for pipebuilder components
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// registry implemented with [etcd-client](https://crates.io/crates/etcd-client)
use crate::{
    read_file, BlobResource, Resource, ResourceKeyBuilder, ResourceType, Result, Snapshot,
};
use etcd_client::{
    Certificate, Client, ConnectOptions, DeleteOptions, DeleteResponse, GetOptions, GetResponse,
    Identity, KeyValue, LeaseGrantResponse, LockOptions, LockResponse, PutOptions, PutResponse,
    TlsOptions, WatchOptions, WatchStream, Watcher,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tracing::info;

use crate::Period;

#[derive(Deserialize)]
pub struct UserConfig {
    pub name: String,
    pub password: String,
}

#[derive(Deserialize)]
pub struct KeepAliveConfig {
    pub keep_alive_interval: Period,
    pub keep_alive_timeout: Period,
}

#[derive(Deserialize)]
pub struct IdentityConfig {
    pub cert: String,
    pub key: String,
}

impl IdentityConfig {
    async fn into_identity(self) -> Result<Identity> {
        let cert = read_file(self.cert).await?;
        let key = read_file(self.key).await?;
        Ok(Identity::from_pem(cert, key))
    }
}

#[derive(Deserialize)]
pub struct TlsConfig {
    pub domain: Option<String>,
    // CA Certificate file, verify the server's TLS certificate
    pub ca_cert: Option<String>,
    // Client identity present to server
    pub identity: Option<IdentityConfig>,
}

impl TlsConfig {
    pub async fn into_tls_options(self) -> Result<TlsOptions> {
        let tls = TlsOptions::new();
        let tls = match self.domain {
            Some(domain) => tls.domain_name(domain),
            None => tls,
        };
        let tls = match self.ca_cert {
            Some(ca_cert) => {
                let pem = read_file(ca_cert).await?;
                tls.ca_certificate(Certificate::from_pem(pem))
            }
            None => tls,
        };
        let tls = match self.identity {
            Some(identity) => {
                let identity = identity.into_identity().await?;
                tls.identity(identity)
            }
            None => tls,
        };
        Ok(tls)
    }
}

#[derive(Deserialize)]
pub struct ConnectConfig {
    pub user: Option<UserConfig>,
    pub keep_alive: Option<KeepAliveConfig>,
    pub tls: Option<TlsConfig>,
}

impl ConnectConfig {
    async fn into_connect_opts(self) -> Result<ConnectOptions> {
        let opts = ConnectOptions::new();
        let opts = match self.user {
            Some(user) => opts.with_user(user.name, user.password),
            None => opts,
        };
        let opts = match self.keep_alive {
            Some(keep_alive) => opts.with_keep_alive(
                keep_alive.keep_alive_interval.into(),
                keep_alive.keep_alive_timeout.into(),
            ),
            None => opts,
        };
        let opts = match self.tls {
            Some(tls) => opts.with_tls(tls.into_tls_options().await?),
            None => opts,
        };
        Ok(opts)
    }
}

// etcd client config
#[derive(Deserialize)]
pub struct RegisterConfig {
    // etcd endpoints
    pub endpoints: Vec<String>,
    pub connect: Option<ConnectConfig>,
}

#[derive(Clone)]
pub struct Register {
    client: Client,
}

impl Register {
    pub async fn new(config: RegisterConfig) -> Result<Register> {
        let connect_opts: Option<ConnectOptions> = match config.connect {
            Some(connect) => Some(connect.into_connect_opts().await?),
            None => None,
        };
        let client = Client::connect(config.endpoints, connect_opts).await?;
        Ok(Register { client })
    }

    async fn put<K, V>(
        &mut self,
        key: K,
        value: V,
        options: Option<PutOptions>,
    ) -> Result<PutResponse>
    where
        K: Into<Vec<u8>>,
        V: Into<Vec<u8>>,
    {
        let resp = self.client.put(key, value, options).await?;
        Ok(resp)
    }

    async fn get<K>(&mut self, key: K, options: Option<GetOptions>) -> Result<GetResponse>
    where
        K: Into<Vec<u8>>,
    {
        let resp = self.client.get(key, options).await?;
        Ok(resp)
    }

    pub async fn delete<K>(
        &mut self,
        key: K,
        options: Option<DeleteOptions>,
    ) -> Result<DeleteResponse>
    where
        K: Into<Vec<u8>>,
    {
        let resp = self.client.delete(key, options).await?;
        Ok(resp)
    }

    // list with key prefix
    async fn list<K>(&mut self, prefix: K) -> Result<GetResponse>
    where
        K: Into<Vec<u8>>,
    {
        let resp = self
            .get(prefix, Some(GetOptions::new().with_prefix()))
            .await?;
        Ok(resp)
    }

    async fn list_json_kvs<K, V>(&mut self, prefix: K) -> Result<Vec<(String, V)>>
    where
        K: Into<Vec<u8>>,
        V: DeserializeOwned,
    {
        let resp = self.list(prefix).await?;
        let kvs = Self::deserialize_json_kvs::<V>(resp.kvs())?;
        Ok(kvs)
    }

    pub async fn list_keys<K>(&mut self, prefix: K) -> Result<Vec<String>>
    where
        K: Into<Vec<u8>>,
    {
        let resp = self
            .get(
                prefix,
                Some(GetOptions::new().with_prefix().with_keys_only()),
            )
            .await?;
        let mut keys = vec![];
        for kv in resp.kvs() {
            keys.push(kv.key_str()?.to_owned());
        }
        Ok(keys)
    }

    pub async fn count<K>(&mut self, key: K) -> Result<usize>
    where
        K: Into<Vec<u8>>,
    {
        let options = GetOptions::new().with_count_only();
        let resp = self.get(key, Some(options)).await?;
        Ok(resp.count() as usize)
    }

    pub async fn is_exist<K>(&mut self, key: K) -> Result<bool>
    where
        K: Into<Vec<u8>>,
    {
        let count = self.count(key).await?;
        Ok(count > 0)
    }

    pub async fn count_prefix<K>(&mut self, prefix: K) -> Result<usize>
    where
        K: Into<Vec<u8>>,
    {
        let options = GetOptions::new().with_prefix().with_count_only();
        let resp = self.get(prefix, Some(options)).await?;
        Ok(resp.count() as usize)
    }

    pub async fn is_prefix_exist<K>(&mut self, prefix: K) -> Result<bool>
    where
        K: Into<Vec<u8>>,
    {
        let count = self.count_prefix(prefix).await?;
        Ok(count > 0)
    }

    pub async fn get_json_value<K, V>(
        &mut self,
        key: K,
        options: Option<GetOptions>,
    ) -> Result<Option<V>>
    where
        K: Into<Vec<u8>>,
        V: DeserializeOwned,
    {
        let get_resp = self.get(key, options).await?;
        let object = match get_resp.kvs().first() {
            Some(kv) => {
                let object = serde_json::from_slice::<V>(kv.value())?;
                Some(object)
            }
            None => None,
        };
        Ok(object)
    }

    pub async fn lease_grant(&mut self, ttl: i64) -> Result<LeaseGrantResponse> {
        let resp = self.client.lease_grant(ttl, None).await?;
        Ok(resp)
    }

    pub async fn lease_keep_alive(&mut self, id: i64) -> Result<()> {
        let (mut keeper, mut stream) = self.client.lease_keep_alive(id).await?;
        keeper.keep_alive().await?;
        if let Some(resp) = stream.message().await? {
            info!(lease_id = resp.id(), ttl = resp.ttl(), "lease keep alive");
        }
        Ok(())
    }

    pub async fn watch(
        &mut self,
        key: &str,
        options: Option<WatchOptions>,
    ) -> Result<(Watcher, WatchStream)> {
        let (watcher, stream) = self.client.watch(key, options).await?;
        Ok((watcher, stream))
    }

    pub async fn watch_prefix(&mut self, prefix: &str) -> Result<(Watcher, WatchStream)> {
        let opts = WatchOptions::new().with_prefix();
        self.watch(prefix, opts.into()).await
    }

    pub async fn watch_nodes(&mut self) -> Result<(Watcher, WatchStream)> {
        let prefix_key = ResourceKeyBuilder::new()
            .resource(ResourceType::Node)
            .build();
        self.watch_prefix(prefix_key.as_str()).await
    }

    pub async fn lock(&mut self, name: &str, options: Option<LockOptions>) -> Result<LockResponse> {
        info!(lock_name = name, "acquire lock ...");
        let resp = self.client.lock(name, options).await?;
        Ok(resp)
    }

    pub async fn unlock(&mut self, name: &str, key: &[u8]) -> Result<()> {
        self.client.unlock(key).await?;
        info!(lock_name = name, "released lock ...");
        Ok(())
    }

    async fn do_update_snapshot_resource<S>(
        &mut self,
        namespace: &str,
        id: &str,
    ) -> Result<(PutResponse, S)>
    where
        S: Resource + Snapshot + Serialize + DeserializeOwned,
    {
        // get current snapshot and incr version
        let new_snapshot = match self.do_get_resource::<S>(Some(namespace), id, None).await? {
            Some(mut snapshot) => {
                snapshot.incr_version();
                snapshot
            }
            None => S::default(),
        };
        let key = ResourceKeyBuilder::new()
            .resource(S::ty())
            .namespace(namespace)
            .id(id)
            .build();
        let value = serde_json::to_vec(&new_snapshot)?;
        let resp = self.put(key, value, None).await?;
        Ok((resp, new_snapshot))
    }

    async fn do_get_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: &str,
        version: Option<u64>,
    ) -> Result<Option<R>>
    where
        R: Resource + DeserializeOwned,
    {
        let builder = ResourceKeyBuilder::new().resource(R::ty()).id(id);
        let builder = match namespace {
            Some(namespace) => builder.namespace(namespace),
            None => builder,
        };
        let key = match version {
            Some(version) => builder.version(version).build(),
            None => builder.build(),
        };
        let resource = self.get_json_value::<String, R>(key, None).await?;
        Ok(resource)
    }

    pub async fn get_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: &str,
        version: Option<u64>,
        lease_id: i64,
    ) -> Result<Option<R>>
    where
        R: Resource + DeserializeOwned,
    {
        let builder = ResourceKeyBuilder::new()
            .lock(true)
            .resource(R::ty())
            .id(id);
        let builder = match namespace {
            Some(namespace) => builder.namespace(namespace),
            None => builder,
        };
        let lock_name = match version {
            Some(version) => builder.version(version).build(),
            None => builder.build(),
        };
        let lock_options = LockOptions::new().with_lease(lease_id);
        let lock_resp = self.lock(lock_name.as_str(), lock_options.into()).await?;
        let key = lock_resp.key();
        let resp = self.do_get_resource::<R>(namespace, id, version).await?;
        self.unlock(lock_name.as_str(), key).await?;
        Ok(resp)
    }

    pub async fn update_snapshot_resource<S>(
        &mut self,
        namespace: &str,
        id: &str,
        lease_id: i64,
    ) -> Result<(PutResponse, S)>
    where
        S: Resource + Snapshot + Serialize + DeserializeOwned,
    {
        let lock_options = LockOptions::new().with_lease(lease_id);
        let lock_name = ResourceKeyBuilder::new()
            .lock(true)
            .resource(S::ty())
            .namespace(namespace)
            .id(id)
            .build();
        let lock_resp = self.lock(lock_name.as_str(), lock_options.into()).await?;
        let key = lock_resp.key();
        let resp = self.do_update_snapshot_resource::<S>(namespace, id).await;
        self.unlock(lock_name.as_str(), key).await?;
        resp
    }

    // list resource metadata or snapshot given resource type namespace, id
    pub async fn list_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: Option<&str>,
    ) -> Result<Vec<(String, R)>>
    where
        R: Resource + DeserializeOwned,
    {
        let builder = ResourceKeyBuilder::new().resource(R::ty());
        let builder = match namespace {
            Some(namespace) => builder.namespace(namespace),
            None => builder,
        };
        let prefix = match id {
            Some(id) => builder.id(id).build(),
            None => builder.build(),
        };
        let builds = self.list_json_kvs::<&str, R>(prefix.as_str()).await?;
        Ok(builds)
    }

    async fn do_put_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: &str,
        version: Option<u64>,
        resource: R,
    ) -> Result<(PutResponse, R)>
    where
        R: Resource + Serialize,
    {
        let builder = ResourceKeyBuilder::new().resource(R::ty()).id(id);
        let builder = match namespace {
            Some(namespace) => builder.namespace(namespace),
            None => builder,
        };
        let key = match version {
            Some(version) => builder.version(version).build(),
            None => builder.build(),
        };
        let value = serde_json::to_vec(&resource)?;
        let resp = self.put(key, value, None).await?;
        Ok((resp, resource))
    }

    pub async fn put_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: &str,
        version: Option<u64>,
        resource: R,
        lease_id: i64,
    ) -> Result<(PutResponse, R)>
    where
        R: Resource + Serialize,
    {
        let builder = ResourceKeyBuilder::new()
            .lock(true)
            .resource(R::ty())
            .id(id);
        let builder = match namespace {
            Some(namespace) => builder.namespace(namespace),
            None => builder,
        };
        let lock_name = match version {
            Some(version) => builder.version(version).build(),
            None => builder.build(),
        };
        let lock_options = LockOptions::new().with_lease(lease_id);
        let lock_resp = self.lock(lock_name.as_str(), lock_options.into()).await?;
        let key = lock_resp.key();
        let resp = self.do_put_resource(namespace, id, version, resource).await;
        self.unlock(lock_name.as_str(), key).await?;
        resp
    }

    async fn do_update_blob_resource<R>(
        &mut self,
        namespace: &str,
        id: &str,
        version: u64,
        size: usize,
    ) -> Result<(PutResponse, R)>
    where
        R: Resource + BlobResource + Serialize + DeserializeOwned,
    {
        let new_metadata = match self
            .do_get_resource::<R>(Some(namespace), id, Some(version))
            .await?
        {
            Some(mut metadata) => {
                metadata.incr_usage();
                metadata
            }
            None => R::new(size),
        };
        let key = ResourceKeyBuilder::new()
            .resource(R::ty())
            .namespace(namespace)
            .id(id)
            .version(version)
            .build();
        let value = serde_json::to_vec(&new_metadata)?;
        let resp = self.put(key, value, None).await?;
        Ok((resp, new_metadata))
    }

    pub async fn update_blob_resource<R>(
        &mut self,
        namespace: &str,
        id: &str,
        version: u64,
        size: usize,
        lease_id: i64,
    ) -> Result<(PutResponse, R)>
    where
        R: Resource + BlobResource + Serialize + DeserializeOwned,
    {
        let lock_options = LockOptions::new().with_lease(lease_id);
        let lock_name = ResourceKeyBuilder::new()
            .lock(true)
            .resource(R::ty())
            .namespace(namespace)
            .id(id)
            .version(version)
            .build();
        let lock_resp = self.lock(lock_name.as_str(), lock_options.into()).await?;
        let key = lock_resp.key();
        let resp = self
            .do_update_blob_resource::<R>(namespace, id, version, size)
            .await;
        self.unlock(lock_name.as_str(), key).await?;
        resp
    }

    async fn do_update_default_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: &str,
    ) -> Result<(PutResponse, R)>
    where
        R: Resource + Default + Serialize + DeserializeOwned,
    {
        let resource = match self.do_get_resource::<R>(namespace, id, None).await? {
            Some(resource) => resource,
            None => R::default(),
        };
        let builder = ResourceKeyBuilder::new().resource(R::ty()).id(id);
        let key = match namespace {
            Some(namespace) => builder.namespace(namespace).build(),
            None => builder.build(),
        };
        let value = serde_json::to_vec(&resource)?;
        let resp = self.put(key, value, None).await?;
        Ok((resp, resource))
    }

    pub async fn update_default_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: &str,
        lease_id: i64,
    ) -> Result<(PutResponse, R)>
    where
        R: Resource + Default + Serialize + DeserializeOwned,
    {
        let builder = ResourceKeyBuilder::new()
            .lock(true)
            .resource(R::ty())
            .id(id);
        let lock_name = match namespace {
            Some(namespace) => builder.namespace(namespace).build(),
            None => builder.build(),
        };
        let lock_options = LockOptions::new().with_lease(lease_id);
        let lock_resp = self.lock(lock_name.as_str(), lock_options.into()).await?;
        let key = lock_resp.key();
        let resp = self.do_update_default_resource::<R>(namespace, id).await;
        self.unlock(lock_name.as_str(), key).await?;
        resp
    }

    pub async fn delete_resource<R>(
        &mut self,
        namespace: Option<&str>,
        id: &str,
        version: Option<u64>,
    ) -> Result<()>
    where
        R: Resource,
    {
        let builder = ResourceKeyBuilder::new().resource(R::ty()).id(id);
        let builder = match namespace {
            Some(namespace) => builder.namespace(namespace),
            None => builder,
        };
        let key = match version {
            Some(version) => builder.version(version).build(),
            None => builder.build(),
        };
        let _ = self.delete(key, None).await?;
        Ok(())
    }

    pub async fn is_resource_exist<R>(&mut self, namespace: &str, id: Option<&str>) -> Result<bool>
    where
        R: Resource,
    {
        let builder = ResourceKeyBuilder::new()
            .resource(R::ty())
            .namespace(namespace);
        let prefix = match id {
            Some(id) => builder.id(id).build(),
            None => builder.build(),
        };
        self.is_prefix_exist(prefix).await
    }

    fn deserialize_json_kvs<T>(kvs: &[KeyValue]) -> Result<Vec<(String, T)>>
    where
        T: DeserializeOwned,
    {
        let mut deserialize_kvs: Vec<(String, T)> = vec![];
        for kv in kvs {
            let key = kv.key_str()?;
            let value = serde_json::from_slice::<T>(kv.value())?;
            deserialize_kvs.push((key.to_owned(), value))
        }
        Ok(deserialize_kvs)
    }
}