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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! Node command implementations for Redis Enterprise

#![allow(dead_code)]

use crate::cli::OutputFormat;
use crate::connection::ConnectionManager;
use crate::error::Result as CliResult;
use anyhow::Context;
use redis_enterprise::nodes::NodeHandler;
use serde_json::Value;
use tabled::{Table, Tabled, settings::Style};

use super::utils::*;

/// Node row for clean table display
#[derive(Tabled)]
struct NodeRow {
    #[tabled(rename = "UID")]
    uid: String,
    #[tabled(rename = "ADDRESS")]
    addr: String,
    #[tabled(rename = "STATUS")]
    status: String,
    #[tabled(rename = "SHARDS")]
    shards: String,
    #[tabled(rename = "MEMORY")]
    memory: String,
    #[tabled(rename = "RACK")]
    rack_id: String,
}

// Node Operations

pub async fn list_nodes(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    let nodes = handler.list().await?;
    let nodes_json = serde_json::to_value(nodes).context("Failed to serialize nodes")?;
    let data = handle_output(nodes_json, output_format, query)?;
    if matches!(resolve_auto(output_format), OutputFormat::Table) {
        print_nodes_table(&data)?;
    } else {
        print_formatted_output(data, output_format)?;
    }
    Ok(())
}

pub async fn get_node(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    let node = handler.get(id).await?;
    let node_json = serde_json::to_value(node).context("Failed to serialize node")?;
    let data = handle_output(node_json, output_format, query)?;
    if matches!(resolve_auto(output_format), OutputFormat::Table) {
        print_node_detail(&data)?;
    } else {
        print_formatted_output(data, output_format)?;
    }
    Ok(())
}

/// Print nodes in clean table format
fn print_nodes_table(data: &Value) -> CliResult<()> {
    let nodes = match data {
        Value::Array(arr) => arr.clone(),
        _ => {
            println!("No nodes found");
            return Ok(());
        }
    };

    if nodes.is_empty() {
        println!("No nodes found");
        return Ok(());
    }

    let mut rows = Vec::new();
    for node in &nodes {
        let total_mem = node.get("total_memory").and_then(|v| v.as_u64());
        let memory = total_mem
            .map(format_bytes)
            .unwrap_or_else(|| "-".to_string());

        rows.push(NodeRow {
            uid: extract_field(node, "uid", "-"),
            addr: extract_field(node, "addr", "-"),
            status: format_status(extract_field(node, "status", "unknown")),
            shards: extract_field(node, "shard_count", "-"),
            memory,
            rack_id: extract_field(node, "rack_id", "-"),
        });
    }

    let mut table = Table::new(&rows);
    table.with(Style::blank());
    output_with_pager(&table.to_string());
    Ok(())
}

/// Print node detail in key-value format
fn print_node_detail(data: &Value) -> CliResult<()> {
    let mut rows = Vec::new();

    let fields = [
        ("UID", "uid"),
        ("Address", "addr"),
        ("Status", "status"),
        ("Rack ID", "rack_id"),
        ("OS Version", "os_version"),
        ("Software Version", "software_version"),
        ("Shard Count", "shard_count"),
        ("Accept Servers", "accept_servers"),
    ];

    for (label, key) in &fields {
        if let Some(val) = data.get(*key) {
            let display = match val {
                Value::Null => continue,
                Value::String(s) => s.clone(),
                Value::Bool(b) => b.to_string(),
                Value::Number(n) => n.to_string(),
                _ => val.to_string(),
            };
            rows.push(DetailRow {
                field: label.to_string(),
                value: display,
            });
        }
    }

    if let Some(mem) = data.get("total_memory").and_then(|v| v.as_u64()) {
        rows.push(DetailRow {
            field: "Total Memory".to_string(),
            value: format_bytes(mem),
        });
    }

    if rows.is_empty() {
        println!("No node information available");
        return Ok(());
    }

    let mut table = Table::new(&rows);
    table.with(Style::blank());
    output_with_pager(&table.to_string());
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub async fn add_node(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    address: Option<&str>,
    username: Option<&str>,
    password: Option<&str>,
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    // Start with JSON from --data if provided, otherwise empty object
    let mut node_data = if let Some(data_str) = data {
        read_json_data(data_str).context("Failed to parse node data")?
    } else {
        serde_json::json!({})
    };

    let node_obj = node_data.as_object_mut().unwrap();

    // CLI parameters override JSON values
    if let Some(addr) = address {
        node_obj.insert("address".to_string(), serde_json::json!(addr));
    }
    if let Some(user) = username {
        node_obj.insert("username".to_string(), serde_json::json!(user));
    }
    if let Some(pass) = password {
        node_obj.insert("password".to_string(), serde_json::json!(pass));
    }

    // Note: The actual add node operation typically requires cluster join operations
    // This is a placeholder for the actual implementation which would use cluster join
    let result = client.post_raw("/v1/nodes", node_data).await?;
    let data = handle_output(result, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn remove_node(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    force: bool,
    _output_format: OutputFormat,
    _query: Option<&str>,
) -> CliResult<()> {
    if !force && !confirm_action(&format!("Remove node {} from cluster?", id))? {
        println!("Operation cancelled");
        return Ok(());
    }

    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    handler.remove(id).await?;
    println!("Node {} removed successfully", id);
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub async fn update_node(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    accept_servers: Option<bool>,
    external_addr: Option<Vec<String>>,
    rack_id: Option<&str>,
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    use crate::error::RedisCtlError;

    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // 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 = read_json_data(json_data).context("Failed to parse JSON data")?;
        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(accept) = accept_servers {
        request_obj.insert("accept_servers".to_string(), serde_json::json!(accept));
    }
    if let Some(addrs) = &external_addr {
        request_obj.insert("external_addr".to_string(), serde_json::json!(addrs));
    }
    if let Some(rack) = rack_id {
        request_obj.insert("rack_id".to_string(), serde_json::json!(rack));
    }

    // 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 (--accept-servers, --external-addr, --rack-id, or --data)".to_string(),
        });
    }

    let update_data = serde_json::Value::Object(request_obj);
    let updated = handler.update(id, update_data).await?;
    let updated_json = serde_json::to_value(updated).context("Failed to serialize updated node")?;
    let data = handle_output(updated_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

// Node Status & Health

pub async fn get_node_status(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    let status = handler.status(id).await?;
    let data = handle_output(status, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_stats(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    let stats = handler.stats(id).await?;
    let stats_json = serde_json::to_value(stats).context("Failed to serialize stats")?;
    let data = handle_output(stats_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_metrics(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    interval: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    // Metrics endpoint typically requires interval parameter
    let endpoint = if let Some(interval) = interval {
        format!("/v1/nodes/{}/metrics?interval={}", id, interval)
    } else {
        format!("/v1/nodes/{}/metrics", id)
    };

    let metrics = client.get_raw(&endpoint).await?;
    let data = handle_output(metrics, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn check_node_health(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    // Health check typically combines multiple status endpoints
    let handler = NodeHandler::new(client);
    let status = handler.status(id).await?;
    let data = handle_output(status, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_alerts(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    let alerts = handler.alerts_for(id).await?;
    let data = handle_output(alerts, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

// Node Maintenance

pub async fn enable_maintenance(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    let result = handler.execute_action(id, "maintenance_on").await?;
    let result_json = serde_json::to_value(result).context("Failed to serialize result")?;
    let data = handle_output(result_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn disable_maintenance(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);
    let result = handler.execute_action(id, "maintenance_off").await?;
    let result_json = serde_json::to_value(result).context("Failed to serialize result")?;
    let data = handle_output(result_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn rebalance_node(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Rebalance typically uses the rebalance action
    let result = handler.execute_action(id, "rebalance").await?;
    let result_json = serde_json::to_value(result).context("Failed to serialize result")?;
    let data = handle_output(result_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn drain_node(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Drain is typically done via the drain action
    let result = handler.execute_action(id, "drain").await?;
    let result_json = serde_json::to_value(result).context("Failed to serialize result")?;
    let data = handle_output(result_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn restart_node(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    force: bool,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    if !force && !confirm_action(&format!("Restart node {} services?", id))? {
        println!("Operation cancelled");
        return Ok(());
    }

    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Restart typically uses the restart action
    let result = handler.execute_action(id, "restart").await?;
    let result_json = serde_json::to_value(result).context("Failed to serialize result")?;
    let data = handle_output(result_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

// Node Configuration

pub async fn get_node_config(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    // Configuration is typically part of the node details
    let handler = NodeHandler::new(client);
    let node = handler.get(id).await?;
    let node_json = serde_json::to_value(node).context("Failed to serialize node")?;
    let data = handle_output(node_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub async fn update_node_config(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    max_redis_servers: Option<u32>,
    bigstore_driver: Option<&str>,
    data: Option<&str>,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Start with JSON from --data if provided, otherwise empty object
    let mut config_data = if let Some(data_str) = data {
        read_json_data(data_str).context("Failed to parse config data")?
    } else {
        serde_json::json!({})
    };

    let config_obj = config_data.as_object_mut().unwrap();

    // CLI parameters override JSON values
    if let Some(max_servers) = max_redis_servers {
        config_obj.insert(
            "max_redis_servers".to_string(),
            serde_json::json!(max_servers),
        );
    }
    if let Some(driver) = bigstore_driver {
        config_obj.insert("bigstore_driver".to_string(), serde_json::json!(driver));
    }

    let updated = handler.update(id, config_data).await?;
    let updated_json = serde_json::to_value(updated).context("Failed to serialize updated node")?;
    let data = handle_output(updated_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_rack(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Rack info is part of node details
    let node = handler.get(id).await?;
    let node_json = serde_json::to_value(node).context("Failed to serialize node")?;
    let data = handle_output(node_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn set_node_rack(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    rack: &str,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    let update_data = serde_json::json!({
        "rack_id": rack
    });

    let updated = handler.update(id, update_data).await?;
    let updated_json = serde_json::to_value(updated).context("Failed to serialize updated node")?;
    let data = handle_output(updated_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_role(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Role info is part of node details
    let node = handler.get(id).await?;
    let node_json = serde_json::to_value(node).context("Failed to serialize node")?;
    let data = handle_output(node_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

// Node Resources

pub async fn get_node_resources(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Resources are typically in stats
    let stats = handler.stats(id).await?;
    let stats_json = serde_json::to_value(stats).context("Failed to serialize stats")?;
    let data = handle_output(stats_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_memory(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Memory details are in stats
    let stats = handler.stats(id).await?;
    let stats_json = serde_json::to_value(stats).context("Failed to serialize stats")?;
    let data = handle_output(stats_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_cpu(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // CPU details are in stats
    let stats = handler.stats(id).await?;
    let stats_json = serde_json::to_value(stats).context("Failed to serialize stats")?;
    let data = handle_output(stats_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_storage(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Storage details are in stats
    let stats = handler.stats(id).await?;
    let stats_json = serde_json::to_value(stats).context("Failed to serialize stats")?;
    let data = handle_output(stats_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

pub async fn get_node_network(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    id: u32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;
    let handler = NodeHandler::new(client);

    // Network stats are typically in stats
    let stats = handler.stats(id).await?;
    let stats_json = serde_json::to_value(stats).context("Failed to serialize stats")?;
    let data = handle_output(stats_json, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}