Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudwatch): dashboardName validation #3382

Merged
merged 7 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(cloudwatch): dashboardName validation
  • Loading branch information
Jimmy Gaussen committed Jul 22, 2019
commit 2bbdc9b106a91fc9037d786d0a3b397369722fed
7 changes: 6 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/lib/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export enum PeriodOverride {

export interface DashboardProps {
/**
* Name of the dashboard
* Name of the dashboard.
* If set, must only contain alphanumerics, dash (-) and underscore (_)
*
* @default Automatically generated name
*/
Expand Down Expand Up @@ -67,6 +68,10 @@ export class Dashboard extends Resource {
physicalName: props.dashboardName,
});

if (props.dashboardName && !props.dashboardName.match(/^[\w-]+$/)) {
eladb marked this conversation as resolved.
Show resolved Hide resolved
eladb marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`The value ${props.dashboardName} for field dashboardName contains invalid characters. It can only contain alphanumerics, dash (-) and underscore (_).`);
}

new CfnDashboard(this, 'Resource', {
dashboardName: this.physicalName,
dashboardBody: Lazy.stringValue({ produce: () => {
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/test.dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ export = {
// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Dashboard', {}));

test.done();
},

'throws if DashboardName is not valid'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'MyStack');

// WHEN
const toThrow = () => {
new Dashboard(stack, 'MyDashboard', {
dashboardName: 'My Invalid Dashboard Name',
});
};

// THEN
test.throws(() => toThrow());
eladb marked this conversation as resolved.
Show resolved Hide resolved

test.done();
}
};
Expand Down