forked from matt-filion/serverless-external-s3-event
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BucketConfig.js
112 lines (95 loc) · 3.01 KB
/
BucketConfig.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
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
'use strict'
const S3 = require('./S3.js')
class BucketConfig {
constructor(config, serverless, options) {
this.config = config
this.options = options;
this.s3 = new S3()
this.serverless = serverless
if (serverless != undefined){
this.service = serverless.service.getServiceObject().name
this.stage = serverless.service.provider.stage
}
}
getConfig() {
return this.config
}
//update the current configuration with the ones stored in serverless.yml
update(fileConfig) {
this.addNewNotifications(fileConfig)
this.removeObsoleteNotifications(fileConfig)
}
remove(functionNames, region) {
functionNames.forEach(name => {
let re = new RegExp(`^arn:[cn\-]?aws:lambda:${region}:[0-9]+:function:${name}$`, 'i');
this.config.results.LambdaFunctionConfigurations =
this.config.results.LambdaFunctionConfigurations.filter(function (e) {
return !re.test(e.LambdaFunctionArn);
});
})
}
addNewNotifications(fileConfig) {
fileConfig.events.forEach((event) => {
if (this.isNew(event)) this.addNewNotification(event)
})
}
isNew(event) {
let id = this.s3.getId(event)
let found = this.config.results.LambdaFunctionConfigurations.find(function(e) {
return e.Id == id
})
return found == undefined
}
addNewNotification(event) {
let id = this.s3.getId(event)
this.config.results.LambdaFunctionConfigurations.push({
Id: id,
LambdaFunctionArn: event.arn,
Events: event.existingS3.events,
Filter: this.notificationFilterFrom(event)
})
}
notificationFilterFrom(event) {
let filter = undefined
if(event.existingS3.rules && event.existingS3.rules.length !== 0) {
filter = {
Key: {
FilterRules: event.existingS3.rules.map( rule => {
const key = Object.keys(rule)[0];
return {
Name: key,
Value: rule[key]
}
})
}
}
}
return filter
}
removeObsoleteNotifications(fileConfig) {
let notifications = this.config
.results
.LambdaFunctionConfigurations
.filter(n => this.isActive(n, fileConfig))
this.config.results.LambdaFunctionConfigurations = notifications
}
isActive(notification, fileConfig) {
return this.inConfig(notification, fileConfig) || this.notRelevant(notification, fileConfig)
}
inConfig(notification, fileConfig) {
let found = fileConfig.events.find((event) => {
let id = this.s3.getId(event)
return id == notification.Id && this.relevantARN(event.arn)
})
return found != undefined
}
notRelevant(notification, fileConfig) {
return (this.relevantARN(notification.LambdaFunctionArn) == null) || !notification.Id.startsWith("exS3-v2")
}
relevantARN(arn) {
const aliasPart = (this.options && this.options.alias) ? `.*:${this.options.alias}` : '';
let re = new RegExp(this.service + "-" + this.stage + aliasPart, 'gi')
return arn.match(re)
}
}
module.exports = BucketConfig