Skip to content

Commit ae61aea

Browse files
Merge pull request #2 from mongodb-js/MCP-153
feat: implemented connect and listDeployments
2 parents ca811f7 + c7b0a7f commit ae61aea

File tree

9 files changed

+313
-20
lines changed

9 files changed

+313
-20
lines changed

.github/workflows/CI.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,15 @@ jobs:
273273
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
274274
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
275275
license-and-audit:
276-
runs-on: ubuntu-latest
277-
steps:
276+
runs-on: ubuntu-latest
277+
steps:
278278
- uses: actions/checkout@v4
279279
- name: Install Rust toolchain
280280
run: |
281281
rustup update stable
282282
rustup default stable
283283
rustup component add clippy rustfmt
284-
284+
285285
- name: Cache cargo registry
286286
uses: actions/cache@v4
287287
with:
@@ -292,29 +292,29 @@ jobs:
292292
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
293293
restore-keys: |
294294
${{ runner.os }}-cargo-
295-
295+
296296
- name: Cache cargo tools
297297
uses: actions/cache@v4
298298
with:
299299
path: ~/.cargo/bin
300300
key: ${{ runner.os }}-cargo-tools-deny-${{ env.CARGO_DENY_VERSION }}-audit-${{ env.CARGO_AUDIT_VERSION }}
301301
restore-keys: |
302302
${{ runner.os }}-cargo-tools-
303-
303+
304304
- name: Install cargo-deny
305305
run: |
306306
if ! command -v cargo-deny &> /dev/null; then
307307
cargo install --locked --version ${{ env.CARGO_DENY_VERSION }} cargo-deny
308308
fi
309-
309+
310310
- name: Install cargo-audit
311311
run: |
312312
if ! command -v cargo-audit &> /dev/null; then
313313
cargo install --locked --version ${{ env.CARGO_AUDIT_VERSION }} cargo-audit
314314
fi
315-
315+
316316
- name: Run cargo deny
317317
run: cargo deny check
318-
318+
319319
- name: Run cargo audit
320-
run: cargo audit
320+
run: cargo audit

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ version = "0.0.0-preview.0"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git" }
13-
napi = "3.0.0"
12+
anyhow = "1.0.99"
13+
atlas-local = { git = "https://github.com/mongodb/atlas-local-lib.git", rev = "36f56065e891bbe045beeb46489dd7d4142dbd41" }
14+
bollard = "0.19.2"
15+
napi = { version = "3.0.0", features = ["async", "anyhow"] }
1416
napi-derive = "3.0.0"
1517

1618
[build-dependencies]

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ Before using this library, make sure you have:
3636
Here's a simple example to get you started:
3737

3838
```typescript
39-
// TODO
39+
const client = await Client.connect();
40+
const deployments = await client.listDeployments();
41+
console.log(deployments);
4042
```
4143

4244
## Development

__test__/index.spec.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
import test from 'ava'
22

3-
import { plus100 } from '../index'
3+
import { Client } from '../index'
44

5-
test('sync function from native code', (t) => {
6-
const fixture = 42
7-
t.is(plus100(fixture), fixture + 100)
5+
test('smoke test: list deployments', async (t) => {
6+
let client: Client | null = null
7+
8+
try {
9+
// Create client
10+
client = await Client.connect()
11+
} catch (e: any) {
12+
// If docker is not running we get this error
13+
// any other error means failure
14+
t.is(e?.message, 'connect to docker')
15+
return
16+
}
17+
18+
if (client == null) {
19+
t.fail('Client not created')
20+
return
21+
}
22+
23+
// List deployments
24+
// We don't care about the number, we're just testing that the method doesn't fail
25+
await client.listDeployments()
26+
t.pass()
827
})

index.d.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
11
/* auto-generated by NAPI-RS */
22
/* eslint-disable */
3-
export declare function plus100(input: number): number
3+
export declare class Client {
4+
static connect(): Client
5+
listDeployments(): Promise<Array<Deployment>>
6+
}
7+
8+
export declare const enum BindingType {
9+
Loopback = 'Loopback',
10+
AnyInterface = 'AnyInterface',
11+
Specific = 'Specific',
12+
}
13+
14+
export interface CreationSource {
15+
type: CreationSourceType
16+
source: string
17+
}
18+
19+
export declare const enum CreationSourceType {
20+
AtlasCLI = 'AtlasCLI',
21+
Container = 'Container',
22+
Other = 'Other',
23+
}
24+
25+
export interface Deployment {
26+
containerId: string
27+
name?: string
28+
state: State
29+
portBindings?: MongoDBPortBinding
30+
mongodbType: MongodbType
31+
mongodbVersion: string
32+
creationSource?: CreationSource
33+
localSeedLocation?: string
34+
mongodbInitdbDatabase?: string
35+
mongodbInitdbRootPasswordFile?: string
36+
mongodbInitdbRootPassword?: string
37+
mongodbInitdbRootUsernameFile?: string
38+
mongodbInitdbRootUsername?: string
39+
mongotLogFile?: string
40+
runnerLogFile?: string
41+
doNotTrack?: string
42+
telemetryBaseUrl?: string
43+
}
44+
45+
export interface MongoDbPortBinding {
46+
type: BindingType
47+
ip: string
48+
port: number
49+
}
50+
51+
export declare const enum MongodbType {
52+
Community = 'Community',
53+
Enterprise = 'Enterprise',
54+
}
55+
56+
export declare const enum State {
57+
Created = 'Created',
58+
Dead = 'Dead',
59+
Exited = 'Exited',
60+
Paused = 'Paused',
61+
Removing = 'Removing',
62+
Restarting = 'Restarting',
63+
Running = 'Running',
64+
}

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,8 @@ if (!nativeBinding) {
393393
}
394394

395395
module.exports = nativeBinding
396-
module.exports.plus100 = nativeBinding.plus100
396+
module.exports.Client = nativeBinding.Client
397+
module.exports.BindingType = nativeBinding.BindingType
398+
module.exports.CreationSourceType = nativeBinding.CreationSourceType
399+
module.exports.MongodbType = nativeBinding.MongodbType
400+
module.exports.State = nativeBinding.State

src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
#![deny(clippy::all)]
22

3+
use anyhow::{Context, Result};
4+
use atlas_local::Client as AtlasLocalClient;
5+
use bollard::Docker;
36
use napi_derive::napi;
47

8+
use crate::models::list_deployments::Deployment;
9+
10+
pub mod models;
11+
12+
#[napi]
13+
pub struct Client {
14+
client: AtlasLocalClient,
15+
}
16+
517
#[napi]
6-
pub fn plus_100(input: u32) -> u32 {
7-
input + 100
18+
impl Client {
19+
#[napi(factory)]
20+
pub fn connect() -> Result<Client> {
21+
let docker = Docker::connect_with_defaults().context("connect to docker")?;
22+
23+
let atlas_local_client = AtlasLocalClient::new(docker);
24+
25+
Ok(Client {
26+
client: atlas_local_client,
27+
})
28+
}
29+
30+
#[napi]
31+
pub async fn list_deployments(&self) -> Result<Vec<Deployment>> {
32+
self
33+
.client
34+
.list_deployments()
35+
.await
36+
.context("list deployments")
37+
.map(|deployments| deployments.into_iter().map(|d| d.into()).collect())
38+
}
839
}

0 commit comments

Comments
 (0)