redis-cloud 0.9.5

Redis Cloud REST API client library
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! AWS `PrivateLink` connectivity operations
//!
//! This module provides AWS `PrivateLink` connectivity functionality for Redis Cloud,
//! enabling secure, private connections from AWS VPCs to Redis Cloud databases.
//!
//! # Overview
//!
//! AWS `PrivateLink` allows you to connect to Redis Cloud from your AWS VPC without
//! traversing the public internet. This provides enhanced security and potentially
//! lower latency.
//!
//! # Features
//!
//! - **`PrivateLink` Management**: Create and retrieve `PrivateLink` configurations
//! - **Principal Management**: Control which AWS principals can access the service
//! - **Endpoint Scripts**: Get scripts to create endpoints in your AWS account
//! - **Active-Active Support**: `PrivateLink` for CRDB (Active-Active) databases
//!
//! # Example Usage
//!
//! ```no_run
//! use redis_cloud::{CloudClient, PrivateLinkHandler, PrivateLinkCreateRequest, PrincipalType};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let client = CloudClient::builder()
//!     .api_key("your-api-key")
//!     .api_secret("your-api-secret")
//!     .build()?;
//!
//! let handler = PrivateLinkHandler::new(client);
//!
//! // Create a PrivateLink
//! let request = PrivateLinkCreateRequest {
//!     share_name: "my-redis-share".to_string(),
//!     principal: "123456789012".to_string(),
//!     principal_type: PrincipalType::AwsAccount,
//!     alias: Some("Production Account".to_string()),
//! };
//! let result = handler.create(123, &request).await?;
//!
//! // Get PrivateLink configuration
//! let config = handler.get(123).await?;
//! # Ok(())
//! # }
//! ```

use crate::{CloudClient, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
// Note: Value is still needed for return types that use raw JSON responses

// ============================================================================
// Request/Response Types
// ============================================================================

/// Principal type for `PrivateLink` access control
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PrincipalType {
    /// AWS account ID
    AwsAccount,
    /// AWS Organization
    Organization,
    /// AWS Organization Unit
    OrganizationUnit,
    /// AWS IAM Role
    IamRole,
    /// AWS IAM User
    IamUser,
    /// Service Principal
    ServicePrincipal,
}

/// Request to create a `PrivateLink` configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLinkCreateRequest {
    /// Share name for the `PrivateLink` service (max 64 characters)
    pub share_name: String,

    /// AWS principal (account ID, role ARN, etc.)
    pub principal: String,

    /// Type of principal
    #[serde(rename = "type")]
    pub principal_type: PrincipalType,

    /// Optional alias for the `PrivateLink`
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alias: Option<String>,
}

/// Request to add a principal to `PrivateLink` access list
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLinkAddPrincipalRequest {
    /// AWS principal (account ID, role ARN, etc.)
    pub principal: String,

    /// Type of principal
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub principal_type: Option<PrincipalType>,

    /// Optional alias for the principal
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alias: Option<String>,
}

/// Request to remove a principal from `PrivateLink` access list
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLinkRemovePrincipalRequest {
    /// AWS principal to remove
    pub principal: String,

    /// Type of principal
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub principal_type: Option<PrincipalType>,

    /// Alias of the principal
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alias: Option<String>,
}

/// `PrivateLink` configuration response
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLink {
    /// `PrivateLink` status
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,

    /// List of principals with access
    #[serde(skip_serializing_if = "Option::is_none")]
    pub principals: Option<Vec<PrivateLinkPrincipal>>,

    /// AWS Resource Configuration ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_configuration_id: Option<String>,

    /// AWS Resource Configuration ARN
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_configuration_arn: Option<String>,

    /// RAM share ARN
    #[serde(skip_serializing_if = "Option::is_none")]
    pub share_arn: Option<String>,

    /// Share name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub share_name: Option<String>,

    /// List of `PrivateLink` connections
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connections: Option<Vec<PrivateLinkConnection>>,

    /// List of databases accessible via `PrivateLink`
    #[serde(skip_serializing_if = "Option::is_none")]
    pub databases: Option<Vec<PrivateLinkDatabase>>,

    /// Subscription ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subscription_id: Option<i32>,

    /// Region ID (for Active-Active)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region_id: Option<i32>,

    /// Error message if any
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_message: Option<String>,
}

/// `PrivateLink` principal information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLinkPrincipal {
    /// AWS principal (account ID, role ARN, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub principal: Option<String>,

    /// Type of principal
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub principal_type: Option<String>,

    /// Alias for the principal
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alias: Option<String>,

    /// Principal status
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// `PrivateLink` connection information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLinkConnection {
    /// Association ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub association_id: Option<String>,

    /// Connection ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connection_id: Option<String>,

    /// Connection type
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub connection_type: Option<String>,

    /// Owner ID (AWS account)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner_id: Option<String>,

    /// Association date
    #[serde(skip_serializing_if = "Option::is_none")]
    pub association_date: Option<String>,
}

/// Database accessible via `PrivateLink`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLinkDatabase {
    /// Database ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub database_id: Option<i32>,

    /// Database port
    #[serde(skip_serializing_if = "Option::is_none")]
    pub port: Option<i32>,

    /// Resource link endpoint URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_link_endpoint: Option<String>,
}

/// `PrivateLink` endpoint script response
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PrivateLinkEndpointScript {
    /// AWS CLI/CloudFormation script
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_endpoint_script: Option<String>,

    /// Terraform AWS script
    #[serde(skip_serializing_if = "Option::is_none")]
    pub terraform_aws_script: Option<String>,
}

/// AWS `PrivateLink` handler
///
/// Manages AWS `PrivateLink` connectivity for Redis Cloud subscriptions.
pub struct PrivateLinkHandler {
    client: CloudClient,
}

impl PrivateLinkHandler {
    /// Create a new `PrivateLink` handler
    #[must_use]
    pub fn new(client: CloudClient) -> Self {
        Self { client }
    }

    /// Get `PrivateLink` configuration
    ///
    /// Gets the AWS `PrivateLink` configuration for a subscription.
    ///
    /// GET /subscriptions/{subscriptionId}/private-link
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    ///
    /// # Returns
    ///
    /// Returns the `PrivateLink` configuration as JSON
    pub async fn get(&self, subscription_id: i32) -> Result<Value> {
        self.client
            .get(&format!("/subscriptions/{subscription_id}/private-link"))
            .await
    }

    /// Create a `PrivateLink`
    ///
    /// Creates a new AWS `PrivateLink` configuration for a subscription.
    ///
    /// POST /subscriptions/{subscriptionId}/private-link
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `request` - `PrivateLink` creation request
    ///
    /// # Returns
    ///
    /// Returns a task response that can be tracked for completion
    pub async fn create(
        &self,
        subscription_id: i32,
        request: &PrivateLinkCreateRequest,
    ) -> Result<Value> {
        self.client
            .post(
                &format!("/subscriptions/{subscription_id}/private-link"),
                request,
            )
            .await
    }

    /// Add principals to `PrivateLink`
    ///
    /// Adds AWS principals (accounts, IAM roles, etc.) that can access the `PrivateLink`.
    ///
    /// POST /subscriptions/{subscriptionId}/private-link/principals
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `request` - Principal to add
    ///
    /// # Returns
    ///
    /// Returns the updated principal configuration
    pub async fn add_principals(
        &self,
        subscription_id: i32,
        request: &PrivateLinkAddPrincipalRequest,
    ) -> Result<Value> {
        self.client
            .post(
                &format!("/subscriptions/{subscription_id}/private-link/principals"),
                request,
            )
            .await
    }

    /// Remove principals from `PrivateLink`
    ///
    /// Removes AWS principals from the `PrivateLink` access list.
    ///
    /// DELETE /subscriptions/{subscriptionId}/private-link/principals
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `request` - Principal to remove
    ///
    /// # Returns
    ///
    /// Returns confirmation of deletion
    pub async fn remove_principals(
        &self,
        subscription_id: i32,
        request: &PrivateLinkRemovePrincipalRequest,
    ) -> Result<Value> {
        self.client
            .delete_with_body(
                &format!("/subscriptions/{subscription_id}/private-link/principals"),
                serde_json::to_value(request).unwrap_or_default(),
            )
            .await
    }

    /// Get endpoint creation script
    ///
    /// Gets a script to create the VPC endpoint in your AWS account.
    ///
    /// GET /subscriptions/{subscriptionId}/private-link/endpoint-script
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    ///
    /// # Returns
    ///
    /// Returns the endpoint creation script
    pub async fn get_endpoint_script(&self, subscription_id: i32) -> Result<Value> {
        self.client
            .get(&format!(
                "/subscriptions/{subscription_id}/private-link/endpoint-script"
            ))
            .await
    }

    /// Delete `PrivateLink`
    ///
    /// Deletes the AWS `PrivateLink` configuration for a subscription.
    ///
    /// DELETE /subscriptions/{subscriptionId}/private-link
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    ///
    /// # Returns
    ///
    /// Returns task information for tracking the deletion
    pub async fn delete(&self, subscription_id: i32) -> Result<Value> {
        self.client
            .delete_raw(&format!("/subscriptions/{subscription_id}/private-link"))
            .await
    }

    /// Get Active-Active `PrivateLink` configuration
    ///
    /// Gets the AWS `PrivateLink` configuration for an Active-Active (CRDB) subscription region.
    ///
    /// GET /subscriptions/{subscriptionId}/regions/{regionId}/private-link
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `region_id` - The region ID
    ///
    /// # Returns
    ///
    /// Returns the `PrivateLink` configuration for the region
    pub async fn get_active_active(&self, subscription_id: i32, region_id: i32) -> Result<Value> {
        self.client
            .get(&format!(
                "/subscriptions/{subscription_id}/regions/{region_id}/private-link"
            ))
            .await
    }

    /// Create Active-Active `PrivateLink`
    ///
    /// Creates a new AWS `PrivateLink` for an Active-Active (CRDB) subscription region.
    ///
    /// POST /subscriptions/{subscriptionId}/regions/{regionId}/private-link
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `region_id` - The region ID
    /// * `request` - `PrivateLink` creation request
    ///
    /// # Returns
    ///
    /// Returns a task response
    pub async fn create_active_active(
        &self,
        subscription_id: i32,
        region_id: i32,
        request: &PrivateLinkCreateRequest,
    ) -> Result<Value> {
        self.client
            .post(
                &format!("/subscriptions/{subscription_id}/regions/{region_id}/private-link"),
                request,
            )
            .await
    }

    /// Add principals to Active-Active `PrivateLink`
    ///
    /// Adds AWS principals to an Active-Active `PrivateLink`.
    ///
    /// POST /subscriptions/{subscriptionId}/regions/{regionId}/private-link/principals
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `region_id` - The region ID
    /// * `request` - Principal to add
    ///
    /// # Returns
    ///
    /// Returns the updated configuration
    pub async fn add_principals_active_active(
        &self,
        subscription_id: i32,
        region_id: i32,
        request: &PrivateLinkAddPrincipalRequest,
    ) -> Result<Value> {
        self.client
            .post(
                &format!(
                    "/subscriptions/{subscription_id}/regions/{region_id}/private-link/principals"
                ),
                request,
            )
            .await
    }

    /// Remove principals from Active-Active `PrivateLink`
    ///
    /// Removes AWS principals from an Active-Active `PrivateLink`.
    ///
    /// DELETE /subscriptions/{subscriptionId}/regions/{regionId}/private-link/principals
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `region_id` - The region ID
    /// * `request` - Principal to remove
    ///
    /// # Returns
    ///
    /// Returns confirmation of deletion
    pub async fn remove_principals_active_active(
        &self,
        subscription_id: i32,
        region_id: i32,
        request: &PrivateLinkRemovePrincipalRequest,
    ) -> Result<Value> {
        self.client
            .delete_with_body(
                &format!(
                    "/subscriptions/{subscription_id}/regions/{region_id}/private-link/principals"
                ),
                serde_json::to_value(request).unwrap_or_default(),
            )
            .await
    }

    /// Get Active-Active endpoint creation script
    ///
    /// Gets a script to create the VPC endpoint for an Active-Active region.
    ///
    /// GET /subscriptions/{subscriptionId}/regions/{regionId}/private-link/endpoint-script
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - The subscription ID
    /// * `region_id` - The region ID
    ///
    /// # Returns
    ///
    /// Returns the endpoint creation script
    pub async fn get_endpoint_script_active_active(
        &self,
        subscription_id: i32,
        region_id: i32,
    ) -> Result<Value> {
        self.client
            .get(&format!(
                "/subscriptions/{subscription_id}/regions/{region_id}/private-link/endpoint-script"
            ))
            .await
    }
}