Skip to content

Commit

Permalink
feat(aws-chatbot): allow adding a sns topic in existing SlackChannel (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xykkong authored Oct 6, 2021
1 parent 6d468d2 commit d29a20b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-chatbot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aw

```ts
import * as chatbot from '@aws-cdk/aws-chatbot';
import * as sns from '@aws-cdk/aws-sns';

const slackChannel = new chatbot.SlackChannelConfiguration(this, 'MySlackChannel', {
slackChannelConfigurationName: 'YOUR_CHANNEL_NAME',
Expand All @@ -31,6 +32,8 @@ slackChannel.addToRolePolicy(new iam.PolicyStatement({
],
resources: ['arn:aws:s3:::abc/xyz/123.txt'],
}));

slackChannel.addNotificationTopic(new sns.Topic(this, 'MyTopic'))
```

## Log Group
Expand Down
19 changes: 17 additions & 2 deletions packages/@aws-cdk/aws-chatbot/lib/slack-channel-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ export class SlackChannelConfiguration extends SlackChannelConfigurationBase {
} else {
this.slackChannelConfigurationName = resourceName.substring('slack-channel/'.length);
}

}
}

Expand Down Expand Up @@ -268,6 +267,12 @@ export class SlackChannelConfiguration extends SlackChannelConfigurationBase {

readonly grantPrincipal: iam.IPrincipal;

/**
* The SNS topic that deliver notifications to AWS Chatbot.
* @attribute
*/
private readonly notificationTopics: sns.ITopic[];

constructor(scope: Construct, id: string, props: SlackChannelConfigurationProps) {
super(scope, id, {
physicalName: props.slackChannelConfigurationName,
Expand All @@ -279,12 +284,14 @@ export class SlackChannelConfiguration extends SlackChannelConfigurationBase {

this.grantPrincipal = this.role;

this.notificationTopics = props.notificationTopics ?? [];

const configuration = new CfnSlackChannelConfiguration(this, 'Resource', {
configurationName: props.slackChannelConfigurationName,
iamRoleArn: this.role.roleArn,
slackWorkspaceId: props.slackWorkspaceId,
slackChannelId: props.slackChannelId,
snsTopicArns: props.notificationTopics?.map(topic => topic.topicArn),
snsTopicArns: cdk.Lazy.list({ produce: () => this.notificationTopics.map(topic => topic.topicArn) }, { omitEmpty: true } ),
loggingLevel: props.loggingLevel?.toString(),
});

Expand All @@ -303,5 +310,13 @@ export class SlackChannelConfiguration extends SlackChannelConfigurationBase {
this.slackChannelConfigurationArn = configuration.ref;
this.slackChannelConfigurationName = props.slackChannelConfigurationName;
}

/**
* Adds a SNS topic that deliver notifications to AWS Chatbot.
* @param notificationTopic
*/
public addNotificationTopic(notificationTopic: sns.ITopic): void {
this.notificationTopics.push(notificationTopic);
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@aws-cdk/assert-internal/jest';
import { ABSENT } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as iam from '@aws-cdk/aws-iam';
import * as logs from '@aws-cdk/aws-logs';
Expand Down Expand Up @@ -99,6 +99,26 @@ describe('SlackChannelConfiguration', () => {
});
});

test('allows adding a Topic after creating the SlackChannel', () => {
const slackChannel = new chatbot.SlackChannelConfiguration(stack, 'MySlackChannel', {
slackWorkspaceId: 'ABC123',
slackChannelId: 'DEF456',
slackChannelConfigurationName: 'Test',
});

const topic = new sns.Topic(stack, 'MyTopic');
slackChannel.addNotificationTopic(topic);

expect(stack).toHaveResourceLike('AWS::Chatbot::SlackChannelConfiguration', {
ConfigurationName: 'Test',
SnsTopicArns: [
{
Ref: 'MyTopic86869434',
},
],
});
});

test('created with existing role', () => {
const role = iam.Role.fromRoleArn(stack, 'Role', 'arn:aws:iam:::role/test-role');

Expand Down

0 comments on commit d29a20b

Please sign in to comment.