Skip to content

Commit 7ab6393

Browse files
author
Eugene Cheung
authored
fix(facade): avoid duplicated construct names for default dashboard factories (#154)
If you have multiple facades in the same app using the default `dashboardFactory`, you'll end up with errors like: ``` [...]/node_modules/constructs/lib/construct.js:335 throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`); ^ Error: There is already a Construct with name 'Dashboards' in MyMonitoringFacade [MyApp-NestedTest] [...] ``` --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 8a9cde6 commit 7ab6393

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

lib/facade/MonitoringFacade.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/facade/MonitoringFacade.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,13 @@ export interface MonitoringFacadeProps {
116116
* @default empty (no preferences)
117117
*/
118118
readonly metricFactoryDefaults?: MetricFactoryDefaults;
119+
119120
/**
120121
* Defaults for alarm factory.
121122
* @default actions enabled, facade logical ID used as default alarm name prefix
122123
*/
123124
readonly alarmFactoryDefaults?: AlarmFactoryDefaults;
125+
124126
/**
125127
* Defaults for dashboard factory.
126128
* @default `DefaultDashboardFactory`; facade logical ID used as default name
@@ -170,7 +172,7 @@ export class MonitoringFacade extends MonitoringScope {
170172
scope: Construct,
171173
defaultName: string
172174
): IDashboardFactory {
173-
return new DefaultDashboardFactory(scope, "Dashboards", {
175+
return new DefaultDashboardFactory(scope, `${defaultName}-Dashboards`, {
174176
dashboardNamePrefix: defaultName,
175177
});
176178
}

test/facade/MonitoringFacade.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,31 @@ import { Template } from "aws-cdk-lib/assertions";
33
import { MonitoringFacade } from "../../lib";
44

55
describe("test of defaults", () => {
6-
const stack = new Stack();
7-
new MonitoringFacade(stack, "Test");
8-
const result = Template.fromStack(stack);
9-
106
test("only default dashboard gets created by default", () => {
7+
const stack = new Stack();
8+
new MonitoringFacade(stack, "Test");
9+
const result = Template.fromStack(stack);
10+
1111
result.resourceCountIs("AWS::CloudWatch::Dashboard", 1);
1212

1313
result.hasResourceProperties("AWS::CloudWatch::Dashboard", {
1414
DashboardName: "Test",
1515
});
1616
});
17+
18+
test("handles multiple facades", () => {
19+
const stack = new Stack();
20+
new MonitoringFacade(stack, "Test1");
21+
new MonitoringFacade(stack, "Test2");
22+
const result = Template.fromStack(stack);
23+
24+
result.resourceCountIs("AWS::CloudWatch::Dashboard", 2);
25+
26+
result.hasResourceProperties("AWS::CloudWatch::Dashboard", {
27+
DashboardName: "Test1",
28+
});
29+
result.hasResourceProperties("AWS::CloudWatch::Dashboard", {
30+
DashboardName: "Test2",
31+
});
32+
});
1733
});

0 commit comments

Comments
 (0)