forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-codepipeline): support notifications on the ManualApprovalAc…
…tion. Fixes aws#1222
- Loading branch information
Showing
5 changed files
with
288 additions
and
2 deletions.
There are no files selected for viewing
49 changes: 48 additions & 1 deletion
49
packages/@aws-cdk/aws-codepipeline/lib/manual-approval-action.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 |
---|---|---|
@@ -1,21 +1,68 @@ | ||
import actions = require('@aws-cdk/aws-codepipeline-api'); | ||
import sns = require('@aws-cdk/aws-sns'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
|
||
// tslint:disable-next-line:no-empty-interface | ||
/** | ||
* Construction properties of the {@link ManualApprovalAction}. | ||
*/ | ||
export interface ManualApprovalActionProps extends actions.CommonActionProps, | ||
actions.CommonActionConstructProps { | ||
/** | ||
* Optional SNS topic to send notifications to when an approval is pending. | ||
*/ | ||
notificationTopic?: sns.TopicRef; | ||
|
||
/** | ||
* A list of email addresses to subscribe to notifications when this Action is pending approval. | ||
* If this has been provided, but not `notificationTopic`, | ||
* a new Topic will be created. | ||
*/ | ||
notifyEmails?: string[]; | ||
|
||
/** | ||
* Any additional information that you want to include in the notification email message. | ||
*/ | ||
additionalInformation?: string; | ||
} | ||
|
||
/** | ||
* Manual approval action. | ||
*/ | ||
export class ManualApprovalAction extends actions.Action { | ||
private _notificationTopic?: sns.TopicRef; | ||
|
||
constructor(parent: cdk.Construct, name: string, props: ManualApprovalActionProps) { | ||
super(parent, name, { | ||
category: actions.ActionCategory.Approval, | ||
provider: 'Manual', | ||
artifactBounds: { minInputs: 0, maxInputs: 0, minOutputs: 0, maxOutputs: 0 }, | ||
configuration: new cdk.Token(() => this.actionConfiguration(props)), | ||
...props, | ||
}); | ||
|
||
const anyEmailsProvided = (props.notifyEmails || []).length > 0; | ||
this._notificationTopic = props.notificationTopic | ||
? props.notificationTopic | ||
: anyEmailsProvided ? new sns.Topic(this, 'TopicResource') : undefined; | ||
|
||
if (this.notificationTopic) { | ||
this.notificationTopic.grantPublish(props.stage.pipeline.role); | ||
for (const notifyEmail of props.notifyEmails || []) { | ||
this.notificationTopic.subscribeEmail(`Subscription-${notifyEmail}`, notifyEmail); | ||
} | ||
} | ||
} | ||
|
||
public get notificationTopic(): sns.TopicRef | undefined { | ||
return this._notificationTopic; | ||
} | ||
|
||
private actionConfiguration(props: ManualApprovalActionProps): any { | ||
return this.notificationTopic | ||
? { | ||
NotificationArn: this.notificationTopic.topicArn, | ||
CustomData: props.additionalInformation, | ||
} | ||
: undefined; | ||
} | ||
} |
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
197 changes: 197 additions & 0 deletions
197
packages/@aws-cdk/aws-codepipeline/test/integ.pipeline-manual-approval.expected.json
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,197 @@ | ||
{ | ||
"Resources": { | ||
"Bucket83908E77": { | ||
"Type": "AWS::S3::Bucket", | ||
"DeletionPolicy": "Retain" | ||
}, | ||
"PipelineRoleD68726F7": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "codepipeline.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"PipelineRoleDefaultPolicyC7A05455": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"s3:GetObject*", | ||
"s3:GetBucket*", | ||
"s3:List*", | ||
"s3:DeleteObject*", | ||
"s3:PutObject*", | ||
"s3:Abort*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": [ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
"/*" | ||
] | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"Action": [ | ||
"s3:GetObject*", | ||
"s3:GetBucket*", | ||
"s3:List*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": [ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
"/*" | ||
] | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"Action": "sns:Publish", | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Ref": "ManualApprovalTopicResource300641E2" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "PipelineRoleDefaultPolicyC7A05455", | ||
"Roles": [ | ||
{ | ||
"Ref": "PipelineRoleD68726F7" | ||
} | ||
] | ||
} | ||
}, | ||
"PipelineC660917D": { | ||
"Type": "AWS::CodePipeline::Pipeline", | ||
"Properties": { | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"PipelineRoleD68726F7", | ||
"Arn" | ||
] | ||
}, | ||
"Stages": [ | ||
{ | ||
"Actions": [ | ||
{ | ||
"ActionTypeId": { | ||
"Category": "Source", | ||
"Owner": "AWS", | ||
"Provider": "S3", | ||
"Version": "1" | ||
}, | ||
"Configuration": { | ||
"S3Bucket": { | ||
"Ref": "Bucket83908E77" | ||
}, | ||
"S3ObjectKey": "file.zip", | ||
"PollForSourceChanges": true | ||
}, | ||
"InputArtifacts": [], | ||
"Name": "S3", | ||
"OutputArtifacts": [ | ||
{ | ||
"Name": "Artifact_awscdkcodepipelinemanualapprovalBucketS39750AFE7" | ||
} | ||
], | ||
"RunOrder": 1 | ||
} | ||
], | ||
"Name": "Source" | ||
}, | ||
{ | ||
"Actions": [ | ||
{ | ||
"ActionTypeId": { | ||
"Category": "Approval", | ||
"Owner": "AWS", | ||
"Provider": "Manual", | ||
"Version": "1" | ||
}, | ||
"Configuration": { | ||
"NotificationArn": { | ||
"Ref": "ManualApprovalTopicResource300641E2" | ||
} | ||
}, | ||
"InputArtifacts": [], | ||
"Name": "ManualApproval", | ||
"OutputArtifacts": [], | ||
"RunOrder": 1 | ||
} | ||
], | ||
"Name": "Approve" | ||
} | ||
], | ||
"ArtifactStore": { | ||
"Location": { | ||
"Ref": "Bucket83908E77" | ||
}, | ||
"Type": "S3" | ||
} | ||
}, | ||
"DependsOn": [ | ||
"PipelineRoleD68726F7", | ||
"PipelineRoleDefaultPolicyC7A05455" | ||
] | ||
}, | ||
"ManualApprovalTopicResource300641E2": { | ||
"Type": "AWS::SNS::Topic" | ||
}, | ||
"ManualApprovalTopicResourceSubscriptionadamruka85gmailcomBACEE98E": { | ||
"Type": "AWS::SNS::Subscription", | ||
"Properties": { | ||
"Endpoint": "adamruka85@gmail.com", | ||
"Protocol": "email", | ||
"TopicArn": { | ||
"Ref": "ManualApprovalTopicResource300641E2" | ||
} | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/@aws-cdk/aws-codepipeline/test/integ.pipeline-manual-approval.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,26 @@ | ||
import s3 = require('@aws-cdk/aws-s3'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
import codepipeline = require('../lib'); | ||
|
||
const app = new cdk.App(); | ||
|
||
const stack = new cdk.Stack(app, 'aws-cdk-codepipeline-manual-approval'); | ||
|
||
const bucket = new s3.Bucket(stack, 'Bucket'); | ||
|
||
const pipeline = new codepipeline.Pipeline(stack, 'Pipeline', { | ||
artifactBucket: bucket, | ||
}); | ||
|
||
const sourceStage = pipeline.addStage('Source'); | ||
bucket.addToPipeline(sourceStage, 'S3', { | ||
bucketKey: 'file.zip', | ||
}); | ||
|
||
const approveStage = pipeline.addStage('Approve'); | ||
new codepipeline.ManualApprovalAction(stack, 'ManualApproval', { | ||
stage: approveStage, | ||
notifyEmails: ['adamruka85@gmail.com'] | ||
}); | ||
|
||
app.run(); |
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