Skip to content

Commit 897a84a

Browse files
Add NAPI wrapper for create_deployment
1 parent 5d89edb commit 897a84a

File tree

7 files changed

+161
-12
lines changed

7 files changed

+161
-12
lines changed

Cargo.toml

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

1111
[dependencies]
1212
anyhow = "1.0.99"
13-
atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git", rev = "36f56065e891bbe045beeb46489dd7d4142dbd41" }
13+
atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git", rev = "d471e14e1a4462cad95b42ce504f2dcd28b81ee5" }
1414
bollard = "0.19.2"
1515
napi = { version = "3.0.0", features = ["async", "anyhow"] }
1616
napi-derive = "3.0.0"
17+
semver = "1.0.26"
1718

1819
[build-dependencies]
1920
napi-build = "2"

__test__/index.spec.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@ test('smoke test', async (t) => {
2020
return
2121
}
2222

23-
// TODO: Implement once createDeployment is added
24-
// let deploymentName = "test_deployment"
25-
// await client.createDeployment(...)
23+
// Count initial deployments
24+
let start_deployments_count = (await client.listDeployments()).length
2625

27-
// List deployments
28-
// We don't care about the number, we're just testing that the method doesn't fail
29-
await client.listDeployments()
26+
// Create deployment
27+
let createDeploymentOptions = {
28+
name: "test_deployment",
29+
}
30+
await client.createDeployment(createDeploymentOptions)
31+
32+
// Count deployments after creation
33+
let after_create_deployment_count = (await client.listDeployments()).length
34+
t.assert(after_create_deployment_count - start_deployments_count === 1)
3035

31-
// TODO: Uncommment when createDeployment is added
32-
// await client.deleteDeployment(deploymentName)
36+
// Delete deployment
37+
await client.deleteDeployment(createDeploymentOptions.name)
3338

34-
t.pass()
39+
// Count deployments after deletion
40+
let after_delete_deployment_count = (await client.listDeployments()).length
41+
t.assert(start_deployments_count === after_delete_deployment_count)
3542
})

index.d.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable */
33
export declare class Client {
44
static connect(): Client
5+
createDeployment(createDeplomentOptions: CreateDeploymentOptions): Promise<void>
56
listDeployments(): Promise<Array<Deployment>>
67
deleteDeployment(deploymentName: string): Promise<void>
78
}
@@ -12,6 +13,24 @@ export declare const enum BindingType {
1213
Specific = 'Specific'
1314
}
1415

16+
export interface CreateDeploymentOptions {
17+
name?: string
18+
image?: string
19+
mongodbVersion?: string
20+
creationSource?: CreationSource
21+
localSeedLocation?: string
22+
mongodbInitdbDatabase?: string
23+
mongodbInitdbRootPasswordFile?: string
24+
mongodbInitdbRootPassword?: string
25+
mongodbInitdbRootUsernameFile?: string
26+
mongodbInitdbRootUsername?: string
27+
mongotLogFile?: string
28+
runnerLogFile?: string
29+
doNotTrack?: boolean
30+
telemetryBaseUrl?: string
31+
mongodbPortBinding?: MongoDBPortBinding
32+
}
33+
1534
export interface CreationSource {
1635
type: CreationSourceType
1736
source: string
@@ -20,7 +39,7 @@ export interface CreationSource {
2039
export declare const enum CreationSourceType {
2140
AtlasCLI = 'AtlasCLI',
2241
Container = 'Container',
23-
MCP = 'MCP',
42+
MCPServer = 'MCPServer',
2443
Other = 'Other'
2544
}
2645

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ impl Client {
2727
})
2828
}
2929

30+
#[napi]
31+
pub async fn create_deployment(
32+
&self,
33+
create_deploment_options: crate::models::create_deployment::CreateDeploymentOptions,
34+
) -> Result<()> {
35+
let options: atlas_local::models::CreateDeploymentOptions = create_deploment_options.into();
36+
self
37+
.client
38+
.create_deployment(&options)
39+
.await
40+
.context("create deployment")
41+
}
42+
3043
#[napi]
3144
pub async fn list_deployments(&self) -> Result<Vec<Deployment>> {
3245
self

src/models/create_deployment.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use crate::models::list_deployments::{CreationSource, MongoDBPortBinding};
2+
use napi_derive::napi;
3+
use semver::Version;
4+
5+
#[napi(object)]
6+
pub struct CreateDeploymentOptions {
7+
// Identifiers
8+
pub name: Option<String>,
9+
10+
// Image details
11+
pub image: Option<String>,
12+
pub mongodb_version: Option<String>,
13+
14+
// Creation source
15+
pub creation_source: Option<CreationSource>,
16+
17+
// Initial database configuration
18+
pub local_seed_location: Option<String>,
19+
pub mongodb_initdb_database: Option<String>,
20+
pub mongodb_initdb_root_password_file: Option<String>,
21+
pub mongodb_initdb_root_password: Option<String>,
22+
pub mongodb_initdb_root_username_file: Option<String>,
23+
pub mongodb_initdb_root_username: Option<String>,
24+
25+
// Logging
26+
pub mongot_log_file: Option<String>,
27+
pub runner_log_file: Option<String>,
28+
29+
// Telemetry
30+
pub do_not_track: Option<bool>,
31+
pub telemetry_base_url: Option<String>,
32+
33+
// Port configuration
34+
pub mongodb_port_binding: Option<MongoDBPortBinding>,
35+
}
36+
37+
impl From<CreateDeploymentOptions> for atlas_local::models::CreateDeploymentOptions {
38+
fn from(source: CreateDeploymentOptions) -> Self {
39+
let version: Option<Version> = match source.mongodb_version.as_deref() {
40+
Some("latest") => None,
41+
None => None,
42+
Some(ver_string) => {
43+
// If malformed Version if given, it will panic here
44+
Some(Version::parse(ver_string).expect("Parse version string"))
45+
}
46+
};
47+
48+
Self {
49+
name: source.name,
50+
image: source.image,
51+
mongodb_version: version,
52+
creation_source: source
53+
.creation_source
54+
.map(atlas_local::models::CreationSource::from),
55+
local_seed_location: source.local_seed_location,
56+
mongodb_initdb_database: source.mongodb_initdb_database,
57+
mongodb_initdb_root_password_file: source.mongodb_initdb_root_password_file,
58+
mongodb_initdb_root_password: source.mongodb_initdb_root_password,
59+
mongodb_initdb_root_username_file: source.mongodb_initdb_root_username_file,
60+
mongodb_initdb_root_username: source.mongodb_initdb_root_username,
61+
mongot_log_file: source.mongot_log_file,
62+
runner_log_file: source.runner_log_file,
63+
do_not_track: source.do_not_track,
64+
telemetry_base_url: source.telemetry_base_url,
65+
mongodb_port_binding: source
66+
.mongodb_port_binding
67+
.map(atlas_local::models::MongoDBPortBinding::from),
68+
}
69+
}
70+
}

src/models/list_deployments.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::net::IpAddr;
2+
13
use napi_derive::napi;
24

35
#[napi(object)]
@@ -77,7 +79,7 @@ pub struct CreationSource {
7779
pub enum CreationSourceType {
7880
AtlasCLI,
7981
Container,
80-
MCP,
82+
MCPServer,
8183
Other,
8284
}
8385

@@ -143,6 +145,27 @@ impl From<atlas_local::models::MongoDBPortBinding> for MongoDBPortBinding {
143145
}
144146
}
145147

148+
impl From<MongoDBPortBinding> for atlas_local::models::MongoDBPortBinding {
149+
fn from(source: MongoDBPortBinding) -> Self {
150+
match source.binding_type {
151+
BindingType::Loopback => atlas_local::models::MongoDBPortBinding {
152+
binding_type: atlas_local::models::BindingType::Loopback,
153+
port: source.port,
154+
},
155+
BindingType::AnyInterface => atlas_local::models::MongoDBPortBinding {
156+
binding_type: atlas_local::models::BindingType::AnyInterface,
157+
port: source.port,
158+
},
159+
BindingType::Specific => atlas_local::models::MongoDBPortBinding {
160+
binding_type: atlas_local::models::BindingType::Specific(
161+
source.ip.parse::<IpAddr>().expect("Parse IP address"),
162+
),
163+
port: source.port,
164+
},
165+
}
166+
}
167+
}
168+
146169
impl From<atlas_local::models::MongodbType> for MongodbType {
147170
fn from(source: atlas_local::models::MongodbType) -> Self {
148171
match source {
@@ -165,10 +188,25 @@ impl From<atlas_local::models::CreationSource> for CreationSource {
165188
source_type: CreationSourceType::Container,
166189
source: "CONTAINER".to_string(),
167190
},
191+
CreationSourceSource::MCPServer => CreationSource {
192+
source_type: CreationSourceType::MCPServer,
193+
source: "MCPSERVER".to_string(),
194+
},
168195
CreationSourceSource::Unknown(source) => CreationSource {
169196
source_type: CreationSourceType::Other,
170197
source,
171198
},
172199
}
173200
}
174201
}
202+
203+
impl From<CreationSource> for atlas_local::models::CreationSource {
204+
fn from(source: CreationSource) -> Self {
205+
match source.source_type {
206+
CreationSourceType::AtlasCLI => atlas_local::models::CreationSource::AtlasCLI,
207+
CreationSourceType::Container => atlas_local::models::CreationSource::Container,
208+
CreationSourceType::MCPServer => atlas_local::models::CreationSource::MCPServer,
209+
CreationSourceType::Other => atlas_local::models::CreationSource::Unknown(source.source),
210+
}
211+
}
212+
}

src/models/mod.rs

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

0 commit comments

Comments
 (0)