-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AWSConfigStore.impl.ts
76 lines (66 loc) · 2.44 KB
/
AWSConfigStore.impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {
DeleteParameterCommand,
GetParameterCommand,
ParameterNotFound,
ParameterType,
PutParameterCommand,
SSMClient,
} from '@aws-sdk/client-ssm'
import type { ObjectWithKeysFromStringArray, StoreBaseConfig } from '@purista/core'
import { ConfigStoreBaseClass, StatusCode, UnhandledError } from '@purista/core'
import type { AWSConfigStoreConfig } from './types.js'
/**
* The config store adapter for AWS System Manager.
* It will store, retrive, update or remove configs in AWS System Manager.
*
* For performance reasons, and to reduce costs, the config values are cached in memory after first fetch.
*
* You can disable the whole caching via config by setting enableCache to false.
* If the cache is enabled, you can set the ttl for cached config values via config cacheTtl (in ms).
*
* This will return the cached config if available and if ttl is not exceeded.
* If a config value exceeds the ttl, it does not automatically get removed from cache.
* It will be removed/overwritten on next get request.
*/
export class AWSConfigStore extends ConfigStoreBaseClass<AWSConfigStoreConfig> {
client: SSMClient
constructor(config: StoreBaseConfig<AWSConfigStoreConfig>) {
super('AWSConfigStore', { enableCache: true, ...config })
this.client = new SSMClient(this.config.client)
}
protected async getConfigImpl<ConfigNames extends string[]>(
...configNames: ConfigNames
): Promise<ObjectWithKeysFromStringArray<ConfigNames>> {
const result: Record<string, string | undefined> = {}
for (const name of configNames) {
try {
const command = new GetParameterCommand({
Name: name,
})
const res = await this.client.send(command)
result[name] = res.Parameter?.Value
} catch (err) {
if (!(err instanceof ParameterNotFound)) {
throw UnhandledError.fromError(err, StatusCode.InternalServerError)
}
result[name] = undefined
}
}
return result as ObjectWithKeysFromStringArray<ConfigNames>
}
protected async removeConfigImpl(configName: string) {
const command = new DeleteParameterCommand({
Name: configName,
})
await this.client.send(command)
}
protected async setConfigImpl(configName: string, configValue: string) {
const command = new PutParameterCommand({
Name: configName,
Value: configValue,
Type: ParameterType.STRING,
Overwrite: true,
})
await this.client.send(command)
}
}