Skip to content

Commit cf1aade

Browse files
authored
feat(dashboard): add support for dashboard variables (#639)
Fixes #391 Not sure if this change is really needed — I’ve been creating dashboards with variables by just grabbing the underlying dashboards and attaching variables that way. That said, it’s a nice cleanup and does make the code a bit cleaner. Totally fine to close the PR if you don’t think it’s worth it. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 1b39b7c commit cf1aade

File tree

5 files changed

+111
-4
lines changed

5 files changed

+111
-4
lines changed

API.md

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dashboard/DefaultDashboardFactory.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Dashboard,
44
DashboardProps,
55
PeriodOverride,
6+
IVariable,
67
} from "aws-cdk-lib/aws-cloudwatch";
78
import { Construct } from "constructs";
89
import { BitmapDashboard } from "./BitmapDashboard";
@@ -66,6 +67,12 @@ export interface MonitoringDashboardsProps {
6667
* @default - DashboardRenderingPreference.INTERACTIVE_ONLY
6768
*/
6869
readonly renderingPreference?: DashboardRenderingPreference;
70+
/**
71+
* Dashboard variables to include in the dashboards.
72+
*
73+
* @default - No variables
74+
*/
75+
readonly variables?: IVariable[];
6976
}
7077

7178
export enum DefaultDashboards {
@@ -118,6 +125,7 @@ export class DefaultDashboardFactory
118125
start: detailStart,
119126
periodOverride:
120127
props.detailDashboardPeriodOverride ?? PeriodOverride.INHERIT,
128+
variables: props.variables,
121129
});
122130
this.dashboards[DefaultDashboards.DETAIL] = this.dashboard;
123131
}
@@ -131,6 +139,7 @@ export class DefaultDashboardFactory
131139
start: summaryStart,
132140
periodOverride:
133141
props.summaryDashboardPeriodOverride ?? PeriodOverride.INHERIT,
142+
variables: props.variables,
134143
},
135144
);
136145
this.dashboards[DefaultDashboards.SUMMARY] = this.summaryDashboard;
@@ -145,6 +154,7 @@ export class DefaultDashboardFactory
145154
start: detailStart,
146155
periodOverride:
147156
props.detailDashboardPeriodOverride ?? PeriodOverride.INHERIT,
157+
variables: props.variables,
148158
},
149159
);
150160
this.dashboards[DefaultDashboards.ALARMS] = this.alarmDashboard;

lib/dashboard/DynamicDashboardFactory.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Dashboard,
44
DashboardProps,
55
PeriodOverride,
6+
IVariable,
67
} from "aws-cdk-lib/aws-cloudwatch";
78
import { Construct } from "constructs";
89

@@ -41,6 +42,13 @@ export interface DynamicDashboardConfiguration {
4142
* @default - respect individual graphs (PeriodOverride.INHERIT)
4243
*/
4344
readonly periodOverride?: PeriodOverride;
45+
46+
/**
47+
* Dashboard variables to include in the dashboards.
48+
*
49+
* @default - No variables
50+
*/
51+
readonly variables?: IVariable[];
4452
}
4553

4654
export interface MonitoringDynamicDashboardsProps {
@@ -100,6 +108,7 @@ export class DynamicDashboardFactory
100108
start,
101109
periodOverride:
102110
dashboardConfig.periodOverride ?? PeriodOverride.INHERIT,
111+
variables: dashboardConfig.variables,
103112
},
104113
);
105114

test/dashboard/DefaultDashboardFactory.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Stack } from "aws-cdk-lib";
2-
import { Template } from "aws-cdk-lib/assertions";
3-
import { TextWidget } from "aws-cdk-lib/aws-cloudwatch";
2+
import { Template, Match } from "aws-cdk-lib/assertions";
3+
import {
4+
TextWidget,
5+
DashboardVariable,
6+
VariableType,
7+
VariableInputType,
8+
} from "aws-cdk-lib/aws-cloudwatch";
49

510
import {
611
DashboardRenderingPreference,
@@ -69,3 +74,28 @@ test("throws error if an empty dashboardNamePrefix is passed but dashboard are t
6974
"A non-empty dashboardNamePrefix is required if dashboards are being created",
7075
);
7176
});
77+
78+
test("default dashboards include variables", () => {
79+
const stack = new Stack();
80+
81+
const var1 = new DashboardVariable({
82+
id: "env",
83+
type: VariableType.PROPERTY,
84+
inputType: VariableInputType.INPUT,
85+
value: "prod",
86+
});
87+
88+
const factory = new DefaultDashboardFactory(stack, "Dashboards", {
89+
dashboardNamePrefix: "DummyDashboard",
90+
renderingPreference: DashboardRenderingPreference.INTERACTIVE_ONLY,
91+
variables: [var1],
92+
});
93+
94+
expect(factory.anyDashboardCreated).toBe(true);
95+
96+
const template = Template.fromStack(stack);
97+
98+
template.hasResourceProperties("AWS::CloudWatch::Dashboard", {
99+
DashboardBody: Match.stringLikeRegexp('"variables":\\['),
100+
});
101+
});

test/dashboard/DynamicDashboardFactory.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Duration, Stack } from "aws-cdk-lib";
2-
import { Template } from "aws-cdk-lib/assertions";
3-
import { IWidget, TextWidget } from "aws-cdk-lib/aws-cloudwatch";
2+
import { Template, Match } from "aws-cdk-lib/assertions";
3+
import {
4+
IWidget,
5+
TextWidget,
6+
DashboardVariable,
7+
VariableType,
8+
VariableInputType,
9+
} from "aws-cdk-lib/aws-cloudwatch";
410
import { DynamicDashboardFactory, IDynamicDashboardSegment } from "../../lib";
511

612
enum TestDashboards {
@@ -74,3 +80,27 @@ test("does not allow reserved dashboard names", () => {
7480
});
7581
}).toThrow("Summary is a reserved name and cannot be used");
7682
});
83+
84+
test("dynamic dashboards include variables", () => {
85+
const stack = new Stack();
86+
87+
const var1 = new DashboardVariable({
88+
id: "env",
89+
type: VariableType.PROPERTY,
90+
inputType: VariableInputType.INPUT,
91+
value: "test",
92+
});
93+
94+
const factory = new DynamicDashboardFactory(stack, "DynamicDashboards", {
95+
dashboardNamePrefix: "testPrefix",
96+
dashboardConfigs: [{ name: TestDashboards.Dynamic1, variables: [var1] }],
97+
});
98+
99+
factory.addDynamicSegment(new TestSegment());
100+
101+
const template = Template.fromStack(stack);
102+
103+
template.hasResourceProperties("AWS::CloudWatch::Dashboard", {
104+
DashboardBody: Match.stringLikeRegexp('"variables":\\['),
105+
});
106+
});

0 commit comments

Comments
 (0)