From 9d28a33bbf2d9eeaa9d82edf6db45bdbcbcdfa03 Mon Sep 17 00:00:00 2001 From: prakashsvmx Date: Wed, 2 Aug 2023 17:59:56 +0530 Subject: [PATCH] refactor setObjectLegalHold and getObjectLegalHold api --- README.md | 4 +- docs/API.md | 62 ++++------ examples/get-object-legal-hold.js | 56 --------- examples/get-object-legal-hold.mjs | 57 +++++++++ ...egal-hold.js => set-object-legal-hold.mjs} | 38 +++--- src/internal/client.ts | 94 ++++++++++++++- src/internal/type.ts | 11 ++ src/internal/xml-parser.ts | 5 + src/minio.d.ts | 29 +---- src/minio.js | 109 +----------------- tests/unit/test.js | 75 +++++++++--- 11 files changed, 275 insertions(+), 265 deletions(-) delete mode 100644 examples/get-object-legal-hold.js create mode 100644 examples/get-object-legal-hold.mjs rename examples/{set-object-legal-hold.js => set-object-legal-hold.mjs} (59%) diff --git a/README.md b/README.md index 0d56cb79..a9a6b591 100644 --- a/README.md +++ b/README.md @@ -207,8 +207,8 @@ The full API Reference is available here. - [put-object-tagging.js](https://github.com/minio/minio-js/blob/master/examples/put-object-tagging.js) - [get-object-tagging.js](https://github.com/minio/minio-js/blob/master/examples/get-object-tagging.js) - [remove-object-tagging.js](https://github.com/minio/minio-js/blob/master/examples/remove-object-tagging.js) -- [set-object-legal-hold.js](https://github.com/minio/minio-js/blob/master/examples/set-object-legalhold.js) -- [get-object-legal-hold.js](https://github.com/minio/minio-js/blob/master/examples/get-object-legal-hold.js) +- [set-object-legal-hold.js](https://github.com/minio/minio-js/blob/master/examples/set-object-legalhold.mjs) +- [get-object-legal-hold.js](https://github.com/minio/minio-js/blob/master/examples/get-object-legal-hold.mjs) - [compose-object.js](https://github.com/minio/minio-js/blob/master/examples/compose-object.js) - [select-object-content.js](https://github.com/minio/minio-js/blob/master/examples/select-object-content.js) diff --git a/docs/API.md b/docs/API.md index 3a2b1654..e79034dd 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1606,30 +1606,24 @@ minioClient.getObjectTagging( -### getObjectLegalHold(bucketName, objectName, getOpts [, callback]) +### getObjectLegalHold(bucketName, objectName, getOpts) Get legal hold on an object. **Parameters** -| Param | Type | Description | -| --------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------- | -| `bucketName` | _string_ | Name of the bucket. | -| `objectName` | _string_ | Name of the object. | -| `getOpts` | _object_ | Legal hold configuration options. e.g `{versionId:'my-version-uuid'}` defaults to `{}` . | -| `callback(err)` | _function_ | Callback function is called with non `null` value in case of error. If no callback is passed, a `Promise` is returned. | +| Param | Type | Description | +| ------------ | -------- | ---------------------------------------------------------------------------------------- | +| `bucketName` | _string_ | Name of the bucket. | +| `objectName` | _string_ | Name of the object. | +| `getOpts` | _object_ | Legal hold configuration options. e.g `{versionId:'my-version-uuid'}` defaults to `{}` . | **Example 1** Get Legal hold of an object. ```js -minioClient.getObjectLegalHold('bucketName', 'objectName', {}, function (err, res) { - if (err) { - return console.log('Unable to get legal hold config for the object', err.message) - } - console.log('Success', res) -}) +const legalholdStatus = await minioClient.getObjectLegalHold('bucketName', 'objectName') ``` **Example 2** @@ -1637,40 +1631,31 @@ minioClient.getObjectLegalHold('bucketName', 'objectName', {}, function (err, re Get Legal hold of an object with versionId. ```js -minioClient.getObjectLegalHold('bucketName', 'objectName', { versionId: 'my-obj-version-uuid' }, function (err, res) { - if (err) { - return console.log('Unable to get legal hold config for the object', err.message) - } - console.log('Success', res) +const legalholdStatus = await minioClient.getObjectLegalHold('bucketName', 'objectName', { + versionId: 'my-obj-version-uuid', }) ``` -### setObjectLegalHold(bucketName, objectName, [,setOpts, callback]) +### setObjectLegalHold(bucketName, objectName, [,setOpts]) Set legal hold on an object. **Parameters** -| Param | Type | Description | -| --------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------- | -| `bucketName` | _string_ | Name of the bucket. | -| `objectName` | _string_ | Name of the object. | -| `setOpts` | _object_ | Legal hold configuration options to set. e.g `{versionId:'my-version-uuid', status:'ON or OFF'}` defaults to `{status:'ON'}` if not passed. | -| `callback(err)` | _function_ | Callback function is called with non `null` value in case of error. If no callback is passed, a `Promise` is returned. | +| Param | Type | Description | +| ------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------- | +| `bucketName` | _string_ | Name of the bucket. | +| `objectName` | _string_ | Name of the object. | +| `setOpts` | _object_ | Legal hold configuration options to set. e.g `{versionId:'my-version-uuid', status:'ON or OFF'}` defaults to `{status:'ON'}` if not passed. | **Example 1** Set Legal hold of an object. ```js -minioClient.setObjectLegalHold('bucketName', 'objectName', { Status: 'ON' }, function (err, res) { - if (err) { - return console.log('Unable to set legal hold config for the object', err.message) - } - console.log('Success') -}) +const legalholdStatus = await minioClient.setObjectLegalHold('bucketName', 'objectName', { Status: 'ON' }) ``` **Example 2** @@ -1678,17 +1663,10 @@ minioClient.setObjectLegalHold('bucketName', 'objectName', { Status: 'ON' }, fun Set Legal hold of an object with versionId. ```js -minioClient.setObjectLegalHold( - 'bucketName', - 'objectName', - { Status: 'ON', versionId: 'my-obj-version-uuid' }, - function (err, res) { - if (err) { - return console.log('Unable to set legal hold config for the object version', err.message) - } - console.log('Success') - }, -) +const legalholdStatus = await minioClient.setObjectLegalHold('bucketName', 'objectName', { + Status: 'ON', + versionId: 'my-obj-version-uuid', +}) ``` diff --git a/examples/get-object-legal-hold.js b/examples/get-object-legal-hold.js deleted file mode 100644 index 5644f926..00000000 --- a/examples/get-object-legal-hold.js +++ /dev/null @@ -1,56 +0,0 @@ -/* - * MinIO Javascript Library for Amazon S3 Compatible Cloud Storage, (C) 2021 MinIO, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are -// dummy values, please replace them with original values. - -var Minio = require('minio') - -var s3Client = new Minio.Client({ - endPoint: 's3.amazonaws.com', - accessKey: 'YOUR-ACCESSKEYID', - secretKey: 'YOUR-SECRETACCESSKEY', -}) - -//Get Legalhold config -s3Client.getObjectLegalHold('bucketName', 'objectName', {}, function (err, res) { - if (err) { - return console.log('Unable to get legal hold config for the object', err.message) // Print only the message. - } - console.log(res) -}) - -//With versionId -s3Client.getObjectLegalHold('bucketName', 'objectName', { versionId: 'my-obj-version-uuid' }, function (err, res) { - if (err) { - return console.log('Unable to get legal hold config for the object', err.message) // Print only the message. - } - console.log(res) -}) - -//Promise based version: -const objectLegalHoldPromise = s3Client.getObjectLegalHold('bucketName', 'objectName', { - versionId: 'my-obj-version-uuid', -}) -objectLegalHoldPromise - .then((data) => { - console.log('Success...', data) - }) - .catch((e) => { - // Print only the error message. if called on an object without object lock config. - // e.g: "The specified object does not have a ObjectLock configuration" - console.log(e.message) - }) diff --git a/examples/get-object-legal-hold.mjs b/examples/get-object-legal-hold.mjs new file mode 100644 index 00000000..95333fb4 --- /dev/null +++ b/examples/get-object-legal-hold.mjs @@ -0,0 +1,57 @@ +/* + * MinIO Javascript Library for Amazon S3 Compatible Cloud Storage, (C) 2021 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are +// dummy values, please replace them with original values. +import * as Minio from 'minio' + +const s3Client = new Minio.Client({ + endPoint: 's3.amazonaws.com', + accessKey: 'YOUR-ACCESSKEYID', + secretKey: 'YOUR-SECRETACCESSKEY', +}) + +s3Client.getObjectLegalHold('test-l-bucket', 'inspect-data.enc', (e, res) => { + if (e) { + console.log('CB Error 1', e.message) + return + } + console.log('CB Success Res', res) +}) + +s3Client.getObjectLegalHold('test-l-bucket', null, (e, res) => { + if (e) { + console.log('CB Error 2', e.message) + return + } + console.log('CB Success Res 2', res) +}) + +try { + const buckets = await s3Client.getObjectLegalHold('test-l-bucket', 'inspect-data.enc', { versionId: 1234 }) + console.log('Default Success', buckets) +} catch (err) { + console.log('Error::', err.message) +} +/* +// with version Id +try { + const buckets = await s3Client.getObjectLegalHold(null, 'inspect-data.enc', {versionId:'d24cbb1b-81c2-449d-b6d0-8a5f19b66b05'}) + console.log('Version Success', buckets) +} catch (err) { + console.log(err.message) +} +*/ diff --git a/examples/set-object-legal-hold.js b/examples/set-object-legal-hold.mjs similarity index 59% rename from examples/set-object-legal-hold.js rename to examples/set-object-legal-hold.mjs index 0b0f2c6a..32b555e7 100644 --- a/examples/set-object-legal-hold.js +++ b/examples/set-object-legal-hold.mjs @@ -17,31 +17,29 @@ // Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are // dummy values, please replace them with original values. -var Minio = require('minio') +import * as Minio from 'minio' -var s3Client = new Minio.Client({ +const s3Client = new Minio.Client({ endPoint: 's3.amazonaws.com', accessKey: 'YOUR-ACCESSKEYID', secretKey: 'YOUR-SECRETACCESSKEY', }) -//Set legal hold config of an object. -s3Client.setObjectLegalHold('bucketName', 'objectName', { status: 'ON' }, function (err, res) { - if (err) { - return console.log('Unable to set legal hold config for the object', err) - } +try { + //Set legal hold config of an object. + await s3Client.setObjectLegalHold('bucketname', 'objectName') console.log('Success') -}) +} catch (err) { + console.log(err.message) +} -//Set legal hold config of an object with versionId. -s3Client.setObjectLegalHold( - 'bucketName', - 'objectName', - { status: 'ON', versionId: 'my-obj-version-uuid' }, - function (err, res) { - if (err) { - return console.log('Unable to set legal hold config for the object version', err) - } - console.log('Success') - }, -) +try { + //Set legal hold config of an object version. + await s3Client.setObjectLegalHold('bucketname', 'objectName', { + status: 'ON', + versionId: '67d3a60d-e837-4218-96d3-44733cdc7c6f', + }) + console.log('Success') +} catch (err) { + console.log(err.message) +} diff --git a/src/internal/client.ts b/src/internal/client.ts index 4f0bc53f..e7f9908c 100644 --- a/src/internal/client.ts +++ b/src/internal/client.ts @@ -9,7 +9,7 @@ import xml2js from 'xml2js' import { CredentialProvider } from '../CredentialProvider.ts' import * as errors from '../errors.ts' -import { DEFAULT_REGION } from '../helpers.ts' +import { DEFAULT_REGION, LEGAL_HOLD_STATUS } from '../helpers.ts' import { signV4 } from '../signing.ts' import { Extensions } from './extensions.ts' import { @@ -43,7 +43,10 @@ import type { Binary, BucketItemFromList, BucketItemStat, + GetObjectLegalHoldOptions, IRequest, + LegalHoldStatus, + PutObjectLegalHoldOptions, ReplicationConfig, ReplicationConfigOpts, RequestHeaders, @@ -54,7 +57,7 @@ import type { } from './type.ts' import type { UploadedPart } from './xml-parser.ts' import * as xmlParsers from './xml-parser.ts' -import { parseInitiateMultipart } from './xml-parser.ts' +import { parseInitiateMultipart, parseObjectLegalHoldConfig } from './xml-parser.ts' // will be replaced by bundler. const Package = { version: process.env.MINIO_JS_PACKAGE_VERSION || 'development' } @@ -1031,4 +1034,91 @@ export class TypedClient { const xmlResult = await readAsString(httpRes) return xmlParsers.parseReplicationConfig(xmlResult) } + + getObjectLegalHold( + bucketName: string, + objectName: string, + getOpts?: GetObjectLegalHoldOptions, + callback?: ResultCallback, + ): Promise + async getObjectLegalHold( + bucketName: string, + objectName: string, + getOpts?: GetObjectLegalHoldOptions, + ): Promise { + { + if (!isValidBucketName(bucketName)) { + throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) + } + if (!isValidObjectName(objectName)) { + throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`) + } + + if (getOpts) { + if (!isObject(getOpts)) { + throw new TypeError('getOpts should be of type "Object"') + } else if (Object.keys(getOpts).length > 0 && getOpts.versionId && !isString(getOpts.versionId)) { + throw new TypeError('versionId should be of type string.:', getOpts.versionId) + } + } + + const method = 'GET' + let query = 'legal-hold' + + if (getOpts?.versionId) { + query += `&versionId=${getOpts.versionId}` + } + + const httpRes = await this.makeRequestAsync({ method, bucketName, objectName, query }, '', [200]) + const strRes = await readAsString(httpRes) + return parseObjectLegalHoldConfig(strRes) + } + } + + setObjectLegalHold(bucketName: string, objectName: string, setOpts?: PutObjectLegalHoldOptions): void + async setObjectLegalHold(bucketName: string, objectName: string, setOpts?: PutObjectLegalHoldOptions): Promise + async setObjectLegalHold(bucketName: string, objectName: string, setOpts?: PutObjectLegalHoldOptions): Promise { + if (!isValidBucketName(bucketName)) { + throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) + } + if (!isValidObjectName(objectName)) { + throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`) + } + + const defaultOpts = { + status: LEGAL_HOLD_STATUS.ENABLED, + } + if (!setOpts) { + setOpts = defaultOpts + } + + if (!isObject(setOpts)) { + throw new TypeError('setOpts should be of type "Object"') + } else { + if (![LEGAL_HOLD_STATUS.ENABLED, LEGAL_HOLD_STATUS.DISABLED].includes(setOpts?.status)) { + throw new TypeError('Invalid status: ' + setOpts.status) + } + if (setOpts.versionId && !setOpts.versionId.length) { + throw new TypeError('versionId should be of type string.:' + setOpts.versionId) + } + } + + const method = 'PUT' + let query = 'legal-hold' + + if (setOpts.versionId) { + query += `&versionId=${setOpts.versionId}` + } + + const config = { + Status: setOpts.status, + } + + const builder = new xml2js.Builder({ rootName: 'LegalHold', renderOpts: { pretty: false }, headless: true }) + const payload = builder.buildObject(config) + const headers: Record = {} + headers['Content-MD5'] = toMd5(payload) + + await this.makeRequestAsyncOmit({ method, bucketName, objectName, query, headers }, payload) + } } diff --git a/src/internal/type.ts b/src/internal/type.ts index ec9499d1..0c0abca0 100644 --- a/src/internal/type.ts +++ b/src/internal/type.ts @@ -187,3 +187,14 @@ export type ReplicationConfig = { /* Replication Config types */ export type ResultCallback = (error: Error | null, result: T) => void + +export type GetObjectLegalHoldOptions = { + versionId: string +} + +export type LegalHoldStatus = LEGAL_HOLD_STATUS + +export type PutObjectLegalHoldOptions = { + versionId?: string + status: LEGAL_HOLD_STATUS +} diff --git a/src/internal/xml-parser.ts b/src/internal/xml-parser.ts index bc5ec60e..0429f48c 100644 --- a/src/internal/xml-parser.ts +++ b/src/internal/xml-parser.ts @@ -241,3 +241,8 @@ export function parseReplicationConfig(xml: string): ReplicationConfig { }, } } + +export function parseObjectLegalHoldConfig(xml: string) { + const xmlObj = parseXml(xml) + return xmlObj.LegalHold +} diff --git a/src/minio.d.ts b/src/minio.d.ts index 6727ec10..354891ed 100644 --- a/src/minio.d.ts +++ b/src/minio.d.ts @@ -24,10 +24,13 @@ import type { BucketItemWithMetadata, BucketStream, ExistingObjectReplication, + GetObjectLegalHoldOptions, IncompleteUploadedBucketItem, ItemBucketMetadata, ItemBucketMetadataList, + LegalHoldStatus, MetadataItem, + PutObjectLegalHoldOptions, ReplicaModifications, ReplicationConfig, ReplicationConfigOpts, @@ -53,11 +56,14 @@ export type { BucketStream, ClientOptions, ExistingObjectReplication, + GetObjectLegalHoldOptions, IncompleteUploadedBucketItem, ItemBucketMetadata, ItemBucketMetadataList, + LegalHoldStatus, MetadataItem, NoResultCallback, + PutObjectLegalHoldOptions, RemoveOptions, ReplicaModifications, ReplicationConfig, @@ -104,7 +110,6 @@ export type LockUnit = RETENTION_VALIDITY_UNITS /** * @deprecated keep for backward compatible */ -export type LegalHoldStatus = LEGAL_HOLD_STATUS export type VersioningConfig = Record export type TagList = Record export type EmptyObject = Record @@ -425,28 +430,6 @@ export class Client extends TypedClient { ): void getObjectTagging(bucketName: string, objectName: string, getOptions?: VersionIdentificator): Promise - getObjectLegalHold(bucketName: string, objectName: string, callback: ResultCallback): void - getObjectLegalHold( - bucketName: string, - objectName: string, - getOptions: VersionIdentificator, - callback: ResultCallback, - ): void - getObjectLegalHold( - bucketName: string, - objectName: string, - getOptions?: VersionIdentificator, - ): Promise - - setObjectLegalHold(bucketName: string, objectName: string, callback: NoResultCallback): void - setObjectLegalHold( - bucketName: string, - objectName: string, - setOptions: LegalHoldOptions, - callback: NoResultCallback, - ): void - setObjectLegalHold(bucketName: string, objectName: string, setOptions?: LegalHoldOptions): Promise - composeObject( destObjConfig: CopyDestinationOptions, sourceObjList: CopySourceOptions[], diff --git a/src/minio.js b/src/minio.js index 36d695c8..b9866979 100644 --- a/src/minio.js +++ b/src/minio.js @@ -60,7 +60,7 @@ import { uriResourceEscape, } from './internal/helper.ts' import { PostPolicy } from './internal/post-policy.ts' -import { LEGAL_HOLD_STATUS, RETENTION_MODES, RETENTION_VALIDITY_UNITS } from './internal/type.ts' +import { RETENTION_MODES, RETENTION_VALIDITY_UNITS } from './internal/type.ts' import { NotificationConfig, NotificationPoller } from './notification.js' import { ObjectUploader } from './object-uploader.js' import { promisify } from './promisify.js' @@ -2239,109 +2239,6 @@ export class Client extends TypedClient { this.makeRequest({ method, bucketName, query }, '', [204], '', false, cb) } - getObjectLegalHold(bucketName, objectName, getOpts = {}, cb) { - if (!isValidBucketName(bucketName)) { - throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) - } - if (!isValidObjectName(objectName)) { - throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`) - } - - if (isFunction(getOpts)) { - cb = getOpts - getOpts = {} - } - - if (!isObject(getOpts)) { - throw new TypeError('getOpts should be of type "Object"') - } else if (Object.keys(getOpts).length > 0 && getOpts.versionId && !isString(getOpts.versionId)) { - throw new TypeError('versionId should be of type string.:', getOpts.versionId) - } - - if (!isFunction(cb)) { - throw new errors.InvalidArgumentError('callback should be of type "function"') - } - - const method = 'GET' - let query = 'legal-hold' - - if (getOpts.versionId) { - query += `&versionId=${getOpts.versionId}` - } - - this.makeRequest({ method, bucketName, objectName, query }, '', [200], '', true, (e, response) => { - if (e) { - return cb(e) - } - - let legalHoldConfig = Buffer.from('') - pipesetup(response, transformers.objectLegalHoldTransformer()) - .on('data', (data) => { - legalHoldConfig = data - }) - .on('error', cb) - .on('end', () => { - cb(null, legalHoldConfig) - }) - }) - } - - setObjectLegalHold(bucketName, objectName, setOpts = {}, cb) { - if (!isValidBucketName(bucketName)) { - throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) - } - if (!isValidObjectName(objectName)) { - throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`) - } - - const defaultOpts = { - status: LEGAL_HOLD_STATUS.ENABLED, - } - if (isFunction(setOpts)) { - cb = setOpts - setOpts = defaultOpts - } - - if (!isObject(setOpts)) { - throw new TypeError('setOpts should be of type "Object"') - } else { - if (![LEGAL_HOLD_STATUS.ENABLED, LEGAL_HOLD_STATUS.DISABLED].includes(setOpts.status)) { - throw new TypeError('Invalid status: ' + setOpts.status) - } - if (setOpts.versionId && !setOpts.versionId.length) { - throw new TypeError('versionId should be of type string.:' + setOpts.versionId) - } - } - - if (!isFunction(cb)) { - throw new errors.InvalidArgumentError('callback should be of type "function"') - } - - if (_.isEmpty(setOpts)) { - setOpts = { - defaultOpts, - } - } - - const method = 'PUT' - let query = 'legal-hold' - - if (setOpts.versionId) { - query += `&versionId=${setOpts.versionId}` - } - - let config = { - Status: setOpts.status, - } - - const builder = new xml2js.Builder({ rootName: 'LegalHold', renderOpts: { pretty: false }, headless: true }) - const payload = builder.buildObject(config) - const headers = {} - headers['Content-MD5'] = toMd5(payload) - - this.makeRequest({ method, bucketName, objectName, query, headers }, payload, [200], '', false, cb) - } - /** * Internal method to upload a part during compose object. * @param partConfig __object__ contains the following. @@ -2683,8 +2580,6 @@ Client.prototype.getObjectRetention = promisify(Client.prototype.getObjectRetent Client.prototype.setBucketEncryption = promisify(Client.prototype.setBucketEncryption) Client.prototype.getBucketEncryption = promisify(Client.prototype.getBucketEncryption) Client.prototype.removeBucketEncryption = promisify(Client.prototype.removeBucketEncryption) -Client.prototype.setObjectLegalHold = promisify(Client.prototype.setObjectLegalHold) -Client.prototype.getObjectLegalHold = promisify(Client.prototype.getObjectLegalHold) Client.prototype.composeObject = promisify(Client.prototype.composeObject) Client.prototype.selectObjectContent = promisify(Client.prototype.selectObjectContent) @@ -2696,3 +2591,5 @@ Client.prototype.listBuckets = callbackify(Client.prototype.listBuckets) Client.prototype.removeBucketReplication = callbackify(Client.prototype.removeBucketReplication) Client.prototype.setBucketReplication = callbackify(Client.prototype.setBucketReplication) Client.prototype.getBucketReplication = callbackify(Client.prototype.getBucketReplication) +Client.prototype.getObjectLegalHold = callbackify(Client.prototype.getObjectLegalHold) +Client.prototype.setObjectLegalHold = callbackify(Client.prototype.setObjectLegalHold) diff --git a/tests/unit/test.js b/tests/unit/test.js index 6905fb40..2f054b13 100644 --- a/tests/unit/test.js +++ b/tests/unit/test.js @@ -1456,17 +1456,28 @@ describe('Client', function () { }) describe('Object Legal Hold APIs', () => { - describe('getObjectLegalHold(bucketName, objectName, getOpts={}, cb)', () => { + describe('getObjectLegalHold(bucketName, objectName, getOpts={})', () => { it('should fail on null bucket', (done) => { try { - client.getObjectLegalHold(null, function () {}) + client.getObjectLegalHold(null, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } }) + it('should fail on empty bucket', (done) => { try { - client.getObjectLegalHold('', function () {}) + client.getObjectLegalHold('', function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } @@ -1474,53 +1485,89 @@ describe('Client', function () { it('should fail on null objectName', (done) => { try { - client.getObjectLegalHold('my-bucket', null, function () {}) + client.getObjectLegalHold('my-bucket', null, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } }) - it('should fail on null getOpts', (done) => { + it('should fail on invalid version id in getOpts', (done) => { try { - client.getObjectLegalHold('my-bucker', 'my-object', null, function () {}) + client.getObjectLegalHold('my-bucket', 'my-object', { versionId: 123 }, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } }) }) - describe('setObjectLegalHold(bucketName, objectName, setOpts={}, cb)', () => { + describe('setObjectLegalHold(bucketName, objectName, setOpts={})', () => { it('should fail on null bucket', (done) => { try { - client.setObjectLegalHold(null, function () {}) + client.setObjectLegalHold(null, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } }) it('should fail on empty bucket', (done) => { try { - client.setObjectLegalHold('', function () {}) + client.setObjectLegalHold('', function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } }) - it('should fail on null objectName', (done) => { + it('should fail on null object', (done) => { try { - client.setObjectLegalHold('my-bucket', null, function () {}) + client.setObjectLegalHold('my-bucket', null, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } }) it('should fail on null setOpts', (done) => { try { - client.setObjectLegalHold('my-bucker', 'my-object', null, function () {}) + client.setObjectLegalHold('my-bucket', 'my-object', null, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() } }) - it('should fail on empty versionId', (done) => { + + it('should fail on empty version', (done) => { try { - client.setObjectLegalHold('my-bucker', 'my-object', {}, function () {}) + client.setObjectLegalHold('my-bucket', 'my-object', {}, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) } catch (e) { done() }