Skip to content

Commit

Permalink
feat(CloudWatch): Dashboard TextWidget background support (aws#23169)
Browse files Browse the repository at this point in the history
Fixes aws#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*
  • Loading branch information
DeerajTheepshi authored and Brennan Ho committed Jan 20, 2023
1 parent 986457f commit 13a4de7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ dashboard.addWidgets(new cloudwatch.TextWidget({
}));
```

Optionally set the TextWidget background to be transparent

```ts
declare const dashboard: cloudwatch.Dashboard;

dashboard.addWidgets(new cloudwatch.TextWidget({
markdown: '# Key Performance Indicators',
background: TextWidgetBackground.TRANSPARENT
}));
```

### Alarm Status widget

An alarm status widget displays instantly the status of any type of alarms and gives the
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { ConcreteWidget } from './widget';

/**
* Background types available
*/
export enum TextWidgetBackground {
/**
* Solid background
*/
SOLID = 'solid',
/**
* Transparent background
*/
TRANSPARENT = 'transparent'
}

/**
* Properties for a Text widget
*/
Expand All @@ -22,17 +36,26 @@ export interface TextWidgetProps {
* @default 2
*/
readonly height?: number;

/**
* Background for the widget
*
* @default solid
*/
readonly background?: TextWidgetBackground;
}

/**
* A dashboard widget that displays MarkDown
*/
export class TextWidget extends ConcreteWidget {
private readonly markdown: string;
private readonly background?: TextWidgetBackground;

constructor(props: TextWidgetProps) {
super(props.width || 6, props.height || 2);
this.markdown = props.markdown;
this.background = props.background;
}

public position(x: number, y: number): void {
Expand All @@ -49,6 +72,7 @@ export class TextWidget extends ConcreteWidget {
y: this.y,
properties: {
markdown: this.markdown,
background: this.background,
},
}];
}
Expand Down
8 changes: 5 additions & 3 deletions packages/@aws-cdk/aws-cloudwatch/test/dashboard.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Template, Annotations, Match } from '@aws-cdk/assertions';
import { App, Stack } from '@aws-cdk/core';
import { Dashboard, GraphWidget, PeriodOverride, TextWidget, MathExpression } from '../lib';
import { Dashboard, GraphWidget, PeriodOverride, TextWidget, MathExpression, TextWidgetBackground } from '../lib';

describe('Dashboard', () => {
test('widgets in different adds are laid out underneath each other', () => {
Expand All @@ -13,11 +13,13 @@ describe('Dashboard', () => {
width: 10,
height: 2,
markdown: 'first',
background: TextWidgetBackground.SOLID,
}));
dashboard.addWidgets(new TextWidget({
width: 1,
height: 4,
markdown: 'second',
background: TextWidgetBackground.TRANSPARENT,
}));
dashboard.addWidgets(new TextWidget({
width: 4,
Expand All @@ -30,8 +32,8 @@ describe('Dashboard', () => {
expect(Object.keys(resources).length).toEqual(1);
const key = Object.keys(resources)[0];
hasWidgets(resources[key].Properties, [
{ type: 'text', width: 10, height: 2, x: 0, y: 0, properties: { markdown: 'first' } },
{ type: 'text', width: 1, height: 4, x: 0, y: 2, properties: { markdown: 'second' } },
{ type: 'text', width: 10, height: 2, x: 0, y: 0, properties: { markdown: 'first', background: TextWidgetBackground.SOLID } },
{ type: 'text', width: 1, height: 4, x: 0, y: 2, properties: { markdown: 'second', background: TextWidgetBackground.TRANSPARENT } },
{ type: 'text', width: 4, height: 1, x: 0, y: 6, properties: { markdown: 'third' } },
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"DashCCD7F836": {
"Type": "AWS::CloudWatch::Dashboard",
"Properties": {
"DashboardBody": "{\"widgets\":[]}"
"DashboardBody": "{\"widgets\":[{\"type\":\"text\",\"width\":6,\"height\":2,\"x\":0,\"y\":0,\"properties\":{\"markdown\":\"I don't have a background\",\"background\":\"transparent\"}}]}"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/integ.dashboard.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import * as cdk from '@aws-cdk/core';
import * as integ from '@aws-cdk/integ-tests';
import * as cloudwatch from '../lib';
import { TextWidgetBackground } from '../lib';

const app = new cdk.App();

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

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

dashboard.addWidgets(new cloudwatch.TextWidget({
markdown: 'I don\'t have a background',
background: TextWidgetBackground.TRANSPARENT,
}));

new cdk.CfnOutput(stack, 'DashboardArn', {
value: dashboard.dashboardArn,
});
Expand Down

0 comments on commit 13a4de7

Please sign in to comment.