Skip to content

Commit 17d4ff2

Browse files
authored
chore(service-provider-server): use atlasVersion to identify Atlas (#796)
This seems a bit more reliable than hostname parsing.
1 parent 0d9959b commit 17d4ff2

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

packages/service-provider-core/src/connect-info.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ describe('getConnectInfo', function() {
5959
'ok': 1
6060
};
6161

62+
const ATLAS_VERSION = {
63+
'atlasVersion': '20210330.0.0.1617063608',
64+
'gitVersion': '8f7e5bdde713391e8123a463895bb7fb660a5ffd'
65+
};
66+
6267
const TOPOLOGY_WITH_CREDENTIALS = {
6368
's': {
6469
'credentials': {
@@ -71,7 +76,7 @@ describe('getConnectInfo', function() {
7176
's': {}
7277
};
7378

74-
const ATLAS_URI = 'mongodb+srv://admin:[email protected].mongodb.net/admin';
79+
const ATLAS_URI = 'mongodb+srv://admin:[email protected].example.net/admin';
7580

7681
it('reports on an enterprise version >=3.2 of mongodb with credentials', function() {
7782
const output = {
@@ -83,6 +88,7 @@ describe('getConnectInfo', function() {
8388
auth_type: 'LDAP',
8489
is_data_lake: false,
8590
dl_version: null,
91+
atlas_version: '20210330.0.0.1617063608',
8692
is_genuine: true,
8793
non_genuine_server_name: 'mongodb',
8894
server_arch: 'x86_64',
@@ -95,6 +101,7 @@ describe('getConnectInfo', function() {
95101
'0.0.6',
96102
BUILD_INFO,
97103
CMD_LINE_OPTS,
104+
ATLAS_VERSION,
98105
TOPOLOGY_WITH_CREDENTIALS)).to.deep.equal(output);
99106
});
100107

@@ -108,6 +115,7 @@ describe('getConnectInfo', function() {
108115
auth_type: null,
109116
is_data_lake: false,
110117
dl_version: null,
118+
atlas_version: '20210330.0.0.1617063608',
111119
is_genuine: true,
112120
non_genuine_server_name: 'mongodb',
113121
server_arch: 'x86_64',
@@ -120,6 +128,7 @@ describe('getConnectInfo', function() {
120128
'0.0.6',
121129
BUILD_INFO,
122130
CMD_LINE_OPTS,
131+
ATLAS_VERSION,
123132
TOPOLOGY_NO_CREDENTIALS)).to.deep.equal(output);
124133
});
125134

@@ -133,6 +142,7 @@ describe('getConnectInfo', function() {
133142
auth_type: 'LDAP',
134143
is_data_lake: false,
135144
dl_version: null,
145+
atlas_version: null,
136146
is_genuine: true,
137147
non_genuine_server_name: 'mongodb',
138148
server_arch: 'x86_64',
@@ -145,6 +155,7 @@ describe('getConnectInfo', function() {
145155
'0.0.6',
146156
BUILD_INFO,
147157
CMD_LINE_OPTS,
158+
null,
148159
TOPOLOGY_WITH_CREDENTIALS)).to.deep.equal(output);
149160
});
150161
});

packages/service-provider-core/src/connect-info.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ export interface ConnectInfo {
1414
auth_type?: string;
1515
is_data_lake: boolean;
1616
dl_version?: string;
17+
atlas_version?: string;
1718
is_genuine: boolean;
1819
non_genuine_server_name: string;
1920
node_version: string;
2021
uri: string;
2122
}
2223

23-
export default function getConnectInfo(uri: string, mongoshVersion: string, buildInfo: any, cmdLineOpts: any, topology: any): ConnectInfo {
24+
export default function getConnectInfo(uri: string, mongoshVersion: string, buildInfo: any, cmdLineOpts: any, atlasVersion: any, topology: any): ConnectInfo {
2425
const { isGenuine: is_genuine, serverName: non_genuine_server_name } =
2526
getBuildInfo.getGenuineMongoDB(buildInfo, cmdLineOpts);
2627
const { isDataLake: is_data_lake, dlVersion: dl_version }
@@ -34,7 +35,7 @@ export default function getConnectInfo(uri: string, mongoshVersion: string, buil
3435
= getBuildInfo.getBuildEnv(buildInfo);
3536

3637
return {
37-
is_atlas: getBuildInfo.isAtlas(uri),
38+
is_atlas: !!atlasVersion?.atlasVersion || getBuildInfo.isAtlas(uri),
3839
is_localhost: getBuildInfo.isLocalhost(uri),
3940
server_version: buildInfo.version,
4041
node_version: process.version,
@@ -46,6 +47,7 @@ export default function getConnectInfo(uri: string, mongoshVersion: string, buil
4647
auth_type,
4748
is_data_lake,
4849
dl_version,
50+
atlas_version: atlasVersion?.atlasVersion ?? null,
4951
is_genuine,
5052
non_genuine_server_name
5153
};

packages/service-provider-server/src/cli-service-provider.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,11 +781,12 @@ describe('CliServiceProvider', () => {
781781
describe('#getConnectionInfo', () => {
782782
let clientStub: any;
783783
let dbStub: any;
784-
let firstCall = true;
784+
let firstCall;
785785

786786
beforeEach(() => {
787787
dbStub = stubInterface<Db>();
788788
clientStub = stubInterface<MongoClient>();
789+
firstCall = true;
789790
dbStub.command.callsFake(() => {
790791
if (firstCall) {
791792
firstCall = false;
@@ -809,7 +810,7 @@ describe('CliServiceProvider', () => {
809810
const info = await serviceProvider.getConnectionInfo();
810811
expect(info.extraInfo.is_atlas).to.equal(false);
811812
expect(info.extraInfo.is_localhost).to.equal(true);
812-
expect(dbStub.command).to.have.callCount(3);
813+
expect(dbStub.command).to.have.callCount(4);
813814
});
814815
});
815816
});

packages/service-provider-server/src/cli-service-provider.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,17 @@ class CliServiceProvider extends ServiceProviderCore implements ServiceProvider
260260
}
261261
const topology = this.getTopology() as Topology;
262262
const { version } = require('../package.json');
263-
let cmdLineOpts = null;
264-
try {
265-
cmdLineOpts = await this.runCommandWithCheck('admin', {
266-
getCmdLineOpts: 1
267-
}, this.baseCmdOptions);
268-
// eslint-disable-next-line no-empty
269-
} catch (e) {
270-
}
263+
const [cmdLineOpts = null, atlasVersion = null] = await Promise.all([
264+
this.runCommandWithCheck('admin', { getCmdLineOpts: 1 }, this.baseCmdOptions).catch(() => {}),
265+
this.runCommandWithCheck('admin', { atlasVersion: 1 }, this.baseCmdOptions).catch(() => {})
266+
]);
271267

272268
const extraConnectionInfo = getConnectInfo(
273269
this.uri?.toString() ?? '',
274270
version,
275271
buildInfo,
276272
cmdLineOpts,
273+
atlasVersion,
277274
topology
278275
);
279276

packages/shell-api/src/shell-internal-state.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,14 @@ describe('ShellInternalState', () => {
259259
serviceProvider.getConnectionInfo.resolves({
260260
extraInfo: {
261261
uri: 'mongodb://localhost/',
262-
is_atlas: true
262+
is_atlas: true,
263+
atlas_version: '20210330.0.0.1617063608'
263264
}
264265
});
265266

266267
await internalState.fetchConnectionInfo();
267268
const prompt = await internalState.getDefaultPrompt();
268-
expect(prompt).to.equal('[atlas proxy]> ');
269+
expect(prompt).to.equal('> ');
269270
});
270271
});
271272

packages/shell-api/src/shell-internal-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export default class ShellInternalState {
352352
serverTypePrompt = '[primary]';
353353
break;
354354
case 'Sharded':
355-
serverTypePrompt = this.connectionInfo?.extraInfo?.is_atlas ? '[atlas proxy]' : '[mongos]';
355+
serverTypePrompt = this.connectionInfo?.extraInfo?.atlas_version ? '' : '[mongos]';
356356
break;
357357
default:
358358
return '';

0 commit comments

Comments
 (0)