|
| 1 | +use serde::Serialize; |
| 2 | +use serde_json::Value; |
| 3 | + |
| 4 | +use crate::api::{ApiClient, ApiError}; |
| 5 | +use crate::output::{ |
| 6 | + OutputFormat, extract_pagination, format_option, print_json, print_key_value, print_message, |
| 7 | + print_pagination, print_table, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug, Serialize)] |
| 11 | +struct PaginationQuery { |
| 12 | + page: u32, |
| 13 | + per_page: u32, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Serialize)] |
| 17 | +struct CreateBackupRequest { |
| 18 | + r#type: String, |
| 19 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 20 | + description: Option<String>, |
| 21 | +} |
| 22 | + |
| 23 | +pub fn list( |
| 24 | + client: &ApiClient, |
| 25 | + site_id: &str, |
| 26 | + page: u32, |
| 27 | + per_page: u32, |
| 28 | + format: OutputFormat, |
| 29 | +) -> Result<(), ApiError> { |
| 30 | + let query = PaginationQuery { page, per_page }; |
| 31 | + let response: Value = |
| 32 | + client.get_with_query(&format!("/api/v1/vector/sites/{}/backups", site_id), &query)?; |
| 33 | + |
| 34 | + if format == OutputFormat::Json { |
| 35 | + print_json(&response); |
| 36 | + return Ok(()); |
| 37 | + } |
| 38 | + |
| 39 | + let backups = response["data"] |
| 40 | + .as_array() |
| 41 | + .ok_or_else(|| ApiError::Other("Invalid response format".to_string()))?; |
| 42 | + |
| 43 | + if backups.is_empty() { |
| 44 | + print_message("No backups found."); |
| 45 | + return Ok(()); |
| 46 | + } |
| 47 | + |
| 48 | + let rows: Vec<Vec<String>> = backups |
| 49 | + .iter() |
| 50 | + .map(|b| { |
| 51 | + vec![ |
| 52 | + b["id"].as_str().unwrap_or("-").to_string(), |
| 53 | + b["type"].as_str().unwrap_or("-").to_string(), |
| 54 | + b["status"].as_str().unwrap_or("-").to_string(), |
| 55 | + format_option(&b["description"].as_str().map(String::from)), |
| 56 | + format_option(&b["created_at"].as_str().map(String::from)), |
| 57 | + ] |
| 58 | + }) |
| 59 | + .collect(); |
| 60 | + |
| 61 | + print_table(vec!["ID", "Type", "Status", "Description", "Created"], rows); |
| 62 | + |
| 63 | + if let Some((current, last, total)) = extract_pagination(&response) { |
| 64 | + print_pagination(current, last, total); |
| 65 | + } |
| 66 | + |
| 67 | + Ok(()) |
| 68 | +} |
| 69 | + |
| 70 | +pub fn show( |
| 71 | + client: &ApiClient, |
| 72 | + site_id: &str, |
| 73 | + backup_id: &str, |
| 74 | + format: OutputFormat, |
| 75 | +) -> Result<(), ApiError> { |
| 76 | + let response: Value = client.get(&format!( |
| 77 | + "/api/v1/vector/sites/{}/backups/{}", |
| 78 | + site_id, backup_id |
| 79 | + ))?; |
| 80 | + |
| 81 | + if format == OutputFormat::Json { |
| 82 | + print_json(&response); |
| 83 | + return Ok(()); |
| 84 | + } |
| 85 | + |
| 86 | + let backup = &response["data"]; |
| 87 | + |
| 88 | + print_key_value(vec![ |
| 89 | + ("ID", backup["id"].as_str().unwrap_or("-").to_string()), |
| 90 | + ("Type", backup["type"].as_str().unwrap_or("-").to_string()), |
| 91 | + ( |
| 92 | + "Status", |
| 93 | + backup["status"].as_str().unwrap_or("-").to_string(), |
| 94 | + ), |
| 95 | + ( |
| 96 | + "Description", |
| 97 | + format_option(&backup["description"].as_str().map(String::from)), |
| 98 | + ), |
| 99 | + ( |
| 100 | + "Snapshot ID", |
| 101 | + format_option(&backup["snapshot_id"].as_str().map(String::from)), |
| 102 | + ), |
| 103 | + ( |
| 104 | + "Started At", |
| 105 | + format_option(&backup["started_at"].as_str().map(String::from)), |
| 106 | + ), |
| 107 | + ( |
| 108 | + "Completed At", |
| 109 | + format_option(&backup["completed_at"].as_str().map(String::from)), |
| 110 | + ), |
| 111 | + ( |
| 112 | + "Created At", |
| 113 | + format_option(&backup["created_at"].as_str().map(String::from)), |
| 114 | + ), |
| 115 | + ( |
| 116 | + "Updated At", |
| 117 | + format_option(&backup["updated_at"].as_str().map(String::from)), |
| 118 | + ), |
| 119 | + ]); |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
| 123 | + |
| 124 | +pub fn create( |
| 125 | + client: &ApiClient, |
| 126 | + site_id: &str, |
| 127 | + description: Option<String>, |
| 128 | + format: OutputFormat, |
| 129 | +) -> Result<(), ApiError> { |
| 130 | + let body = CreateBackupRequest { |
| 131 | + r#type: "manual".to_string(), |
| 132 | + description, |
| 133 | + }; |
| 134 | + |
| 135 | + let response: Value = |
| 136 | + client.post(&format!("/api/v1/vector/sites/{}/backups", site_id), &body)?; |
| 137 | + |
| 138 | + if format == OutputFormat::Json { |
| 139 | + print_json(&response); |
| 140 | + return Ok(()); |
| 141 | + } |
| 142 | + |
| 143 | + let backup = &response["data"]; |
| 144 | + print_message(&format!( |
| 145 | + "Backup created: {} ({})", |
| 146 | + backup["id"].as_str().unwrap_or("-"), |
| 147 | + backup["status"].as_str().unwrap_or("-") |
| 148 | + )); |
| 149 | + |
| 150 | + print_key_value(vec![ |
| 151 | + ("ID", backup["id"].as_str().unwrap_or("-").to_string()), |
| 152 | + ("Type", backup["type"].as_str().unwrap_or("-").to_string()), |
| 153 | + ( |
| 154 | + "Status", |
| 155 | + backup["status"].as_str().unwrap_or("-").to_string(), |
| 156 | + ), |
| 157 | + ( |
| 158 | + "Description", |
| 159 | + format_option(&backup["description"].as_str().map(String::from)), |
| 160 | + ), |
| 161 | + ( |
| 162 | + "Created At", |
| 163 | + format_option(&backup["created_at"].as_str().map(String::from)), |
| 164 | + ), |
| 165 | + ]); |
| 166 | + |
| 167 | + Ok(()) |
| 168 | +} |
0 commit comments