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
//! Cost report command implementations
//!
//! Handles generating and downloading cost reports in FOCUS format.

#![allow(dead_code)] // Functions used from main.rs binary

use crate::cli::{CloudCostReportCommands, OutputFormat};
use crate::commands::cloud::async_utils::{AsyncOperationArgs, handle_async_response};
use crate::connection::ConnectionManager;
use crate::error::{RedisCtlError, Result as CliResult};
use anyhow::Context;
use indicatif::{ProgressBar, ProgressStyle};
use redis_cloud::cost_report::{CostReportCreateRequest, CostReportFormat, SubscriptionType, Tag};
use serde_json::{Value, json};
use std::io::Write;
use std::time::{Duration, Instant};
use tokio::time::sleep;

/// Handle cost report commands
pub async fn handle_cost_report_command(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    command: CloudCostReportCommands,
    output_format: OutputFormat,
) -> CliResult<()> {
    match command {
        CloudCostReportCommands::Generate {
            start_date,
            end_date,
            format,
            subscription_ids,
            database_ids,
            subscription_type,
            regions,
            tags,
            async_ops,
        } => {
            generate_cost_report(
                conn_mgr,
                profile_name,
                start_date,
                end_date,
                format,
                subscription_ids,
                database_ids,
                subscription_type,
                regions,
                tags,
                async_ops,
                output_format,
            )
            .await
        }
        CloudCostReportCommands::Download {
            cost_report_id,
            file,
        } => {
            download_cost_report(conn_mgr, profile_name, cost_report_id, file, output_format).await
        }
        CloudCostReportCommands::Export {
            start_date,
            end_date,
            format,
            file,
            subscription_ids,
            database_ids,
            subscription_type,
            regions,
            tags,
            timeout,
        } => {
            export_cost_report(
                conn_mgr,
                profile_name,
                start_date,
                end_date,
                format,
                file,
                subscription_ids,
                database_ids,
                subscription_type,
                regions,
                tags,
                timeout,
                output_format,
            )
            .await
        }
    }
}

/// Generate a cost report
#[allow(clippy::too_many_arguments)]
async fn generate_cost_report(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    start_date: String,
    end_date: String,
    format: String,
    subscription_ids: Vec<i32>,
    database_ids: Vec<i32>,
    subscription_type: Option<String>,
    regions: Vec<String>,
    tags: Vec<String>,
    async_ops: AsyncOperationArgs,
    output_format: OutputFormat,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Build the request
    let mut request = CostReportCreateRequest::new(&start_date, &end_date);

    // Set format
    request.format = Some(match format.as_str() {
        "json" => CostReportFormat::Json,
        _ => CostReportFormat::Csv,
    });

    // Set subscription IDs if provided
    if !subscription_ids.is_empty() {
        request.subscription_ids = Some(subscription_ids);
    }

    // Set database IDs if provided
    if !database_ids.is_empty() {
        request.database_ids = Some(database_ids);
    }

    // Set subscription type if provided
    if let Some(sub_type) = subscription_type {
        request.subscription_type = Some(match sub_type.as_str() {
            "essentials" => SubscriptionType::Essentials,
            _ => SubscriptionType::Pro,
        });
    }

    // Set regions if provided
    if !regions.is_empty() {
        request.regions = Some(regions);
    }

    // Parse and set tags if provided
    if !tags.is_empty() {
        let parsed_tags: Vec<Tag> = tags
            .iter()
            .filter_map(|t| {
                let parts: Vec<&str> = t.splitn(2, ':').collect();
                if parts.len() == 2 {
                    Some(Tag::new(parts[0], parts[1]))
                } else {
                    eprintln!("Warning: Invalid tag format '{}', expected 'key:value'", t);
                    None
                }
            })
            .collect();
        if !parsed_tags.is_empty() {
            request.tags = Some(parsed_tags);
        }
    }

    // Convert to JSON for the raw API call
    let body = serde_json::to_value(&request).context("Failed to serialize request")?;

    // Make the API call
    let response = client
        .post_raw("/cost-report", body)
        .await
        .context("Failed to generate cost report")?;

    // Handle async response
    handle_async_response(
        conn_mgr,
        profile_name,
        response,
        &async_ops,
        output_format,
        None,
        "Cost report generation",
    )
    .await
}

/// Download a generated cost report
async fn download_cost_report(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    cost_report_id: String,
    output: Option<String>,
    output_format: OutputFormat,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    let bytes = client
        .get_bytes(&format!("/cost-report/{}", cost_report_id))
        .await?;

    match output {
        Some(path) => {
            // Write to file
            std::fs::write(&path, &bytes)
                .with_context(|| format!("Failed to write cost report to '{}'", path))?;

            match output_format {
                OutputFormat::Json => {
                    let result = json!({
                        "success": true,
                        "cost_report_id": cost_report_id,
                        "output_file": path,
                        "bytes_written": bytes.len(),
                    });
                    println!("{}", serde_json::to_string_pretty(&result)?);
                }
                _ => {
                    println!(
                        "Cost report downloaded successfully to '{}' ({} bytes)",
                        path,
                        bytes.len()
                    );
                }
            }
        }
        None => {
            // Write to stdout
            match output_format {
                OutputFormat::Json => {
                    // For JSON output format, try to parse the content as JSON
                    // If it's CSV, wrap it in a JSON structure
                    if let Ok(json_content) = serde_json::from_slice::<serde_json::Value>(&bytes) {
                        println!("{}", serde_json::to_string_pretty(&json_content)?);
                    } else {
                        // It's probably CSV, wrap in JSON
                        let content = String::from_utf8_lossy(&bytes);
                        let result = json!({
                            "cost_report_id": cost_report_id,
                            "format": "csv",
                            "content": content,
                        });
                        println!("{}", serde_json::to_string_pretty(&result)?);
                    }
                }
                _ => {
                    // Write raw content to stdout
                    std::io::stdout()
                        .write_all(&bytes)
                        .context("Failed to write cost report to stdout")?;
                }
            }
        }
    }

    Ok(())
}

/// Export a cost report (generate + wait + download in one step)
#[allow(clippy::too_many_arguments)]
async fn export_cost_report(
    conn_mgr: &ConnectionManager,
    profile_name: Option<&str>,
    start_date: String,
    end_date: String,
    format: String,
    file: Option<String>,
    subscription_ids: Vec<i32>,
    database_ids: Vec<i32>,
    subscription_type: Option<String>,
    regions: Vec<String>,
    tags: Vec<String>,
    timeout: u64,
    output_format: OutputFormat,
) -> CliResult<()> {
    let client = conn_mgr.create_cloud_client(profile_name).await?;

    // Build the request
    let mut request = CostReportCreateRequest::new(&start_date, &end_date);

    // Set format
    request.format = Some(match format.as_str() {
        "json" => CostReportFormat::Json,
        _ => CostReportFormat::Csv,
    });

    // Set subscription IDs if provided
    if !subscription_ids.is_empty() {
        request.subscription_ids = Some(subscription_ids);
    }

    // Set database IDs if provided
    if !database_ids.is_empty() {
        request.database_ids = Some(database_ids);
    }

    // Set subscription type if provided
    if let Some(sub_type) = subscription_type {
        request.subscription_type = Some(match sub_type.as_str() {
            "essentials" => SubscriptionType::Essentials,
            _ => SubscriptionType::Pro,
        });
    }

    // Set regions if provided
    if !regions.is_empty() {
        request.regions = Some(regions);
    }

    // Parse and set tags if provided
    if !tags.is_empty() {
        let parsed_tags: Vec<Tag> = tags
            .iter()
            .filter_map(|t| {
                let parts: Vec<&str> = t.splitn(2, ':').collect();
                if parts.len() == 2 {
                    Some(Tag::new(parts[0], parts[1]))
                } else {
                    eprintln!("Warning: Invalid tag format '{}', expected 'key:value'", t);
                    None
                }
            })
            .collect();
        if !parsed_tags.is_empty() {
            request.tags = Some(parsed_tags);
        }
    }

    // Convert to JSON for the raw API call
    let body = serde_json::to_value(&request).context("Failed to serialize request")?;

    // Create progress bar
    let pb = ProgressBar::new_spinner();
    pb.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} {msg} [{elapsed_precise}]")
            .unwrap(),
    );
    pb.set_message("Generating cost report...");

    // Make the API call to generate the report
    let response = client
        .post_raw("/cost-report", body)
        .await
        .context("Failed to generate cost report")?;

    // Extract task ID
    let task_id = response
        .get("taskId")
        .and_then(|v| v.as_str())
        .ok_or_else(|| RedisCtlError::InvalidInput {
            message: "No taskId in response".to_string(),
        })?;

    pb.set_message(format!("Waiting for task {}...", task_id));

    // Wait for task to complete and get the result
    let task_result = wait_for_task_result(&client, task_id, timeout, &pb).await?;

    // Extract cost report ID from task response
    let cost_report_id = task_result
        .get("response")
        .and_then(|r| r.get("resource"))
        .and_then(|r| r.get("costReportId"))
        .and_then(|v| v.as_str())
        .ok_or_else(|| RedisCtlError::InvalidInput {
            message: "No costReportId in task response".to_string(),
        })?;

    pb.set_message(format!("Downloading report {}...", cost_report_id));

    // Download the report
    let bytes = client
        .get_bytes(&format!("/cost-report/{}", cost_report_id))
        .await?;

    pb.finish_and_clear();

    // Write output
    match file {
        Some(path) => {
            std::fs::write(&path, &bytes)
                .with_context(|| format!("Failed to write cost report to '{}'", path))?;

            match output_format {
                OutputFormat::Json => {
                    let result = json!({
                        "success": true,
                        "cost_report_id": cost_report_id,
                        "output_file": path,
                        "bytes_written": bytes.len(),
                        "date_range": {
                            "start": start_date,
                            "end": end_date
                        }
                    });
                    println!("{}", serde_json::to_string_pretty(&result)?);
                }
                _ => {
                    println!("Cost report exported to '{}' ({} bytes)", path, bytes.len());
                }
            }
        }
        None => {
            // Write raw content to stdout
            std::io::stdout()
                .write_all(&bytes)
                .context("Failed to write cost report to stdout")?;
        }
    }

    Ok(())
}

/// Wait for a task to complete and return the final task state
async fn wait_for_task_result(
    client: &redis_cloud::CloudClient,
    task_id: &str,
    timeout_secs: u64,
    pb: &ProgressBar,
) -> CliResult<Value> {
    let start = Instant::now();
    let timeout = Duration::from_secs(timeout_secs);
    let interval = Duration::from_secs(5);

    loop {
        let task = client
            .get_raw(&format!("/tasks/{}", task_id))
            .await
            .map_err(|e| RedisCtlError::ApiError {
                message: format!("Failed to fetch task {}: {}", task_id, e),
            })?;

        let state = task
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown");

        pb.set_message(format!("Task {}: {}", task_id, state));

        // Check for terminal states
        match state.to_lowercase().as_str() {
            "completed" | "processing-completed" | "succeeded" | "success" => {
                return Ok(task);
            }
            "failed" | "error" | "processing-error" => {
                let description = task
                    .get("description")
                    .and_then(|v| v.as_str())
                    .unwrap_or("Unknown error");
                return Err(RedisCtlError::ApiError {
                    message: format!("Task {} failed: {}", task_id, description),
                });
            }
            "cancelled" => {
                return Err(RedisCtlError::ApiError {
                    message: format!("Task {} was cancelled", task_id),
                });
            }
            _ => {}
        }

        // Check timeout
        if start.elapsed() > timeout {
            return Err(RedisCtlError::Timeout {
                message: format!(
                    "Task {} did not complete within {} seconds",
                    task_id, timeout_secs
                ),
            });
        }

        sleep(interval).await;
    }
}