volcengine_rust_sdk/service/redis/
service_redis.rs

1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-11-07 14:39:42
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-19 11:03:29
6 * @Description: Implementation of the Redis service, providing methods for managing Redis database instances.
7 */
8use crate::service::redis::api_create_db_instance;
9use crate::service::redis::api_decrease_db_instance_node_number;
10use crate::service::redis::api_describe_db_instance_detail;
11use crate::service::redis::api_describe_db_instances;
12use crate::service::redis::api_enable_sharded_cluster;
13use crate::service::redis::api_increase_db_instance_node_number;
14use crate::service::redis::api_modify_allow_list;
15use crate::service::redis::api_modify_db_instance_shard_capacity;
16use crate::service::redis::api_modify_db_instance_shard_number;
17use crate::service::redis::{Redis, RedisService};
18use crate::volcengine::client::client;
19use crate::volcengine::client::client_info;
20use crate::volcengine::client::config as client_config;
21use crate::volcengine::common;
22use crate::volcengine::error::error;
23use crate::volcengine::request::handles;
24use crate::volcengine::session::session;
25
26/// Implementation of RedisService trait for Redis struct.
27/// Provides core functionality for interacting with Volcengine Redis service,
28/// including instance lifecycle management and security configurations.
29impl RedisService for Redis {
30    /// Initializes a new Redis service client from a session configuration.
31    ///
32    /// # Parameters
33    /// - `session`: Authenticated session containing credentials and regional configuration
34    ///
35    /// # Workflow
36    /// 1. Creates service-specific client configuration
37    /// 2. Constructs client information (service metadata)
38    /// 3. Initializes network handles
39    /// 4. Builds complete client instance
40    ///
41    /// # Returns
42    /// - `Ok(Redis)`: Configured Redis client ready for API operations
43    /// - `Err(error::Error)`: Configuration errors during client setup
44    fn new_redis(session: session::Session) -> Result<Self, error::Error> {
45        // Initialize Redis-specific client configuration from session
46        let client_config = session.new_client_config(client_config::ClientServiceName::Redis);
47
48        // Build service metadata for API requests
49        let client_info = client_info::ClientInfo::builder()
50            .with_service_name(client_config::ClientServiceName::Redis)
51            .with_api_version(common::COMMON_VERSION_2020_12_07)
52            .with_signing_region(&client_config.signing_region)
53            .build()?;
54
55        // Initialize network connection pool and TLS configuration
56        let request_handles = handles::Handles {};
57
58        // Construct complete client instance
59        let client = client::Client::builder()
60            .with_client_info(&client_info) // Service metadata
61            .with_config(&client_config) // Connection parameters
62            .with_handles(&request_handles) // Network resources
63            .build()?; // Validate configuration
64
65        Ok(Redis { client: client })
66    }
67
68    /// Creates a new Redis database instance with specified configuration.
69    ///
70    /// # Parameters
71    /// - `request`: CreateDbInstanceReq containing:
72    ///   - Instance specifications (memory, version, etc.)
73    ///   - Network configuration (VPC, subnet)
74    ///   - Authentication parameters
75    ///
76    /// # Returns
77    /// - `Ok(CreateDbInstanceResp)`: Contains created instance ID and status
78    /// - `Err(error::Error)`: Creation failures (quota exceeded, invalid params)
79    async fn new_create_db_instance(
80        &self,
81        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisCreateDbInstanceReq,
82    ) -> Result<
83        volcengine_sdk_protobuf::protobuf::redis_instance::RedisCreateDbInstanceResp,
84        error::Error,
85    > {
86        // Delegate to API implementation module
87        api_create_db_instance::ApiCreateDBInstanceRedis
88            .new_create_db_instance(self, request)
89            .await
90    }
91
92    /// Retrieves detailed configuration and status of a Redis instance.
93    ///
94    /// # Parameters
95    /// - `request`: DescribeDbInstanceDetailReq with target instance ID
96    ///
97    /// # Returns
98    /// - `Ok(DescribeDbInstanceDetailResp)`: Detailed instance metadata including:
99    ///   - Current state (running/creating/error)
100    ///   - Connection endpoints
101    ///   - Resource utilization metrics
102    ///   - Configuration parameters
103    async fn new_describe_db_instance_detail(
104        &self,
105        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisDescribeDbInstanceDetailReq,
106    ) -> Result<
107        volcengine_sdk_protobuf::protobuf::redis_instance::RedisDescribeDbInstanceDetailResp,
108        error::Error,
109    > {
110        api_describe_db_instance_detail::ApiDescribeDBInstanceDetailRedis
111            .new_describe_db_instance(self, request)
112            .await
113    }
114
115    /// Updates network access rules (allow list) for a Redis instance.
116    ///
117    /// # Security Note
118    /// - Modifies IP whitelist rules controlling instance access
119    /// - Supports CIDR ranges and individual IP addresses
120    ///
121    /// # Parameters
122    /// - `request`: ModifyAllowListReq containing:
123    ///   - Allow list ID to modify
124    ///   - New IP/CIDR rules
125    ///   - Optional rule description
126    ///
127    /// # Returns
128    /// - `Ok(ModifyAllowListResp)`: Updated allow list version and status
129    async fn new_modify_allow_list(
130        &self,
131        request: volcengine_sdk_protobuf::protobuf::redis_allow::RedisModifyAllowListReq,
132    ) -> Result<
133        volcengine_sdk_protobuf::protobuf::redis_allow::RedisModifyAllowListResp,
134        error::Error,
135    > {
136        api_modify_allow_list::ApiModifyAllowListRedis
137            .new_modify_allow_list(self, request)
138            .await
139    }
140
141    /// Increases the node number of a Redis database instance.
142    ///
143    /// This function sends a request to the Volcengine Redis service to increase the number of nodes
144    /// in a Redis database instance. It delegates the actual request handling to the
145    /// `ApiIncreaseDbInstanceNodeNumber` struct.
146    ///
147    /// # Arguments
148    /// * `request` - A `IncreaseDbInstanceNodeNumberReq` structure containing the request parameters.
149    ///
150    /// # Returns
151    /// * `Result<IncreaseDbInstanceNodeNumberResp, error::Error>` - On success, returns a `IncreaseDbInstanceNodeNumberResp` structure containing the response from the Redis service.
152    ///   On failure, returns an `error::Error` indicating the cause of the failure.
153    async fn new_increase_db_instance_node_number(
154        &self,
155        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisIncreaseDbInstanceNodeNumberReq,
156    ) -> Result<
157        volcengine_sdk_protobuf::protobuf::redis_instance::RedisIncreaseDbInstanceNodeNumberResp,
158        error::Error,
159    > {
160        api_increase_db_instance_node_number::ApiIncreaseDbInstanceNodeNumber
161            .new_increase_db_instance_node_number(self, request)
162            .await
163    }
164
165    /// Decreases the node number of a Redis database instance.
166    ///
167    /// This function sends a request to the Volcengine Redis service to decrease the number of nodes
168    /// in a Redis database instance. It delegates the actual request handling to the
169    /// `ApiDecreaseDBInstanceNodeNumber` struct.
170    ///
171    /// # Arguments
172    /// * `request` - A `DecreaseDbInstanceNodeNumberReq` structure containing the request parameters.
173    ///
174    /// # Returns
175    /// * `Result<DecreaseDbInstanceNodeNumberResp, error::Error>` - On success, returns a `DecreaseDbInstanceNodeNumberResp` structure containing the response from the Redis service.
176    ///   On failure, returns an `error::Error` indicating the cause of the failure.
177    async fn new_decrease_db_instance_node_number(
178        &self,
179        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisDecreaseDbInstanceNodeNumberReq,
180    ) -> Result<
181        volcengine_sdk_protobuf::protobuf::redis_instance::RedisDecreaseDbInstanceNodeNumberResp,
182        error::Error,
183    > {
184        api_decrease_db_instance_node_number::ApiDecreaseDBInstanceNodeNumber
185            .new_decrease_db_instance_node_number(self, request)
186            .await
187    }
188
189    /// Modifies the shard capacity of a Redis database instance.
190    ///
191    /// This function sends a request to the Volcengine Redis service to modify the shard capacity
192    /// of a Redis database instance. It delegates the actual request handling to the
193    /// `ApiModifyDBInstanceShardCapacity` struct.
194    ///
195    /// # Arguments
196    /// * `request` - A `ModifyDbInstanceShardCapacityReq` structure containing the request parameters.
197    ///
198    /// # Returns
199    /// * `Result<ModifyDbInstanceShardCapacityResp, error::Error>` - On success, returns a `ModifyDbInstanceShardCapacityResp` structure containing the response from the Redis service.
200    ///   On failure, returns an `error::Error` indicating the cause of the failure.
201    async fn new_modify_db_instance_shard_capacity(
202        &self,
203        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisModifyDbInstanceShardCapacityReq,
204    ) -> Result<
205        volcengine_sdk_protobuf::protobuf::redis_instance::RedisModifyDbInstanceShardCapacityResp,
206        error::Error,
207    > {
208        api_modify_db_instance_shard_capacity::ApiModifyDBInstanceShardCapacity
209            .new_modify_db_instance_shard_capacity(self, request)
210            .await
211    }
212
213    /// Modifies the shard number of a Redis database instance.
214    ///
215    /// This function sends a request to the Volcengine Redis service to modify the shard number
216    /// of a Redis database instance. It delegates the actual request handling to the
217    /// `ApiModifyDBInstanceShardNumber` struct.
218    ///
219    /// # Arguments
220    /// * `request` - A `ModifyDbInstanceShardNumberReq` structure containing the request parameters.
221    ///
222    /// # Returns
223    /// * `Result<ModifyDbInstanceShardNumberResp, error::Error>` - On success, returns a `ModifyDbInstanceShardNumberResp` structure containing the response from the Redis service.
224    ///   On failure, returns an `error::Error` indicating the cause of the failure.
225    async fn new_modify_db_instance_shard_number(
226        &self,
227        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisModifyDbInstanceShardNumberReq,
228    ) -> Result<
229        volcengine_sdk_protobuf::protobuf::redis_instance::RedisModifyDbInstanceShardNumberResp,
230        error::Error,
231    > {
232        api_modify_db_instance_shard_number::ApiModifyDBInstanceShardNumber
233            .new_modify_db_instance_shard_number(self, request)
234            .await
235    }
236
237    /// Enables sharded cluster mode for a Redis database instance.
238    ///
239    /// This function sends a request to the Volcengine Redis service to enable sharded cluster mode
240    /// for a Redis database instance. It delegates the actual request handling to the
241    /// `ApiEnableShardedCluster` struct.
242    ///
243    /// # Arguments
244    /// * `request` - A `EnableShardedClusterReq` structure containing the request parameters.
245    ///
246    /// # Returns
247    /// * `Result<EnableShardedClusterResp, error::Error>` - On success, returns a `EnableShardedClusterResp` structure containing the response from the Redis service.
248    ///   On failure, returns an `error::Error` indicating the cause of the failure.
249    async fn new_enable_sharded_cluster(
250        &self,
251        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisEnableShardedClusterReq,
252    ) -> Result<
253        volcengine_sdk_protobuf::protobuf::redis_instance::RedisEnableShardedClusterResp,
254        error::Error,
255    > {
256        api_enable_sharded_cluster::ApiEnableShardedCluster
257            .new_enable_sharded_cluster(self, request)
258            .await
259    }
260
261    /// Public method to describe the details of a Redis database instance.
262    ///
263    /// This method acts as a wrapper to call the `new_describe_db_instances` method of the `ApiDescribeDBInstancesRedis` struct.
264    /// It is used to send a request to the Volcengine Redis service to retrieve detailed information about Redis database instances.
265    ///
266    /// # Arguments
267    /// - `&self`: Reference to the current instance of the struct.
268    /// - `request`: A `RedisDescribeDbInstancesReq` structure containing the request parameters.
269    ///
270    /// # Returns
271    /// - `Result<RedisDescribeDbInstancesResp, error::Error>`: On success, returns a `RedisDescribeDbInstancesResp` structure containing the response from the Redis service.
272    ///   On failure, returns an `error::Error` indicating the cause of the failure.
273    async fn new_describe_db_instances(
274        &self,
275        request: volcengine_sdk_protobuf::protobuf::redis_instance::RedisDescribeDbInstancesReq,
276    ) -> Result<
277        volcengine_sdk_protobuf::protobuf::redis_instance::RedisDescribeDbInstancesResp,
278        error::Error,
279    > {
280        api_describe_db_instances::ApiDescribeDBInstancesRedis
281            .new_describe_db_instances(self, request)
282            .await
283    }
284}