Skip to content

Commit 2506a01

Browse files
feat(CloudWatch): Dashboard TextWidget background support (#23169)
Fixes #23114 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c31b919 commit 2506a01

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

packages/@aws-cdk/aws-cloudwatch/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,17 @@ dashboard.addWidgets(new cloudwatch.TextWidget({
508508
}));
509509
```
510510

511+
Optionally set the TextWidget background to be transparent
512+
513+
```ts
514+
declare const dashboard: cloudwatch.Dashboard;
515+
516+
dashboard.addWidgets(new cloudwatch.TextWidget({
517+
markdown: '# Key Performance Indicators',
518+
background: TextWidgetBackground.TRANSPARENT
519+
}));
520+
```
521+
511522
### Alarm Status widget
512523

513524
An alarm status widget displays instantly the status of any type of alarms and gives the

packages/@aws-cdk/aws-cloudwatch/lib/text.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import { ConcreteWidget } from './widget';
22

3+
/**
4+
* Background types available
5+
*/
6+
export enum TextWidgetBackground {
7+
/**
8+
* Solid background
9+
*/
10+
SOLID = 'solid',
11+
/**
12+
* Transparent background
13+
*/
14+
TRANSPARENT = 'transparent'
15+
}
16+
317
/**
418
* Properties for a Text widget
519
*/
@@ -22,17 +36,26 @@ export interface TextWidgetProps {
2236
* @default 2
2337
*/
2438
readonly height?: number;
39+
40+
/**
41+
* Background for the widget
42+
*
43+
* @default solid
44+
*/
45+
readonly background?: TextWidgetBackground;
2546
}
2647

2748
/**
2849
* A dashboard widget that displays MarkDown
2950
*/
3051
export class TextWidget extends ConcreteWidget {
3152
private readonly markdown: string;
53+
private readonly background?: TextWidgetBackground;
3254

3355
constructor(props: TextWidgetProps) {
3456
super(props.width || 6, props.height || 2);
3557
this.markdown = props.markdown;
58+
this.background = props.background;
3659
}
3760

3861
public position(x: number, y: number): void {
@@ -49,6 +72,7 @@ export class TextWidget extends ConcreteWidget {
4972
y: this.y,
5073
properties: {
5174
markdown: this.markdown,
75+
background: this.background,
5276
},
5377
}];
5478
}

packages/@aws-cdk/aws-cloudwatch/test/dashboard.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Template, Annotations, Match } from '@aws-cdk/assertions';
22
import { App, Stack } from '@aws-cdk/core';
3-
import { Dashboard, GraphWidget, PeriodOverride, TextWidget, MathExpression } from '../lib';
3+
import { Dashboard, GraphWidget, PeriodOverride, TextWidget, MathExpression, TextWidgetBackground } from '../lib';
44

55
describe('Dashboard', () => {
66
test('widgets in different adds are laid out underneath each other', () => {
@@ -13,11 +13,13 @@ describe('Dashboard', () => {
1313
width: 10,
1414
height: 2,
1515
markdown: 'first',
16+
background: TextWidgetBackground.SOLID,
1617
}));
1718
dashboard.addWidgets(new TextWidget({
1819
width: 1,
1920
height: 4,
2021
markdown: 'second',
22+
background: TextWidgetBackground.TRANSPARENT,
2123
}));
2224
dashboard.addWidgets(new TextWidget({
2325
width: 4,
@@ -30,8 +32,8 @@ describe('Dashboard', () => {
3032
expect(Object.keys(resources).length).toEqual(1);
3133
const key = Object.keys(resources)[0];
3234
hasWidgets(resources[key].Properties, [
33-
{ type: 'text', width: 10, height: 2, x: 0, y: 0, properties: { markdown: 'first' } },
34-
{ type: 'text', width: 1, height: 4, x: 0, y: 2, properties: { markdown: 'second' } },
35+
{ type: 'text', width: 10, height: 2, x: 0, y: 0, properties: { markdown: 'first', background: TextWidgetBackground.SOLID } },
36+
{ type: 'text', width: 1, height: 4, x: 0, y: 2, properties: { markdown: 'second', background: TextWidgetBackground.TRANSPARENT } },
3537
{ type: 'text', width: 4, height: 1, x: 0, y: 6, properties: { markdown: 'third' } },
3638
]);
3739

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.js.snapshot/DashboardIntegrationTestStack.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"DashCCD7F836": {
44
"Type": "AWS::CloudWatch::Dashboard",
55
"Properties": {
6-
"DashboardBody": "{\"widgets\":[]}"
6+
"DashboardBody": "{\"widgets\":[{\"type\":\"text\",\"width\":6,\"height\":2,\"x\":0,\"y\":0,\"properties\":{\"markdown\":\"I don't have a background\",\"background\":\"transparent\"}}]}"
77
}
88
}
99
},

packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import * as cdk from '@aws-cdk/core';
22
import * as integ from '@aws-cdk/integ-tests';
33
import * as cloudwatch from '../lib';
4+
import { TextWidgetBackground } from '../lib';
45

56
const app = new cdk.App();
67

78
const stack = new cdk.Stack(app, 'DashboardIntegrationTestStack');
89

910
const dashboard = new cloudwatch.Dashboard(stack, 'Dash');
1011

12+
dashboard.addWidgets(new cloudwatch.TextWidget({
13+
markdown: 'I don\'t have a background',
14+
background: TextWidgetBackground.TRANSPARENT,
15+
}));
16+
1117
new cdk.CfnOutput(stack, 'DashboardArn', {
1218
value: dashboard.dashboardArn,
1319
});

0 commit comments

Comments
 (0)