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
use anyhow::Context;
use clap::Subcommand;

use crate::error::RedisCtlError;
use crate::{cli::OutputFormat, connection::ConnectionManager, error::Result as CliResult};

#[allow(dead_code)]
pub async fn handle_migration_command(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    migration_cmd: MigrationCommands,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    migration_cmd
        .execute(conn_mgr, profile_name, output_format, query)
        .await
}

#[derive(Debug, Clone, Subcommand)]
pub enum MigrationCommands {
    /// List all migrations
    List,

    /// Get migration status
    Get {
        /// Migration UID
        uid: String,
    },

    /// Create a new migration
    #[command(after_help = "EXAMPLES:
    # Migrate from external Redis to internal database
    redisctl enterprise migration create --source-host redis.external.com --source-port 6379 \\
        --target-bdb 1

    # Migrate with authentication
    redisctl enterprise migration create --source-host redis.external.com --source-port 6379 \\
        --source-password secret123 --target-bdb 1

    # Migrate with SSL
    redisctl enterprise migration create --source-host redis.external.com --source-port 6379 \\
        --source-ssl --target-bdb 1

    # Migrate specific key pattern
    redisctl enterprise migration create --source-host redis.external.com --source-port 6379 \\
        --target-bdb 1 --key-pattern 'user:*'

    # Flush target before migration
    redisctl enterprise migration create --source-host redis.external.com --source-port 6379 \\
        --target-bdb 1 --flush-target

    # Using JSON for advanced configuration
    redisctl enterprise migration create --data @migration.json")]
    Create {
        /// Source hostname or IP address
        #[arg(long)]
        source_host: Option<String>,

        /// Source port number
        #[arg(long)]
        source_port: Option<u16>,

        /// Source endpoint type (redis, cluster, azure-cache)
        #[arg(long, default_value = "redis")]
        source_type: String,

        /// Source authentication password
        #[arg(long)]
        source_password: Option<String>,

        /// Use SSL for source connection
        #[arg(long)]
        source_ssl: bool,

        /// Target database UID
        #[arg(long)]
        target_bdb: Option<u32>,

        /// Target hostname (for external targets)
        #[arg(long)]
        target_host: Option<String>,

        /// Target port (for external targets)
        #[arg(long)]
        target_port: Option<u16>,

        /// Target authentication password
        #[arg(long)]
        target_password: Option<String>,

        /// Use SSL for target connection
        #[arg(long)]
        target_ssl: bool,

        /// Migration type (full, incremental)
        #[arg(long)]
        migration_type: Option<String>,

        /// Redis key pattern to migrate (supports wildcards like 'user:*')
        #[arg(long)]
        key_pattern: Option<String>,

        /// Flush target database before migration
        #[arg(long)]
        flush_target: bool,

        /// JSON data for advanced configuration (overridden by other flags)
        #[arg(long)]
        data: Option<String>,
    },

    /// Start a migration
    Start {
        /// Migration UID
        uid: String,
    },

    /// Pause a migration
    Pause {
        /// Migration UID
        uid: String,
    },

    /// Resume a migration
    Resume {
        /// Migration UID
        uid: String,
    },

    /// Cancel a migration
    Cancel {
        /// Migration UID
        uid: String,

        /// Skip confirmation prompt
        #[arg(long)]
        force: bool,
    },

    /// Export database data (legacy command)
    Export {
        /// Database UID
        bdb_uid: u64,
    },

    /// Import database data (legacy command)
    #[command(after_help = "EXAMPLES:
    # Import with source URI
    redisctl enterprise migration import 1 --source-uri redis://external-redis:6379

    # Import with authentication
    redisctl enterprise migration import 1 --source-uri redis://external-redis:6379 --source-password secret

    # Import specific keys
    redisctl enterprise migration import 1 --source-uri redis://external-redis:6379 --key-pattern 'app:*'

    # Using JSON for advanced configuration
    redisctl enterprise migration import 1 --data @import.json")]
    Import {
        /// Database UID
        bdb_uid: u64,

        /// Source Redis URI (e.g., redis://host:port)
        #[arg(long)]
        source_uri: Option<String>,

        /// Source authentication password
        #[arg(long)]
        source_password: Option<String>,

        /// Redis key pattern to import (supports wildcards)
        #[arg(long)]
        key_pattern: Option<String>,

        /// Flush database before import
        #[arg(long)]
        flush_before: bool,

        /// Import data (use @filename or - for stdin) for advanced configuration
        #[arg(long)]
        data: Option<String>,
    },
}

impl MigrationCommands {
    #[allow(dead_code)]
    pub async fn execute(
        &self,
        conn_mgr: &ConnectionManager,
        profile_name: Option<&str>,
        output_format: OutputFormat,
        query: Option<&str>,
    ) -> CliResult<()> {
        handle_migration_command_impl(conn_mgr, profile_name, self, output_format, query).await
    }
}

#[allow(dead_code)]
async fn handle_migration_command_impl(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    command: &MigrationCommands,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr.create_enterprise_client(profile_name).await?;

    match command {
        MigrationCommands::List => {
            let response: serde_json::Value = client
                .get("/v1/migrations")
                .await
                .context("Failed to list migrations")?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }

        MigrationCommands::Get { uid } => {
            let response: serde_json::Value = client
                .get(&format!("/v1/migrations/{}", uid))
                .await
                .context(format!("Failed to get migration {}", uid))?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }

        MigrationCommands::Create {
            source_host,
            source_port,
            source_type,
            source_password,
            source_ssl,
            target_bdb,
            target_host,
            target_port,
            target_password,
            target_ssl,
            migration_type,
            key_pattern,
            flush_target,
            data,
        } => {
            // 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 = super::utils::read_json_data(json_data)?;
                    parsed
                        .as_object()
                        .cloned()
                        .unwrap_or_else(serde_json::Map::new)
                } else {
                    serde_json::Map::new()
                };

            // Build source endpoint if first-class params provided
            if source_host.is_some() || source_port.is_some() {
                let mut source = serde_json::Map::new();
                source.insert("endpoint_type".to_string(), serde_json::json!(source_type));
                if let Some(h) = source_host {
                    source.insert("host".to_string(), serde_json::json!(h));
                }
                if let Some(p) = source_port {
                    source.insert("port".to_string(), serde_json::json!(p));
                }
                if let Some(pw) = source_password {
                    source.insert("password".to_string(), serde_json::json!(pw));
                }
                if *source_ssl {
                    source.insert("ssl".to_string(), serde_json::json!(true));
                }
                request_obj.insert("source".to_string(), serde_json::Value::Object(source));
            }

            // Build target endpoint if first-class params provided
            if target_bdb.is_some() || target_host.is_some() {
                let mut target = serde_json::Map::new();
                if let Some(bdb) = target_bdb {
                    target.insert("endpoint_type".to_string(), serde_json::json!("redis"));
                    target.insert("bdb_uid".to_string(), serde_json::json!(bdb));
                } else {
                    target.insert("endpoint_type".to_string(), serde_json::json!("redis"));
                    if let Some(h) = target_host {
                        target.insert("host".to_string(), serde_json::json!(h));
                    }
                    if let Some(p) = target_port {
                        target.insert("port".to_string(), serde_json::json!(p));
                    }
                }
                if let Some(pw) = target_password {
                    target.insert("password".to_string(), serde_json::json!(pw));
                }
                if *target_ssl {
                    target.insert("ssl".to_string(), serde_json::json!(true));
                }
                request_obj.insert("target".to_string(), serde_json::Value::Object(target));
            }

            // Add other parameters
            if let Some(mt) = migration_type {
                request_obj.insert("migration_type".to_string(), serde_json::json!(mt));
            }
            if let Some(kp) = key_pattern {
                request_obj.insert("key_pattern".to_string(), serde_json::json!(kp));
            }
            if *flush_target {
                request_obj.insert("flush_target".to_string(), serde_json::json!(true));
            }

            // Validate required fields
            if !request_obj.contains_key("source") {
                return Err(RedisCtlError::InvalidInput {
                    message: "--source-host is required when not using --data".to_string(),
                });
            }
            if !request_obj.contains_key("target") {
                return Err(RedisCtlError::InvalidInput {
                    message: "--target-bdb or --target-host is required when not using --data"
                        .to_string(),
                });
            }

            let payload = serde_json::Value::Object(request_obj);
            let response: serde_json::Value = client
                .post("/v1/migrations", &payload)
                .await
                .context("Failed to create migration")?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }

        MigrationCommands::Start { uid } => {
            let response: serde_json::Value = client
                .post(
                    &format!("/v1/migrations/{}/start", uid),
                    &serde_json::Value::Null,
                )
                .await
                .context(format!("Failed to start migration {}", uid))?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }

        MigrationCommands::Pause { uid } => {
            let response: serde_json::Value = client
                .post(
                    &format!("/v1/migrations/{}/pause", uid),
                    &serde_json::Value::Null,
                )
                .await
                .context(format!("Failed to pause migration {}", uid))?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }

        MigrationCommands::Resume { uid } => {
            let response: serde_json::Value = client
                .post(
                    &format!("/v1/migrations/{}/resume", uid),
                    &serde_json::Value::Null,
                )
                .await
                .context(format!("Failed to resume migration {}", uid))?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }

        MigrationCommands::Cancel { uid, force } => {
            if !force && !super::utils::confirm_action(&format!("Cancel migration {}?", uid))? {
                return Ok(());
            }

            client
                .delete(&format!("/v1/migrations/{}", uid))
                .await
                .context(format!("Failed to cancel migration {}", uid))?;

            println!("Migration {} cancelled successfully", uid);
        }

        MigrationCommands::Export { bdb_uid } => {
            let response: serde_json::Value = client
                .post(
                    &format!("/v1/bdbs/{}/actions/export", bdb_uid),
                    &serde_json::json!({}),
                )
                .await
                .context(format!("Failed to export database {}", bdb_uid))?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }

        MigrationCommands::Import {
            bdb_uid,
            source_uri,
            source_password,
            key_pattern,
            flush_before,
            data,
        } => {
            // 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 = super::utils::read_json_data(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(uri) = source_uri {
                request_obj.insert("source_uri".to_string(), serde_json::json!(uri));
            }
            if let Some(pw) = source_password {
                request_obj.insert("source_password".to_string(), serde_json::json!(pw));
            }
            if let Some(kp) = key_pattern {
                request_obj.insert("key_pattern".to_string(), serde_json::json!(kp));
            }
            if *flush_before {
                request_obj.insert("flush_before".to_string(), serde_json::json!(true));
            }

            // Validate at least some configuration is provided
            if request_obj.is_empty() {
                return Err(RedisCtlError::InvalidInput {
                    message: "At least one import configuration is required (--source-uri, --key-pattern, --flush-before, or --data)".to_string(),
                });
            }

            let payload = serde_json::Value::Object(request_obj);
            let response: serde_json::Value = client
                .post(&format!("/v1/bdbs/{}/actions/import", bdb_uid), &payload)
                .await
                .context(format!("Failed to import data to database {}", bdb_uid))?;

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

            super::utils::print_formatted_output(output_data, output_format)?;
        }
    }

    Ok(())
}

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

    #[test]
    fn test_migration_command_parsing() {
        use clap::Parser;

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

        // Test list command
        let cli = TestCli::parse_from(["test", "list"]);
        assert!(matches!(cli.cmd, MigrationCommands::List));

        // Test get command
        let cli = TestCli::parse_from(["test", "get", "mig-1"]);
        if let MigrationCommands::Get { uid } = cli.cmd {
            assert_eq!(uid, "mig-1");
        } else {
            panic!("Expected Get command");
        }

        // Test create command with first-class params
        let cli = TestCli::parse_from([
            "test",
            "create",
            "--source-host",
            "redis.external.com",
            "--source-port",
            "6379",
            "--target-bdb",
            "1",
            "--key-pattern",
            "user:*",
        ]);
        if let MigrationCommands::Create {
            source_host,
            source_port,
            target_bdb,
            key_pattern,
            ..
        } = cli.cmd
        {
            assert_eq!(source_host, Some("redis.external.com".to_string()));
            assert_eq!(source_port, Some(6379));
            assert_eq!(target_bdb, Some(1));
            assert_eq!(key_pattern, Some("user:*".to_string()));
        } else {
            panic!("Expected Create command");
        }

        // Test start command
        let cli = TestCli::parse_from(["test", "start", "mig-1"]);
        if let MigrationCommands::Start { uid } = cli.cmd {
            assert_eq!(uid, "mig-1");
        } else {
            panic!("Expected Start command");
        }

        // Test pause command
        let cli = TestCli::parse_from(["test", "pause", "mig-1"]);
        if let MigrationCommands::Pause { uid } = cli.cmd {
            assert_eq!(uid, "mig-1");
        } else {
            panic!("Expected Pause command");
        }

        // Test resume command
        let cli = TestCli::parse_from(["test", "resume", "mig-1"]);
        if let MigrationCommands::Resume { uid } = cli.cmd {
            assert_eq!(uid, "mig-1");
        } else {
            panic!("Expected Resume command");
        }

        // Test cancel command
        let cli = TestCli::parse_from(["test", "cancel", "mig-1", "--force"]);
        if let MigrationCommands::Cancel { uid, force } = cli.cmd {
            assert_eq!(uid, "mig-1");
            assert!(force);
        } else {
            panic!("Expected Cancel command");
        }

        // Test export command
        let cli = TestCli::parse_from(["test", "export", "2"]);
        if let MigrationCommands::Export { bdb_uid } = cli.cmd {
            assert_eq!(bdb_uid, 2);
        } else {
            panic!("Expected Export command");
        }

        // Test import command with first-class params
        let cli = TestCli::parse_from([
            "test",
            "import",
            "3",
            "--source-uri",
            "redis://external:6379",
            "--flush-before",
        ]);
        if let MigrationCommands::Import {
            bdb_uid,
            source_uri,
            flush_before,
            ..
        } = cli.cmd
        {
            assert_eq!(bdb_uid, 3);
            assert_eq!(source_uri, Some("redis://external:6379".to_string()));
            assert!(flush_before);
        } else {
            panic!("Expected Import command");
        }
    }
}