|
| 1 | +import * as iam from '@aws-cdk/aws-iam'; |
| 2 | +import * as sns from '@aws-cdk/aws-sns'; |
| 3 | +import * as cdk from '@aws-cdk/core'; |
| 4 | +import { CfnSlackChannelConfiguration } from './chatbot.generated'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Properties for a new Slack channel configuration |
| 8 | + */ |
| 9 | +export interface SlackChannelConfigurationProps { |
| 10 | + |
| 11 | + /** |
| 12 | + * The name of Slack channel configuration |
| 13 | + */ |
| 14 | + readonly slackChannelConfigurationName: string; |
| 15 | + |
| 16 | + /** |
| 17 | + * The permission role of Slack channel configuration |
| 18 | + * |
| 19 | + * @default - A role will be created. |
| 20 | + */ |
| 21 | + readonly role?: iam.IRole; |
| 22 | + |
| 23 | + /** |
| 24 | + * The ID of the Slack workspace authorized with AWS Chatbot. |
| 25 | + * |
| 26 | + * To get the workspace ID, you must perform the initial authorization flow with Slack in the AWS Chatbot console. |
| 27 | + * Then you can copy and paste the workspace ID from the console. |
| 28 | + * For more details, see steps 1-4 in Setting Up AWS Chatbot with Slack in the AWS Chatbot User Guide. |
| 29 | + * @see https://docs.aws.amazon.com/chatbot/latest/adminguide/setting-up.html#Setup_intro |
| 30 | + */ |
| 31 | + readonly slackWorkspaceId: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * The ID of the Slack channel. |
| 35 | + * |
| 36 | + * To get the ID, open Slack, right click on the channel name in the left pane, then choose Copy Link. |
| 37 | + * The channel ID is the 9-character string at the end of the URL. For example, ABCBBLZZZ. |
| 38 | + */ |
| 39 | + readonly slackChannelId: string; |
| 40 | + |
| 41 | + /** |
| 42 | + * The SNS topics that deliver notifications to AWS Chatbot. |
| 43 | + * |
| 44 | + * @default None |
| 45 | + */ |
| 46 | + readonly notificationTopics?: sns.ITopic[]; |
| 47 | + |
| 48 | + /** |
| 49 | + * Specifies the logging level for this configuration. |
| 50 | + * This property affects the log entries pushed to Amazon CloudWatch Logs. |
| 51 | + * |
| 52 | + * @default LoggingLevel.NONE |
| 53 | + */ |
| 54 | + readonly loggingLevel?: LoggingLevel; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Logging levels include ERROR, INFO, or NONE. |
| 59 | + */ |
| 60 | +export enum LoggingLevel { |
| 61 | + /** |
| 62 | + * ERROR |
| 63 | + */ |
| 64 | + ERROR = 'ERROR', |
| 65 | + |
| 66 | + /** |
| 67 | + * INFO |
| 68 | + */ |
| 69 | + INFO = 'INFO', |
| 70 | + |
| 71 | + /** |
| 72 | + * NONE |
| 73 | + */ |
| 74 | + NONE = 'NONE', |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Represents a Slack channel configuration |
| 79 | + */ |
| 80 | +export interface ISlackChannelConfiguration extends cdk.IResource, iam.IGrantable { |
| 81 | + |
| 82 | + /** |
| 83 | + * The ARN of the Slack channel configuration |
| 84 | + * In the form of arn:aws:chatbot:{region}:{account}:chat-configuration/slack-channel/{slackChannelName} |
| 85 | + * @attribute |
| 86 | + */ |
| 87 | + readonly slackChannelConfigurationArn: string; |
| 88 | + |
| 89 | + /** |
| 90 | + * The name of Slack channel configuration |
| 91 | + * @attribute |
| 92 | + */ |
| 93 | + readonly slackChannelConfigurationName: string; |
| 94 | + |
| 95 | + /** |
| 96 | + * The permission role of Slack channel configuration |
| 97 | + * @attribute |
| 98 | + * |
| 99 | + * @default - A role will be created. |
| 100 | + */ |
| 101 | + readonly role?: iam.IRole; |
| 102 | + |
| 103 | + /** |
| 104 | + * Adds a statement to the IAM role. |
| 105 | + */ |
| 106 | + addToRolePolicy(statement: iam.PolicyStatement): void; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Either a new or imported Slack channel configuration |
| 111 | + */ |
| 112 | +abstract class SlackChannelConfigurationBase extends cdk.Resource implements ISlackChannelConfiguration { |
| 113 | + abstract readonly slackChannelConfigurationArn: string; |
| 114 | + |
| 115 | + abstract readonly slackChannelConfigurationName: string; |
| 116 | + |
| 117 | + abstract readonly grantPrincipal: iam.IPrincipal; |
| 118 | + |
| 119 | + abstract readonly role?: iam.IRole; |
| 120 | + |
| 121 | + /** |
| 122 | + * Adds extra permission to iam-role of Slack channel configuration |
| 123 | + * @param statement |
| 124 | + */ |
| 125 | + public addToRolePolicy(statement: iam.PolicyStatement): void { |
| 126 | + if (!this.role) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + this.role.addToPrincipalPolicy(statement); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * A new Slack channel configuration |
| 136 | + */ |
| 137 | +export class SlackChannelConfiguration extends SlackChannelConfigurationBase { |
| 138 | + |
| 139 | + /** |
| 140 | + * Import an existing Slack channel configuration provided an ARN |
| 141 | + * @param scope The parent creating construct |
| 142 | + * @param id The construct's name |
| 143 | + * @param slackChannelConfigurationArn configuration ARN (i.e. arn:aws:chatbot::1234567890:chat-configuration/slack-channel/my-slack) |
| 144 | + * |
| 145 | + * @returns a reference to the existing Slack channel configuration |
| 146 | + */ |
| 147 | + public static fromSlackChannelConfigurationArn(scope: cdk.Construct, id: string, slackChannelConfigurationArn: string): ISlackChannelConfiguration { |
| 148 | + const re = /^slack-channel\//; |
| 149 | + const resourceName = cdk.Stack.of(scope).parseArn(slackChannelConfigurationArn).resourceName as string; |
| 150 | + |
| 151 | + if (!re.test(resourceName)) { |
| 152 | + throw new Error('The ARN of a Slack integration must be in the form: arn:aws:chatbot:{region}:{account}:chat-configuration/slack-channel/{slackChannelName}'); |
| 153 | + } |
| 154 | + |
| 155 | + class Import extends SlackChannelConfigurationBase { |
| 156 | + |
| 157 | + /** |
| 158 | + * @attribute |
| 159 | + */ |
| 160 | + readonly slackChannelConfigurationArn = slackChannelConfigurationArn; |
| 161 | + readonly role?: iam.IRole = undefined; |
| 162 | + readonly grantPrincipal: iam.IPrincipal; |
| 163 | + |
| 164 | + /** |
| 165 | + * Returns a name of Slack channel configuration |
| 166 | + * |
| 167 | + * NOTE: |
| 168 | + * For example: arn:aws:chatbot::1234567890:chat-configuration/slack-channel/my-slack |
| 169 | + * The ArnComponents API will return `slack-channel/my-slack` |
| 170 | + * It need to handle that to gets a correct name.`my-slack` |
| 171 | + */ |
| 172 | + readonly slackChannelConfigurationName = resourceName.substring('slack-channel/'.length); |
| 173 | + |
| 174 | + constructor(s: cdk.Construct, i: string) { |
| 175 | + super(s, i); |
| 176 | + this.grantPrincipal = new iam.UnknownPrincipal({ resource: this }); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return new Import(scope, id); |
| 181 | + } |
| 182 | + |
| 183 | + readonly slackChannelConfigurationArn: string; |
| 184 | + |
| 185 | + readonly slackChannelConfigurationName: string; |
| 186 | + |
| 187 | + readonly role?: iam.IRole; |
| 188 | + |
| 189 | + readonly grantPrincipal: iam.IPrincipal; |
| 190 | + |
| 191 | + constructor(scope: cdk.Construct, id: string, props: SlackChannelConfigurationProps) { |
| 192 | + super(scope, id, { |
| 193 | + physicalName: props.slackChannelConfigurationName, |
| 194 | + }); |
| 195 | + |
| 196 | + this.role = props.role || new iam.Role(this, 'ConfigurationRole', { |
| 197 | + assumedBy: new iam.ServicePrincipal('chatbot.amazonaws.com'), |
| 198 | + }); |
| 199 | + |
| 200 | + this.grantPrincipal = this.role; |
| 201 | + |
| 202 | + const configuration = new CfnSlackChannelConfiguration(this, 'Resource', { |
| 203 | + configurationName: props.slackChannelConfigurationName, |
| 204 | + iamRoleArn: this.role.roleArn, |
| 205 | + slackWorkspaceId: props.slackWorkspaceId, |
| 206 | + slackChannelId: props.slackChannelId, |
| 207 | + snsTopicArns: props.notificationTopics?.map(topic => topic.topicArn), |
| 208 | + loggingLevel: props.loggingLevel?.toString(), |
| 209 | + }); |
| 210 | + |
| 211 | + this.slackChannelConfigurationArn = configuration.ref; |
| 212 | + this.slackChannelConfigurationName = props.slackChannelConfigurationName; |
| 213 | + } |
| 214 | +} |
| 215 | + |
0 commit comments