oci-rust-sdk 0.4.4

Unofficial Oracle OCI SDK for Rust
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
# Adding New OCI Services

This guide explains how to add new OCI services to the SDK following the established trait-based pattern.

## Overview

Each OCI service follows a consistent structure:
- A trait defining service operations
- A client struct implementing the trait
- Request and response models
- Module organization for discoverability

## Step-by-Step Guide

### 1. Create Service Module Structure

For a new service (e.g., Compute), create the following directory structure:

```
src/
  compute/
    mod.rs              - Module exports
    trait.rs            - Service trait definition
    client.rs           - Client implementation
    models/             - Response models
      mod.rs
      instance.rs
      image.rs
    requests/           - Request builders
      mod.rs
      list_instances.rs
      get_instance.rs
```

### 2. Define the Service Trait

Create `src/compute/trait.rs`:

```rust
use crate::core::Result;
use crate::compute::requests::*;
use crate::compute::models::*;

/// Trait defining operations for Compute service
///
/// This trait abstracts all Compute API operations, enabling
/// dependency injection and custom mock implementations.
pub trait Compute: Send + Sync {
    /// List compute instances
    async fn list_instances(
        &self,
        request: ListInstancesRequest,
    ) -> Result<ListInstancesResponse>;

    /// Get a specific compute instance
    async fn get_instance(
        &self,
        request: GetInstanceRequest,
    ) -> Result<GetInstanceResponse>;

    // Add more methods as needed
}
```

**Key Points:**
- Use `Send + Sync` bounds for async compatibility
- Import types from local modules
- Document each method
- Keep method signatures async

### 3. Create Request Models

Create `src/compute/requests/list_instances.rs`:

```rust
/// Request to list compute instances
#[derive(Debug, Clone, Default)]
pub struct ListInstancesRequest {
    pub compartment_id: Option<String>,
    pub availability_domain: Option<String>,
    pub limit: Option<i32>,
    pub page: Option<String>,
}

impl ListInstancesRequest {
    pub fn builder() -> ListInstancesRequestBuilder {
        ListInstancesRequestBuilder::default()
    }

    pub(crate) fn to_query_params(&self) -> Vec<(String, String)> {
        let mut params = Vec::new();

        if let Some(ref compartment_id) = self.compartment_id {
            params.push(("compartmentId".to_string(), compartment_id.clone()));
        }
        if let Some(ref availability_domain) = self.availability_domain {
            params.push(("availabilityDomain".to_string(), availability_domain.clone()));
        }
        if let Some(limit) = self.limit {
            params.push(("limit".to_string(), limit.to_string()));
        }
        if let Some(ref page) = self.page {
            params.push(("page".to_string(), page.clone()));
        }

        params
    }
}

#[derive(Debug, Default)]
pub struct ListInstancesRequestBuilder {
    compartment_id: Option<String>,
    availability_domain: Option<String>,
    limit: Option<i32>,
    page: Option<String>,
}

impl ListInstancesRequestBuilder {
    pub fn compartment_id(mut self, compartment_id: impl Into<String>) -> Self {
        self.compartment_id = Some(compartment_id.into());
        self
    }

    pub fn availability_domain(mut self, availability_domain: impl Into<String>) -> Self {
        self.availability_domain = Some(availability_domain.into());
        self
    }

    pub fn limit(mut self, limit: i32) -> Self {
        self.limit = Some(limit);
        self
    }

    pub fn page(mut self, page: impl Into<String>) -> Self {
        self.page = Some(page.into());
        self
    }

    pub fn build(self) -> ListInstancesRequest {
        ListInstancesRequest {
            compartment_id: self.compartment_id,
            availability_domain: self.availability_domain,
            limit: self.limit,
            page: self.page,
        }
    }
}
```

### 4. Create Response Models

Create `src/compute/models/instance.rs`:

```rust
use serde::{Deserialize, Serialize};

/// Response containing list of instances
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListInstancesResponse {
    pub instances: Vec<Instance>,
    #[serde(skip)]
    pub opc_request_id: Option<String>,
    #[serde(skip)]
    pub opc_next_page: Option<String>,
}

/// Represents a compute instance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Instance {
    pub id: String,
    #[serde(rename = "displayName")]
    pub display_name: Option<String>,
    #[serde(rename = "compartmentId")]
    pub compartment_id: String,
    #[serde(rename = "lifecycleState")]
    pub lifecycle_state: String,
    // Add more fields as needed
}
```

### 5. Implement the Client

Create `src/compute/client.rs`:

```rust
use std::sync::Arc;
use crate::core::{OciHttpClient, Result, auth::AuthProvider, region::Region};
use crate::compute::{Compute, requests::*, models::*};

/// Client for Compute service operations
pub struct ComputeClient {
    http_client: OciHttpClient,
}

impl ComputeClient {
    /// Create a new Compute client
    pub fn new(
        auth_provider: Arc<dyn AuthProvider>,
        region: Region,
    ) -> Result<Self> {
        let endpoint = region.endpoint("iaas");  // Compute uses iaas endpoint
        let http_client = OciHttpClient::new(auth_provider, endpoint)?;
        Ok(Self { http_client })
    }
}

impl Compute for ComputeClient {
    async fn list_instances(
        &self,
        request: ListInstancesRequest,
    ) -> Result<ListInstancesResponse> {
        let query_params = request.to_query_params();
        let query_string = if query_params.is_empty() {
            String::new()
        } else {
            format!(
                "?{}",
                query_params
                    .iter()
                    .map(|(k, v)| format!("{}={}",
                        urlencoding::encode(k),
                        urlencoding::encode(v)))
                    .collect::<Vec<_>>()
                    .join("&")
            )
        };

        let path = format!("/instances{}", query_string);
        let oci_response = self.http_client.get::<Vec<Instance>>(&path).await?;

        Ok(ListInstancesResponse {
            instances: oci_response.body,
            opc_request_id: oci_response.get_header("opc-request-id"),
            opc_next_page: oci_response.get_header("opc-next-page"),
        })
    }

    async fn get_instance(
        &self,
        request: GetInstanceRequest,
    ) -> Result<GetInstanceResponse> {
        let path = format!("/instances/{}", request.instance_id);
        let oci_response = self.http_client.get::<Instance>(&path).await?;

        Ok(GetInstanceResponse {
            instance: oci_response.body,
            opc_request_id: oci_response.get_header("opc-request-id"),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Example mock for testing
    struct MockCompute;

    impl Compute for MockCompute {
        async fn list_instances(
            &self,
            _request: ListInstancesRequest,
        ) -> Result<ListInstancesResponse> {
            Ok(ListInstancesResponse {
                instances: vec![],
                opc_request_id: Some("test-id".to_string()),
                opc_next_page: None,
            })
        }

        async fn get_instance(
            &self,
            _request: GetInstanceRequest,
        ) -> Result<GetInstanceResponse> {
            Ok(GetInstanceResponse {
                instance: Instance {
                    id: "test-instance".to_string(),
                    display_name: Some("Test Instance".to_string()),
                    compartment_id: "test-compartment".to_string(),
                    lifecycle_state: "RUNNING".to_string(),
                },
                opc_request_id: Some("test-id".to_string()),
            })
        }
    }

    #[tokio::test]
    async fn test_with_mock() {
        let mock = MockCompute;
        let response = mock.list_instances(
            ListInstancesRequest::builder().build()
        ).await.unwrap();
        assert_eq!(response.instances.len(), 0);
    }
}
```

### 6. Set Up Module Exports

Create `src/compute/mod.rs`:

```rust
pub mod models;
pub mod requests;
pub mod client;
mod r#trait;

pub use client::ComputeClient;
pub use r#trait::Compute;
pub use models::*;
pub use requests::*;
```

### 7. Add to Top-Level Library

Update `src/lib.rs`:

```rust
#[cfg(feature = "compute")]
pub mod compute;

#[cfg(feature = "compute")]
pub use compute::{
    Compute,
    ComputeClient,
    models::*,
    requests::*,
};
```

### 8. Add Feature Flag

Update `Cargo.toml`:

```toml
[features]
default = []
os_management_hub = []
compute = []  # Add new feature
```

## Endpoint Configuration

Different services use different endpoint prefixes. Common ones:

- OS Management Hub: `"osmh"`
- Compute (IaaS): `"iaas"`
- Object Storage: `"objectstorage"`
- Identity: `"identity"`

Check OCI documentation for the correct endpoint prefix for your service.

## Query Parameter Naming

OCI APIs typically use camelCase for query parameters and JSON fields:
- `compartmentId` not `compartment_id`
- `displayName` not `display_name`

Use `#[serde(rename = "...")]` for struct fields but keep Rust field names as snake_case.

## Testing Your Service

1. **Unit Tests**: Add tests in `client.rs` using mock implementations
2. **Integration Tests**: Create tests in `tests/` directory
3. **Examples**: Add usage examples in `examples/`

Example integration test:

```rust
// tests/compute_integration.rs
#[cfg(feature = "compute")]
mod compute_tests {
    use std::sync::Arc;
    use oci_rust_sdk::compute::{Compute, ComputeClient, ListInstancesRequest};
    use oci_rust_sdk::core::{auth::ConfigFileAuthProvider, region::Region};

    #[tokio::test]
    #[ignore]  // Requires real OCI credentials
    async fn test_list_instances() {
        let auth = Arc::new(ConfigFileAuthProvider::from_default().unwrap());
        let client = ComputeClient::new(auth, Region::UsPhoenix1).unwrap();

        let request = ListInstancesRequest::builder()
            .compartment_id("ocid1.compartment.oc1..xxxxx")
            .limit(10)
            .build();

        let response = client.list_instances(request).await.unwrap();
        println!("Found {} instances", response.instances.len());
    }
}
```

## Checklist

When adding a new service, ensure:

- [ ] Trait defined with `Send + Sync` bounds
- [ ] Client struct implements the trait
- [ ] Request models use builder pattern
- [ ] Response models use serde for JSON deserialization
- [ ] Module exports are correct
- [ ] Feature flag added to Cargo.toml
- [ ] Top-level exports added to lib.rs
- [ ] Example tests showing mock usage
- [ ] Documentation for all public types
- [ ] Example usage in `examples/` directory

## Common Patterns

### Pagination

```rust
pub async fn list_all_instances<T: Compute>(
    client: &T,
    compartment_id: &str,
) -> Result<Vec<Instance>> {
    let mut all_instances = Vec::new();
    let mut page = None;

    loop {
        let request = ListInstancesRequest::builder()
            .compartment_id(compartment_id)
            .page(page.clone().unwrap_or_default())
            .build();

        let response = client.list_instances(request).await?;
        all_instances.extend(response.instances);

        if response.opc_next_page.is_none() {
            break;
        }
        page = response.opc_next_page;
    }

    Ok(all_instances)
}
```

### Error Handling

```rust
use crate::core::{Result, OciError};

impl Compute for ComputeClient {
    async fn delete_instance(
        &self,
        request: DeleteInstanceRequest,
    ) -> Result<()> {
        let path = format!("/instances/{}", request.instance_id);
        self.http_client.delete::<()>(&path).await?;
        Ok(())
    }
}
```

## See Also

- [Testing Guide]TESTING.md - How to create mocks
- [examples/using_traits.rs]../examples/using_traits.rs - Complete example
- [OCI API Documentation]https://docs.oracle.com/en-us/iaas/api/ - Official API docs