-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathSSMProvider.ts
86 lines (73 loc) · 2.92 KB
/
SSMProvider.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
77
78
79
80
81
82
83
84
85
86
import { BaseProvider, DEFAULT_PROVIDERS } from './BaseProvider';
import { SSMClient, GetParameterCommand, paginateGetParametersByPath } from '@aws-sdk/client-ssm';
import type { SSMClientConfig, GetParameterCommandInput, GetParametersByPathCommandInput } from '@aws-sdk/client-ssm';
import type { SSMGetMultipleOptionsInterface, SSMGetOptionsInterface } from 'types/SSMProvider';
import type { PaginationConfiguration } from '@aws-sdk/types';
class SSMProvider extends BaseProvider {
public client: SSMClient;
public constructor(config: SSMClientConfig = {}) {
super();
this.client = new SSMClient(config);
}
protected async _get(name: string, sdkOptions?: Partial<GetParameterCommandInput>): Promise<string | undefined> {
const options: GetParameterCommandInput = {
Name: name,
};
if (sdkOptions) {
Object.assign(options, sdkOptions);
}
const result = await this.client.send(new GetParameterCommand(options));
return result.Parameter?.Value;
}
protected async _getMultiple(path: string, sdkOptions?: Partial<GetParametersByPathCommandInput>): Promise<Record<string, string | undefined>> {
const options: GetParametersByPathCommandInput = {
Path: path,
};
const paginationOptions: PaginationConfiguration = {
client: this.client
};
if (sdkOptions) {
Object.assign(options, sdkOptions);
if (sdkOptions.MaxResults) {
paginationOptions.pageSize = sdkOptions.MaxResults;
}
}
const parameters: Record<string, string | undefined> = {};
for await (const page of paginateGetParametersByPath(paginationOptions, options)) {
for (const parameter of page.Parameters || []) {
/**
* Standardize the parameter name
*
* The parameter name returned by SSM will contain the full path.
* However, for readability, we should return only the part after the path.
**/
// If the parameter is present in the response, then it has a Name
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
let name = parameter.Name!;
name = name.replace(path, '');
if (name.startsWith('/')) {
name = name.replace('/', '');
}
parameters[name] = parameter.Value;
}
}
return parameters;
}
}
const getParameter = (name: string, options?: SSMGetOptionsInterface): Promise<undefined | string | Record<string, unknown>> => {
if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) {
DEFAULT_PROVIDERS.ssm = new SSMProvider();
}
return DEFAULT_PROVIDERS.ssm.get(name, options);
};
const getParameters = (path: string, options?: SSMGetMultipleOptionsInterface): Promise<undefined | Record<string, unknown>> => {
if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) {
DEFAULT_PROVIDERS.ssm = new SSMProvider();
}
return DEFAULT_PROVIDERS.ssm.getMultiple(path, options);
};
export {
SSMProvider,
getParameter,
getParameters,
};