Skip to content

Commit d18038c

Browse files
committed
feat: Implement get_connection_string
1 parent 5079e7a commit d18038c

File tree

7 files changed

+267
-32
lines changed

7 files changed

+267
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
anyhow = "1.0.99"
13-
atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git", rev = "7d8de1e2b9b865fafeb4120c765b1223d500bbc1" }
13+
atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git", rev = "c5ac7e298dc7a654028784f94737b7100a2ad7ff" }
1414
bollard = "0.19.2"
1515
napi = { version = "3.0.0", features = ["async", "anyhow"] }
1616
napi-derive = "3.0.0"

__test__/index.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ test('smoke test', async (t) => {
4141
let getDeployment = await client.getDeployment(createDeploymentOptions.name)
4242
t.is(getDeployment.name,createDeploymentOptions.name)
4343

44+
let getConnectionStringOptions = {
45+
containerIdOrName: createDeploymentOptions.name,
46+
}
47+
let connString = await client.getConnectionString(getConnectionStringOptions)
48+
t.assert(connString === `mongodb://127.0.0.1:${getDeployment.portBindings.port}/?directConnection=true`)
49+
4450
// Count deployments after creation
4551
let after_create_deployment_count = (await client.listDeployments()).length
4652
t.assert(after_create_deployment_count - start_deployments_count === 1)

index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export declare class Client {
66
listDeployments(): Promise<Array<Deployment>>
77
deleteDeployment(deploymentName: string): Promise<void>
88
getDeployment(deploymentName: string): Promise<Deployment>
9+
getConnectionString(options: GetConnectionStringOptions): Promise<string>
910
}
1011

1112
export declare const enum BindingType {
@@ -66,6 +67,13 @@ export interface Deployment {
6667
telemetryBaseUrl?: string
6768
}
6869

70+
export interface GetConnectionStringOptions {
71+
containerIdOrName: string
72+
dbUsername?: string
73+
dbPassword?: string
74+
verify?: boolean
75+
}
76+
6977
export interface MongoDbPortBinding {
7078
type: BindingType
7179
ip: string

index.js

Lines changed: 195 additions & 31 deletions
Large diffs are not rendered by default.

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,17 @@ impl Client {
6969
.context("get deployment")
7070
.map(|d| d.into())
7171
}
72+
73+
#[napi]
74+
pub async fn get_connection_string(
75+
&self,
76+
options: crate::models::get_connection_string::GetConnectionStringOptions,
77+
) -> Result<String> {
78+
let options: atlas_local::models::GetConnectionStringOptions = options.into();
79+
self
80+
.client
81+
.get_connection_string(options)
82+
.await
83+
.context("get connection string")
84+
}
7285
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use napi_derive::napi;
2+
3+
#[napi(object)]
4+
pub struct GetConnectionStringOptions {
5+
pub container_id_or_name: String,
6+
pub db_username: Option<String>,
7+
pub db_password: Option<String>,
8+
pub verify: Option<bool>,
9+
}
10+
11+
impl From<GetConnectionStringOptions> for atlas_local::models::GetConnectionStringOptions {
12+
fn from(source: GetConnectionStringOptions) -> Self {
13+
Self {
14+
container_id_or_name: source.container_id_or_name,
15+
db_username: source.db_username,
16+
db_password: source.db_password,
17+
verify: source.verify,
18+
}
19+
}
20+
}
21+
22+
// test
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn test_get_connection_string_options() {
29+
let options = GetConnectionStringOptions {
30+
container_id_or_name: "test_container".into(),
31+
db_username: Some("test_user".into()),
32+
db_password: Some("test_pass".into()),
33+
verify: Some(true),
34+
};
35+
36+
let get_connection_string_options: atlas_local::models::GetConnectionStringOptions = options.into();
37+
38+
assert_eq!(get_connection_string_options.container_id_or_name, "test_container");
39+
assert_eq!(get_connection_string_options.db_username, Some("test_user".into()));
40+
assert_eq!(get_connection_string_options.db_password, Some("test_pass".into()));
41+
assert_eq!(get_connection_string_options.verify, Some(true));
42+
}
43+
}

src/models/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod create_deployment;
2+
pub mod get_connection_string;
23
pub mod list_deployments;

0 commit comments

Comments
 (0)