Skip to content

Commit

Permalink
Ensure TagSet defaults to an empty array instead of undefined
Browse files Browse the repository at this point in the history
There are various places where the TagSet is assumed to always be an array.
However, Dell ECS may return situations where the TagSet is outright
undefined instead of yielding an empty array. We compensate for this to
prevent any undesired undefined parsing issues.

Signed-off-by: Jeremy Ho <[email protected]>
  • Loading branch information
jujaga committed Sep 1, 2023
1 parent af39350 commit 9ccc10c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 10 additions & 4 deletions app/src/controllers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ const controller = {
}
// get existing tags on source object, eg: { 'animal': 'bear', colour': 'black' }
const sourceObject = await storageService.getObjectTagging({ filePath: objPath, s3VersionId: sourceS3VersionId, bucketId: bucketId });
const sourceTags = Object.assign({}, ...(sourceObject.TagSet.map(item => ({ [item.Key]: item.Value }))));
const sourceTags = Object.assign({}, {
...(sourceObject.TagSet?.map(item => ({ [item.Key]: item.Value })))
});

const metadataToAppend = getMetadata(req.headers);
const data = {
Expand Down Expand Up @@ -179,7 +181,7 @@ const controller = {

const newSet = newTags
// Join new tags and existing tags
.concat(existingTags)
.concat(existingTags ?? [])
// remove existing 'coms-id' tag if it exists
.filter(x => x.Key !== 'coms-id')
// filter duplicates
Expand Down Expand Up @@ -528,7 +530,9 @@ const controller = {
s3VersionId: sourceS3VersionId,
bucketId: bucketId
});
const sourceTags = Object.assign({}, ...(sourceObject.TagSet.map(item => ({ [item.Key]: item.Value }))));
const sourceTags = Object.assign({}, {
...(sourceObject.TagSet?.map(item => ({ [item.Key]: item.Value })))
});

const data = {
bucketId: bucketId,
Expand Down Expand Up @@ -892,7 +896,9 @@ const controller = {
s3VersionId: sourceS3VersionId,
bucketId: bucketId
});
const sourceTags = Object.assign({}, ...(sourceObject.TagSet.map(item => ({ [item.Key]: item.Value }))));
const sourceTags = Object.assign({}, {
...(sourceObject.TagSet?.map(item => ({ [item.Key]: item.Value })))
});

const newMetadata = getMetadata(req.headers);

Expand Down
15 changes: 9 additions & 6 deletions app/src/services/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const service = {
let objId = uuidv4();

if (typeof s3Object === 'object') { // If regular S3 Object
const { TagSet } = await storageService.getObjectTagging({ filePath: path, bucketId: bucketId });
const TagSet = await storageService.getObjectTagging({ filePath: path, bucketId: bucketId }).then(result => result.TagSet ?? []);
const s3ObjectComsId = TagSet.find(obj => (obj.Key === 'coms-id'))?.Value;

if (s3ObjectComsId && uuidValidate(s3ObjectComsId)) {
Expand All @@ -48,12 +48,12 @@ const service = {
const { Versions } = await storageService.listAllObjectVersions({ filePath: path, bucketId: bucketId });

for (const versionId of Versions.map(version => version.VersionId)) {
const result = await storageService.getObjectTagging({
const TagSet = await storageService.getObjectTagging({
filePath: path,
s3VersionId: versionId,
bucketId: bucketId
});
const oldObjId = result?.TagSet.find(obj => obj.Key === 'coms-id')?.Value;
}).then(result => result.TagSet ?? []);
const oldObjId = TagSet.find(obj => obj.Key === 'coms-id')?.Value;

if (oldObjId && uuidValidate(oldObjId)) {
objId = oldObjId;
Expand Down Expand Up @@ -331,13 +331,16 @@ const service = {
// COMS Tags
const comsTags = comsTagsForVersion[0]?.tagset ?? [];
// S3 Tags
const s3Tags = toLowerKeys(s3TagsForVersion?.TagSet);
const s3Tags = toLowerKeys(s3TagsForVersion?.TagSet ?? []);

// Ensure `coms-id` tag exists on this version in S3
if (s3Tags.length < 10 && !s3Tags.find(s3T => s3T.key === 'coms-id')) {
await storageService.putObjectTagging({
filePath: path,
tags: s3TagsForVersion?.TagSet.concat([{ Key: 'coms-id', Value: comsVersion.objectId }]),
tags: (s3TagsForVersion?.TagSet ?? []).concat([{
Key: 'coms-id',
Value: comsVersion.objectId
}]),
s3VersionId: comsVersion.s3VersionId,
bucketId: bucketId,
});
Expand Down

0 comments on commit 9ccc10c

Please sign in to comment.