This repository was archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_impl.js
76 lines (67 loc) · 2.58 KB
/
index_impl.js
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
const snsApi = require('./snsApi.js');
const response = require("cf-fetch-response");
exports.handler = async (event, context) => {
let {
RequestType,
ResourceProperties: {
FilterPolicy,
SubscriptionArn,
}
} = event;
switch (RequestType.toLowerCase()) {
case 'create': {
console.log('Create sns filter policy');
return create({FilterPolicy, SubscriptionArn})
.then(() => response.sendSuccess(event, context));
}
case 'update': {
console.log('Update sns filter policy');
let OldSubscriptionArn = event.OldResourceProperties.SubscriptionArn;
if (OldSubscriptionArn === SubscriptionArn) {
console.log('Update filter policy on same subscription arn');
return create({FilterPolicy, SubscriptionArn})
.then(() => response.sendSuccess(event, context))
} else {
console.log('Update filter policy on new subscription arn');
console.log('Delete old filter policy');
await deleteFn({SubscriptionArn: OldSubscriptionArn});
console.log('Create new filter policy');
return create({FilterPolicy, SubscriptionArn})
.then(() => response.sendSuccess(event, context));
}
}
case 'delete': {
console.log('Delete sns filter policy');
return deleteFn({SubscriptionArn})
.then(() => response.sendSuccess(event, context));
}
}
};
let create = async ({FilterPolicy, SubscriptionArn} = {}) => {
if (!FilterPolicy || !SubscriptionArn || !Array.isArray(FilterPolicy)) {
throw `missing mandatory argument: FilterPolicy=${FilterPolicy} SubscriptionArn=${SubscriptionArn}`
}
let value = FilterPolicy.reduce((acc, cur) => {
if (acc[cur.Attribute]) {
throw `Duplicate entry: ${cur.Attribute}`;
}
acc[cur.Attribute] = JSON.parse(cur.Policy);
console.log(`Attribute: ${cur.Attribute} Policy: ${acc[cur.Attribute]}`);
return acc;
}, {});
return snsApi.setSubscriptionAttributes({
AttributeName: 'FilterPolicy',
SubscriptionArn: SubscriptionArn,
AttributeValue: JSON.stringify(value)
});
};
let deleteFn = async ({SubscriptionArn} = {}) => {
if (!SubscriptionArn) {
return;
}
return snsApi.setSubscriptionAttributes({
AttributeName: 'FilterPolicy',
SubscriptionArn: SubscriptionArn,
AttributeValue: '{}'
});
};