-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): hotswap deployments for CloudWatch Dashboards
- Loading branch information
Showing
7 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ISDK } from '../aws-auth'; | ||
import { ChangeHotswapImpact, ChangeHotswapResult, HotswapOperation, HotswappableChangeCandidate, establishResourcePhysicalName } from './common'; | ||
import { EvaluateCloudFormationTemplate } from './evaluate-cloudformation-template'; | ||
|
||
export async function isHotswappableDashboardChange( | ||
logicalId: string, change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate, | ||
): Promise<ChangeHotswapResult> { | ||
const dashboardBodyChange = await isDashboardBodyOnlyChange(change, evaluateCfnTemplate); | ||
if (dashboardBodyChange === ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT || | ||
dashboardBodyChange === ChangeHotswapImpact.IRRELEVANT) { | ||
return dashboardBodyChange; | ||
} | ||
|
||
const dashboardNameInCfnTemplate = change.newValue?.Properties?.DashboardName; | ||
const dashboardName = await establishResourcePhysicalName(logicalId, dashboardNameInCfnTemplate, evaluateCfnTemplate); | ||
if (!dashboardName) { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
|
||
return new DashboardHotswapOperation({ | ||
body: dashboardBodyChange, | ||
dashboardName: dashboardName, | ||
}); | ||
} | ||
|
||
async function isDashboardBodyOnlyChange( | ||
change: HotswappableChangeCandidate, evaluateCfnTemplate: EvaluateCloudFormationTemplate, | ||
): Promise<string | ChangeHotswapImpact> { | ||
const newResourceType = change.newValue.Type; | ||
if (newResourceType !== 'AWS::CloudWatch::Dashboard') { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
|
||
const propertyUpdates = change.propertyUpdates; | ||
for (const updatedPropName in propertyUpdates) { | ||
// ensure that only changes to the DashboardBody result in a hotswap | ||
if (updatedPropName !== 'DashboardBody') { | ||
return ChangeHotswapImpact.REQUIRES_FULL_DEPLOYMENT; | ||
} | ||
} | ||
|
||
return evaluateCfnTemplate.evaluateCfnExpression(propertyUpdates.DashboardBody.newValue); | ||
} | ||
|
||
interface DashboardResource { | ||
readonly dashboardName: string; | ||
readonly body: string; | ||
} | ||
|
||
class DashboardHotswapOperation implements HotswapOperation { | ||
constructor(private readonly dashboardResource: DashboardResource) { | ||
} | ||
|
||
public async apply(sdk: ISDK): Promise<any> { | ||
return sdk.cloudWatch().putDashboard({ | ||
DashboardName: this.dashboardResource.dashboardName, | ||
DashboardBody: this.dashboardResource.body, | ||
}).promise(); | ||
} | ||
} |
229 changes: 229 additions & 0 deletions
229
packages/aws-cdk/test/api/hotswap/dashboard-hotswap-deployments.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
import { CloudWatch } from 'aws-sdk'; | ||
import * as setup from './hotswap-test-setup'; | ||
|
||
let mockPutDashboard: (params: CloudWatch.Types.PutDashboardInput) => CloudWatch.Types.PutDashboardOutput; | ||
let cfnMockProvider: setup.CfnMockProvider; | ||
|
||
beforeEach(() => { | ||
cfnMockProvider = setup.setupHotswapTests(); | ||
mockPutDashboard = jest.fn(); | ||
cfnMockProvider.setPutDashboardMock(mockPutDashboard); | ||
}); | ||
|
||
test('returns undefined when a new Dashboard is added to the Stack', async () => { | ||
// GIVEN | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const deployStackResult = await cfnMockProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
expect(deployStackResult).toBeUndefined(); | ||
}); | ||
|
||
test('calls the putDashboard() API when it receives only a DashboardBody change', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: '{ "widgets": [] }', | ||
DashboardName: 'my-dashboard', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: '{ "widgets": [{ "type": "text" }] }', | ||
DashboardName: 'my-dashboard', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const deployStackResult = await cfnMockProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
expect(deployStackResult).not.toBeUndefined(); | ||
expect(mockPutDashboard).toHaveBeenCalledWith({ | ||
DashboardName: 'my-dashboard', | ||
DashboardBody: '{ "widgets": [{ "type": "text" }] }', | ||
}); | ||
}); | ||
|
||
test('does not call the putDashboard() API when a non-DashboardBody property is changed', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: '{ "widgets": [] }', | ||
Tags: [ | ||
{ Key: 'Environment', Value: 'Dev' }, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: '{ "widgets": [] }', | ||
Tags: [ | ||
{ Key: 'Environment', Value: 'Prod' }, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const deployStackResult = await cfnMockProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
expect(deployStackResult).toBeUndefined(); | ||
expect(mockPutDashboard).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('does not call the putDashboard() API when the resource is not an AWS::CloudWatch::Dashboard', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::NotCloudWatch::NotDashboard', | ||
Properties: { | ||
DashboardBody: '{ "widgets": [] }', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::NotCloudWatch::NotDashboard', | ||
Properties: { | ||
DashboardBody: '{ "widgets": [{ "type": "text" }] }', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const deployStackResult = await cfnMockProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
expect(deployStackResult).toBeUndefined(); | ||
expect(mockPutDashboard).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('can hotswap a dashboard with nested Fn::Join in DashboardBody', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: { | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'{ "widgets": [', | ||
'{ "type": "text", "text": "Old" }', | ||
'] }', | ||
], | ||
], | ||
}, | ||
DashboardName: 'my-dashboard', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: { | ||
'Fn::Join': [ | ||
'', | ||
[ | ||
'{ "widgets": [', | ||
'{ "type": "text", "text": "New" }', | ||
'] }', | ||
], | ||
], | ||
}, | ||
DashboardName: 'my-dashboard', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// WHEN | ||
const deployStackResult = await cfnMockProvider.tryHotswapDeployment(cdkStackArtifact); | ||
|
||
// THEN | ||
expect(deployStackResult).not.toBeUndefined(); | ||
expect(mockPutDashboard).toHaveBeenCalledWith({ | ||
DashboardName: 'my-dashboard', | ||
DashboardBody: '{ "widgets": [{ "type": "text", "text": "New" }] }', | ||
}); | ||
}); | ||
|
||
test('throws an error for invalid DashboardBody', async () => { | ||
// GIVEN | ||
setup.setCurrentCfnStackTemplate({ | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: '{ "invalid": "data" }', | ||
DashboardName: 'invalid-dashboard', | ||
}, | ||
}, | ||
}, | ||
}); | ||
const cdkStackArtifact = setup.cdkStackArtifactOf({ | ||
template: { | ||
Resources: { | ||
Dashboard: { | ||
Type: 'AWS::CloudWatch::Dashboard', | ||
Properties: { | ||
DashboardBody: '{ "widgets": "invalid-format" }', | ||
DashboardName: 'invalid-dashboard', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// THEN | ||
await expect(() => | ||
cfnMockProvider.tryHotswapDeployment(cdkStackArtifact), | ||
).rejects.toThrow(/Invalid DashboardBody/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters