diff --git a/packages/i18n/src/locales/en_US.ts b/packages/i18n/src/locales/en_US.ts index d29e8475c4..f1c16d6060 100644 --- a/packages/i18n/src/locales/en_US.ts +++ b/packages/i18n/src/locales/en_US.ts @@ -1474,7 +1474,22 @@ const translations: Catalog = { shardCollection: { link: 'https://docs.mongodb.com/manual/reference/method/sh.shardCollection', description: 'Enables sharding for a collection. Uses the shardCollection command', - example: 'sh.shardCollection(namesapce, key, unique?, options?)', + example: 'sh.shardCollection(namespace, key, unique?, options?)', + }, + reshardCollection: { + link: 'https://docs.mongodb.com/manual/reference/method/sh.reshardCollection', + description: 'Enables sharding for a collection. Uses the reshardCollection command', + example: 'sh.reshardCollection(namespace, key, unique?, options?)', + }, + commitReshardCollection: { + link: 'https://docs.mongodb.com/manual/reference/method/sh.commitReshardCollection', + description: 'Commits the current reshardCollection on a given collection', + example: 'sh.commitReshardCollection(namespace)', + }, + abortReshardCollection: { + link: 'https://docs.mongodb.com/manual/reference/method/sh.abortReshardCollection', + description: 'Abort the current reshardCollection on a given collection', + example: 'sh.abortReshardCollection(namespace)', }, status: { link: 'https://docs.mongodb.com/manual/reference/method/sh.status', diff --git a/packages/shell-api/src/shard.spec.ts b/packages/shell-api/src/shard.spec.ts index fbd9b31a89..accc2aeaed 100644 --- a/packages/shell-api/src/shard.spec.ts +++ b/packages/shell-api/src/shard.spec.ts @@ -155,6 +155,98 @@ describe('Shard', () => { expect(catchedError).to.equal(expectedError); }); }); + describe('reshardCollection', () => { + it('calls serviceProvider.runCommandWithCheck without optional args', async() => { + await shard.reshardCollection('db.coll', { key: 1 } ); + expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith( + ADMIN_DB, + { + reshardCollection: 'db.coll', + key: { key: 1 } + } + ); + }); + + it('calls serviceProvider.runCommandWithCheck with optional args', async() => { + await shard.reshardCollection('db.coll', { key: 1 }, true, { option1: 1 }); + expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith( + ADMIN_DB, + { + reshardCollection: 'db.coll', + key: { key: 1 }, + unique: true, + option1: 1 + } + ); + }); + + it('returns whatever serviceProvider.runCommandWithCheck returns', async() => { + const expectedResult = { ok: 1 }; + serviceProvider.runCommandWithCheck.resolves(expectedResult); + const result = await shard.reshardCollection('db.coll', { key: 1 }); + expect(result).to.deep.equal(expectedResult); + }); + + it('throws if serviceProvider.runCommandWithCheck rejects', async() => { + const expectedError = new Error(); + serviceProvider.runCommandWithCheck.rejects(expectedError); + const catchedError = await shard.reshardCollection('db.coll', { key: 1 }) + .catch(e => e); + expect(catchedError).to.equal(expectedError); + }); + }); + describe('commitReshardCollection', () => { + it('calls serviceProvider.runCommandWithCheck', async() => { + await shard.commitReshardCollection('db.coll'); + expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith( + ADMIN_DB, + { + commitReshardCollection: 'db.coll' + } + ); + }); + + it('returns whatever serviceProvider.runCommandWithCheck returns', async() => { + const expectedResult = { ok: 1 }; + serviceProvider.runCommandWithCheck.resolves(expectedResult); + const result = await shard.commitReshardCollection('db.coll'); + expect(result).to.deep.equal(expectedResult); + }); + + it('throws if serviceProvider.runCommandWithCheck rejects', async() => { + const expectedError = new Error(); + serviceProvider.runCommandWithCheck.rejects(expectedError); + const catchedError = await shard.commitReshardCollection('db.coll') + .catch(e => e); + expect(catchedError).to.equal(expectedError); + }); + }); + describe('abortReshardCollection', () => { + it('calls serviceProvider.runCommandWithCheck', async() => { + await shard.abortReshardCollection('db.coll'); + expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith( + ADMIN_DB, + { + abortReshardCollection: 'db.coll' + } + ); + }); + + it('returns whatever serviceProvider.runCommandWithCheck returns', async() => { + const expectedResult = { ok: 1 }; + serviceProvider.runCommandWithCheck.resolves(expectedResult); + const result = await shard.abortReshardCollection('db.coll'); + expect(result).to.deep.equal(expectedResult); + }); + + it('throws if serviceProvider.runCommandWithCheck rejects', async() => { + const expectedError = new Error(); + serviceProvider.runCommandWithCheck.rejects(expectedError); + const catchedError = await shard.abortReshardCollection('db.coll') + .catch(e => e); + expect(catchedError).to.equal(expectedError); + }); + }); describe('addShard', () => { it('calls serviceProvider.runCommandWithCheck with arg', async() => { serviceProvider.runCommandWithCheck.resolves({ ok: 1, msg: 'isdbgrid' }); diff --git a/packages/shell-api/src/shard.ts b/packages/shell-api/src/shard.ts index ac1adba3dc..6179940642 100644 --- a/packages/shell-api/src/shard.ts +++ b/packages/shell-api/src/shard.ts @@ -68,12 +68,51 @@ export default class Shard extends ShellApiWithMongoClass { } @returnsPromise - async shardCollection(namespace: string, key: Document, unique?: boolean, options?: Document): Promise { - assertArgsDefinedType([namespace, key, unique, options], ['string', 'object', [undefined, 'boolean'], [undefined, 'object']], 'Shard.shardCollection'); - this._emitShardApiCall('shardCollection', { namespace, key, unique, options }); + @serverVersions(['5.0.0', ServerVersions.latest]) + async commitReshardCollection(namespace: string): Promise { + assertArgsDefinedType([namespace], ['string'], 'Shard.commitReshardCollection'); + this._emitShardApiCall('commitReshardCollection', { namespace }); + return await this._database._runAdminCommand({ + commitReshardCollection: namespace + }); + } + + @returnsPromise + @serverVersions(['5.0.0', ServerVersions.latest]) + async abortReshardCollection(namespace: string): Promise { + assertArgsDefinedType([namespace], ['string'], 'Shard.abortReshardCollection'); + this._emitShardApiCall('abortReshardCollection', { namespace }); + return await this._database._runAdminCommand({ + abortReshardCollection: namespace + }); + } + + @returnsPromise + async shardCollection(namespace: string, key: Document, unique?: boolean | Document, options?: Document): Promise { + return await this._runShardCollection('shardCollection', namespace, key, unique, options); + } + + @returnsPromise + @serverVersions(['5.0.0', ServerVersions.latest]) + async reshardCollection(namespace: string, key: Document, unique?: boolean | Document, options?: Document): Promise { + return await this._runShardCollection('reshardCollection', namespace, key, unique, options); + } + + async _runShardCollection( + command: 'shardCollection' | 'reshardCollection', + namespace: string, + key: Document, + unique?: boolean | Document, + options?: Document): Promise { + assertArgsDefinedType([namespace, key, unique, options], ['string', 'object', [undefined, 'boolean', 'object'], [undefined, 'object']], `Shard.${command}`); + this._emitShardApiCall(command, { namespace, key, unique, options }); + if (typeof unique === 'object' && unique !== null) { + options = unique; + unique = undefined; + } const cmd = { - shardCollection: namespace, + [command]: namespace, key: key } as any; if (unique !== undefined) {