redisctl 0.10.1

Unified CLI for Redis Cloud and Enterprise
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
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
#![allow(dead_code)]

use crate::cli::OutputFormat;
use crate::commands::enterprise::utils;
use crate::connection::ConnectionManager;
use crate::error::RedisCtlError;
use anyhow::Context;
use clap::Subcommand;
use serde_json::Value;

#[derive(Debug, Clone, Subcommand)]
pub enum LdapCommands {
    /// Get LDAP configuration
    Get,

    /// Update LDAP configuration
    #[command(after_help = "EXAMPLES:
    # Update LDAP server URLs
    redisctl enterprise ldap update --server-urls ldap://ldap.example.com:389

    # Update bind credentials
    redisctl enterprise ldap update --bind-dn 'CN=admin,DC=example,DC=com' --bind-pass secret

    # Update search settings
    redisctl enterprise ldap update --user-dn 'OU=Users,DC=example,DC=com' \\
        --group-dn 'OU=Groups,DC=example,DC=com'

    # Using JSON for full configuration
    redisctl enterprise ldap update --data @ldap.json")]
    Update {
        /// LDAP server URLs (comma-separated)
        #[arg(long)]
        server_urls: Option<String>,
        /// Bind DN for LDAP connection
        #[arg(long)]
        bind_dn: Option<String>,
        /// Bind password for LDAP connection
        #[arg(long)]
        bind_pass: Option<String>,
        /// Base DN for user searches
        #[arg(long)]
        user_dn: Option<String>,
        /// Base DN for group searches
        #[arg(long)]
        group_dn: Option<String>,
        /// Enable/disable LDAP
        #[arg(long)]
        enabled: Option<bool>,
        /// JSON data for LDAP configuration (overridden by other flags)
        #[arg(long)]
        data: Option<String>,
    },

    /// Delete LDAP configuration
    Delete,

    /// Test LDAP connection
    Test {
        /// Optional test configuration JSON
        #[arg(long)]
        data: Option<String>,
    },
}

#[derive(Debug, Clone, Subcommand)]
pub enum LdapMappingsCommands {
    /// List all LDAP mappings
    List,

    /// Get specific LDAP mapping
    Get {
        /// Mapping UID
        uid: u64,
    },

    /// Create new LDAP mapping
    #[command(after_help = "EXAMPLES:
    # Create LDAP mapping with required fields
    redisctl enterprise ldap-mappings create --name engineers --dn 'CN=Engineers,OU=Groups,DC=example,DC=com' --role db_viewer

    # Create with email alerts
    redisctl enterprise ldap-mappings create --name admins --dn 'CN=Admins,OU=Groups,DC=example,DC=com' --role admin --email alerts@example.com

    # Using JSON for advanced configuration
    redisctl enterprise ldap-mappings create --data '{\"name\":\"ops\",\"dn\":\"CN=Ops,OU=Groups,DC=example,DC=com\",\"role\":\"db_member\"}'")]
    Create {
        /// Mapping name
        #[arg(long)]
        name: Option<String>,
        /// LDAP group distinguished name
        #[arg(long)]
        dn: Option<String>,
        /// Role identifier
        #[arg(long)]
        role: Option<String>,
        /// Email address for alerts
        #[arg(long)]
        email: Option<String>,
        /// JSON data for advanced configuration (overridden by other flags)
        #[arg(long)]
        data: Option<String>,
    },

    /// Update existing LDAP mapping
    #[command(after_help = "EXAMPLES:
    # Update mapping name
    redisctl enterprise ldap-mappings update 1 --name new-name

    # Update role
    redisctl enterprise ldap-mappings update 1 --role admin

    # Update email
    redisctl enterprise ldap-mappings update 1 --email newalerts@example.com

    # Using JSON for advanced configuration
    redisctl enterprise ldap-mappings update 1 --data '{\"role_uids\":[1,2,3]}'")]
    Update {
        /// Mapping UID
        uid: u64,
        /// Mapping name
        #[arg(long)]
        name: Option<String>,
        /// LDAP group distinguished name
        #[arg(long)]
        dn: Option<String>,
        /// Role identifier
        #[arg(long)]
        role: Option<String>,
        /// Email address for alerts
        #[arg(long)]
        email: Option<String>,
        /// JSON data for advanced configuration (overridden by other flags)
        #[arg(long)]
        data: Option<String>,
    },

    /// Delete LDAP mapping
    Delete {
        /// Mapping UID
        uid: u64,
    },
}

pub async fn handle_ldap_command(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    cmd: LdapCommands,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    match cmd {
        LdapCommands::Get => handle_ldap_get(conn_mgr, profile_name, output_format, query).await,
        LdapCommands::Update {
            server_urls,
            bind_dn,
            bind_pass,
            user_dn,
            group_dn,
            enabled,
            data,
        } => {
            handle_ldap_update(
                conn_mgr,
                profile_name,
                server_urls.as_deref(),
                bind_dn.as_deref(),
                bind_pass.as_deref(),
                user_dn.as_deref(),
                group_dn.as_deref(),
                enabled,
                data.as_deref(),
                output_format,
                query,
            )
            .await
        }
        LdapCommands::Delete => {
            handle_ldap_delete(conn_mgr, profile_name, output_format, query).await
        }
        LdapCommands::Test { data } => {
            handle_ldap_test(
                conn_mgr,
                profile_name,
                data.as_deref(),
                output_format,
                query,
            )
            .await
        }
    }
}

pub async fn handle_ldap_mappings_command(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    cmd: LdapMappingsCommands,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    match cmd {
        LdapMappingsCommands::List => {
            handle_mappings_list(conn_mgr, profile_name, output_format, query).await
        }
        LdapMappingsCommands::Get { uid } => {
            handle_mappings_get(conn_mgr, profile_name, uid, output_format, query).await
        }
        LdapMappingsCommands::Create {
            name,
            dn,
            role,
            email,
            data,
        } => {
            handle_mappings_create(
                conn_mgr,
                profile_name,
                name.as_deref(),
                dn.as_deref(),
                role.as_deref(),
                email.as_deref(),
                data.as_deref(),
                output_format,
                query,
            )
            .await
        }
        LdapMappingsCommands::Update {
            uid,
            name,
            dn,
            role,
            email,
            data,
        } => {
            handle_mappings_update(
                conn_mgr,
                profile_name,
                uid,
                name.as_deref(),
                dn.as_deref(),
                role.as_deref(),
                email.as_deref(),
                data.as_deref(),
                output_format,
                query,
            )
            .await
        }
        LdapMappingsCommands::Delete { uid } => {
            handle_mappings_delete(conn_mgr, profile_name, uid, output_format, query).await
        }
    }
}

async fn handle_ldap_get(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    let response = client
        .get::<Value>("/v1/cluster/ldap")
        .await
        .map_err(RedisCtlError::from)?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

#[allow(clippy::too_many_arguments)]
async fn handle_ldap_update(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    server_urls: Option<&str>,
    bind_dn: Option<&str>,
    bind_pass: Option<&str>,
    user_dn: Option<&str>,
    group_dn: Option<&str>,
    enabled: Option<bool>,
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut payload = if let Some(data_str) = data {
        utils::read_json_data(data_str).context("Invalid JSON data for LDAP configuration")?
    } else {
        serde_json::json!({})
    };

    let payload_obj = payload.as_object_mut().unwrap();

    // CLI parameters override JSON values
    if let Some(urls) = server_urls {
        payload_obj.insert(
            "uris".to_string(),
            serde_json::json!(urls.split(',').collect::<Vec<_>>()),
        );
    }
    if let Some(dn) = bind_dn {
        payload_obj.insert("bind_dn".to_string(), serde_json::json!(dn));
    }
    if let Some(pass) = bind_pass {
        payload_obj.insert("bind_pass".to_string(), serde_json::json!(pass));
    }
    if let Some(dn) = user_dn {
        payload_obj.insert("user_dn".to_string(), serde_json::json!(dn));
    }
    if let Some(dn) = group_dn {
        payload_obj.insert("group_dn".to_string(), serde_json::json!(dn));
    }
    if let Some(en) = enabled {
        payload_obj.insert("enabled".to_string(), serde_json::json!(en));
    }

    let response = client
        .put_raw("/v1/cluster/ldap", payload)
        .await
        .map_err(RedisCtlError::from)?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

async fn handle_ldap_delete(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    let response = client
        .delete_raw("/v1/cluster/ldap")
        .await
        .map_err(RedisCtlError::from)?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

async fn handle_ldap_test(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    let payload = if let Some(d) = data {
        serde_json::from_str(d).context("Invalid JSON data for LDAP test")?
    } else {
        serde_json::json!({})
    };

    let response = client
        .post_raw("/v1/cluster/ldap/test", payload)
        .await
        .map_err(RedisCtlError::from)?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

// LDAP Mappings handlers
async fn handle_mappings_list(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    let response = client
        .get::<Value>("/v1/ldap_mappings")
        .await
        .map_err(RedisCtlError::from)?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

async fn handle_mappings_get(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    uid: u64,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    let endpoint = format!("/v1/ldap_mappings/{}", uid);
    let response = client
        .get::<Value>(&endpoint)
        .await
        .context(format!("Failed to get LDAP mapping {}", uid))?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

#[allow(clippy::too_many_arguments)]
async fn handle_mappings_create(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    name: Option<&str>,
    dn: Option<&str>,
    role: Option<&str>,
    email: Option<&str>,
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    // Start with JSON data if provided, otherwise empty object
    let mut request_obj: serde_json::Map<String, serde_json::Value> = if let Some(json_data) = data
    {
        let parsed: Value =
            serde_json::from_str(json_data).context("Invalid JSON data for LDAP mapping")?;
        parsed
            .as_object()
            .cloned()
            .unwrap_or_else(serde_json::Map::new)
    } else {
        serde_json::Map::new()
    };

    // Override with first-class parameters if provided
    if let Some(n) = name {
        request_obj.insert("name".to_string(), serde_json::json!(n));
    }
    if let Some(d) = dn {
        request_obj.insert("dn".to_string(), serde_json::json!(d));
    }
    if let Some(r) = role {
        request_obj.insert("role".to_string(), serde_json::json!(r));
    }
    if let Some(e) = email {
        request_obj.insert("email".to_string(), serde_json::json!(e));
    }

    // Validate required fields for create
    if !request_obj.contains_key("name") {
        return Err(RedisCtlError::InvalidInput {
            message: "--name is required when not using --data".to_string(),
        });
    }
    if !request_obj.contains_key("dn") {
        return Err(RedisCtlError::InvalidInput {
            message: "--dn is required when not using --data".to_string(),
        });
    }
    if !request_obj.contains_key("role") {
        return Err(RedisCtlError::InvalidInput {
            message: "--role is required when not using --data".to_string(),
        });
    }

    let payload = serde_json::Value::Object(request_obj);
    let response = client
        .post_raw("/v1/ldap_mappings", payload)
        .await
        .map_err(RedisCtlError::from)?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

#[allow(clippy::too_many_arguments)]
async fn handle_mappings_update(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    uid: u64,
    name: Option<&str>,
    dn: Option<&str>,
    role: Option<&str>,
    email: Option<&str>,
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    // Start with JSON data if provided, otherwise empty object
    let mut request_obj: serde_json::Map<String, serde_json::Value> = if let Some(json_data) = data
    {
        let parsed: Value =
            serde_json::from_str(json_data).context("Invalid JSON data for LDAP mapping update")?;
        parsed
            .as_object()
            .cloned()
            .unwrap_or_else(serde_json::Map::new)
    } else {
        serde_json::Map::new()
    };

    // Override with first-class parameters if provided
    if let Some(n) = name {
        request_obj.insert("name".to_string(), serde_json::json!(n));
    }
    if let Some(d) = dn {
        request_obj.insert("dn".to_string(), serde_json::json!(d));
    }
    if let Some(r) = role {
        request_obj.insert("role".to_string(), serde_json::json!(r));
    }
    if let Some(e) = email {
        request_obj.insert("email".to_string(), serde_json::json!(e));
    }

    // Validate at least one update field is provided
    if request_obj.is_empty() {
        return Err(RedisCtlError::InvalidInput {
            message:
                "At least one update field is required (--name, --dn, --role, --email, or --data)"
                    .to_string(),
        });
    }

    let payload = serde_json::Value::Object(request_obj);
    let endpoint = format!("/v1/ldap_mappings/{}", uid);
    let response = client
        .put_raw(&endpoint, payload)
        .await
        .context(format!("Failed to update LDAP mapping {}", uid))?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

async fn handle_mappings_delete(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    uid: u64,
    output_format: OutputFormat,
    query: Option<&str>,
) -> Result<(), RedisCtlError> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    let endpoint = format!("/v1/ldap_mappings/{}", uid);
    let response = client
        .delete_raw(&endpoint)
        .await
        .context(format!("Failed to delete LDAP mapping {}", uid))?;

    let result = if let Some(q) = query {
        utils::apply_jmespath(&response, q)?
    } else {
        response
    };

    utils::print_formatted_output(result, output_format)
}

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

    #[test]
    fn test_ldap_commands() {
        use clap::CommandFactory;

        #[derive(clap::Parser)]
        struct TestCli {
            #[command(subcommand)]
            cmd: LdapCommands,
        }

        TestCli::command().debug_assert();
    }

    #[test]
    fn test_ldap_mappings_commands() {
        use clap::CommandFactory;

        #[derive(clap::Parser)]
        struct TestCli {
            #[command(subcommand)]
            cmd: LdapMappingsCommands,
        }

        TestCli::command().debug_assert();
    }
}