Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTEGRATION [PR#1198 > development/8.1] ft: S3C-3118-flatten-objmd-retentioninfo #1204

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions lib/models/ObjectMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,25 +1084,39 @@ class ObjectMD {
}

/**
* Set object retention information
* @param {object} retentionInfo - retention information object
* Set object retention mode
* @param {string} mode - should be one of 'GOVERNANCE', 'COMPLIANCE'
* @return {ObjectMD} itself
*/
setRetentionInfo(retentionInfo) {
const { mode, retainUntilDate } = retentionInfo;
this._data.retentionInfo = {
mode,
retainUntilDate,
};
setRetentionMode(mode) {
this._data.retentionMode = mode;
return this;
}

/**
* Returns object retention information
* @return {object} metadata object
* Set object retention retain until date
* @param {string} date - date in ISO-8601 format
* @return {ObjectMD} itself
*/
setRetentionDate(date) {
this._data.retentionDate = date;
return this;
}

/**
* Returns object retention mode
* @return {string} retention mode string
*/
getRetentionMode() {
return this._data.retentionMode;
}

/**
* Returns object retention retain until date
* @return {string} retention date string
*/
getRetentionInfo() {
return this._data.retentionInfo;
getRetentionDate() {
return this._data.retentionDate;
}

/**
Expand Down
20 changes: 9 additions & 11 deletions lib/s3middleware/objectRetention.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,14 @@ function validateRetention(parsedXml) {
retentionObj.error = dateObj.error;
return retentionObj;
}
retentionObj.retention = {
mode: modeObj.mode,
retainUntilDate: dateObj.date,
};
retentionObj.mode = modeObj.mode;
retentionObj.date = dateObj.date;
return retentionObj;
}

/**
* parseRetentionXml - Parse and validate xml body, returning callback with
* object retentionInfo: { mode: <value>, retainUntilDate: <value> }
* object retentionObj: { mode: <value>, date: <value> }
* @param {string} xml - xml body to parse and validate
* @param {object} log - Werelogs logger
* @param {function} cb - callback to server
Expand Down Expand Up @@ -135,16 +133,16 @@ function parseRetentionXml(xml, log, cb) {

/**
* convertToXml - Convert retention info object to xml
* @param {object} retentionInfo - retention information object
* @param {string} mode - retention mode
* @param {string} date - retention retain until date
* @return {string} - returns retention information xml string
*/
function convertToXml(retentionInfo) {
function convertToXml(mode, date) {
const xml = [];
const ret = retentionInfo.retention;
xml.push('<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">');
if (ret && ret.mode && ret.retainUntilDate) {
xml.push(`<Mode>${ret.mode}</Mode>`);
xml.push(`<RetainUntilDate>${ret.retainUntilDate}</RetainUntilDate>`);
if (mode && date) {
xml.push(`<Mode>${mode}</Mode>`);
xml.push(`<RetainUntilDate>${date}</RetainUntilDate>`);
} else {
return '';
}
Expand Down
25 changes: 12 additions & 13 deletions tests/unit/models/ObjectMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const constants = require('../../../lib/constants');

const retainDate = new Date();
retainDate.setDate(retainDate.getDate() + 1);
const laterDate = new Date();
laterDate.setDate(laterDate.getDate() + 5);

describe('ObjectMD class setters/getters', () => {
let md = null;
Expand Down Expand Up @@ -124,10 +126,8 @@ describe('ObjectMD class setters/getters', () => {
}],
['LegalHold', null, false],
['LegalHold', true],
['RetentionInfo', {
mode: 'GOVERNANCE',
retainUntilDate: retainDate.toISOString(),
}],
['RetentionMode', 'GOVERNANCE'],
['RetentionDate', retainDate.toISOString()],
].forEach(test => {
const property = test[0];
const testValue = test[1];
Expand Down Expand Up @@ -286,15 +286,14 @@ describe('ObjectMD class setters/getters', () => {
});
});

it('ObjectMD::set/getRetentionInfo', () => {
md.setRetentionInfo({
mode: 'COMPLIANCE',
retainUntilDate: retainDate.toISOString(),
});
assert.deepStrictEqual(md.getRetentionInfo(), {
mode: 'COMPLIANCE',
retainUntilDate: retainDate.toISOString(),
});
it('ObjectMD::set/getRetentionMode', () => {
md.setRetentionMode('COMPLIANCE');
assert.deepStrictEqual(md.getRetentionMode(), 'COMPLIANCE');
});

it('ObjectMD::set/getRetentionDate', () => {
md.setRetentionDate(laterDate.toISOString());
assert.deepStrictEqual(md.getRetentionDate(), laterDate.toISOString());
});
});

Expand Down
17 changes: 7 additions & 10 deletions tests/unit/s3middleware/objectRetention.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ function buildXml(key, value) {
}

const expectedRetention = {
retention: {
mode: 'GOVERNANCE',
retainUntilDate: passDate.toISOString(),
},
mode: 'GOVERNANCE',
date: passDate.toISOString(),
};

const expectedXml =
Expand Down Expand Up @@ -109,21 +107,20 @@ describe('object Retention validation', () => {
});

describe('object Retention xml', () => {
it('should return empty string if no retention info', done => {
const xml = convertToXml({});
it('should return empty string if no retention date', done => {
const xml = convertToXml('GOVERNANCE', '');
assert.equal(xml, '');
done();
});

it('should return empty string if retention info is empty', done => {
const xml = convertToXml(
{ retention: { mode: '', retainUntilDate: '' } });
it('should return empty string if no retention mode', done => {
const xml = convertToXml('', passDate.toISOString());
assert.equal(xml, '');
done();
});

it('should return xml string', done => {
const xml = convertToXml(expectedRetention);
const xml = convertToXml('GOVERNANCE', passDate.toISOString());
assert.strictEqual(xml, expectedXml);
done();
});
Expand Down