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(events): better modeling of CW Event Targets #2576

Merged
merged 37 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7112f84
WIP
rix0rrr May 8, 2019
065d4fe
Merge remote-tracking branch 'origin/master' into huijbers/ecs-events
rix0rrr May 8, 2019
fffe4ad
Trying to abstract over CWE input
rix0rrr May 9, 2019
ddc7584
Make resolve framework to more general, get rid of global options
rix0rrr May 9, 2019
a397a2e
asEventRuleTarget => bind
rix0rrr May 9, 2019
9c0f3bf
Group CloudFormation aware routines together
rix0rrr May 10, 2019
2e717ad
WIP
rix0rrr May 10, 2019
43e01d6
Rewrite toJSON() in terms of new resolver mechanisms
rix0rrr May 10, 2019
16d74a9
Merge commit 'fffe4ad' into huijbers/ecs-events
rix0rrr May 10, 2019
32a56e7
Using new Token mechanisms to implement EventField substitution
rix0rrr May 10, 2019
e50265a
Fix JSII complaints by reodering exports
rix0rrr May 10, 2019
4b1d82a
Object-scoped path replacement works
rix0rrr May 10, 2019
2343be8
Mass implement tasks
rix0rrr May 14, 2019
ef204ac
Fix some more build issues
rix0rrr May 14, 2019
08a444a
Improve error message for string key resolve
rix0rrr May 14, 2019
46725eb
Fix a bad bug about cross-stack references in a lazy context
rix0rrr May 15, 2019
e01e3bc
Make the API of resolving jsii-exportable
rix0rrr May 15, 2019
066c521
Add in awsvpcconfig, state machine
rix0rrr May 15, 2019
0405f41
Get rid of premature resolution during toCloudFormation()
rix0rrr May 15, 2019
c5d7446
Fix API gateway token issues
rix0rrr May 16, 2019
b9ad058
Add tests
rix0rrr May 16, 2019
2d000d5
Remove AssignPublicIp default in test
rix0rrr May 17, 2019
52377d9
Don't use overrides during rendering in Pipeline
rix0rrr May 17, 2019
8e7e480
Centralize role creation, update integ tests
rix0rrr May 17, 2019
f8ba529
Update another expectation
rix0rrr May 17, 2019
fa04166
Token JSONification no longer fails
rix0rrr May 17, 2019
4ccf95b
Undo default change
rix0rrr May 17, 2019
02a1451
Also undo S3Selector change
rix0rrr May 17, 2019
22e588c
Undo test change
rix0rrr May 17, 2019
f79599c
ID rename back to plural
rix0rrr May 17, 2019
dd8322f
Update decdk snapshot
rix0rrr May 19, 2019
e0302ce
Merge remote-tracking branch 'origin/master' into huijbers/ecs-events
rix0rrr May 20, 2019
42673c0
Fix some build
rix0rrr May 20, 2019
5127d22
Update ECS README
rix0rrr May 20, 2019
a745c73
Merge branch 'huijbers/ecs-events' of github.com:awslabs/aws-cdk into…
rix0rrr May 20, 2019
c55c048
Simplify names
rix0rrr May 20, 2019
80f020e
Fix integ test
rix0rrr May 20, 2019
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
48 changes: 4 additions & 44 deletions packages/@aws-cdk/aws-apigateway/lib/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,51 +91,13 @@ export class Deployment extends Resource {
}

class LatestDeploymentResource extends CfnDeployment {
private originalLogicalId?: string;
private lazyLogicalIdRequired: boolean;
private lazyLogicalId?: string;
private logicalIdToken: Token;
private hashComponents = new Array<any>();
private originalLogicalId: string;

constructor(scope: Construct, id: string, props: CfnDeploymentProps) {
super(scope, id, props);

// from this point, don't allow accessing logical ID before synthesis
this.lazyLogicalIdRequired = true;

this.logicalIdToken = new Token(() => this.lazyLogicalId);
}

/**
* Returns either the original or the custom logical ID of this resource.
*/
public get logicalId() {
if (!this.lazyLogicalIdRequired) {
return this.originalLogicalId!;
}

return this.logicalIdToken.toString();
}

/**
* Sets the logical ID of this resource.
*/
public set logicalId(v: string) {
this.originalLogicalId = v;
}

/**
* Returns a lazy reference to this resource (evaluated only upon synthesis).
*/
public get ref() {
return new Token(() => ({ Ref: this.lazyLogicalId })).toString();
}

/**
* Does nothing.
*/
public set ref(_v: string) {
return;
this.originalLogicalId = this.node.stack.logicalIds.getLogicalId(this);
}

/**
Expand All @@ -159,15 +121,13 @@ class LatestDeploymentResource extends CfnDeployment {
protected prepare() {
// if hash components were added to the deployment, we use them to calculate
// a logical ID for the deployment resource.
if (this.hashComponents.length === 0) {
this.lazyLogicalId = this.originalLogicalId;
} else {
if (this.hashComponents.length > 0) {
const md5 = crypto.createHash('md5');
this.hashComponents
.map(c => this.node.resolve(c))
.forEach(c => md5.update(JSON.stringify(c)));

this.lazyLogicalId = this.originalLogicalId + md5.digest("hex");
this.overrideLogicalId(this.originalLogicalId + md5.digest("hex"));
}

super.prepare();
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-cloudtrail/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import events = require('@aws-cdk/aws-events');
import iam = require('@aws-cdk/aws-iam');
import kms = require('@aws-cdk/aws-kms');
import logs = require('@aws-cdk/aws-logs');
Expand Down Expand Up @@ -209,6 +210,21 @@ export class Trail extends Resource {
}]
});
}

/**
* Create an event rule for when an event is recorded by any trail.
*
* Note that the event doesn't necessarily have to come from this
* trail. Be sure to filter the event properly using an event pattern.
*/
public onEvent(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
const rule = new events.Rule(this, name, options);
rule.addTarget(target);
rule.addEventPattern({
detailType: ['AWS API Call via CloudTrail']
});
return rule;
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-cloudtrail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"pkglint": "^0.31.0"
},
"dependencies": {
"@aws-cdk/aws-events": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-kms": "^0.31.0",
"@aws-cdk/aws-logs": "^0.31.0",
Expand All @@ -75,6 +76,7 @@
},
"homepage": "https://github.com/awslabs/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-events": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-kms": "^0.31.0",
"@aws-cdk/aws-logs": "^0.31.0",
Expand Down
34 changes: 33 additions & 1 deletion packages/@aws-cdk/aws-cloudtrail/test/test.cloudtrail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,39 @@ export = {
test.done();
},
}
}
},

'add an event rule'(test: Test) {
// GIVEN
const stack = getTestStack();
const trail = new Trail(stack, 'MyAmazingCloudTrail', { managementEvents: ReadWriteType.WriteOnly });

// WHEN
trail.onEvent('DoEvents', {
bind: () => ({
arn: 'arn',
id: 'myid'
})
});

// THEN
expect(stack).to(haveResource('AWS::Events::Rule', {
EventPattern: {
"detail-type": [
"AWS API Call via CloudTrail"
]
},
State: "ENABLED",
Targets: [
{
Arn: "arn",
Id: "myid"
}
]
}));

test.done();
},
};

function getTestStack(): Stack {
Expand Down
88 changes: 88 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import events = require('@aws-cdk/aws-events');

/**
* Event fields for the CodeBuild "state change" event
*
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html#sample-build-notifications-ref
*/
export class StateChangeEvent {
/**
* The triggering build's status
*/
public static get buildStatus() {
return events.EventField.fromPath('$.detail.build-status');
}

/**
* The triggering build's project name
*/
public static get projectName() {
return events.EventField.fromPath('$.detail.project-name');
}

/**
* Return the build id
*/
public static get buildId() {
return events.EventField.fromPath('$.detail.build-id');
}

public static get currentPhase() {
return events.EventField.fromPath('$.detail.current-phase');
}

private constructor() {
}
}

/**
* Event fields for the CodeBuild "phase change" event
*
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html#sample-build-notifications-ref
*/
export class PhaseChangeEvent {
/**
* The triggering build's project name
*/
public static get projectName() {
return events.EventField.fromPath('$.detail.project-name');
}

/**
* The triggering build's id
*/
public static get buildId() {
return events.EventField.fromPath('$.detail.build-id');
}

/**
* The phase that was just completed
*/
public static get completedPhase() {
return events.EventField.fromPath('$.detail.completed-phase');
}

/**
* The status of the completed phase
*/
public static get completedPhaseStatus() {
return events.EventField.fromPath('$.detail.completed-phase-status');
}

/**
* The duration of the completed phase
*/
public static get completedPhaseDurationSeconds() {
return events.EventField.fromPath('$.detail.completed-phase-duration-seconds');
}

/**
* Whether the build is complete
*/
public static get buildComplete() {
return events.EventField.fromPath('$.detail.build-complete');
}

private constructor() {
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-codebuild/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './events';
export * from './pipeline-project';
export * from './project';
export * from './source';
Expand Down
39 changes: 27 additions & 12 deletions packages/@aws-cdk/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,35 @@ export interface IProject extends IResource, iam.IGrantable {
* You can also use the methods `onBuildFailed` and `onBuildSucceeded` to define rules for
* these specific state changes.
*
* To access fields from the event in the event target input,
* use the static fields on the `StateChangeEvent` class.
*
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
*/
onStateChange(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps): events.EventRule;
onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;

/**
* Defines a CloudWatch event rule that triggers upon phase change of this
* build project.
*
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
*/
onPhaseChange(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps): events.EventRule;
onPhaseChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;

/**
* Defines an event rule which triggers when a build starts.
*/
onBuildStarted(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps): events.EventRule;
onBuildStarted(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;

/**
* Defines an event rule which triggers when a build fails.
*/
onBuildFailed(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps): events.EventRule;
onBuildFailed(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;

/**
* Defines an event rule which triggers when a build completes successfully.
*/
onBuildSucceeded(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps): events.EventRule;
onBuildSucceeded(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule;

/**
* @returns a CloudWatch metric associated with this build project.
Expand Down Expand Up @@ -174,10 +177,13 @@ abstract class ProjectBase extends Resource implements IProject {
* You can also use the methods `onBuildFailed` and `onBuildSucceeded` to define rules for
* these specific state changes.
*
* To access fields from the event in the event target input,
* use the static fields on the `StateChangeEvent` class.
*
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
*/
public onStateChange(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps) {
const rule = new events.EventRule(this, name, options);
public onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
const rule = new events.Rule(this, name, options);
rule.addTarget(target);
rule.addEventPattern({
source: ['aws.codebuild'],
Expand All @@ -197,8 +203,8 @@ abstract class ProjectBase extends Resource implements IProject {
*
* @see https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html
*/
public onPhaseChange(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps) {
const rule = new events.EventRule(this, name, options);
public onPhaseChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
const rule = new events.Rule(this, name, options);
rule.addTarget(target);
rule.addEventPattern({
source: ['aws.codebuild'],
Expand All @@ -214,8 +220,11 @@ abstract class ProjectBase extends Resource implements IProject {

/**
* Defines an event rule which triggers when a build starts.
*
* To access fields from the event in the event target input,
* use the static fields on the `StateChangeEvent` class.
*/
public onBuildStarted(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps) {
public onBuildStarted(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
const rule = this.onStateChange(name, target, options);
rule.addEventPattern({
detail: {
Expand All @@ -227,8 +236,11 @@ abstract class ProjectBase extends Resource implements IProject {

/**
* Defines an event rule which triggers when a build fails.
*
* To access fields from the event in the event target input,
* use the static fields on the `StateChangeEvent` class.
*/
public onBuildFailed(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps) {
public onBuildFailed(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
const rule = this.onStateChange(name, target, options);
rule.addEventPattern({
detail: {
Expand All @@ -240,8 +252,11 @@ abstract class ProjectBase extends Resource implements IProject {

/**
* Defines an event rule which triggers when a build completes successfully.
*
* To access fields from the event in the event target input,
* use the static fields on the `StateChangeEvent` class.
*/
public onBuildSucceeded(name: string, target?: events.IEventRuleTarget, options?: events.EventRuleProps) {
public onBuildSucceeded(name: string, target?: events.IRuleTarget, options?: events.RuleProps) {
const rule = this.onStateChange(name, target, options);
rule.addEventPattern({
detail: {
Expand Down
Loading