forked from anomalyco/sst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopic.ts
More file actions
222 lines (195 loc) · 6.44 KB
/
Copy pathTopic.ts
File metadata and controls
222 lines (195 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import * as cdk from "@aws-cdk/core";
import * as sns from "@aws-cdk/aws-sns";
import * as snsSubscriptions from "@aws-cdk/aws-sns-subscriptions";
import { App } from "./App";
import { Function as Fn, FunctionProps, FunctionDefinition } from "./Function";
import { Queue } from "./Queue";
import { Permissions } from "./util/permission";
import { isConstructOf } from "./util/construct";
/////////////////////
// Interfaces
/////////////////////
export interface TopicProps {
readonly snsTopic?: sns.ITopic | sns.TopicProps;
readonly subscribers?: (
| FunctionDefinition
| TopicFunctionSubscriberProps
| Queue
| TopicQueueSubscriberProps
)[];
readonly defaultFunctionProps?: FunctionProps;
}
export interface TopicFunctionSubscriberProps {
readonly function: FunctionDefinition;
readonly subscriberProps?: snsSubscriptions.LambdaSubscriptionProps;
}
export interface TopicQueueSubscriberProps {
readonly queue: Queue;
readonly subscriberProps?: snsSubscriptions.SqsSubscriptionProps;
}
/////////////////////
// Construct
/////////////////////
export class Topic extends cdk.Construct {
public readonly snsTopic: sns.Topic;
private readonly subscribers: (Fn | Queue)[];
private readonly permissionsAttachedForAllSubscribers: Permissions[];
private readonly defaultFunctionProps?: FunctionProps;
constructor(scope: cdk.Construct, id: string, props?: TopicProps) {
super(scope, id);
const root = scope.node.root as App;
const { snsTopic, subscribers, defaultFunctionProps } = props || {};
this.subscribers = [];
this.permissionsAttachedForAllSubscribers = [];
this.defaultFunctionProps = defaultFunctionProps;
////////////////////
// Create Topic
////////////////////
if (cdk.Construct.isConstruct(snsTopic)) {
this.snsTopic = snsTopic as sns.Topic;
} else {
const snsTopicProps = (snsTopic || {}) as sns.TopicProps;
this.snsTopic = new sns.Topic(this, "Topic", {
topicName: root.logicalPrefixedName(id),
...snsTopicProps,
});
}
///////////////////////////
// Create Subscribers
///////////////////////////
this.addSubscribers(this, subscribers || []);
}
public get topicArn(): string {
return this.snsTopic.topicArn;
}
public get topicName(): string {
return this.snsTopic.topicName;
}
public get subscriberFunctions(): Fn[] {
return this.subscribers.filter(
(subscriber) => subscriber instanceof Fn
) as Fn[];
}
public get snsSubscriptions(): sns.Subscription[] {
return this.subscribers.map((sub) => {
let children;
// look for sns.Subscription inside Queue.sqsQueue
if (sub instanceof Queue) {
children = sub.sqsQueue.node.children;
}
// look for sns.Subscription inside Function
else {
children = sub.node.children;
}
const child = children.find((child) => {
return isConstructOf(child as cdk.Construct, "aws-sns.Subscription");
});
return child as sns.Subscription;
});
}
public addSubscribers(
scope: cdk.Construct,
subscribers: (
| FunctionDefinition
| TopicFunctionSubscriberProps
| Queue
| TopicQueueSubscriberProps
)[]
): void {
subscribers.forEach((subscriber) => this.addSubscriber(scope, subscriber));
}
public attachPermissions(permissions: Permissions): void {
this.subscribers
.filter((subscriber) => subscriber instanceof Fn)
.forEach((subscriber) => subscriber.attachPermissions(permissions));
this.permissionsAttachedForAllSubscribers.push(permissions);
}
public attachPermissionsToSubscriber(
index: number,
permissions: Permissions
): void {
const subscriber = this.subscribers[index];
if (!(subscriber instanceof Fn)) {
throw new Error(
`Cannot attach permissions to the "${this.node.id}" Topic subscriber because it's not a Lambda function`
);
}
subscriber.attachPermissions(permissions);
}
private addSubscriber(
scope: cdk.Construct,
subscriber:
| FunctionDefinition
| TopicFunctionSubscriberProps
| Queue
| TopicQueueSubscriberProps
): void {
if (
subscriber instanceof Queue ||
(subscriber as TopicQueueSubscriberProps).queue
) {
subscriber = subscriber as Queue | TopicQueueSubscriberProps;
this.addQueueSubscriber(scope, subscriber);
} else {
subscriber = subscriber as
| FunctionDefinition
| TopicFunctionSubscriberProps;
this.addFunctionSubscriber(scope, subscriber);
}
}
private addQueueSubscriber(
scope: cdk.Construct,
subscriber: Queue | TopicQueueSubscriberProps
): void {
// Parse subscriber props
let subscriberProps;
let queue;
if (subscriber instanceof Queue) {
subscriber = subscriber as Queue;
queue = subscriber;
} else {
subscriber = subscriber as TopicQueueSubscriberProps;
subscriberProps = subscriber.subscriberProps;
queue = subscriber.queue;
}
this.subscribers.push(queue);
// Create Subscription
this.snsTopic.addSubscription(
new snsSubscriptions.SqsSubscription(queue.sqsQueue, subscriberProps)
);
}
private addFunctionSubscriber(
scope: cdk.Construct,
subscriber: FunctionDefinition | TopicFunctionSubscriberProps
): void {
// Parse subscriber props
let subscriberProps;
let functionDefinition;
if ((subscriber as TopicFunctionSubscriberProps).function) {
subscriber = subscriber as TopicFunctionSubscriberProps;
subscriberProps = subscriber.subscriberProps;
functionDefinition = subscriber.function;
} else {
subscriber = subscriber as FunctionDefinition;
functionDefinition = subscriber;
}
// Create function
const i = this.subscribers.length;
const fn = Fn.fromDefinition(
scope,
`Subscriber_${i}`,
functionDefinition,
this.defaultFunctionProps,
`The "defaultFunctionProps" cannot be applied if an instance of a Function construct is passed in. Make sure to define all the subscribers using FunctionProps, so the Topic construct can apply the "defaultFunctionProps" to them.`
);
this.subscribers.push(fn);
// Create Subscription
this.snsTopic.addSubscription(
new snsSubscriptions.LambdaSubscription(fn, subscriberProps)
);
// Attach existing permissions
this.permissionsAttachedForAllSubscribers.forEach((permissions) =>
fn.attachPermissions(permissions)
);
}
}