-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load metadata from mapped path (#298)
* feat: load metadata from mapped path * fix: always provide a default in `state.mappedMetadata` * fix: add surrogate key * fix: no mapped metadata when target is in code-bus * fix: getModifiers missing
- Loading branch information
1 parent
e51b14f
commit a1fc7b0
Showing
8 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,55 @@ | ||
/* | ||
* Copyright 2021 Adobe. All rights reserved. | ||
* This file is licensed to you 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 REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
import { PipelineStatusError } from '../PipelineStatusError.js'; | ||
import { Modifiers } from '../utils/modifiers.js'; | ||
|
||
/** | ||
* Loads metadata for a mapped path and puts it into `state.mappedMetadata`. If path is not | ||
* mapped or there is no `metadata.json` at that path, puts an `EMPTY` Modifiers object into | ||
* `state.mappedMetadata`. | ||
* | ||
* @type PipelineStep | ||
* @param {PipelineState} state | ||
* @returns {Promise<void>} | ||
*/ | ||
export default async function fetchMappedMetadata(state) { | ||
state.mappedMetadata = Modifiers.EMPTY; | ||
if (!state.mapped) { | ||
return; | ||
} | ||
const { contentBusId, partition } = state; | ||
const metadataPath = `${state.info.path}/metadata.json`; | ||
const key = `${contentBusId}/${partition}${metadataPath}`; | ||
const ret = await state.s3Loader.getObject('helix-content-bus', key); | ||
if (ret.status === 200) { | ||
let json; | ||
try { | ||
json = JSON.parse(ret.body); | ||
} catch (e) { | ||
throw new PipelineStatusError(500, `failed parsing of ${metadataPath}: ${e.message}`); | ||
} | ||
|
||
const { data } = json.default ?? json; | ||
if (!Array.isArray(data)) { | ||
throw new PipelineStatusError(500, `failed loading of ${metadataPath}: data must be an array`); | ||
} | ||
|
||
state.mappedMetadata = Modifiers.fromModifierSheet( | ||
data, | ||
); | ||
return; | ||
} | ||
if (ret.status !== 404) { | ||
throw new PipelineStatusError(502, `failed to load ${metadataPath}: ${ret.status}`); | ||
} | ||
} |
This file contains 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 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 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 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,75 @@ | ||
/* | ||
* Copyright 2018 Adobe. All rights reserved. | ||
* This file is licensed to you 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 REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
/* eslint-env mocha */ | ||
import assert from 'assert'; | ||
import { PipelineStatusError } from '../../src/index.js'; | ||
import { StaticS3Loader } from '../StaticS3Loader.js'; | ||
import fetchMappedMetadata from '../../src/steps/fetch-mapped-metadata.js'; | ||
|
||
describe('Fetch Mapped Metadata', () => { | ||
it('throws error on invalid json', async () => { | ||
const promise = fetchMappedMetadata({ | ||
log: console, | ||
contentBusId: 'foo-id', | ||
partition: 'live', | ||
mapped: true, | ||
info: { | ||
path: '/mapped', | ||
}, | ||
s3Loader: new StaticS3Loader() | ||
.reply('helix-content-bus', 'foo-id/live/mapped/metadata.json', { | ||
status: 200, | ||
body: 'this is no json!', | ||
headers: new Map(), | ||
}), | ||
}); | ||
await assert.rejects(promise, new PipelineStatusError(500, 'failed parsing of /mapped/metadata.json: Unexpected token h in JSON at position 1')); | ||
}); | ||
|
||
it('throws error on metadata with no data array', async () => { | ||
const promise = fetchMappedMetadata({ | ||
log: console, | ||
contentBusId: 'foo-id', | ||
partition: 'live', | ||
mapped: true, | ||
info: { | ||
path: '/mapped', | ||
}, | ||
s3Loader: new StaticS3Loader() | ||
.reply('helix-content-bus', 'foo-id/live/mapped/metadata.json', { | ||
status: 200, | ||
body: '{}', | ||
headers: new Map(), | ||
}), | ||
}); | ||
await assert.rejects(promise, new PipelineStatusError(500, 'failed loading of /mapped/metadata.json: data must be an array')); | ||
}); | ||
|
||
it('throws error on generic error', async () => { | ||
const promise = fetchMappedMetadata({ | ||
log: console, | ||
contentBusId: 'foo-id', | ||
partition: 'live', | ||
mapped: true, | ||
info: { | ||
path: '/mapped', | ||
}, | ||
s3Loader: new StaticS3Loader() | ||
.reply('helix-content-bus', 'foo-id/live/mapped/metadata.json', { | ||
status: 500, | ||
body: '', | ||
headers: new Map(), | ||
}), | ||
}); | ||
await assert.rejects(promise, new PipelineStatusError(502, 'failed to load /mapped/metadata.json: 500')); | ||
}); | ||
}); |