Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.

Commit 89b6d88

Browse files
author
Raphaël Beamonte
committed
Add an environment provisioner that allows to specify environment variables to tweak the configuration
Signed-off-by: Raphaël Beamonte <[email protected]>
1 parent dccd224 commit 89b6d88

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/EnvironmentProvisioner.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import DefaultProvisioner from './configuration/DefaultProvisioner';
2+
3+
/*
4+
* Go through the given JSON object and replace values using environment
5+
* variables if available. The environment variables follow the scheme of
6+
* the names in the JSON. For instance, the following JSON scheme will be
7+
* populated with the environment variables whose names are the given values:
8+
*
9+
* {
10+
* "ReadCapacity": {
11+
* "Min": READCAPACITY_MIN,
12+
* "Max": READCAPACITY_MAX,
13+
* "Increment": {
14+
* "When": {
15+
* "UtilisationIsAbovePercent": READCAPACITY_INCREMENT_WHEN_UTILISATIONISABOVEPERCENT,
16+
* "ThrottledEventsPerMinuteIsAbove": READCAPACITY_INCREMENT_WHEN_THROTTLEDEVENTSPERMINUTEISABOVE
17+
* },
18+
* }
19+
* }
20+
*/
21+
function populateJson(json, prefixEnv=false, list_available=false) {
22+
var ret = {}
23+
24+
if (prefixEnv) {
25+
prefixEnv = prefixEnv + '_'
26+
} else {
27+
prefixEnv = ''
28+
}
29+
30+
for (var key in json) {
31+
env = (prefixEnv + key).toUpperCase()
32+
if (typeof json[key] == 'object') {
33+
if (list_available) {
34+
Object.assign(ret, populateJson(json[key], env, list_available));
35+
} else {
36+
ret[key] = populateJson(json[key], env, list_available)
37+
}
38+
} else {
39+
if (list_available) {
40+
ret[env] = json[key]
41+
} else {
42+
if (typeof process.env[env] === 'undefined'
43+
|| isNaN(process.env[env]) != isNaN(json[key])) {
44+
ret[key] = json[key]
45+
} else if (isNaN(process.env[env])) {
46+
ret[key] = process.env[env]
47+
} else {
48+
ret[key] = Number(process.env[env])
49+
}
50+
}
51+
}
52+
}
53+
54+
return ret
55+
}
56+
57+
if (typeof process.env['ENVIRONMENTPROVISIONER_LIST_AVAILABLE'] !== 'undefined' &&
58+
process.env['ENVIRONMENTPROVISIONER_LIST_AVAILABLE'] == 'true') {
59+
console.log('%j', populateJson(DefaultProvisioner, false, true))
60+
process.exit(0)
61+
}
62+
63+
/*
64+
* Use the DefaultProvisioner as basis for the EnvironmentProvisioner
65+
*/
66+
module.exports = populateJson(DefaultProvisioner)

src/Provisioner.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Region } from './configuration/Region';
88
import DefaultProvisioner from './configuration/DefaultProvisioner';
99
import { invariant } from './Global';
1010
import type { TableProvisionedAndConsumedThroughput, ProvisionerConfig, AdjustmentContext } from './flow/FlowTypes';
11+
import EnvironmentProvisioner from './EnvironmentProvisioner';
1112

1213
export default class Provisioner extends ProvisionerConfigurableBase {
1314

@@ -34,7 +35,7 @@ export default class Provisioner extends ProvisionerConfigurableBase {
3435
getTableConfig(data: TableProvisionedAndConsumedThroughput): ProvisionerConfig {
3536

3637
// Option 1 - Default settings for all tables
37-
return DefaultProvisioner;
38+
return EnvironmentProvisioner;
3839

3940
// Option 2 - Bespoke table specific settings
4041
// return data.TableName === 'Table1' ? Climbing : Default;

0 commit comments

Comments
 (0)