From 79425dd620a72e835a2d3c5243dee0b563f93d7a Mon Sep 17 00:00:00 2001 From: Prakash Senthil Vel <23444145+prakashsvmx@users.noreply.github.com> Date: Sat, 4 Nov 2023 07:15:06 +0530 Subject: [PATCH] refactor getObjectLockConfig and setObjectLockConfig to ts (#1200) --- README.md | 4 +- docs/API.md | 37 ++---- ...k-config.js => get-object-lock-config.mjs} | 16 +-- ...k-config.js => set-object-lock-config.mjs} | 25 ++-- src/internal/client.ts | 85 +++++++++++++- src/internal/type.ts | 29 ++++- src/internal/xml-parser.ts | 34 +++++- src/minio.d.ts | 19 ++- src/minio.js | 92 +-------------- src/transformers.js | 4 - src/xml-parsers.js | 32 ----- tests/unit/test.js | 110 ++++++++++-------- 12 files changed, 248 insertions(+), 239 deletions(-) rename examples/{get-object-lock-config.js => get-object-lock-config.mjs} (78%) rename examples/{set-object-lock-config.js => set-object-lock-config.mjs} (72%) diff --git a/README.md b/README.md index f02be1e2..fb5ba099 100644 --- a/README.md +++ b/README.md @@ -183,8 +183,8 @@ The full API Reference is available here. - [set-bucket-lifecycle.js](https://github.com/minio/minio-js/blob/master/examples/set-bucket-lifecycle.js) - [get-bucket-lifecycle.js](https://github.com/minio/minio-js/blob/master/examples/get-bucket-lifecycle.js) - [remove-bucket-lifecycle.js](https://github.com/minio/minio-js/blob/master/examples/remove-bucket-lifecycle.js) -- [get-object-lock-config.js](https://github.com/minio/minio-js/blob/master/examples/get-object-lock-config.js) -- [set-object-lock-config.js](https://github.com/minio/minio-js/blob/master/examples/set-object-lock-config.js) +- [get-object-lock-config.mjs](https://github.com/minio/minio-js/blob/master/examples/get-object-lock-config.mjs) +- [set-object-lock-config.mjs](https://github.com/minio/minio-js/blob/master/examples/set-object-lock-config.mjs) - [set-bucket-replication.mjs](https://github.com/minio/minio-js/blob/master/examples/set-bucket-replication.mjs) - [get-bucket-replication.mjs](https://github.com/minio/minio-js/blob/master/examples/get-bucket-replication.mjs) - [remove-bucket-replication.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-bucket-replication.mjs) diff --git a/docs/API.md b/docs/API.md index 3d7e5c2c..4dfc5cb4 100644 --- a/docs/API.md +++ b/docs/API.md @@ -765,33 +765,22 @@ Set Object lock config on a Bucket **Parameters** -| Param | Type | Description | -| --------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `bucketName` | _string_ | Name of the bucket. | -| `lockConfig` | _object_ | Lock Configuration can be either `{}` to reset or object with all of the following key/value pairs: `{mode: ["COMPLIANCE"/'GOVERNANCE'], unit: ["Days"/"Years"], validity: }` | -| `callback(err)` | _function_ | Callback is called with `err` in case of error. | +| Param | Type | Description | +| ------------ | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `bucketName` | _string_ | Name of the bucket. | +| `lockConfig` | _object_ | Lock Configuration can be either `{}` to reset or object with all of the following key/value pairs: `{mode: ["COMPLIANCE"/'GOVERNANCE'], unit: ["Days"/"Years"], validity: }` | **Example 1** ```js -s3Client.setObjectLockConfig('my-bucketname', { mode: 'COMPLIANCE', unit: 'Days', validity: 10 }, function (err) { - if (err) { - return console.log(err) - } - console.log('Success') -}) +await minioClient.setObjectLockConfig('my-bucketname', { mode: 'COMPLIANCE', unit: 'Days', validity: 10 }) ``` **Example 2** To reset/remove object lock config on a bucket. ```js -s3Client.setObjectLockConfig('my-bucketname', {}, function (err) { - if (err) { - return console.log(err) - } - console.log('Success') -}) +await s3Client.setObjectLockConfig('my-bucketname', {}) ``` @@ -802,21 +791,15 @@ Get Lock config on a Bucket **Parameters** -| Param | Type | Description | -| --------------------------- | ---------- | ----------------------------------------------------------------------------------------- | -| `bucketName` | _string_ | Name of the bucket. | -| `callback(err, lockConfig)` | _function_ | Callback is called with `err` in case of error. else it is called with lock configuration | +| Param | Type | Description | +| ------------ | -------- | ------------------- | +| `bucketName` | _string_ | Name of the bucket. | **Example ** Get object lock configuration on a Bucket ```js -s3Client.getObjectLockConfig('my-bucketname', function (err, lockConfig) { - if (err) { - return console.log(err) - } - console.log(lockConfig) -}) +await minioClient.getObjectLockConfig('my-bucketname') ``` diff --git a/examples/get-object-lock-config.js b/examples/get-object-lock-config.mjs similarity index 78% rename from examples/get-object-lock-config.js rename to examples/get-object-lock-config.mjs index 42e044b9..35055427 100644 --- a/examples/get-object-lock-config.js +++ b/examples/get-object-lock-config.mjs @@ -17,17 +17,17 @@ // 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', }) -s3Client.getObjectLockConfig('my-bucketname', function (err, lockConfig) { - if (err) { - return console.log(err) - } - console.log(lockConfig) -}) +try { + const lockConfig = await s3Client.getObjectLockConfig('my-bucketname') + console.log('Success', lockConfig) +} catch (err) { + console.log('Error ', err.message) +} diff --git a/examples/set-object-lock-config.js b/examples/set-object-lock-config.mjs similarity index 72% rename from examples/set-object-lock-config.js rename to examples/set-object-lock-config.mjs index 3b5faae3..55519d24 100644 --- a/examples/set-object-lock-config.js +++ b/examples/set-object-lock-config.mjs @@ -17,26 +17,27 @@ // 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 Object lock on a bucket -s3Client.setObjectLockConfig('my-bucketname', { mode: 'COMPLIANCE', unit: 'Days', validity: 10 }, function (err) { - if (err) { - return console.log(err) - } +try { + await s3Client.setObjectLockConfig('my-bucketname', { mode: 'GOVERNANCE', unit: 'Days', validity: 20 }) console.log('Success') -}) +} catch (err) { + console.log('Error::', err.message) +} //To reset/remove object lock config. -s3Client.setObjectLockConfig('my-bucketname', {}, function (err) { - if (err) { - return console.log(err) - } +//Set Object lock on a bucket +try { + await s3Client.setObjectLockConfig('my-bucketname', {}) console.log('Success') -}) +} catch (err) { + console.log('Error::', err.message) +} diff --git a/src/internal/client.ts b/src/internal/client.ts index 82bea52e..cf6237d4 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, LEGAL_HOLD_STATUS } from '../helpers.ts' +import { DEFAULT_REGION, LEGAL_HOLD_STATUS, RETENTION_MODES, RETENTION_VALIDITY_UNITS } from '../helpers.ts' import { signV4 } from '../signing.ts' import { Extensions } from './extensions.ts' import { @@ -46,6 +46,8 @@ import type { GetObjectLegalHoldOptions, IRequest, LegalHoldStatus, + ObjectLockConfigParam, + ObjectLockInfo, PutObjectLegalHoldOptions, ReplicationConfig, ReplicationConfigOpts, @@ -1166,4 +1168,85 @@ export class TypedClient { const body = await readAsString(response) return xmlParsers.parseTagging(body) } + + getObjectLockConfig(bucketName: string, callback: ResultCallback): void + getObjectLockConfig(bucketName: string): void + async getObjectLockConfig(bucketName: string): Promise + async getObjectLockConfig(bucketName: string) { + if (!isValidBucketName(bucketName)) { + throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) + } + const method = 'GET' + const query = 'object-lock' + + const httpRes = await this.makeRequestAsync({ method, bucketName, query }) + const xmlResult = await readAsString(httpRes) + return xmlParsers.parseObjectLockConfig(xmlResult) + } + + setObjectLockConfig(bucketName: string, lockConfigOpts: Omit): void + async setObjectLockConfig( + bucketName: string, + lockConfigOpts: Omit, + ): Promise + async setObjectLockConfig(bucketName: string, lockConfigOpts: Omit) { + const retentionModes = [RETENTION_MODES.COMPLIANCE, RETENTION_MODES.GOVERNANCE] + const validUnits = [RETENTION_VALIDITY_UNITS.DAYS, RETENTION_VALIDITY_UNITS.YEARS] + + if (!isValidBucketName(bucketName)) { + throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) + } + + if (lockConfigOpts.mode && !retentionModes.includes(lockConfigOpts.mode)) { + throw new TypeError(`lockConfigOpts.mode should be one of ${retentionModes}`) + } + if (lockConfigOpts.unit && !validUnits.includes(lockConfigOpts.unit)) { + throw new TypeError(`lockConfigOpts.unit should be one of ${validUnits}`) + } + if (lockConfigOpts.validity && !isNumber(lockConfigOpts.validity)) { + throw new TypeError(`lockConfigOpts.validity should be a number`) + } + + const method = 'PUT' + const query = 'object-lock' + + const config: ObjectLockConfigParam = { + ObjectLockEnabled: 'Enabled', + } + const configKeys = Object.keys(lockConfigOpts) + + const isAllKeysSet = ['unit', 'mode', 'validity'].every((lck) => configKeys.includes(lck)) + // Check if keys are present and all keys are present. + if (configKeys.length > 0) { + if (!isAllKeysSet) { + throw new TypeError( + `lockConfigOpts.mode,lockConfigOpts.unit,lockConfigOpts.validity all the properties should be specified.`, + ) + } else { + config.Rule = { + DefaultRetention: {}, + } + if (lockConfigOpts.mode) { + config.Rule.DefaultRetention.Mode = lockConfigOpts.mode + } + if (lockConfigOpts.unit === RETENTION_VALIDITY_UNITS.DAYS) { + config.Rule.DefaultRetention.Days = lockConfigOpts.validity + } else if (lockConfigOpts.unit === RETENTION_VALIDITY_UNITS.YEARS) { + config.Rule.DefaultRetention.Years = lockConfigOpts.validity + } + } + } + + const builder = new xml2js.Builder({ + rootName: 'ObjectLockConfiguration', + renderOpts: { pretty: false }, + headless: true, + }) + const payload = builder.buildObject(config) + + const headers: RequestHeaders = {} + headers['Content-MD5'] = toMd5(payload) + + await this.makeRequestAsyncOmit({ method, bucketName, query, headers }, payload) + } } diff --git a/src/internal/type.ts b/src/internal/type.ts index 130bfff2..55b3cfb9 100644 --- a/src/internal/type.ts +++ b/src/internal/type.ts @@ -24,6 +24,7 @@ export type Encryption = KMSMasterKeyID?: string } +export type EnabledOrDisabledStatus = 'Enabled' | 'Disabled' export enum ENCRYPTION_TYPES { /** * SSEC represents server-side-encryption with customer provided keys @@ -137,7 +138,7 @@ export type StatObjectOpts = { /* Replication Config types */ export type ReplicationRuleStatus = { - Status: 'Enabled' | 'Disabled' + Status: EnabledOrDisabledStatus } export type Tag = { @@ -208,3 +209,29 @@ export type PutObjectLegalHoldOptions = { versionId?: string status: LEGAL_HOLD_STATUS } + +export type EmptyObject = Record + +export type ObjectLockInfo = + | { + objectLockEnabled: EnabledOrDisabledStatus + mode: RETENTION_MODES + unit: RETENTION_VALIDITY_UNITS + validity: number + } + | EmptyObject + +export type ObjectLockConfigParam = { + ObjectLockEnabled?: 'Enabled' | undefined + Rule?: + | { + DefaultRetention: + | { + Mode: RETENTION_MODES + Days: number + Years: number + } + | EmptyObject + } + | EmptyObject +} diff --git a/src/internal/xml-parser.ts b/src/internal/xml-parser.ts index 30e676c1..ba274fd5 100644 --- a/src/internal/xml-parser.ts +++ b/src/internal/xml-parser.ts @@ -5,7 +5,8 @@ import { XMLParser } from 'fast-xml-parser' import * as errors from '../errors.ts' import { isObject, parseXml, sanitizeETag, sanitizeObjectKey, toArray } from './helper.ts' import { readAsString } from './response.ts' -import type { BucketItemFromList, BucketItemWithMetadata, ReplicationConfig } from './type.ts' +import type { BucketItemFromList, BucketItemWithMetadata, ObjectLockInfo, ReplicationConfig } from './type.ts' +import { RETENTION_VALIDITY_UNITS } from './type.ts' // parse XML response for bucket region export function parseBucketRegion(xml: string): string { @@ -261,3 +262,34 @@ export function parseTagging(xml: string) { } return result } + +export function parseObjectLockConfig(xml: string): ObjectLockInfo { + const xmlObj = parseXml(xml) + let lockConfigResult = {} as ObjectLockInfo + if (xmlObj.ObjectLockConfiguration) { + lockConfigResult = { + objectLockEnabled: xmlObj.ObjectLockConfiguration.ObjectLockEnabled, + } as ObjectLockInfo + let retentionResp + if ( + xmlObj.ObjectLockConfiguration && + xmlObj.ObjectLockConfiguration.Rule && + xmlObj.ObjectLockConfiguration.Rule.DefaultRetention + ) { + retentionResp = xmlObj.ObjectLockConfiguration.Rule.DefaultRetention || {} + lockConfigResult.mode = retentionResp.Mode + } + if (retentionResp) { + const isUnitYears = retentionResp.Years + if (isUnitYears) { + lockConfigResult.validity = isUnitYears + lockConfigResult.unit = RETENTION_VALIDITY_UNITS.YEARS + } else { + lockConfigResult.validity = retentionResp.Days + lockConfigResult.unit = RETENTION_VALIDITY_UNITS.DAYS + } + } + } + + return lockConfigResult +} diff --git a/src/minio.d.ts b/src/minio.d.ts index 13162a2b..6d5c4ab7 100644 --- a/src/minio.d.ts +++ b/src/minio.d.ts @@ -28,8 +28,8 @@ import type { IncompleteUploadedBucketItem, ItemBucketMetadata, ItemBucketMetadataList, - LegalHoldStatus, MetadataItem, + ObjectLockInfo, PutObjectLegalHoldOptions, ReplicaModifications, ReplicationConfig, @@ -42,7 +42,6 @@ import type { ResultCallback, SourceSelectionCriteria, Tag, - VersionIdentificator, } from './internal/type.ts' export * from './helpers.ts' @@ -61,9 +60,9 @@ export type { IncompleteUploadedBucketItem, ItemBucketMetadata, ItemBucketMetadataList, - LegalHoldStatus, MetadataItem, NoResultCallback, + ObjectLockInfo, PutObjectLegalHoldOptions, RemoveOptions, ReplicaModifications, @@ -76,7 +75,6 @@ export type { ReplicationRuleStatus, SourceSelectionCriteria, Tag, - VersionIdentificator, } // Exports only from typings @@ -109,11 +107,15 @@ export type Mode = RETENTION_MODES */ 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 +export type VersionIdentificator = Pick export type Lifecycle = LifecycleConfig | null | '' -export type Lock = LockConfig | EmptyObject export type Encryption = EncryptionConfig | EmptyObject export type Retention = RetentionOptions | EmptyObject export type IsoDate = string @@ -268,13 +270,6 @@ export class Client extends TypedClient { removeBucketLifecycle(bucketName: string, callback: NoResultCallback): void removeBucketLifecycle(bucketName: string): Promise - setObjectLockConfig(bucketName: string, callback: NoResultCallback): void - setObjectLockConfig(bucketName: string, lockConfig: Lock, callback: NoResultCallback): void - setObjectLockConfig(bucketName: string, lockConfig?: Lock): Promise - - getObjectLockConfig(bucketName: string, callback: ResultCallback): void - getObjectLockConfig(bucketName: string): Promise - getBucketEncryption(bucketName: string, callback: ResultCallback): void getBucketEncryption(bucketName: string): Promise diff --git a/src/minio.js b/src/minio.js index 71cddf7d..98d2651c 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 { RETENTION_MODES, RETENTION_VALIDITY_UNITS } from './internal/type.ts' +import { RETENTION_MODES } from './internal/type.ts' import { NotificationConfig, NotificationPoller } from './notification.js' import { ObjectUploader } from './object-uploader.js' import { promisify } from './promisify.js' @@ -1898,92 +1898,6 @@ export class Client extends TypedClient { }) } - setObjectLockConfig(bucketName, lockConfigOpts = {}, cb) { - const retentionModes = [RETENTION_MODES.COMPLIANCE, RETENTION_MODES.GOVERNANCE] - const validUnits = [RETENTION_VALIDITY_UNITS.DAYS, RETENTION_VALIDITY_UNITS.YEARS] - - if (!isValidBucketName(bucketName)) { - throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) - } - - if (lockConfigOpts.mode && !retentionModes.includes(lockConfigOpts.mode)) { - throw new TypeError(`lockConfigOpts.mode should be one of ${retentionModes}`) - } - if (lockConfigOpts.unit && !validUnits.includes(lockConfigOpts.unit)) { - throw new TypeError(`lockConfigOpts.unit should be one of ${validUnits}`) - } - if (lockConfigOpts.validity && !isNumber(lockConfigOpts.validity)) { - throw new TypeError(`lockConfigOpts.validity should be a number`) - } - - const method = 'PUT' - const query = 'object-lock' - - let config = { - ObjectLockEnabled: 'Enabled', - } - const configKeys = Object.keys(lockConfigOpts) - // Check if keys are present and all keys are present. - if (configKeys.length > 0) { - if (_.difference(configKeys, ['unit', 'mode', 'validity']).length !== 0) { - throw new TypeError( - `lockConfigOpts.mode,lockConfigOpts.unit,lockConfigOpts.validity all the properties should be specified.`, - ) - } else { - config.Rule = { - DefaultRetention: {}, - } - if (lockConfigOpts.mode) { - config.Rule.DefaultRetention.Mode = lockConfigOpts.mode - } - if (lockConfigOpts.unit === RETENTION_VALIDITY_UNITS.DAYS) { - config.Rule.DefaultRetention.Days = lockConfigOpts.validity - } else if (lockConfigOpts.unit === RETENTION_VALIDITY_UNITS.YEARS) { - config.Rule.DefaultRetention.Years = lockConfigOpts.validity - } - } - } - - const builder = new xml2js.Builder({ - rootName: 'ObjectLockConfiguration', - renderOpts: { pretty: false }, - headless: true, - }) - const payload = builder.buildObject(config) - - const headers = {} - headers['Content-MD5'] = toMd5(payload) - - this.makeRequest({ method, bucketName, query, headers }, payload, [200], '', false, cb) - } - - getObjectLockConfig(bucketName, cb) { - if (!isValidBucketName(bucketName)) { - throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) - } - if (!isFunction(cb)) { - throw new errors.InvalidArgumentError('callback should be of type "function"') - } - const method = 'GET' - const query = 'object-lock' - - this.makeRequest({ method, bucketName, query }, '', [200], '', true, (e, response) => { - if (e) { - return cb(e) - } - - let objectLockConfig = Buffer.from('') - pipesetup(response, transformers.objectLockTransformer()) - .on('data', (data) => { - objectLockConfig = data - }) - .on('error', cb) - .on('end', () => { - cb(null, objectLockConfig) - }) - }) - } - putObjectRetention(bucketName, objectName, retentionOpts = {}, cb) { if (!isValidBucketName(bucketName)) { throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName) @@ -2496,8 +2410,6 @@ Client.prototype.removeObjectTagging = promisify(Client.prototype.removeObjectTa Client.prototype.setBucketLifecycle = promisify(Client.prototype.setBucketLifecycle) Client.prototype.getBucketLifecycle = promisify(Client.prototype.getBucketLifecycle) Client.prototype.removeBucketLifecycle = promisify(Client.prototype.removeBucketLifecycle) -Client.prototype.setObjectLockConfig = promisify(Client.prototype.setObjectLockConfig) -Client.prototype.getObjectLockConfig = promisify(Client.prototype.getObjectLockConfig) Client.prototype.putObjectRetention = promisify(Client.prototype.putObjectRetention) Client.prototype.getObjectRetention = promisify(Client.prototype.getObjectRetention) Client.prototype.setBucketEncryption = promisify(Client.prototype.setBucketEncryption) @@ -2518,3 +2430,5 @@ Client.prototype.getObjectLegalHold = callbackify(Client.prototype.getObjectLega Client.prototype.setObjectLegalHold = callbackify(Client.prototype.setObjectLegalHold) Client.prototype.getBucketTagging = callbackify(Client.prototype.getBucketTagging) Client.prototype.getObjectTagging = callbackify(Client.prototype.getObjectTagging) +Client.prototype.setObjectLockConfig = callbackify(Client.prototype.setObjectLockConfig) +Client.prototype.getObjectLockConfig = callbackify(Client.prototype.getObjectLockConfig) diff --git a/src/transformers.js b/src/transformers.js index 4e2564c3..b12f27eb 100644 --- a/src/transformers.js +++ b/src/transformers.js @@ -145,10 +145,6 @@ export function lifecycleTransformer() { return getConcater(xmlParsers.parseLifecycleConfig) } -export function objectLockTransformer() { - return getConcater(xmlParsers.parseObjectLockConfig) -} - export function objectRetentionTransformer() { return getConcater(xmlParsers.parseObjectRetentionConfig) } diff --git a/src/xml-parsers.js b/src/xml-parsers.js index ffe7fdfa..96a66eed 100644 --- a/src/xml-parsers.js +++ b/src/xml-parsers.js @@ -28,7 +28,6 @@ import { sanitizeSize, toArray, } from './internal/helper.ts' -import { RETENTION_VALIDITY_UNITS } from './internal/type.ts' const fxpWithoutNumParser = new XMLParser({ numberParseOptions: { @@ -386,37 +385,6 @@ export function parseLifecycleConfig(xml) { const xmlObj = parseXml(xml) return xmlObj.LifecycleConfiguration } - -export function parseObjectLockConfig(xml) { - const xmlObj = parseXml(xml) - let lockConfigResult = {} - if (xmlObj.ObjectLockConfiguration) { - lockConfigResult = { - objectLockEnabled: xmlObj.ObjectLockConfiguration.ObjectLockEnabled, - } - let retentionResp - if ( - xmlObj.ObjectLockConfiguration && - xmlObj.ObjectLockConfiguration.Rule && - xmlObj.ObjectLockConfiguration.Rule.DefaultRetention - ) { - retentionResp = xmlObj.ObjectLockConfiguration.Rule.DefaultRetention || {} - lockConfigResult.mode = retentionResp.Mode - } - if (retentionResp) { - const isUnitYears = retentionResp.Years - if (isUnitYears) { - lockConfigResult.validity = isUnitYears - lockConfigResult.unit = RETENTION_VALIDITY_UNITS.YEARS - } else { - lockConfigResult.validity = retentionResp.Days - lockConfigResult.unit = RETENTION_VALIDITY_UNITS.DAYS - } - } - return lockConfigResult - } -} - export function parseObjectRetentionConfig(xml) { const xmlObj = parseXml(xml) const retentionConfig = xmlObj.Retention diff --git a/tests/unit/test.js b/tests/unit/test.js index ed19d368..14135d9b 100644 --- a/tests/unit/test.js +++ b/tests/unit/test.js @@ -1074,71 +1074,81 @@ describe('Client', function () { describe('Object Locking APIs', () => { describe('getObjectLockConfig(bucket, callback)', () => { it('should fail on null bucket', (done) => { - try { - client.getObjectLockConfig(null, function () {}) - } catch (e) { - done() - } + client.getObjectLockConfig(null, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) }) - it('should fail on empty bucket', (done) => { - try { - client.getObjectLockConfig('', function () {}) - } catch (e) { - done() - } + it('should fail on null bucket', (done) => { + client.getObjectLockConfig('', function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) }) }) describe('setObjectLockConfig(bucket, lockConfig, callback)', () => { it('should fail on null bucket', (done) => { - try { - client.setObjectLockConfig(null, function () {}) - } catch (e) { - done() - } + client.setObjectLockConfig(null, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) }) - it('should fail on empty bucket', (done) => { - try { - client.setObjectLockConfig('', function () {}) - } catch (e) { - done() - } + it('should fail on null bucket', (done) => { + client.setObjectLockConfig('', function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) }) it('should fail on passing invalid mode ', (done) => { - try { - client.setObjectLockConfig('my-bucket', { mode: 'invalid_mode' }, function () {}) - } catch (e) { - done() - } + client.setObjectLockConfig('my-bucket', { mode: 'invalid_mode' }, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) }) it('should fail on passing invalid unit ', (done) => { - try { - client.setObjectLockConfig('my-bucket', { mode: 'COMPLIANCE', unit: 'invalid_unit' }, function () {}) - } catch (e) { - done() - } + client.setObjectLockConfig('my-bucket', { mode: 'COMPLIANCE', unit: 'invalid_unit' }, function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }) }) + it('should fail on passing invalid validity ', (done) => { - try { - client.setObjectLockConfig( - 'my-bucket', - { mode: 'COMPLIANCE', unit: 'invalid_unit', validity: '' }, - function () {}, - ) - } catch (e) { - done() - } + client.setObjectLockConfig( + 'my-bucket', + { mode: 'COMPLIANCE', unit: 'invalid_unit', validity: '' }, + function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }, + ) }) + it('should fail on passing invalid config ', (done) => { - try { - client.setObjectLockConfig( - 'my-bucket', - { mode: 'COMPLIANCE', randomProp: true, nonExisting: false }, - function () {}, - ) - } catch (e) { - done() - } + client.setObjectLockConfig( + 'my-bucket', + { mode: 'COMPLIANCE', randomProp: true, nonExisting: false }, + function (err) { + if (err) { + return done() + } + done(new Error('callback should receive error')) + }, + ) }) }) })