Skip to content

Commit 112c2f6

Browse files
abetomoDeviaVir
authored andcommitted
Add updateS3Events to main (#394)
* Add S3Events to event_sources.json * Modify to promise chain * Add _updateS3Events() * Add _updateS3Events() call * Improve display of deploy results Since the structure of results has become complicated, I added a method
1 parent 286e063 commit 112c2f6

File tree

3 files changed

+171
-22
lines changed

3 files changed

+171
-22
lines changed

lib/event_sources.json.example

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,19 @@
1818
"key2": "value"
1919
}
2020
}
21-
]
21+
],
22+
"S3Events": [{
23+
"Bucket": "BUCKET_NAME",
24+
"Events": [
25+
"s3:ObjectCreated:*"
26+
],
27+
"Filter": {
28+
"Key": {
29+
"FilterRules": [{
30+
"Name": "prefix",
31+
"Value": "STRING_VALUE"
32+
}]
33+
}
34+
}
35+
}]
2236
}

lib/main.js

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const archiver = require('archiver')
1313
const dotenv = require('dotenv')
1414
const proxy = require('proxy-agent')
1515
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
16+
const S3Events = require(path.join(__dirname, 's3_events'))
1617
const CloudWatchLogs = require(path.join(__dirname, 'cloudwatch_logs'))
1718

1819
const maxBufferSize = 50 * 1024 * 1024
@@ -226,7 +227,8 @@ so you can easily test run multiple events.
226227
if (!program.eventSourceFile) {
227228
return {
228229
EventSourceMappings: null,
229-
ScheduleEvents: null
230+
ScheduleEvents: null,
231+
S3Events: null
230232
}
231233
}
232234
const list = (() => {
@@ -241,7 +243,8 @@ so you can easily test run multiple events.
241243
// backward-compatible
242244
return {
243245
EventSourceMappings: list,
244-
ScheduleEvents: []
246+
ScheduleEvents: [],
247+
S3Events: []
245248
}
246249
}
247250
if (!list.EventSourceMappings) {
@@ -250,6 +253,9 @@ so you can easily test run multiple events.
250253
if (!list.ScheduleEvents) {
251254
list.ScheduleEvents = []
252255
}
256+
if (!list.S3Events) {
257+
list.S3Events = []
258+
}
253259
return list
254260
}
255261

@@ -701,6 +707,23 @@ so you can easily test run multiple events.
701707
})
702708
}
703709

710+
_updateS3Events (s3Events, functionArn, s3EventsList) {
711+
if (s3EventsList == null) return Promise.resolve([])
712+
713+
const paramsList = s3EventsList.map(s3event =>
714+
Object.assign(s3event, { FunctionArn: functionArn }))
715+
716+
// series
717+
return paramsList.map(params => {
718+
return s3Events.add(params)
719+
}).reduce((a, b) => {
720+
return a.then(b)
721+
}, Promise.resolve()).then(() => {
722+
// Since it is similar to _updateScheduleEvents, it returns meaningful values
723+
return paramsList
724+
})
725+
}
726+
704727
_setLogsRetentionPolicy (cloudWatchLogs, program, functionName) {
705728
const days = parseInt(program.retentionInDays)
706729
if (!Number.isInteger(days)) return Promise.resolve({})
@@ -781,6 +804,7 @@ so you can easily test run multiple events.
781804

782805
const lambda = new aws.Lambda({ apiVersion: '2015-03-31' })
783806
const scheduleEvents = new ScheduleEvents(aws)
807+
const s3Events = new S3Events(aws)
784808
const cloudWatchLogs = new CloudWatchLogs(aws)
785809

786810
// Checking function
@@ -795,11 +819,20 @@ so you can easily test run multiple events.
795819
this._uploadExisting(lambda, params).then((results) => {
796820
console.log('=> Zip file(s) done uploading. Results follow: ')
797821
console.log(results)
798-
return this._updateScheduleEvents(
799-
scheduleEvents,
800-
results.FunctionArn,
801-
eventSourceList.ScheduleEvents
802-
)
822+
return results
823+
}).then(results => {
824+
return Promise.all([
825+
this._updateScheduleEvents(
826+
scheduleEvents,
827+
results.FunctionArn,
828+
eventSourceList.ScheduleEvents
829+
),
830+
this._updateS3Events(
831+
s3Events,
832+
results.FunctionArn,
833+
eventSourceList.S3Events
834+
)
835+
])
803836
}),
804837
this._updateEventSources(
805838
lambda,
@@ -836,6 +869,11 @@ so you can easily test run multiple events.
836869
results.FunctionArn,
837870
eventSourceList.ScheduleEvents
838871
),
872+
this._updateS3Events(
873+
s3Events,
874+
results.FunctionArn,
875+
eventSourceList.S3Events
876+
),
839877
this._setLogsRetentionPolicy(
840878
cloudWatchLogs,
841879
program,
@@ -851,6 +889,20 @@ so you can easily test run multiple events.
851889
})
852890
}
853891

892+
_printDeployResults (results, isFirst) {
893+
if (!Array.isArray(results)) {
894+
if (results == null) return
895+
console.log(results)
896+
return
897+
}
898+
if (results.length === 0) return
899+
900+
if (isFirst === true) console.log('=> All tasks done. Results follow:')
901+
results.forEach(result => {
902+
this._printDeployResults(result)
903+
})
904+
}
905+
854906
deploy (program) {
855907
const regions = program.region.split(',')
856908
return this._archive(program).then((buffer) => {
@@ -859,16 +911,8 @@ so you can easily test run multiple events.
859911

860912
return Promise.all(regions.map((region) => {
861913
return this._deployToRegion(program, params, region)
862-
})).then((results) => {
863-
const resultsIsEmpty = results.filter((result) => {
864-
return result.filter((res) => {
865-
return res.length > 0
866-
}).length > 0
867-
}).length === 0
868-
if (!resultsIsEmpty) {
869-
console.log('=> All tasks done. Results follow: ')
870-
console.log(JSON.stringify(results, null, ' '))
871-
}
914+
})).then(results => {
915+
this._printDeployResults(results, true)
872916
})
873917
}).catch((err) => {
874918
process.exitCode = 1

test/main.js

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ const _mockSetting = () => {
9797
awsMock.mock('CloudWatchLogs', 'putRetentionPolicy', (params, callback) => {
9898
callback(null, {})
9999
})
100+
awsMock.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
101+
callback(null, {})
102+
})
100103

101104
Object.keys(lambdaMockSettings).forEach((method) => {
102105
awsMock.mock('Lambda', method, (params, callback) => {
@@ -110,6 +113,7 @@ const _mockSetting = () => {
110113
const _awsRestore = () => {
111114
awsMock.restore('CloudWatchEvents')
112115
awsMock.restore('CloudWatchLogs')
116+
awsMock.restore('S3')
113117
awsMock.restore('Lambda')
114118
}
115119

@@ -781,7 +785,11 @@ describe('lib/main', function () {
781785
program.eventSourceFile = ''
782786
assert.deepEqual(
783787
lambda._eventSourceList(program),
784-
{ EventSourceMappings: null, ScheduleEvents: null }
788+
{
789+
EventSourceMappings: null,
790+
ScheduleEvents: null,
791+
S3Events: null
792+
}
785793
)
786794
})
787795

@@ -803,18 +811,23 @@ describe('lib/main', function () {
803811
fs.writeFileSync('only_ScheduleEvents.json', JSON.stringify({
804812
ScheduleEvents: [{ test: 2 }]
805813
}))
814+
fs.writeFileSync('only_S3Events.json', JSON.stringify({
815+
S3Events: [{ test: 3 }]
816+
}))
806817
})
807818

808819
after(() => {
809820
fs.unlinkSync('only_EventSourceMappings.json')
810821
fs.unlinkSync('only_ScheduleEvents.json')
822+
fs.unlinkSync('only_S3Events.json')
811823
})
812824

813825
it('only EventSourceMappings', () => {
814826
program.eventSourceFile = 'only_EventSourceMappings.json'
815827
const expected = {
816828
EventSourceMappings: [{ test: 1 }],
817-
ScheduleEvents: []
829+
ScheduleEvents: [],
830+
S3Events: []
818831
}
819832
assert.deepEqual(lambda._eventSourceList(program), expected)
820833
})
@@ -823,7 +836,18 @@ describe('lib/main', function () {
823836
program.eventSourceFile = 'only_ScheduleEvents.json'
824837
const expected = {
825838
EventSourceMappings: [],
826-
ScheduleEvents: [{ test: 2 }]
839+
ScheduleEvents: [{ test: 2 }],
840+
S3Events: []
841+
}
842+
assert.deepEqual(lambda._eventSourceList(program), expected)
843+
})
844+
845+
it('only S3Events', () => {
846+
program.eventSourceFile = 'only_S3Events.json'
847+
const expected = {
848+
EventSourceMappings: [],
849+
ScheduleEvents: [],
850+
S3Events: [{ test: 3 }]
827851
}
828852
assert.deepEqual(lambda._eventSourceList(program), expected)
829853
})
@@ -845,6 +869,20 @@ describe('lib/main', function () {
845869
key1: 'value',
846870
key2: 'value'
847871
}
872+
}],
873+
S3Events: [{
874+
Bucket: 'BUCKET_NAME',
875+
Events: [
876+
's3:ObjectCreated:*'
877+
],
878+
Filter: {
879+
Key: {
880+
FilterRules: [{
881+
Name: 'prefix',
882+
Value: 'STRING_VALUE'
883+
}]
884+
}
885+
}
848886
}]
849887
}
850888
assert.deepEqual(lambda._eventSourceList(program), expected)
@@ -867,7 +905,8 @@ describe('lib/main', function () {
867905
program.eventSourceFile = fileName
868906
const expected = {
869907
EventSourceMappings: oldStyleValue,
870-
ScheduleEvents: []
908+
ScheduleEvents: [],
909+
S3Events: []
871910
}
872911
assert.deepEqual(lambda._eventSourceList(program), expected)
873912
})
@@ -1015,6 +1054,58 @@ describe('lib/main', function () {
10151054
})
10161055
})
10171056

1057+
describe('_updateS3Events', () => {
1058+
const S3Events = require(path.join('..', 'lib', 's3_events'))
1059+
const eventSourcesJsonValue = {
1060+
S3Events: [{
1061+
Bucket: 'node-lambda-test-bucket',
1062+
Events: ['s3:ObjectCreated:*'],
1063+
Filter: null
1064+
}]
1065+
}
1066+
1067+
let s3Events = null
1068+
1069+
before(() => {
1070+
fs.writeFileSync(
1071+
'event_sources.json',
1072+
JSON.stringify(eventSourcesJsonValue)
1073+
)
1074+
s3Events = new S3Events(aws)
1075+
})
1076+
1077+
after(() => fs.unlinkSync('event_sources.json'))
1078+
1079+
it('program.eventSourceFile is empty value', () => {
1080+
program.eventSourceFile = ''
1081+
const eventSourceList = lambda._eventSourceList(program)
1082+
return lambda._updateS3Events(
1083+
s3Events,
1084+
'',
1085+
eventSourceList.S3Events
1086+
).then(results => {
1087+
assert.deepEqual(results, [])
1088+
})
1089+
})
1090+
1091+
it('simple test with mock', () => {
1092+
program.eventSourceFile = 'event_sources.json'
1093+
const eventSourceList = lambda._eventSourceList(program)
1094+
const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function'
1095+
return lambda._updateS3Events(
1096+
s3Events,
1097+
functionArn,
1098+
eventSourceList.S3Events
1099+
).then(results => {
1100+
const expected = [Object.assign(
1101+
eventSourcesJsonValue.S3Events[0],
1102+
{ FunctionArn: functionArn }
1103+
)]
1104+
assert.deepEqual(results, expected)
1105+
})
1106+
})
1107+
})
1108+
10181109
describe('_uploadNew', () => {
10191110
it('simple test with mock', () => {
10201111
const params = lambda._params(program, null)

0 commit comments

Comments
 (0)