-
Notifications
You must be signed in to change notification settings - Fork 252
ft/ZENKO-229 get using location #1204
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
lib/api/apiUtils/object/getReplicationBackendDataLocator.js
This file contains hidden or 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,45 @@ | ||
| const { errors } = require('arsenal'); | ||
|
|
||
| /** | ||
| * getReplicationBackendDataLocator - compares given location constraint to | ||
| * replication backends | ||
| * @param {object} locationObj - object containing location information | ||
| * @param {string} locationObj.location - name of location constraint | ||
| * @param {string} locationObj.key - keyname of object in location constraint | ||
| * @param {string} locationObj.locationType - type of location constraint | ||
| * @param {object} replicationInfo - information about object replication | ||
| * @param {array} replicationInfo.backends - array containing information about | ||
| * each replication location | ||
| * @param {string} replicationInfo.backends[].site - name of replication | ||
| * location | ||
| * @param {string} replicationInfo.backends[].status - status of replication | ||
| * @param {string} replicationInfo.backends[].dataStoreVersionId - version id | ||
| * of object at replication location | ||
| * @return {object} contains error if no replication backend matches or | ||
| * dataLocator object | ||
| */ | ||
| function getReplicationBackendDataLocator(locationObj, replicationInfo) { | ||
| const repBackendResult = {}; | ||
| const locMatch = replicationInfo.backends.find( | ||
| backend => backend.site === locationObj.location); | ||
| if (!locMatch) { | ||
| repBackendResult.error = errors.InvalidLocationConstraint. | ||
| customizeDescription('Object is not replicated to location ' + | ||
| 'passed in location header'); | ||
| return repBackendResult; | ||
| } | ||
| if (['PENDING', 'FAILED'].includes(locMatch.status)) { | ||
| repBackendResult.error = errors.NoSuchKey.customizeDescription( | ||
| `Object replication to specified backend is ${locMatch.status}`); | ||
| repBackendResult.status = locMatch.status; | ||
| return repBackendResult; | ||
| } | ||
| repBackendResult.dataLocator = [{ | ||
| key: locationObj.key, | ||
| dataStoreName: locationObj.location, | ||
| dataStoreType: locationObj.locationType, | ||
| dataStoreVersionId: locMatch.dataStoreVersionId }]; | ||
| return repBackendResult; | ||
| } | ||
|
|
||
| module.exports = getReplicationBackendDataLocator; |
This file contains hidden or 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,37 @@ | ||
| const { errors } = require('arsenal'); | ||
|
|
||
| const { config } = require('../../../Config'); | ||
|
|
||
| /** | ||
| * locationHeaderCheck - compares 'x-amz-location-constraint' header | ||
| * to location constraints in config | ||
| * @param {object} headers - request headers | ||
| * @param {string} objectKey - key name of object | ||
| * @param {string} bucketName - name of bucket | ||
| * @return {undefined|Object} returns error, object, or undefined | ||
| * @return {string} return.location - name of location constraint | ||
| * @return {string} return.key - name of object at location constraint | ||
| * @return {string} - return.locationType - type of location constraint | ||
| */ | ||
| function locationHeaderCheck(headers, objectKey, bucketName) { | ||
| const location = headers['x-amz-location-constraint']; | ||
| if (location) { | ||
| const validLocation = config.locationConstraints[location]; | ||
| if (!validLocation) { | ||
| return errors.InvalidLocationConstraint.customizeDescription( | ||
| 'Invalid location constraint specified in header'); | ||
| } | ||
| const bucketMatch = validLocation.details.bucketMatch; | ||
| const backendKey = bucketMatch ? objectKey : | ||
| `${bucketName}/${objectKey}`; | ||
| return { | ||
| location, | ||
| key: backendKey, | ||
| locationType: validLocation.type, | ||
| }; | ||
| } | ||
| // no location header was passed | ||
| return undefined; | ||
| } | ||
|
|
||
| module.exports = locationHeaderCheck; |
This file contains hidden or 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
53 changes: 53 additions & 0 deletions
53
tests/unit/multipleBackend/getReplicationBackendDataLocator.js
This file contains hidden or 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,53 @@ | ||
| const assert = require('assert'); | ||
|
|
||
| const getReplicationBackendDataLocator = require( | ||
| '../../../lib/api/apiUtils/object/getReplicationBackendDataLocator'); | ||
|
|
||
| const locCheckResult = { | ||
| location: 'spoofbackend', | ||
| key: 'spoofkey', | ||
| locationType: 'spoof', | ||
| }; | ||
| const repNoMatch = { backends: [{ site: 'nomatch' }] }; | ||
| const repMatchPending = { backends: | ||
| [{ site: 'spoofbackend', status: 'PENDING', dataVersionId: '' }] }; | ||
| const repMatchFailed = { backends: | ||
| [{ site: 'spoofbackend', status: 'FAILED', dataVersionId: '' }] }; | ||
| const repMatch = { backends: [{ | ||
| site: 'spoofbackend', | ||
| status: 'COMPLETE', | ||
| dataStoreVersionId: 'spoofid' }], | ||
| }; | ||
| const expDataLocator = [{ | ||
| key: locCheckResult.key, | ||
| dataStoreName: locCheckResult.location, | ||
| dataStoreType: locCheckResult.locationType, | ||
| dataStoreVersionId: repMatch.backends[0].dataStoreVersionId, | ||
| }]; | ||
|
|
||
|
|
||
| describe('Replication Backend Compare', () => { | ||
| it('should return error if no match in replication backends', () => { | ||
| const repBackendResult = | ||
| getReplicationBackendDataLocator(locCheckResult, repNoMatch); | ||
| assert(repBackendResult.error.InvalidLocationConstraint); | ||
| }); | ||
| it('should return error if backend status is PENDING', () => { | ||
| const repBackendResult = | ||
| getReplicationBackendDataLocator(locCheckResult, repMatchPending); | ||
| assert(repBackendResult.error.NoSuchKey); | ||
| assert.strictEqual(repBackendResult.status, 'PENDING'); | ||
| }); | ||
| it('should return error if backend status is FAILED', () => { | ||
| const repBackendResult = | ||
| getReplicationBackendDataLocator(locCheckResult, repMatchFailed); | ||
| assert(repBackendResult.error.NoSuchKey); | ||
| assert.strictEqual(repBackendResult.status, 'FAILED'); | ||
| }); | ||
| it('should return dataLocator obj if backend matches and rep is complete', | ||
| () => { | ||
| const repBackendResult = | ||
| getReplicationBackendDataLocator(locCheckResult, repMatch); | ||
| assert.deepStrictEqual(repBackendResult.dataLocator, expDataLocator); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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,45 @@ | ||
| const assert = require('assert'); | ||
| const { errors } = require('arsenal'); | ||
|
|
||
| const locationHeaderCheck = | ||
| require('../../../lib/api/apiUtils/object/locationHeaderCheck'); | ||
|
|
||
| const objectKey = 'locationHeaderCheckObject'; | ||
| const bucketName = 'locationHeaderCheckBucket'; | ||
|
|
||
| const testCases = [ | ||
| { | ||
| location: 'doesnotexist', | ||
| expRes: errors.InvalidLocationConstraint.customizeDescription( | ||
| 'Invalid location constraint specified in header'), | ||
| }, { | ||
| location: '', | ||
| expRes: undefined, | ||
| }, { | ||
| location: 'awsbackend', | ||
| expRes: { | ||
| location: 'awsbackend', | ||
| key: objectKey, | ||
| locationType: 'aws_s3', | ||
| }, | ||
| }, { | ||
| location: 'awsbackendmismatch', | ||
| expRes: { | ||
| location: 'awsbackendmismatch', | ||
| key: `${bucketName}/${objectKey}`, | ||
| locationType: 'aws_s3', | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| describe('Location Header Check', () => { | ||
| testCases.forEach(test => { | ||
| it('should return expected result with location constraint header ' + | ||
| `set to ${test.location}`, () => { | ||
| const headers = { 'x-amz-location-constraint': `${test.location}` }; | ||
| const checkRes = | ||
| locationHeaderCheck(headers, objectKey, bucketName); | ||
| assert.deepStrictEqual(checkRes, test.expRes); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can return the callback early avoid any further processing.