-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(compass-connections): split out connectionSupports into a funct…
…ion that we can use outside of hooks (#6311) split out connectionSupports into a function that we can use outside of hooks
- Loading branch information
Showing
4 changed files
with
253 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
packages/compass-connections/src/utils/connection-supports.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import { connectionSupports } from './connection-supports'; | ||
import { type ConnectionInfo } from '@mongodb-js/connection-storage/provider'; | ||
import { expect } from 'chai'; | ||
|
||
const mockConnections: ConnectionInfo[] = [ | ||
{ | ||
id: 'no-atlasMetadata', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
}, | ||
{ | ||
id: 'host-cluster', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
metricsId: 'metricsId', | ||
metricsType: 'host', | ||
instanceSize: 'M10', | ||
clusterType: 'REPLICASET', | ||
clusterUniqueId: 'clusterUniqueId', | ||
}, | ||
}, | ||
{ | ||
id: 'free-cluster', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
metricsId: 'metricsId', | ||
metricsType: 'replicaSet', | ||
instanceSize: 'M0', | ||
clusterType: 'REPLICASET', | ||
clusterUniqueId: 'clusterUniqueId', | ||
}, | ||
}, | ||
{ | ||
id: 'serverless-cluster', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
metricsId: 'metricsId', | ||
metricsType: 'serverless', | ||
instanceSize: 'SERVERLESS_V2', | ||
clusterType: 'REPLICASET', | ||
clusterUniqueId: 'clusterUniqueId', | ||
}, | ||
}, | ||
{ | ||
id: 'dedicated-replicaSet', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
metricsId: 'metricsId', | ||
metricsType: 'replicaSet', | ||
instanceSize: 'M10', | ||
clusterType: 'REPLICASET', | ||
clusterUniqueId: 'clusterUniqueId', | ||
}, | ||
}, | ||
{ | ||
id: 'dedicated-sharded', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
metricsId: 'metricsId', | ||
metricsType: 'cluster', | ||
instanceSize: 'M10', | ||
clusterType: 'SHARDED', | ||
clusterUniqueId: 'clusterUniqueId', | ||
}, | ||
}, | ||
{ | ||
id: 'dedicated-geo-sharded', | ||
connectionOptions: { | ||
connectionString: 'mongodb://foo', | ||
}, | ||
atlasMetadata: { | ||
orgId: 'orgId', | ||
projectId: 'projectId', | ||
clusterName: 'clusterName', | ||
regionalBaseUrl: 'https://example.com', | ||
metricsId: 'metricsId', | ||
metricsType: 'cluster', | ||
instanceSize: 'M30', | ||
clusterType: 'GEOSHARDED', | ||
clusterUniqueId: 'clusterUniqueId', | ||
}, | ||
}, | ||
]; | ||
|
||
function connectionInfoById(connectionId: string): ConnectionInfo { | ||
const connectionInfo = mockConnections.find(({ id }) => id === connectionId); | ||
if (!connectionInfo) { | ||
throw new Error(`No connection for id "${connectionId}"`); | ||
} | ||
return connectionInfo; | ||
} | ||
|
||
describe('connectionSupports', function () { | ||
context('rollingIndexCreation', function () { | ||
it('should return false if the connection has no atlasMetadata', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('no-atlasMetadata'), | ||
'rollingIndexCreation' | ||
) | ||
).to.be.false; | ||
}); | ||
|
||
it('should return false for host cluster type', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('host-cluster'), | ||
'rollingIndexCreation' | ||
) | ||
).to.be.false; | ||
}); | ||
|
||
it('should return false for serverless cluster type', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('serverless-cluster'), | ||
'rollingIndexCreation' | ||
) | ||
).to.be.false; | ||
}); | ||
|
||
it('should return false for free/shared tier clusters', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('free-cluster'), | ||
'rollingIndexCreation' | ||
) | ||
).to.be.false; | ||
}); | ||
|
||
it('should return true for dedicated replicaSet clusters', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('dedicated-replicaSet'), | ||
'rollingIndexCreation' | ||
) | ||
).to.be.true; | ||
}); | ||
|
||
it('should return true for dedicated sharded clusters', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('dedicated-sharded'), | ||
'rollingIndexCreation' | ||
) | ||
).to.be.true; | ||
}); | ||
}); | ||
context('globalWrites', function () { | ||
it('should return false if the connection has no atlasMetadata', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('no-atlasMetadata'), | ||
'globalWrites' | ||
) | ||
).to.be.false; | ||
}); | ||
|
||
it('should return true if the cluster type is geosharded', function () { | ||
expect( | ||
connectionSupports( | ||
connectionInfoById('dedicated-geo-sharded'), | ||
'globalWrites' | ||
) | ||
).to.be.true; | ||
}); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
packages/compass-connections/src/utils/connection-supports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { ConnectionInfo } from '@mongodb-js/connection-info'; | ||
export type ConnectionFeature = 'rollingIndexCreation' | 'globalWrites'; | ||
|
||
function isFreeOrSharedTierCluster(instanceSize: string | undefined): boolean { | ||
if (!instanceSize) { | ||
return false; | ||
} | ||
|
||
return ['M0', 'M2', 'M5'].includes(instanceSize); | ||
} | ||
|
||
function supportsRollingIndexCreation(connectionInfo: ConnectionInfo) { | ||
const atlasMetadata = connectionInfo.atlasMetadata; | ||
|
||
if (!atlasMetadata) { | ||
return false; | ||
} | ||
|
||
const { metricsType, instanceSize } = atlasMetadata; | ||
return ( | ||
!isFreeOrSharedTierCluster(instanceSize) && | ||
(metricsType === 'cluster' || metricsType === 'replicaSet') | ||
); | ||
} | ||
|
||
function supportsGlobalWrites(connectionInfo: ConnectionInfo) { | ||
const atlasMetadata = connectionInfo.atlasMetadata; | ||
|
||
if (!atlasMetadata) { | ||
return false; | ||
} | ||
|
||
return atlasMetadata.clusterType === 'GEOSHARDED'; | ||
} | ||
|
||
export function connectionSupports( | ||
connectionInfo: ConnectionInfo, | ||
connectionFeature: ConnectionFeature | ||
): boolean { | ||
if (connectionFeature === 'rollingIndexCreation') { | ||
return supportsRollingIndexCreation(connectionInfo); | ||
} | ||
|
||
if (connectionFeature === 'globalWrites') { | ||
return supportsGlobalWrites(connectionInfo); | ||
} | ||
|
||
return false; | ||
} |