Skip to content

Commit 9c09dd9

Browse files
mabaasitaddaleax
andauthored
feat: local atlas detection support COMPASS-7213 (#25)
Co-authored-by: Anna Henningsen <[email protected]>
1 parent 299a884 commit 9c09dd9

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ Returns a boolean.
4444
### isAtlas(uri)
4545
Returns a boolean.
4646

47+
### isLocalAtlas(count: (db: string, coll: string, query: Document) => Promise\<number\>)
48+
Returns a Promise\<boolean\>.
49+
4750
### isAtlasStream(uri)
4851
Returns a boolean.
4952

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export declare function getDataLake(buildInfo: any): {
55

66
export declare function isEnterprise(buildInfo: any): boolean;
77
export declare function isAtlas(uri: string): boolean;
8+
type IsLocalAtlasCountFn = (db: string, ns: string, query: Record<string, any>) => Promise<number>;
9+
export declare function isLocalAtlas(countFn: IsLocalAtlasCountFn): Promise<boolean>;
810
export declare function isAtlasStream(uri: string): boolean;
911
export declare function isLocalhost(uri: string): boolean;
1012
export declare function isDigitalOcean(uri: string): boolean;

index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ function isAtlas(uri) {
5959
return !!getHostnameFromUrl(uri).match(ATLAS_REGEX);
6060
}
6161

62+
function isLocalAtlas(countFn) {
63+
return countFn('admin', 'atlascli', {
64+
managedClusterType: 'atlasCliLocalDevCluster'
65+
}).then(count => count > 0).catch(() => false);
66+
}
67+
6268
function isAtlasStream(uri) {
6369
const host = getHostnameFromUrl(uri);
6470
return !!(host.match(ATLAS_REGEX) && host.match(ATLAS_STREAM_REGEX));
@@ -103,4 +109,14 @@ function getGenuineMongoDB(uri) {
103109
};
104110
}
105111

106-
module.exports = { getDataLake, isEnterprise, isAtlas, isAtlasStream, isLocalhost, isDigitalOcean, getGenuineMongoDB, getBuildEnv };
112+
module.exports = {
113+
getDataLake,
114+
isEnterprise,
115+
isAtlas,
116+
isLocalAtlas,
117+
isAtlasStream,
118+
isLocalhost,
119+
isDigitalOcean,
120+
getGenuineMongoDB,
121+
getBuildEnv
122+
};

test/index.spec.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
const expect = require('chai').expect;
22
const fixtures = require('./fixtures');
3-
const isAtlas = require('../.').isAtlas;
4-
const isAtlasStream = require('../.').isAtlasStream;
5-
const getDataLake = require('../.').getDataLake;
6-
const isLocalhost = require('../.').isLocalhost;
7-
const isDigitalOcean = require('../.').isDigitalOcean;
8-
const getBuildEnv = require('../.').getBuildEnv;
9-
const isEnterprise = require('../.').isEnterprise;
10-
const getGenuineMongoDB = require('../.').getGenuineMongoDB;
3+
const {
4+
isAtlas,
5+
isAtlasStream,
6+
getDataLake,
7+
isLocalhost,
8+
isLocalAtlas,
9+
isDigitalOcean,
10+
getBuildEnv,
11+
isEnterprise,
12+
getGenuineMongoDB,
13+
} = require('..');
1114

1215
describe('mongodb-build-info', () => {
1316
context('isDataLake', () => {
@@ -95,6 +98,40 @@ describe('mongodb-build-info', () => {
9598
});
9699
});
97100

101+
context('isLocalAtlas', () => {
102+
it('calls counts function with expected args', (done) => {
103+
isLocalAtlas((db, coll, query) => {
104+
expect(db).to.equal('admin');
105+
expect(coll).to.equal('atlascli');
106+
expect(query).to.deep.equal({
107+
managedClusterType: 'atlasCliLocalDevCluster'
108+
});
109+
done();
110+
});
111+
});
112+
it('returns false when count resolves to 0', (done) => {
113+
isLocalAtlas(() => Promise.resolve(0))
114+
.then(res => {
115+
expect(res).to.be.false;
116+
done();
117+
});
118+
});
119+
it('returns false when count throws', (done) => {
120+
isLocalAtlas(() => Promise.reject('No such db'))
121+
.then(res => {
122+
expect(res).to.be.false;
123+
done();
124+
});
125+
});
126+
it('returns true when count resolves to 1', (done) => {
127+
isLocalAtlas(() => Promise.resolve(1))
128+
.then(res => {
129+
expect(res).to.be.true;
130+
done();
131+
});
132+
});
133+
});
134+
98135
context('isAtlasStream', () => {
99136
it('reports on atlas', () => {
100137
expect(isAtlasStream('mongodb://admin:catscatscats@atlas-stream-64ba1372b2a9f1545031f34d-gkumd.virginia-usa.a.query.mongodb.net/')).to.be.true;

0 commit comments

Comments
 (0)