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
use crate::error::RedisCtlError;
use anyhow::Context;
use clap::Subcommand;
use redis_enterprise::ActionHandler;
use crate::cli::OutputFormat;
use crate::connection::ConnectionManager;
use crate::error::Result as CliResult;
#[derive(Debug, Clone, Subcommand)]
pub enum ActionCommands {
/// List all actions (tasks) in the cluster
List {
/// Filter by action status (running, completed, failed)
#[arg(long)]
status: Option<String>,
/// Filter by action type
#[arg(long)]
action_type: Option<String>,
/// Use v2 API endpoint (default: v1)
#[arg(long)]
v2: bool,
},
/// Get details of a specific action by UID
Get {
/// Action UID
uid: String,
/// Use v2 API endpoint (default: v1)
#[arg(long)]
v2: bool,
},
/// Get the status of a specific action
Status {
/// Action UID
uid: String,
/// Use v2 API endpoint (default: v1)
#[arg(long)]
v2: bool,
},
/// Cancel a running action
Cancel {
/// Action UID
uid: String,
},
/// List actions for a specific database
#[command(name = "list-for-bdb")]
ListForBdb {
/// Database UID
bdb_uid: u32,
},
}
impl ActionCommands {
#[allow(dead_code)]
pub async fn execute(
&self,
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 = ActionHandler::new(client);
match self {
ActionCommands::List {
status,
action_type,
v2,
} => {
let actions = if *v2 {
handler
.list_v2()
.await
.context("Failed to list actions (v2)")?
} else {
handler.list().await.map_err(RedisCtlError::from)?
};
// Convert to JSON Value for filtering and output
let mut response = serde_json::to_value(&actions)?;
// Apply filters if provided
if (status.is_some() || action_type.is_some())
&& let Some(actions) = response.as_array_mut()
{
actions.retain(|action| {
let mut keep = true;
if let Some(status_filter) = status {
if let Some(action_status) =
action.get("status").and_then(|s| s.as_str())
{
keep = keep && action_status.eq_ignore_ascii_case(status_filter);
} else {
keep = false;
}
}
if let Some(type_filter) = action_type {
if let Some(action_type) = action.get("type").and_then(|t| t.as_str()) {
keep = keep && action_type.eq_ignore_ascii_case(type_filter);
} else {
keep = false;
}
}
keep
});
}
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)?;
}
ActionCommands::Get { uid, v2 } => {
let action = if *v2 {
handler
.get_v2(uid)
.await
.context(format!("Failed to get action {} (v2)", uid))?
} else {
handler
.get(uid)
.await
.context(format!("Failed to get action {}", uid))?
};
// Convert to JSON Value
let response = serde_json::to_value(&action)?;
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)?;
}
ActionCommands::Status { uid, v2 } => {
let action = if *v2 {
handler
.get_v2(uid)
.await
.context(format!("Failed to get action status {} (v2)", uid))?
} else {
handler
.get(uid)
.await
.context(format!("Failed to get action status {}", uid))?
};
// Extract just the status information
let response = serde_json::to_value(&action)?;
let status_info = if let Some(obj) = response.as_object() {
let mut status = serde_json::Map::new();
if let Some(v) = obj.get("uid") {
status.insert("uid".to_string(), v.clone());
}
if let Some(v) = obj.get("status") {
status.insert("status".to_string(), v.clone());
}
if let Some(v) = obj.get("progress") {
status.insert("progress".to_string(), v.clone());
}
if let Some(v) = obj.get("error") {
status.insert("error".to_string(), v.clone());
}
if let Some(v) = obj.get("type") {
status.insert("type".to_string(), v.clone());
}
if let Some(v) = obj.get("description") {
status.insert("description".to_string(), v.clone());
}
serde_json::Value::Object(status)
} else {
response
};
let output_data = if let Some(q) = query {
super::utils::apply_jmespath(&status_info, q)?
} else {
status_info
};
super::utils::print_formatted_output(output_data, output_format)?;
}
ActionCommands::Cancel { uid } => {
handler
.cancel(uid)
.await
.context(format!("Failed to cancel action {}", uid))?;
// Create success response
let response = serde_json::json!({
"status": "success",
"message": format!("Action '{}' cancelled successfully", 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)?;
}
ActionCommands::ListForBdb { bdb_uid } => {
let actions = handler
.list_for_bdb(*bdb_uid)
.await
.context(format!("Failed to list actions for database {}", bdb_uid))?;
// Convert to JSON Value
let response = serde_json::to_value(&actions)?;
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(())
}
}
#[allow(dead_code)]
pub async fn handle_action_command(
conn_mgr: &ConnectionManager,
profile_name: Option<&str>,
action_cmd: ActionCommands,
output_format: OutputFormat,
query: Option<&str>,
) -> CliResult<()> {
action_cmd
.execute(conn_mgr, profile_name, output_format, query)
.await
}