Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/compass-connect/src/stores/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const StateMixin = require('reflux-state-mixin');
const { promisify } = require('util');
const { getConnectionTitle, convertConnectionModelToInfo } = require('mongodb-data-service');
const debug = require('debug')('compass-connect:store');
const { isAtlas, isLocalhost, isDigitalOcean } = require('mongodb-build-info');
const { isLocalhost, isDigitalOcean } = require('mongodb-build-info');
const { getCloudInfo } = require('mongodb-cloud-info');

const Actions = require('../actions');
Expand Down Expand Up @@ -977,6 +977,7 @@ const Store = Reflux.createStore({
genuineMongoDB,
host,
build,
isAtlas
} = await dataService.instance();
const {
hostname,
Expand All @@ -993,7 +994,7 @@ const Store = Reflux.createStore({

const trackEvent = {
is_localhost: isLocalhost(hostname),
is_atlas: isAtlas(hostname),
is_atlas: isAtlas,
is_dataLake: dataLake.isDataLake,
is_enterprise: build.isEnterprise,
is_public_cloud: isPublicCloud,
Expand Down
3 changes: 1 addition & 2 deletions packages/compass-metrics/src/modules/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import schemaStats from 'mongodb-schema/lib/stats';
import { getCloudInfo } from 'mongodb-cloud-info';
import ConnectionString from 'mongodb-connection-string-url';

const ATLAS = /mongodb.net[:/]/i;
const LOCALHOST = /(^localhost)|(^127\.0\.0\.1)/gi;

async function getCloudInfoFromDataService(dataService) {
Expand Down Expand Up @@ -83,7 +82,7 @@ const RULES = [
'server name': state.instance.genuineMongoDB === undefined ? 'mongodb' : state.instance.genuineMongoDB.dbType,
'is data lake': state.instance.dataLake === undefined ? false : state.instance.dataLake.isDataLake,
'data lake version': state.instance.dataLake === undefined ? null : state.instance.dataLake.version,
is_atlas: !!state.instance._id.match(ATLAS),
is_atlas: state.instance.isAtlas,
is_localhost: !!state.instance._id.match(LOCALHOST),
compass_version: version,
...cloudInfo
Expand Down
51 changes: 51 additions & 0 deletions packages/data-service/src/instance-detail-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ describe('instance-detail-helper', function () {
null
);
});

it('should not be identified as atlas', function () {
expect(instanceDetails).to.have.property('isAtlas', false);
});
});
});

Expand Down Expand Up @@ -241,6 +245,53 @@ describe('instance-detail-helper', function () {
'documentdb'
);
});
it(`should be identified as atlas with hostname correct hostnames`, function () {
['myserver.mongodb.net', 'myserver.mongodb-dev.net'].map(
async (hostname) => {
const client = createMongoClientMock({
hosts: [{ host: hostname, port: 9999 }],
commands: {
buildInfo: {},
getCmdLineOpts: fixtures.CMD_LINE_OPTS,
},
});

const instanceDetails = await getInstance(client);

expect(instanceDetails).to.have.property('isAtlas', true);
}
);
});

it(`should be identified as atlas when atlasVersion command is present`, async function () {
const client = createMongoClientMock({
hosts: [{ host: 'fakehost.my.server.com', port: 9999 }],
commands: {
atlasVersion: { version: '1.1.1', gitVersion: '1.2.3' },
buildInfo: {},
getCmdLineOpts: fixtures.CMD_LINE_OPTS,
},
});

const instanceDetails = await getInstance(client);

expect(instanceDetails).to.have.property('isAtlas', true);
});

it(`should not be identified as atlas when atlasVersion command is missing`, async function () {
const client = createMongoClientMock({
hosts: [{ host: 'fakehost.my.server.com', port: 9999 }],
commands: {
atlasVersion: new Error('command not found'),
buildInfo: {},
getCmdLineOpts: fixtures.CMD_LINE_OPTS,
},
});

const instanceDetails = await getInstance(client);

expect(instanceDetails).to.have.property('isAtlas', false);
});
});
});

Expand Down
20 changes: 20 additions & 0 deletions packages/data-service/src/instance-detail-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import toNS from 'mongodb-ns';
import createLogger from '@mongodb-js/compass-logging';

import {
AtlasVersionInfo,
BuildInfo,
CmdLineOpts,
CollectionInfo,
Expand Down Expand Up @@ -88,6 +89,7 @@ export type InstanceDetails = {
genuineMongoDB: GenuineMongoDBDetails;
dataLake: DataLakeDetails;
featureCompatibilityVersion: string | null;
isAtlas: boolean;
};

export async function getInstance(
Expand All @@ -100,6 +102,7 @@ export async function getInstance(
hostInfoResult,
buildInfoResult,
getParameterResult,
atlasVersionResult,
] = await Promise.all([
runCommand(adminDb, { getCmdLineOpts: 1 }).catch((err) => {
/**
Expand All @@ -122,6 +125,10 @@ export async function getInstance(
getParameter: 1,
featureCompatibilityVersion: 1,
}).catch(() => null),

runCommand(adminDb, { atlasVersion: 1 }).catch((err) => {
return { version: '', gitVersion: '' };
}),
]);

return {
Expand All @@ -134,9 +141,22 @@ export async function getInstance(
dataLake: buildDataLakeInfo(buildInfoResult),
featureCompatibilityVersion:
getParameterResult?.featureCompatibilityVersion.version ?? null,
isAtlas: checkIsAtlas(client, atlasVersionResult),
};
}

function checkIsAtlas(
client: MongoClient,
atlasVersionInfo: AtlasVersionInfo
): boolean {
const firstHost = client.options.hosts[0]?.host || '';

if (atlasVersionInfo.version === '') {
return /mongodb(-dev)?\.net$/i.test(firstHost);
}
return true;
}

function buildGenuineMongoDBInfo(
buildInfo: Partial<BuildInfo>,
cmdLineOpts: Partial<CmdLineOpts & { errmsg: string }>
Expand Down
10 changes: 10 additions & 0 deletions packages/data-service/src/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ interface RunDiagnosticsCommand {
spec: { dbStats: 1; scale?: number },
options?: RunCommandOptions
): Promise<DbStats>;
(
db: Db,
spec: { atlasVersion: 1 },
options?: RunCommandOptions
): Promise<AtlasVersionInfo>;
}

export type ListDatabasesOptions = {
Expand Down Expand Up @@ -196,6 +201,11 @@ export type ListCollectionsOptionsNamesOnly = Omit<
nameOnly: true;
};

export type AtlasVersionInfo = {
version: string;
gitVersion: string;
};

export type ListCollectionsResult<CollectionType> = {
cursor: { firstBatch: CollectionType };
};
Expand Down
7 changes: 7 additions & 0 deletions packages/data-service/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { Db, MongoClient } from 'mongodb';

type ClientMockOptions = {
hosts: [{ host: string; port: number }];
commands: Partial<{
connectionStatus: unknown;
getCmdLineOpts: unknown;
hostInfo: unknown;
buildInfo: unknown;
getParameter: unknown;
atlasVersion: unknown;
}>;
collections: Record<string, string[]>;
};

export function createMongoClientMock({
hosts = [{ host: 'localhost', port: 9999 }],
commands = {},
collections = {},
}: Partial<ClientMockOptions> = {}): MongoClient {
Expand All @@ -25,6 +28,7 @@ export function createMongoClientMock({
'hostInfo',
'buildInfo',
'getParameter',
'atlasVersion',
].includes(key)
);

Expand Down Expand Up @@ -67,6 +71,9 @@ export function createMongoClientMock({
},
};
},
options: {
hosts,
},
};

return client as MongoClient;
Expand Down
7 changes: 1 addition & 6 deletions packages/instance-model/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,9 @@ const InstanceModel = AmpersandModel.extend(
databasesStatusError: { type: 'string', default: null },
refreshingStatus: { type: 'string', default: 'initial' },
refreshingStatusError: { type: 'string', default: null },
isAtlas: { type: 'boolean', default: false }
},
derived: {
isAtlas: {
deps: ['hostname'],
fn() {
return /mongodb.net$/i.test(this.hostname);
},
},
isRefreshing: {
deps: ['refreshingStatus'],
fn() {
Expand Down