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
//! Transit Gateway (TGW) command implementations

#![allow(dead_code)]

use super::ConnectivityOperationParams;
use crate::cli::{OutputFormat, TgwCommands};
use crate::commands::cloud::async_utils::handle_async_response;
use crate::commands::cloud::utils::{
    confirm_action, handle_output, print_formatted_output, read_file_input,
};
use crate::connection::ConnectionManager;
use crate::error::Result as CliResult;
use anyhow::Context;
use redis_cloud::CloudClient;
use redis_cloud::connectivity::transit_gateway::{TgwAttachmentRequest, TransitGatewayHandler};

/// Parameters for TGW attachment create/update operations
#[derive(Debug, Default)]
pub struct TgwAttachmentParams {
    pub aws_account_id: Option<String>,
    pub tgw_id: Option<String>,
    pub cidrs: Vec<String>,
    pub data: Option<String>,
}

/// Handle TGW commands
pub async fn handle_tgw_command(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    command: &TgwCommands,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let client = conn_mgr
        .create_cloud_client(profile_name)
        .await
        .context("Failed to create Cloud client")?;

    match command {
        // Standard TGW operations
        TgwCommands::AttachmentsList { subscription_id } => {
            list_attachments(&client, *subscription_id, output_format, query).await
        }
        TgwCommands::AttachmentCreate {
            subscription_id,
            aws_account_id,
            tgw_id,
            cidrs,
            data,
            async_ops,
        } => {
            let params = ConnectivityOperationParams {
                conn_mgr,
                profile_name,
                client: &client,
                subscription_id: *subscription_id,
                async_ops,
                output_format,
                query,
            };
            let attachment_params = TgwAttachmentParams {
                aws_account_id: aws_account_id.clone(),
                tgw_id: tgw_id.clone(),
                cidrs: cidrs.clone(),
                data: data.clone(),
            };
            create_attachment(&params, &attachment_params).await
        }
        TgwCommands::AttachmentCreateWithId {
            subscription_id,
            tgw_id,
            async_ops,
        } => {
            let params = ConnectivityOperationParams {
                conn_mgr,
                profile_name,
                client: &client,
                subscription_id: *subscription_id,
                async_ops,
                output_format,
                query,
            };
            create_attachment_with_id(&params, tgw_id).await
        }
        TgwCommands::AttachmentUpdate {
            subscription_id,
            attachment_id,
            cidrs,
            data,
            async_ops,
        } => {
            let params = ConnectivityOperationParams {
                conn_mgr,
                profile_name,
                client: &client,
                subscription_id: *subscription_id,
                async_ops,
                output_format,
                query,
            };
            let attachment_params = TgwAttachmentParams {
                aws_account_id: None,
                tgw_id: None,
                cidrs: cidrs.clone(),
                data: data.clone(),
            };
            update_attachment_cidrs(&params, attachment_id, &attachment_params).await
        }
        TgwCommands::AttachmentDelete {
            subscription_id,
            attachment_id,
            yes,
            async_ops,
        } => {
            let params = ConnectivityOperationParams {
                conn_mgr,
                profile_name,
                client: &client,
                subscription_id: *subscription_id,
                async_ops,
                output_format,
                query,
            };
            delete_attachment(&params, attachment_id, *yes).await
        }
        TgwCommands::InvitationsList { subscription_id } => {
            list_invitations(&client, *subscription_id, output_format, query).await
        }
        TgwCommands::InvitationAccept {
            subscription_id,
            invitation_id,
        } => {
            accept_invitation(
                &client,
                *subscription_id,
                invitation_id,
                output_format,
                query,
            )
            .await
        }
        TgwCommands::InvitationReject {
            subscription_id,
            invitation_id,
        } => {
            reject_invitation(
                &client,
                *subscription_id,
                invitation_id,
                output_format,
                query,
            )
            .await
        }

        // Active-Active TGW operations
        TgwCommands::AaAttachmentsList { subscription_id } => {
            list_attachments_aa(&client, *subscription_id, output_format, query).await
        }
        TgwCommands::AaAttachmentCreate {
            subscription_id,
            region_id,
            aws_account_id,
            tgw_id,
            cidrs,
            data,
            async_ops,
        } => {
            let params = ConnectivityOperationParams {
                conn_mgr,
                profile_name,
                client: &client,
                subscription_id: *subscription_id,
                async_ops,
                output_format,
                query,
            };
            let attachment_params = TgwAttachmentParams {
                aws_account_id: aws_account_id.clone(),
                tgw_id: tgw_id.clone(),
                cidrs: cidrs.clone(),
                data: data.clone(),
            };
            create_attachment_aa(&params, *region_id, &attachment_params).await
        }
        TgwCommands::AaAttachmentUpdate {
            subscription_id,
            region_id,
            attachment_id,
            cidrs,
            data,
            async_ops,
        } => {
            let params = ConnectivityOperationParams {
                conn_mgr,
                profile_name,
                client: &client,
                subscription_id: *subscription_id,
                async_ops,
                output_format,
                query,
            };
            let attachment_params = TgwAttachmentParams {
                aws_account_id: None,
                tgw_id: None,
                cidrs: cidrs.clone(),
                data: data.clone(),
            };
            update_attachment_cidrs_aa(&params, *region_id, attachment_id, &attachment_params).await
        }
        TgwCommands::AaAttachmentDelete {
            subscription_id,
            region_id,
            attachment_id,
            yes,
            async_ops,
        } => {
            let params = ConnectivityOperationParams {
                conn_mgr,
                profile_name,
                client: &client,
                subscription_id: *subscription_id,
                async_ops,
                output_format,
                query,
            };
            delete_attachment_aa(&params, *region_id, attachment_id, *yes).await
        }
        TgwCommands::AaInvitationsList { subscription_id } => {
            list_invitations_aa(&client, *subscription_id, output_format, query).await
        }
        TgwCommands::AaInvitationAccept {
            subscription_id,
            region_id,
            invitation_id,
        } => {
            accept_invitation_aa(
                &client,
                *subscription_id,
                *region_id,
                invitation_id,
                output_format,
                query,
            )
            .await
        }
        TgwCommands::AaInvitationReject {
            subscription_id,
            region_id,
            invitation_id,
        } => {
            reject_invitation_aa(
                &client,
                *subscription_id,
                *region_id,
                invitation_id,
                output_format,
                query,
            )
            .await
        }
    }
}

// ============================================================================
// Standard TGW Operations
// ============================================================================

async fn list_attachments(
    client: &CloudClient,
    subscription_id: i32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .get_attachments(subscription_id)
        .await
        .context("Failed to get TGW attachments")?;

    let json_response = serde_json::to_value(response).context("Failed to serialize response")?;
    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

/// Build TGW attachment request from parameters
fn build_tgw_attachment_request(
    attachment_params: &TgwAttachmentParams,
) -> CliResult<TgwAttachmentRequest> {
    // If --data is provided, use it as the base (escape hatch)
    if let Some(data) = &attachment_params.data {
        let json_string = read_file_input(data)?;
        let request: TgwAttachmentRequest =
            serde_json::from_str(&json_string).context("Invalid TGW attachment configuration")?;
        return Ok(request);
    }

    // Build from first-class parameters
    let cidrs = if attachment_params.cidrs.is_empty() {
        None
    } else {
        Some(attachment_params.cidrs.clone())
    };

    Ok(TgwAttachmentRequest {
        aws_account_id: attachment_params.aws_account_id.clone(),
        tgw_id: attachment_params.tgw_id.clone(),
        cidrs,
    })
}

async fn create_attachment(
    params: &ConnectivityOperationParams<'_>,
    attachment_params: &TgwAttachmentParams,
) -> CliResult<()> {
    let request = build_tgw_attachment_request(attachment_params)?;

    let handler = TransitGatewayHandler::new(params.client.clone());
    let response = handler
        .create_attachment(params.subscription_id, &request)
        .await
        .context("Failed to create TGW attachment")?;

    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;

    handle_async_response(
        params.conn_mgr,
        params.profile_name,
        json_response,
        params.async_ops,
        params.output_format,
        params.query,
        "TGW attachment created successfully",
    )
    .await
}

async fn create_attachment_with_id(
    params: &ConnectivityOperationParams<'_>,
    tgw_id: &str,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(params.client.clone());
    let response = handler
        .create_attachment_with_id(params.subscription_id, tgw_id)
        .await
        .context("Failed to create TGW attachment")?;

    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;

    handle_async_response(
        params.conn_mgr,
        params.profile_name,
        json_response,
        params.async_ops,
        params.output_format,
        params.query,
        "TGW attachment created successfully",
    )
    .await
}

async fn update_attachment_cidrs(
    params: &ConnectivityOperationParams<'_>,
    attachment_id: &str,
    attachment_params: &TgwAttachmentParams,
) -> CliResult<()> {
    let request = build_tgw_attachment_request(attachment_params)?;

    let handler = TransitGatewayHandler::new(params.client.clone());
    let response = handler
        .update_attachment_cidrs(params.subscription_id, attachment_id.to_string(), &request)
        .await
        .context("Failed to update TGW attachment CIDRs")?;

    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;

    handle_async_response(
        params.conn_mgr,
        params.profile_name,
        json_response,
        params.async_ops,
        params.output_format,
        params.query,
        "TGW attachment updated successfully",
    )
    .await
}

async fn delete_attachment(
    params: &ConnectivityOperationParams<'_>,
    attachment_id: &str,
    yes: bool,
) -> CliResult<()> {
    if !yes {
        let prompt = format!(
            "Delete TGW attachment {} for subscription {}?",
            attachment_id, params.subscription_id
        );
        if !confirm_action(&prompt)? {
            eprintln!("Operation cancelled");
            return Ok(());
        }
    }

    let handler = TransitGatewayHandler::new(params.client.clone());
    handler
        .delete_attachment(params.subscription_id, attachment_id.to_string())
        .await
        .context("Failed to delete TGW attachment")?;

    eprintln!("TGW attachment deleted successfully");
    Ok(())
}

async fn list_invitations(
    client: &CloudClient,
    subscription_id: i32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .get_shared_invitations(subscription_id)
        .await
        .context("Failed to get TGW invitations")?;

    let json_response = serde_json::to_value(response).context("Failed to serialize response")?;
    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

async fn accept_invitation(
    client: &CloudClient,
    subscription_id: i32,
    invitation_id: &str,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .accept_resource_share(subscription_id, invitation_id.to_string())
        .await
        .context("Failed to accept TGW invitation")?;

    // Convert response to JSON and check for task ID
    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;
    if let Some(task_id) = json_response.get("taskId").and_then(|v| v.as_str()) {
        eprintln!("TGW invitation acceptance initiated. Task ID: {}", task_id);
        eprintln!(
            "Use 'redisctl cloud task wait {}' to monitor progress",
            task_id
        );
    }

    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

async fn reject_invitation(
    client: &CloudClient,
    subscription_id: i32,
    invitation_id: &str,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .reject_resource_share(subscription_id, invitation_id.to_string())
        .await
        .context("Failed to reject TGW invitation")?;

    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;
    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

// ============================================================================
// Active-Active TGW Operations
// ============================================================================

async fn list_attachments_aa(
    client: &CloudClient,
    subscription_id: i32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .get_attachments_active_active(subscription_id)
        .await
        .context("Failed to get Active-Active TGW attachments")?;

    let json_response = serde_json::to_value(response).context("Failed to serialize response")?;
    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

async fn create_attachment_aa(
    params: &ConnectivityOperationParams<'_>,
    region_id: i32,
    attachment_params: &TgwAttachmentParams,
) -> CliResult<()> {
    let request = build_tgw_attachment_request(attachment_params)?;

    let handler = TransitGatewayHandler::new(params.client.clone());
    let response = handler
        .create_attachment_active_active(params.subscription_id, region_id, &request)
        .await
        .context("Failed to create Active-Active TGW attachment")?;

    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;

    handle_async_response(
        params.conn_mgr,
        params.profile_name,
        json_response,
        params.async_ops,
        params.output_format,
        params.query,
        "Active-Active TGW attachment created successfully",
    )
    .await
}

async fn update_attachment_cidrs_aa(
    params: &ConnectivityOperationParams<'_>,
    region_id: i32,
    attachment_id: &str,
    attachment_params: &TgwAttachmentParams,
) -> CliResult<()> {
    let request = build_tgw_attachment_request(attachment_params)?;

    let handler = TransitGatewayHandler::new(params.client.clone());
    let response = handler
        .update_attachment_cidrs_active_active(
            params.subscription_id,
            region_id,
            attachment_id.to_string(),
            &request,
        )
        .await
        .context("Failed to update Active-Active TGW attachment CIDRs")?;

    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;

    handle_async_response(
        params.conn_mgr,
        params.profile_name,
        json_response,
        params.async_ops,
        params.output_format,
        params.query,
        "Active-Active TGW attachment updated successfully",
    )
    .await
}

async fn delete_attachment_aa(
    params: &ConnectivityOperationParams<'_>,
    region_id: i32,
    attachment_id: &str,
    yes: bool,
) -> CliResult<()> {
    if !yes {
        let prompt = format!(
            "Delete Active-Active TGW attachment {} in region {} for subscription {}?",
            attachment_id, region_id, params.subscription_id
        );
        if !confirm_action(&prompt)? {
            eprintln!("Operation cancelled");
            return Ok(());
        }
    }

    let handler = TransitGatewayHandler::new(params.client.clone());
    handler
        .delete_attachment_active_active(
            params.subscription_id,
            region_id,
            attachment_id.to_string(),
        )
        .await
        .context("Failed to delete Active-Active TGW attachment")?;

    eprintln!("Active-Active TGW attachment deleted successfully");
    Ok(())
}

async fn list_invitations_aa(
    client: &CloudClient,
    subscription_id: i32,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .get_shared_invitations_active_active(subscription_id)
        .await
        .context("Failed to get Active-Active TGW invitations")?;

    let json_response = serde_json::to_value(response).context("Failed to serialize response")?;
    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

async fn accept_invitation_aa(
    client: &CloudClient,
    subscription_id: i32,
    region_id: i32,
    invitation_id: &str,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .accept_resource_share_active_active(subscription_id, region_id, invitation_id.to_string())
        .await
        .context("Failed to accept Active-Active TGW invitation")?;

    // Convert response to JSON and check for task ID
    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;
    if let Some(task_id) = json_response.get("taskId").and_then(|v| v.as_str()) {
        eprintln!(
            "Active-Active TGW invitation acceptance initiated. Task ID: {}",
            task_id
        );
        eprintln!(
            "Use 'redisctl cloud task wait {}' to monitor progress",
            task_id
        );
    }

    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}

async fn reject_invitation_aa(
    client: &CloudClient,
    subscription_id: i32,
    region_id: i32,
    invitation_id: &str,
    output_format: OutputFormat,
    query: Option<&str>,
) -> CliResult<()> {
    let handler = TransitGatewayHandler::new(client.clone());
    let response = handler
        .reject_resource_share_active_active(subscription_id, region_id, invitation_id.to_string())
        .await
        .context("Failed to reject Active-Active TGW invitation")?;

    let json_response = serde_json::to_value(&response).context("Failed to serialize response")?;
    let data = handle_output(json_response, output_format, query)?;
    print_formatted_output(data, output_format)?;
    Ok(())
}