Skip to content

Commit 9825e33

Browse files
committed
feat: convert core Cloud operations from raw to typed APIs
Converted the following Cloud operations to use typed handler methods: - Database operations (list, get, create, update, delete, backup, import) - Subscription operations (list, get, create, update, delete, pricing, CIDR) - User operations (list, get, create, update, delete) - Account operations (info, owner, users, payment methods) - ACL operations (list, get, create, update, delete) - Peering operations (list, get, delete - create kept as raw) - Task operations (list, get) Benefits: - Better type safety and IDE support - Consistent error handling through handler pattern - Easier maintenance and debugging - Dogfooding our own redis-cloud library Remaining: 72 raw API calls for advanced features (CRDB, metrics, logs, etc.)
1 parent f1124f0 commit 9825e33

File tree

1 file changed

+57
-111
lines changed

1 file changed

+57
-111
lines changed

crates/redisctl/src/commands/cloud.rs

Lines changed: 57 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub async fn handle_database_command(
132132
memory_limit,
133133
} => {
134134
let (subscription_id, database_id) = parse_database_id(&id)?;
135+
let handler = redis_cloud::CloudDatabaseHandler::new(client.clone());
135136
let mut update_data = serde_json::Map::new();
136137

137138
if let Some(name) = name {
@@ -144,56 +145,36 @@ pub async fn handle_database_command(
144145
);
145146
}
146147

147-
let database = client
148-
.put_raw(
149-
&format!(
150-
"/subscriptions/{}/databases/{}",
151-
subscription_id, database_id
152-
),
148+
let database = handler
149+
.update_raw(
150+
subscription_id,
151+
database_id,
153152
serde_json::Value::Object(update_data),
154153
)
155154
.await?;
156155
print_output(database, output_format, query)?;
157156
}
158157
DatabaseCommands::Delete { id, force: _ } => {
159158
let (subscription_id, database_id) = parse_database_id(&id)?;
160-
client
161-
.delete_raw(&format!(
162-
"/subscriptions/{}/databases/{}",
163-
subscription_id, database_id
164-
))
165-
.await?;
159+
let handler = redis_cloud::CloudDatabaseHandler::new(client.clone());
160+
handler.delete(subscription_id, database_id).await?;
166161
println!("Database {} deleted successfully", id);
167162
}
168163
DatabaseCommands::Backup { id } => {
169164
let (subscription_id, database_id) = parse_database_id(&id)?;
170-
let backup_data = serde_json::json!({
171-
"description": format!("Database backup for {}", id)
172-
});
173-
let task = client
174-
.post_raw(
175-
&format!(
176-
"/subscriptions/{}/databases/{}/backup",
177-
subscription_id, database_id
178-
),
179-
backup_data,
180-
)
181-
.await?;
165+
let handler = redis_cloud::CloudDatabaseHandler::new(client.clone());
166+
let task = handler.backup(subscription_id, database_id).await?;
182167
print_output(task, output_format, query)?;
183168
}
184169
DatabaseCommands::Import { id, url } => {
185170
let (subscription_id, database_id) = parse_database_id(&id)?;
171+
let handler = redis_cloud::CloudDatabaseHandler::new(client.clone());
186172
let import_data = serde_json::json!({
187-
"sourceUri": url
173+
"source_type": "ftp",
174+
"import_from_uri": [url]
188175
});
189-
let task = client
190-
.post_raw(
191-
&format!(
192-
"/subscriptions/{}/databases/{}/import",
193-
subscription_id, database_id
194-
),
195-
import_data,
196-
)
176+
let task = handler
177+
.import(subscription_id, database_id, import_data)
197178
.await?;
198179
print_output(task, output_format, query)?;
199180
}
@@ -244,27 +225,26 @@ pub async fn handle_subscription_command(
244225
provider,
245226
region,
246227
} => {
228+
let handler = redis_cloud::CloudSubscriptionHandler::new(client.clone());
247229
let create_data = serde_json::json!({
248230
"name": name,
249231
"cloudProvider": provider,
250232
"region": region
251233
});
252-
let subscription = client.post_raw("/subscriptions", create_data).await?;
234+
let subscription = handler.create_raw(create_data).await?;
253235
print_output(subscription, output_format, query)?;
254236
}
255237
SubscriptionCommands::Update { id, name } => {
238+
let handler = redis_cloud::CloudSubscriptionHandler::new(client.clone());
256239
let mut update_data = serde_json::Map::new();
257240
if let Some(name) = name {
258241
update_data.insert("name".to_string(), serde_json::Value::String(name));
259242
}
260243
if update_data.is_empty() {
261244
anyhow::bail!("No update fields provided");
262245
}
263-
let subscription = client
264-
.put_raw(
265-
&format!("/subscriptions/{}", id),
266-
serde_json::Value::Object(update_data),
267-
)
246+
let subscription = handler
247+
.update_raw(id.parse()?, serde_json::Value::Object(update_data))
268248
.await?;
269249
print_output(subscription, output_format, query)?;
270250
}
@@ -276,34 +256,33 @@ pub async fn handle_subscription_command(
276256
);
277257
return Ok(());
278258
}
279-
client.delete_raw(&format!("/subscriptions/{}", id)).await?;
259+
let handler = redis_cloud::CloudSubscriptionHandler::new(client.clone());
260+
handler.delete(id.parse()?).await?;
280261
println!("Subscription {} deleted successfully", id);
281262
}
282263
SubscriptionCommands::Pricing { id } => {
283-
let pricing = client
284-
.get_raw(&format!("/subscriptions/{}/pricing", id))
285-
.await?;
264+
let handler = redis_cloud::CloudSubscriptionHandler::new(client.clone());
265+
let pricing = handler.pricing(id.parse()?).await?;
286266
print_output(pricing, output_format, query)?;
287267
}
288268
SubscriptionCommands::Databases { id } => {
289-
let databases = client
290-
.get_raw(&format!("/subscriptions/{}/databases", id))
291-
.await?;
269+
let handler = redis_cloud::CloudDatabaseHandler::new(client.clone());
270+
let databases = handler.list(id.parse()?).await?;
292271
print_output(databases, output_format, query)?;
293272
}
294273
SubscriptionCommands::CidrList { id } => {
295-
let cidr = client
296-
.get_raw(&format!("/subscriptions/{}/cidr", id))
297-
.await?;
274+
let handler = redis_cloud::CloudSubscriptionHandler::new(client.clone());
275+
let cidr = handler.get_cidr_whitelist(id.parse()?).await?;
298276
print_output(cidr, output_format, query)?;
299277
}
300278
SubscriptionCommands::CidrUpdate { id, cidrs } => {
279+
let handler = redis_cloud::CloudSubscriptionHandler::new(client.clone());
301280
let cidr_list: Vec<&str> = cidrs.split(',').map(|s| s.trim()).collect();
302281
let update_data = serde_json::json!({
303282
"cidr": cidr_list
304283
});
305-
let cidr = client
306-
.put_raw(&format!("/subscriptions/{}/cidr", id), update_data)
284+
let cidr = handler
285+
.update_cidr_whitelist(id.parse()?, update_data)
307286
.await?;
308287
print_output(cidr, output_format, query)?;
309288
}
@@ -384,6 +363,7 @@ pub async fn handle_user_command(
384363
password,
385364
roles,
386365
} => {
366+
let handler = redis_cloud::CloudUsersHandler::new(client.clone());
387367
let mut create_data = serde_json::json!({
388368
"name": name
389369
});
@@ -400,14 +380,15 @@ pub async fn handle_user_command(
400380
);
401381
}
402382

403-
let user = client.post_raw("/users", create_data).await?;
383+
let user = handler.create(create_data).await?;
404384
print_output(user, output_format, query)?;
405385
}
406386
UserCommands::Update {
407387
id,
408388
email,
409389
password,
410390
} => {
391+
let handler = redis_cloud::CloudUsersHandler::new(client.clone());
411392
let mut update_data = serde_json::Map::new();
412393
if let Some(email) = email {
413394
update_data.insert("email".to_string(), serde_json::Value::String(email));
@@ -418,11 +399,8 @@ pub async fn handle_user_command(
418399
if update_data.is_empty() {
419400
anyhow::bail!("No update fields provided");
420401
}
421-
let user = client
422-
.put_raw(
423-
&format!("/users/{}", id),
424-
serde_json::Value::Object(update_data),
425-
)
402+
let user = handler
403+
.update(id.parse()?, serde_json::Value::Object(update_data))
426404
.await?;
427405
print_output(user, output_format, query)?;
428406
}
@@ -434,7 +412,8 @@ pub async fn handle_user_command(
434412
);
435413
return Ok(());
436414
}
437-
client.delete_raw(&format!("/users/{}", id)).await?;
415+
let handler = redis_cloud::CloudUsersHandler::new(client.clone());
416+
handler.delete(id.parse()?).await?;
438417
println!("User {} deleted successfully", id);
439418
}
440419
}
@@ -545,25 +524,17 @@ pub async fn handle_acl_command(
545524
subscription_id,
546525
database_id,
547526
} => {
548-
let acls = client
549-
.get_raw(&format!(
550-
"/subscriptions/{}/databases/{}/acls",
551-
subscription_id, database_id
552-
))
553-
.await?;
527+
let handler = redis_cloud::CloudAclHandler::new(client.clone());
528+
let acls = handler.list(subscription_id, database_id).await?;
554529
print_output(acls, output_format, query)?;
555530
}
556531
AclCommands::Show {
557532
subscription_id,
558533
database_id,
559534
acl_id,
560535
} => {
561-
let acl = client
562-
.get_raw(&format!(
563-
"/subscriptions/{}/databases/{}/acls/{}",
564-
subscription_id, database_id, acl_id
565-
))
566-
.await?;
536+
let handler = redis_cloud::CloudAclHandler::new(client.clone());
537+
let acl = handler.get(subscription_id, database_id, acl_id).await?;
567538
print_output(acl, output_format, query)?;
568539
}
569540
AclCommands::Create {
@@ -572,18 +543,13 @@ pub async fn handle_acl_command(
572543
name,
573544
rule,
574545
} => {
546+
let handler = redis_cloud::CloudAclHandler::new(client.clone());
575547
let create_data = serde_json::json!({
576548
"name": name,
577549
"aclRule": rule
578550
});
579-
let acl = client
580-
.post_raw(
581-
&format!(
582-
"/subscriptions/{}/databases/{}/acls",
583-
subscription_id, database_id
584-
),
585-
create_data,
586-
)
551+
let acl = handler
552+
.create(subscription_id, database_id, create_data)
587553
.await?;
588554
print_output(acl, output_format, query)?;
589555
}
@@ -593,17 +559,12 @@ pub async fn handle_acl_command(
593559
acl_id,
594560
rule,
595561
} => {
562+
let handler = redis_cloud::CloudAclHandler::new(client.clone());
596563
let update_data = serde_json::json!({
597564
"aclRule": rule
598565
});
599-
let acl = client
600-
.put_raw(
601-
&format!(
602-
"/subscriptions/{}/databases/{}/acls/{}",
603-
subscription_id, database_id, acl_id
604-
),
605-
update_data,
606-
)
566+
let acl = handler
567+
.update(subscription_id, database_id, acl_id, update_data)
607568
.await?;
608569
print_output(acl, output_format, query)?;
609570
}
@@ -620,12 +581,8 @@ pub async fn handle_acl_command(
620581
);
621582
return Ok(());
622583
}
623-
client
624-
.delete_raw(&format!(
625-
"/subscriptions/{}/databases/{}/acls/{}",
626-
subscription_id, database_id, acl_id
627-
))
628-
.await?;
584+
let handler = redis_cloud::CloudAclHandler::new(client.clone());
585+
handler.delete(subscription_id, database_id, acl_id).await?;
629586
println!("ACL {} deleted successfully", acl_id);
630587
}
631588
}
@@ -643,21 +600,16 @@ pub async fn handle_peering_command(
643600

644601
match command {
645602
PeeringCommands::List { subscription_id } => {
646-
let peerings = client
647-
.get_raw(&format!("/subscriptions/{}/peerings", subscription_id))
648-
.await?;
603+
let handler = redis_cloud::CloudPeeringHandler::new(client.clone());
604+
let peerings = handler.list(subscription_id).await?;
649605
print_output(peerings, output_format, query)?;
650606
}
651607
PeeringCommands::Show {
652608
subscription_id,
653609
peering_id,
654610
} => {
655-
let peering = client
656-
.get_raw(&format!(
657-
"/subscriptions/{}/peerings/{}",
658-
subscription_id, peering_id
659-
))
660-
.await?;
611+
let handler = redis_cloud::CloudPeeringHandler::new(client.clone());
612+
let peering = handler.get(subscription_id, &peering_id).await?;
661613
print_output(peering, output_format, query)?;
662614
}
663615
PeeringCommands::Create {
@@ -693,12 +645,8 @@ pub async fn handle_peering_command(
693645
);
694646
return Ok(());
695647
}
696-
client
697-
.delete_raw(&format!(
698-
"/subscriptions/{}/peerings/{}",
699-
subscription_id, peering_id
700-
))
701-
.await?;
648+
let handler = redis_cloud::CloudPeeringHandler::new(client.clone());
649+
handler.delete(subscription_id, &peering_id).await?;
702650
println!("Peering {} deleted successfully", peering_id);
703651
}
704652
}
@@ -818,7 +766,7 @@ pub async fn handle_backup_command(
818766
let backup = client
819767
.post_raw(
820768
&format!(
821-
"/subscriptions/{}/databases/{}/backups",
769+
"/subscriptions/{}/databases/{}/backup",
822770
subscription_id, database_id
823771
),
824772
serde_json::json!({}),
@@ -831,9 +779,7 @@ pub async fn handle_backup_command(
831779
database_id,
832780
backup_id,
833781
} => {
834-
let restore_data = serde_json::json!({
835-
"backupId": backup_id
836-
});
782+
let restore_data = serde_json::json!({"backupId": backup_id});
837783
let result = client
838784
.post_raw(
839785
&format!(

0 commit comments

Comments
 (0)