-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource-name-formatter.ts
40 lines (32 loc) · 1.43 KB
/
resource-name-formatter.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
import { Construct } from 'constructs'
import { AzureResourceNameFormatterProps } from './types'
import { CommonAzureStackProps } from './types'
export class AzureResourceNameFormatter extends Construct {
props: CommonAzureStackProps
constructor(parent: Construct, id: string, props: CommonAzureStackProps) {
super(parent, id)
this.props = props
}
/**
* @summary Helper method to format azure resource name based on the provided options
* @param resourceName The azure resource name to format
* @param options Options to control the formatting of the resource name
* @returns The formatted Azure-compliant resource name
*/
public format(resourceName: string, options?: AzureResourceNameFormatterProps) {
const azureResourceNameElements = []
if (!options?.exclude) {
azureResourceNameElements.push(options?.globalPrefix ? this.props.globalPrefix : undefined)
azureResourceNameElements.push(options?.prefix ?? this.props.resourcePrefix)
}
azureResourceNameElements.push(resourceName)
if (!options?.exclude) {
azureResourceNameElements.push(options?.suffix ?? this.props.resourceSuffix)
azureResourceNameElements.push(options?.globalSuffix ? this.props.globalSuffix : undefined)
}
azureResourceNameElements.push(this.props.stage)
return azureResourceNameElements
.filter(azureResourceNameElements => azureResourceNameElements != undefined)
.join('-')
}
}