redis_cloud/
lib.rs

1//! Redis Cloud REST API Client
2//!
3//! A comprehensive Rust client for the Redis Cloud REST API, providing full access to
4//! subscription management, database operations, billing, monitoring, and advanced features
5//! like VPC peering, SSO/SAML, and Private Service Connect.
6//!
7//! ## Features
8//!
9//! - **Subscription Management**: Create, update, delete subscriptions across AWS, GCP, Azure
10//! - **Database Operations**: Full CRUD operations, backups, imports, metrics
11//! - **Advanced Networking**: VPC peering, Transit Gateway, Private Service Connect
12//! - **Security & Access**: ACLs, SSO/SAML integration, API key management
13//! - **Monitoring & Billing**: Comprehensive metrics, logs, billing and payment management
14//! - **Enterprise Features**: Active-Active databases (CRDB), fixed/essentials plans
15//!
16//! ## Quick Start
17//!
18//! ```rust,no_run
19//! use redis_cloud::{CloudClient, CloudDatabaseHandler};
20//!
21//! #[tokio::main]
22//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
23//!     // Create client with API credentials
24//!     let client = CloudClient::builder()
25//!         .api_key("your-api-key")
26//!         .api_secret("your-api-secret")
27//!         .build()?;
28//!
29//!     // List all databases  
30//!     let db_handler = CloudDatabaseHandler::new(client.clone());
31//!     let databases = db_handler.list(123).await?;
32//!     println!("Found {} databases", databases.as_array().unwrap_or(&vec![]).len());
33//!
34//!     Ok(())
35//! }
36//! ```
37//!
38//! ## Core Usage Patterns
39//!
40//! ### Client Creation
41//!
42//! The client uses a builder pattern for flexible configuration:
43//!
44//! ```rust,no_run
45//! use redis_cloud::CloudClient;
46//!
47//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
48//! // Basic client with default settings
49//! let client = CloudClient::builder()
50//!     .api_key("your-api-key")
51//!     .api_secret("your-api-secret")
52//!     .build()?;
53//!
54//! // Custom configuration
55//! let client2 = CloudClient::builder()
56//!     .api_key("your-api-key")
57//!     .api_secret("your-api-secret")
58//!     .base_url("https://api.redislabs.com/v1".to_string())
59//!     .timeout(std::time::Duration::from_secs(60))
60//!     .build()?;
61//! # Ok(())
62//! # }
63//! ```
64//!
65//! ### Working with Subscriptions
66//!
67//! ```rust,no_run
68//! use redis_cloud::{CloudClient, CloudSubscriptionHandler};
69//! use serde_json::json;
70//!
71//! # #[tokio::main]
72//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
73//! let client = CloudClient::builder()
74//!     .api_key("key")
75//!     .api_secret("secret")
76//!     .build()?;
77//!
78//! let sub_handler = CloudSubscriptionHandler::new(client.clone());
79//!
80//! // List subscriptions
81//! let subscriptions = sub_handler.list().await?;
82//!
83//! // Create a new subscription using raw API
84//! let new_subscription = json!({
85//!     "name": "my-redis-subscription",
86//!     "provider": "AWS",
87//!     "region": "us-east-1",
88//!     "plan": "cache.m5.large"
89//! });
90//! let created = client.post_raw("/subscriptions", new_subscription).await?;
91//! # Ok(())
92//! # }
93//! ```
94//!
95//! ### Database Management
96//!
97//! ```rust,no_run
98//! use redis_cloud::{CloudClient, CloudDatabaseHandler};
99//! use serde_json::json;
100//!
101//! # #[tokio::main]
102//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
103//! let client = CloudClient::builder()
104//!     .api_key("key")
105//!     .api_secret("secret")
106//!     .build()?;
107//!
108//! let db_handler = CloudDatabaseHandler::new(client.clone());
109//!
110//! // Create database using raw API
111//! let database_config = json!({
112//!     "name": "my-database",
113//!     "memory_limit_in_gb": 1.0,
114//!     "support_oss_cluster_api": false,
115//!     "replication": true
116//! });
117//! let database = client.post_raw("/subscriptions/123/databases", database_config).await?;
118//!
119//! // Get database info
120//! let db_info = db_handler.get(123, 456).await?;
121//! # Ok(())
122//! # }
123//! ```
124//!
125//! ### Advanced Features
126//!
127//! #### VPC Peering
128//! ```rust,no_run
129//! use redis_cloud::{CloudClient, CloudPeeringHandler};
130//! use serde_json::json;
131//!
132//! # #[tokio::main]
133//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
134//! let client = CloudClient::builder()
135//!     .api_key("key")
136//!     .api_secret("secret")
137//!     .build()?;
138//!
139//! let peering_handler = CloudPeeringHandler::new(client.clone());
140//!
141//! let peering_request = json!({
142//!     "aws_account_id": "123456789012",
143//!     "vpc_id": "vpc-12345678",
144//!     "vpc_cidr": "10.0.0.0/16",
145//!     "region": "us-east-1"
146//! });
147//! let peering = client.post_raw("/subscriptions/123/peerings", peering_request).await?;
148//! # Ok(())
149//! # }
150//! ```
151//!
152//! #### SSO/SAML Management
153//! ```rust,no_run
154//! use redis_cloud::{CloudClient, CloudSsoHandler};
155//! use serde_json::json;
156//!
157//! # #[tokio::main]
158//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
159//! let client = CloudClient::builder()
160//!     .api_key("key")
161//!     .api_secret("secret")
162//!     .build()?;
163//!
164//! let sso_handler = CloudSsoHandler::new(client.clone());
165//!
166//! // Configure SSO using raw API
167//! let sso_config = json!({
168//!     "enabled": true,
169//!     "auto_provision": true
170//! });
171//! let config = client.put_raw("/sso", sso_config).await?;
172//! # Ok(())
173//! # }
174//! ```
175//!
176//! ## Error Handling
177//!
178//! The client provides comprehensive error handling for different failure scenarios:
179//!
180//! ```rust,no_run
181//! use redis_cloud::{CloudClient, CloudError, CloudDatabaseHandler};
182//!
183//! # #[tokio::main]
184//! # async fn main() {
185//! let client = CloudClient::builder()
186//!     .api_key("key")
187//!     .api_secret("secret")
188//!     .build().unwrap();
189//!
190//! let db_handler = CloudDatabaseHandler::new(client.clone());
191//!
192//! match db_handler.get(123, 456).await {
193//!     Ok(database) => println!("Database: {:?}", database),
194//!     Err(CloudError::ApiError { code: 404, .. }) => {
195//!         println!("Database not found");
196//!     },
197//!     Err(CloudError::AuthenticationFailed) => {
198//!         println!("Invalid API credentials");
199//!     },
200//!     Err(e) => println!("Other error: {}", e),
201//! }
202//! # }
203//! ```
204//!
205//! ## Handler Overview
206//!
207//! The client provides specialized handlers for different API domains:
208//!
209//! | Handler | Purpose | Key Operations |
210//! |---------|---------|----------------|
211//! | [`CloudSubscriptionHandler`] | Subscription management | create, list, update, delete, pricing |
212//! | [`CloudDatabaseHandler`] | Database operations | create, backup, import, metrics, resize |
213//! | [`CloudAccountHandler`] | Account information | info, users, payment methods |
214//! | [`CloudUsersHandler`] | User management | create, update, delete, invite |
215//! | [`CloudBillingHandler`] | Billing & payments | invoices, payment methods, usage reports |
216//! | [`CloudBackupHandler`] | Database backups | create, restore, list, delete |
217//! | [`CloudAclHandler`] | Access control | users, roles, Redis rules |
218//! | [`CloudPeeringHandler`] | VPC peering | create, delete, list peering connections |
219//! | [`CloudSsoHandler`] | SSO/SAML | configure, test, user/group mappings |
220//! | [`CloudMetricsHandler`] | Monitoring | database and subscription metrics |
221//! | [`CloudLogsHandler`] | Audit trails | system, database, and session logs |
222//! | [`CloudTasksHandler`] | Async operations | track long-running operations |
223//!
224//! ## Authentication
225//!
226//! Redis Cloud uses API key authentication with two required headers:
227//! - `x-api-key`: Your API key
228//! - `x-api-secret-key`: Your API secret
229//!
230//! These credentials can be obtained from the Redis Cloud console under Account Settings > API Keys.
231
232pub mod client;
233pub mod handlers;
234pub mod models;
235
236#[cfg(test)]
237mod lib_tests;
238
239// Re-export from the new structure
240pub use client::{CloudClient, CloudClientBuilder};
241
242// Re-export handlers explicitly
243pub use handlers::{
244    CloudAccountHandler, CloudAccountsHandler, CloudAclHandler, CloudApiKeysHandler,
245    CloudBackupHandler, CloudBillingHandler, CloudCrdbHandler, CloudDatabaseHandler,
246    CloudFixedHandler, CloudLogsHandler, CloudMetricsHandler, CloudPeeringHandler,
247    CloudPrivateServiceConnectHandler, CloudRegionHandler, CloudSsoHandler,
248    CloudSubscriptionHandler, CloudTasksHandler, CloudTransitGatewayHandler, CloudUsersHandler,
249};
250
251// Re-export models explicitly
252pub use models::{
253    // Account models
254    AccountKey,
255    CloudAccount,
256    // Backup models
257    CloudBackup,
258    // Database models
259    CloudDatabase,
260    // Metrics models
261    CloudMetrics,
262    // Peering models
263    CloudPeering,
264    // Subscription models
265    CloudProvider,
266    CloudProviderConfig,
267    CloudRegion,
268    CloudRegionConfig,
269    CloudSubscription,
270    CreateBackupRequest,
271    CreateDatabaseRequest,
272    CreatePeeringRequest,
273    CreateSubscriptionRequest,
274    Measurement,
275    MetricValue,
276    ThroughputMeasurement,
277    UpdateDatabaseRequest,
278    UpdateSubscriptionRequest,
279};
280
281// Re-export error types
282use thiserror::Error;
283
284#[derive(Error, Debug)]
285pub enum CloudError {
286    #[error("HTTP request failed: {0}")]
287    Request(#[from] reqwest::Error),
288
289    #[error("Authentication failed")]
290    AuthenticationFailed,
291
292    #[error("API error ({code}): {message}")]
293    ApiError { code: u16, message: String },
294
295    #[error("Connection error: {0}")]
296    ConnectionError(String),
297
298    #[error("JSON error: {0}")]
299    JsonError(#[from] serde_json::Error),
300}
301
302pub type Result<T> = std::result::Result<T, CloudError>;