dco3 0.19.0

Async API wrapper for DRACOON in Rust.
Documentation
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
//! This module implments basic models for the DRACOON API.
use std::fmt::Debug;

use chrono::{DateTime, Utc};
use dco3_crypto::PlainUserKeyPairContainer;
use secrecy::{zeroize::Zeroize, CloneableSecret};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::{
    client::DracoonClient, config::ConfigEndpoint, eventlog::EventlogEndpoint,
    groups::GroupsEndpoint, nodes::NodesEndpoint, provisioning::ProvisioningEndpoint,
    public::PublicEndpoint, roles::RolesEndpoint, settings::SettingsEndpoint,
    shares::SharesEndpoint, system::SystemEndpoint, user::UserEndpoint, users::UsersEndpoint,
};

use super::client::errors::DracoonClientError;

pub trait ConnectedClient {}

// struct for internal mutability
#[derive(Debug, Clone, Default)]
pub struct Container<T: Clone> {
    data: Arc<RwLock<Option<T>>>,
}

impl<T: Clone> Container<T> {
    pub fn new_from(data: T) -> Self {
        Self {
            data: Arc::new(RwLock::new(Some(data))),
        }
    }

    pub fn new() -> Self {
        Self {
            data: Arc::new(RwLock::new(None)),
        }
    }

    pub async fn set(&self, data: T) {
        let mut lock = self.data.write().await;
        *lock = Some(data);
    }

    pub async fn get(&self) -> Option<T> {
        let lock = self.data.read().await;
        lock.clone()
    }

    pub async fn is_some(&self) -> bool {
        let lock = self.data.read().await;
        lock.is_some()
    }

    pub async fn is_none(&self) -> bool {
        let lock = self.data.read().await;
        lock.is_none()
    }
}

#[derive(Debug)]
pub struct ListAllParams {
    pub offset: Option<u64>,
    pub limit: Option<u64>,
    pub filter: Option<FilterQueries>,
    pub sort: Option<SortQueries>,
}

impl Default for ListAllParams {
    fn default() -> Self {
        Self {
            offset: Some(0),
            limit: None,
            filter: None,
            sort: None,
        }
    }
}

/// This is a struct to pass params for listing requests (GET)
/// - offset: offset of the list
/// - limit: limit of the list
/// - filter: filter queries
/// - sort: sort queries
///
/// To build filters, use appropiate filters in relevant modules
/// To build sorts, use appropiate sorts in relevant modules
///
/// Example:
///
/// ´´´
/// use dco3::models::ListAllParams;
///
/// let params = ListAllParams::builder()
///    .with_offset(0)
///   .with_limit(100)
///   .with_filter(dco3::nodes::models::NodesFilter::name_equals("test"))
///   .with_sort(dco3::nodes::models::NodesSort::name_asc())
///  .build();
///
/// ´´´
///   
impl ListAllParams {
    /// Creates a builder to pass filters, sorting, offset and limit
    pub fn builder() -> ListAllParamsBuilder {
        ListAllParamsBuilder::new()
    }

    pub fn filter_to_string(&self) -> String {
        match self.filter.as_deref() {
            Some(filters) => filters
                .iter()
                .map(|filter| filter.to_filter_string())
                .collect::<Vec<String>>()
                .join("|"),
            None => String::new(),
        }
    }

    pub fn sort_to_string(&self) -> String {
        match self.sort.as_deref() {
            Some(sorts) => sorts
                .iter()
                .map(|sort| sort.to_sort_string())
                .collect::<Vec<String>>()
                .join("|"),
            None => String::new(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.offset.is_none()
            && self.limit.is_none()
            && self.filter.is_none()
            && self.sort.is_none()
    }
}

pub struct ListAllParamsBuilder {
    params: ListAllParams,
}

impl ListAllParamsBuilder {
    pub fn new() -> Self {
        Self {
            params: ListAllParams::default(),
        }
    }
    pub fn with_offset(mut self, offset: u64) -> Self {
        self.params.offset = Some(offset);
        self
    }

    pub fn with_limit(mut self, limit: u64) -> Self {
        self.params.limit = Some(limit);
        self
    }

    pub fn with_filter<F>(mut self, filter: F) -> Self
    where
        F: Into<Box<dyn FilterQuery>>,
    {
        match self.params.filter {
            Some(mut filters) => {
                filters.push(filter.into());
                self.params.filter = Some(filters);
            }
            None => {
                let filters = vec![filter.into()];
                self.params.filter = Some(filters);
            }
        }
        self
    }

    pub fn with_sort<S>(mut self, sort: S) -> Self
    where
        S: Into<Box<dyn SortQuery>>,
    {
        match self.params.sort {
            Some(mut sorts) => {
                sorts.push(sort.into());
                self.params.sort = Some(sorts);
            }
            None => {
                let sorts = vec![sort.into()];
                self.params.sort = Some(sorts);
            }
        }

        self
    }

    pub fn build(self) -> ListAllParams {
        self.params
    }
}

impl Default for ListAllParamsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl From<ListAllParams> for String {
    fn from(value: ListAllParams) -> Self {
        let params = format!("?offset={}", value.offset.unwrap_or(0));

        let filters = value.filter_to_string();
        let sorts = value.sort_to_string();

        let params = value
            .filter
            .map(|_| format!("{params}&filter={}", filters))
            .unwrap_or(params);
        let params = value
            .sort
            .map(|_| format!("{params}&sort={}", sorts))
            .unwrap_or(params);

        value
            .limit
            .map(|limit| format!("{params}&limit={limit}"))
            .unwrap_or(params)
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Range {
    pub offset: u64,
    pub limit: u64,
    pub total: u64,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct ObjectExpiration {
    pub enable_expiration: bool,
    pub expire_at: Option<String>,
}

impl ObjectExpiration {
    pub fn new(expire_at: DateTime<Utc>) -> Self {
        Self {
            enable_expiration: true,
            expire_at: Some(expire_at.to_rfc3339()),
        }
    }
}

impl AsRef<ObjectExpiration> for ObjectExpiration {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl From<DateTime<Utc>> for ObjectExpiration {
    fn from(value: DateTime<Utc>) -> Self {
        Self::new(value)
    }
}

#[derive(Deserialize, Debug, Clone)]
pub struct RangedItems<T> {
    pub range: Range,
    pub items: Vec<T>,
}

impl<T> IntoIterator for RangedItems<T> {
    type Item = T;
    type IntoIter = std::vec::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.items.into_iter()
    }
}

pub trait FilterQuery: Debug + Send + Sync {
    fn to_filter_string(&self) -> String;

    fn builder() -> FilterQueryBuilder
    where
        Self: Sized,
    {
        FilterQueryBuilder::new()
    }
}

pub trait SortQuery: Debug + Send + Sync {
    fn to_sort_string(&self) -> String;

    fn builder() -> SortQueryBuilder
    where
        Self: Sized,
    {
        SortQueryBuilder::new()
    }
}

pub type FilterQueries = Vec<Box<dyn FilterQuery>>;
pub type SortQueries = Vec<Box<dyn SortQuery>>;

#[derive(Debug, Clone)]
pub enum FilterOperator {
    Eq,
    Cn,
    Neq,
    Ge,
    Le,
}

impl From<FilterOperator> for String {
    fn from(value: FilterOperator) -> Self {
        match value {
            FilterOperator::Eq => "eq".to_string(),
            FilterOperator::Cn => "cn".to_string(),
            FilterOperator::Neq => "neq".to_string(),
            FilterOperator::Ge => "ge".to_string(),
            FilterOperator::Le => "le".to_string(),
        }
    }
}

impl From<&FilterOperator> for String {
    fn from(value: &FilterOperator) -> Self {
        match value {
            FilterOperator::Eq => "eq".to_string(),
            FilterOperator::Cn => "cn".to_string(),
            FilterOperator::Neq => "neq".to_string(),
            FilterOperator::Ge => "ge".to_string(),
            FilterOperator::Le => "le".to_string(),
        }
    }
}

#[derive(Debug, Clone)]
pub enum SortOrder {
    Asc,
    Desc,
}

impl From<SortOrder> for String {
    fn from(value: SortOrder) -> Self {
        match value {
            SortOrder::Asc => "asc".to_string(),
            SortOrder::Desc => "desc".to_string(),
        }
    }
}

impl From<&SortOrder> for String {
    fn from(value: &SortOrder) -> Self {
        match value {
            SortOrder::Asc => "asc".to_string(),
            SortOrder::Desc => "desc".to_string(),
        }
    }
}

#[derive(Default)]
pub struct FilterQueryBuilder {
    field: Option<String>,
    operator: Option<FilterOperator>,
    value: Option<String>,
}

impl FilterQueryBuilder {
    pub fn new() -> Self {
        Self {
            field: None,
            operator: None,
            value: None,
        }
    }

    pub fn with_field(mut self, field: impl Into<String>) -> Self {
        self.field = Some(field.into());
        self
    }

    pub fn with_operator(mut self, operator: FilterOperator) -> Self {
        self.operator = Some(operator);
        self
    }

    pub fn with_value(mut self, value: impl Into<String>) -> Self {
        self.value = Some(value.into());
        self
    }

    pub fn try_build(self) -> Result<String, DracoonClientError> {
        let field = self.field.ok_or(DracoonClientError::MissingArgument)?;
        let operator = self.operator.ok_or(DracoonClientError::MissingArgument)?;
        let operator: String = operator.into();
        let value = self.value.ok_or(DracoonClientError::MissingArgument)?;

        Ok(format!("{}:{}:{}", field, operator, value))
    }
}

#[derive(Default)]
pub struct SortQueryBuilder {
    field: Option<String>,
    order: Option<SortOrder>,
}

impl SortQueryBuilder {
    pub fn new() -> Self {
        Self {
            field: None,
            order: None,
        }
    }

    pub fn with_field(mut self, field: impl Into<String>) -> Self {
        self.field = Some(field.into());
        self
    }

    pub fn with_order(mut self, order: SortOrder) -> Self {
        self.order = Some(order);
        self
    }

    pub fn try_build(self) -> Result<String, DracoonClientError> {
        let field = self.field.ok_or(DracoonClientError::MissingArgument)?;
        let order = self.order.ok_or(DracoonClientError::MissingArgument)?;
        let order: String = order.into();

        Ok(format!("{}:{}", field, order))
    }
}

impl FilterQuery for String {
    fn to_filter_string(&self) -> String {
        self.clone()
    }
}

impl SortQuery for String {
    fn to_sort_string(&self) -> String {
        self.clone()
    }
}

impl From<String> for Box<dyn FilterQuery> {
    fn from(value: String) -> Self {
        Box::new(value)
    }
}

impl From<String> for Box<dyn SortQuery> {
    fn from(value: String) -> Self {
        Box::new(value)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct KeyValueEntry {
    pub key: String,
    pub value: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_filter_query_builder() {
        let query = FilterQueryBuilder::new()
            .with_field("field")
            .with_operator(FilterOperator::Eq)
            .with_value("value")
            .try_build()
            .unwrap();

        assert_eq!(query, "field:eq:value");

        let params = ListAllParams::builder()
            .with_filter(query.to_filter_string())
            .build();

        assert_eq!(params.filter_to_string(), "field:eq:value");
    }

    #[test]
    fn test_sort_query_builder() {
        let query = SortQueryBuilder::new()
            .with_field("field")
            .with_order(SortOrder::Asc)
            .try_build()
            .unwrap();

        assert_eq!(query, "field:asc");

        let params = ListAllParams::builder()
            .with_sort(query.to_sort_string())
            .build();

        assert_eq!(params.sort_to_string(), "field:asc");
    }

    #[test]
    fn test_filter_query_builder_missing_field() {
        let query = FilterQueryBuilder::new()
            .with_operator(FilterOperator::Eq)
            .with_value("value")
            .try_build();

        assert!(query.is_err());
    }

    #[test]
    fn test_filter_query_builder_missing_operator() {
        let query = FilterQueryBuilder::new()
            .with_field("field")
            .with_value("value")
            .try_build();

        assert!(query.is_err());
    }

    #[test]
    fn test_filter_query_builder_missing_value() {
        let query = FilterQueryBuilder::new()
            .with_field("field")
            .with_operator(FilterOperator::Eq)
            .try_build();

        assert!(query.is_err());
    }

    #[test]
    fn test_sort_query_builder_missing_field() {
        let query = SortQueryBuilder::new()
            .with_order(SortOrder::Asc)
            .try_build();

        assert!(query.is_err());
    }

    #[test]
    fn test_sort_query_builder_missing_order() {
        let query = SortQueryBuilder::new().with_field("field").try_build();

        assert!(query.is_err());
    }
}

#[derive(Clone)]
pub struct Endpoints<S> {
    pub user: UserEndpoint<S>,
    pub public: PublicEndpoint<S>,
    pub shares: SharesEndpoint<S>,
    pub users: UsersEndpoint<S>,
    pub groups: GroupsEndpoint<S>,
    pub settings: SettingsEndpoint<S>,
    pub provisioning: ProvisioningEndpoint<S>,
    pub config: ConfigEndpoint<S>,
    pub system: SystemEndpoint<S>,
    pub nodes: NodesEndpoint<S>,
    pub eventlog: EventlogEndpoint<S>,
    pub roles: RolesEndpoint<S>,
}

impl<S> From<&Arc<DracoonClient<S>>> for Endpoints<S> {
    fn from(client: &Arc<DracoonClient<S>>) -> Self {
        Self {
            user: UserEndpoint::new(client.clone()),
            public: PublicEndpoint::new(client.clone()),
            shares: SharesEndpoint::new(client.clone()),
            users: UsersEndpoint::new(client.clone()),
            groups: GroupsEndpoint::new(client.clone()),
            settings: SettingsEndpoint::new(client.clone()),
            provisioning: ProvisioningEndpoint::new(client.clone()),
            config: ConfigEndpoint::new(client.clone()),
            system: SystemEndpoint::new(client.clone()),
            nodes: NodesEndpoint::new(client.clone()),
            eventlog: EventlogEndpoint::new(client.clone()),
            roles: RolesEndpoint::new(client.clone()),
        }
    }
}

#[derive(Clone)]
pub struct WrappedUserKeypair(PlainUserKeyPairContainer);

impl CloneableSecret for WrappedUserKeypair {}

impl Zeroize for WrappedUserKeypair {
    fn zeroize(&mut self) {
        self.0.private_key_container.private_key.zeroize();
    }
}

impl WrappedUserKeypair {
    pub fn new(keypair: PlainUserKeyPairContainer) -> Self {
        Self(keypair)
    }

    pub fn keypair(&self) -> &PlainUserKeyPairContainer {
        &self.0
    }
}