-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathutil.ts
71 lines (58 loc) · 2.84 KB
/
util.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
import { ArnFormat, Stack, Token } from '@aws-cdk/core';
import { IConstruct } from 'constructs';
export const AUTOGEN_MARKER = '$$autogen$$';
export interface ArnForParameterNameOptions {
readonly physicalName?: string;
readonly simpleName?: boolean;
}
/**
* Renders an ARN for an SSM parameter given a parameter name.
* @param scope definition scope
* @param parameterName the parameter name to include in the ARN
* @param physicalName optional physical name specified by the user (to auto-detect separator)
*/
export function arnForParameterName(scope: IConstruct, parameterName: string, options: ArnForParameterNameOptions = { }): string {
const physicalName = options.physicalName;
const nameToValidate = physicalName || parameterName;
if (!Token.isUnresolved(nameToValidate) && nameToValidate.includes('/') && !nameToValidate.startsWith('/')) {
throw new Error(`Parameter names must be fully qualified (if they include "/" they must also begin with a "/"): ${nameToValidate}`);
}
if (isSimpleName()) {
return Stack.of(scope).formatArn({
service: 'ssm',
resource: 'parameter',
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
resourceName: parameterName,
});
} else {
return Stack.of(scope).formatArn({
service: 'ssm',
resource: `parameter${parameterName}`,
});
}
/**
* Determines the ARN separator for this parameter: if we have a concrete
* parameter name (or explicitly defined physical name), we will parse them
* and decide whether a "/" is needed or not. Otherwise, users will have to
* explicitly specify `simpleName` when they import the ARN.
*/
function isSimpleName(): boolean {
// look for a concrete name as a hint for determining the separator
const concreteName = !Token.isUnresolved(parameterName) ? parameterName : physicalName;
if (!concreteName || Token.isUnresolved(concreteName)) {
if (options.simpleName === undefined) {
throw new Error('Unable to determine ARN separator for SSM parameter since the parameter name is an unresolved token. Use "fromAttributes" and specify "simpleName" explicitly');
}
return options.simpleName;
}
const result = !concreteName.startsWith('/');
// if users explicitly specify the separator and it conflicts with the one we need, it's an error.
if (options.simpleName !== undefined && options.simpleName !== result) {
if (concreteName === AUTOGEN_MARKER) {
throw new Error('If "parameterName" is not explicitly defined, "simpleName" must be "true" or undefined since auto-generated parameter names always have simple names');
}
throw new Error(`Parameter name "${concreteName}" is ${result ? 'a simple name' : 'not a simple name'}, but "simpleName" was explicitly set to ${options.simpleName}. Either omit it or set it to ${result}`);
}
return result;
}
}