Skip to content

Commit

Permalink
fix(awards): ignore null configs in awards.storage (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhammer committed Feb 28, 2024
1 parent 667a67d commit e568c37
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
29 changes: 29 additions & 0 deletions plugins/awards-backend/src/service/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ describe('getStorageClient', () => {
);
});

// this allows for app-config overlays to cancel out engines specified in previous configs
// e.g. app-config.yaml sets 'fs' but app-config.prod.yaml sets 's3', it can then set fs: null.
it('allows multiple storage engines as long as all but one are null', () => {
const config = new ConfigReader({
awards: {
storage: {
fs: null,
s3: {
bucket: 'my-bucket',
region: 'us-east-1',
accessKey: 'my-access-key',
secretKey: 'my-secret-key',
endpoint: '127.0.0.1',
},
},
},
});

const storage = getStorageClient(config);
expect(storage.getConfig()).toEqual({
accessKeyId: 'my-access-key',
secretAccessKey: 'my-secret-key',
endpoint: '127.0.0.1',
bucketName: 'my-bucket',
region: 'us-east-1',
type: StorageType.S3,
});
});

it('errors if no storage engines provided', () => {
const config = new ConfigReader({
awards: {
Expand Down
20 changes: 15 additions & 5 deletions plugins/awards-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,27 @@ function buildFsAdapter(config: Config): Storage {

export function getStorageClient(config: Config): Storage {
const storageConfig = config.getConfig('awards.storage');
if (storageConfig.keys().length !== 1) {
const configs: Record<string, Config> = {};
storageConfig.keys().forEach(key => {
try {
configs[key] = storageConfig.getConfig(key);
} catch {
// config object isn't instantiated for this key
}
});
if (Object.keys(configs).length !== 1) {
throw new Error(
`Must specify exactly one storage engine in awards.storage, got ${storageConfig.keys()}`,
`Must specify exactly one storage engine in awards.storage, got ${Object.keys(
configs,
)}`,
);
}
const key = storageConfig.keys()[0];
const key = Object.keys(configs)[0];
switch (key) {
case 's3':
return buildS3Adapter(storageConfig.getConfig('s3'));
return buildS3Adapter(configs.s3);
case 'fs':
return buildFsAdapter(storageConfig.getConfig('fs'));
return buildFsAdapter(configs.fs);
default:
throw new Error(
`Invalid storage engine type, valid types are "s3", "fs", got: ${key}`,
Expand Down

0 comments on commit e568c37

Please sign in to comment.