Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updateS3Events to main #394

Merged
merged 5 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/event_sources.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,19 @@
"key2": "value"
}
}
]
],
"S3Events": [{
"Bucket": "BUCKET_NAME",
"Events": [
"s3:ObjectCreated:*"
],
"Filter": {
"Key": {
"FilterRules": [{
"Name": "prefix",
"Value": "STRING_VALUE"
}]
}
}
}]
}
78 changes: 61 additions & 17 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const archiver = require('archiver')
const dotenv = require('dotenv')
const proxy = require('proxy-agent')
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
const S3Events = require(path.join(__dirname, 's3_events'))
const CloudWatchLogs = require(path.join(__dirname, 'cloudwatch_logs'))

const maxBufferSize = 50 * 1024 * 1024
Expand Down Expand Up @@ -226,7 +227,8 @@ so you can easily test run multiple events.
if (!program.eventSourceFile) {
return {
EventSourceMappings: null,
ScheduleEvents: null
ScheduleEvents: null,
S3Events: null
}
}
const list = (() => {
Expand All @@ -241,7 +243,8 @@ so you can easily test run multiple events.
// backward-compatible
return {
EventSourceMappings: list,
ScheduleEvents: []
ScheduleEvents: [],
S3Events: []
}
}
if (!list.EventSourceMappings) {
Expand All @@ -250,6 +253,9 @@ so you can easily test run multiple events.
if (!list.ScheduleEvents) {
list.ScheduleEvents = []
}
if (!list.S3Events) {
list.S3Events = []
}
return list
}

Expand Down Expand Up @@ -701,6 +707,23 @@ so you can easily test run multiple events.
})
}

_updateS3Events (s3Events, functionArn, s3EventsList) {
if (s3EventsList == null) return Promise.resolve([])

const paramsList = s3EventsList.map(s3event =>
Object.assign(s3event, { FunctionArn: functionArn }))

// series
return paramsList.map(params => {
return s3Events.add(params)
}).reduce((a, b) => {
return a.then(b)
}, Promise.resolve()).then(() => {
// Since it is similar to _updateScheduleEvents, it returns meaningful values
return paramsList
})
}

_setLogsRetentionPolicy (cloudWatchLogs, program, functionName) {
const days = parseInt(program.retentionInDays)
if (!Number.isInteger(days)) return Promise.resolve({})
Expand Down Expand Up @@ -781,6 +804,7 @@ so you can easily test run multiple events.

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

// Checking function
Expand All @@ -795,11 +819,20 @@ so you can easily test run multiple events.
this._uploadExisting(lambda, params).then((results) => {
console.log('=> Zip file(s) done uploading. Results follow: ')
console.log(results)
return this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
)
return results
}).then(results => {
return Promise.all([
this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
),
this._updateS3Events(
s3Events,
results.FunctionArn,
eventSourceList.S3Events
)
])
}),
this._updateEventSources(
lambda,
Expand Down Expand Up @@ -836,6 +869,11 @@ so you can easily test run multiple events.
results.FunctionArn,
eventSourceList.ScheduleEvents
),
this._updateS3Events(
s3Events,
results.FunctionArn,
eventSourceList.S3Events
),
this._setLogsRetentionPolicy(
cloudWatchLogs,
program,
Expand All @@ -851,6 +889,20 @@ so you can easily test run multiple events.
})
}

_printDeployResults (results, isFirst) {
if (!Array.isArray(results)) {
if (results == null) return
console.log(results)
return
}
if (results.length === 0) return

if (isFirst === true) console.log('=> All tasks done. Results follow:')
results.forEach(result => {
this._printDeployResults(result)
})
}

deploy (program) {
const regions = program.region.split(',')
return this._archive(program).then((buffer) => {
Expand All @@ -859,16 +911,8 @@ so you can easily test run multiple events.

return Promise.all(regions.map((region) => {
return this._deployToRegion(program, params, region)
})).then((results) => {
const resultsIsEmpty = results.filter((result) => {
return result.filter((res) => {
return res.length > 0
}).length > 0
}).length === 0
if (!resultsIsEmpty) {
console.log('=> All tasks done. Results follow: ')
console.log(JSON.stringify(results, null, ' '))
}
})).then(results => {
this._printDeployResults(results, true)
})
}).catch((err) => {
process.exitCode = 1
Expand Down
99 changes: 95 additions & 4 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ const _mockSetting = () => {
awsMock.mock('CloudWatchLogs', 'putRetentionPolicy', (params, callback) => {
callback(null, {})
})
awsMock.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
callback(null, {})
})

Object.keys(lambdaMockSettings).forEach((method) => {
awsMock.mock('Lambda', method, (params, callback) => {
Expand All @@ -110,6 +113,7 @@ const _mockSetting = () => {
const _awsRestore = () => {
awsMock.restore('CloudWatchEvents')
awsMock.restore('CloudWatchLogs')
awsMock.restore('S3')
awsMock.restore('Lambda')
}

Expand Down Expand Up @@ -781,7 +785,11 @@ describe('lib/main', function () {
program.eventSourceFile = ''
assert.deepEqual(
lambda._eventSourceList(program),
{ EventSourceMappings: null, ScheduleEvents: null }
{
EventSourceMappings: null,
ScheduleEvents: null,
S3Events: null
}
)
})

Expand All @@ -803,18 +811,23 @@ describe('lib/main', function () {
fs.writeFileSync('only_ScheduleEvents.json', JSON.stringify({
ScheduleEvents: [{ test: 2 }]
}))
fs.writeFileSync('only_S3Events.json', JSON.stringify({
S3Events: [{ test: 3 }]
}))
})

after(() => {
fs.unlinkSync('only_EventSourceMappings.json')
fs.unlinkSync('only_ScheduleEvents.json')
fs.unlinkSync('only_S3Events.json')
})

it('only EventSourceMappings', () => {
program.eventSourceFile = 'only_EventSourceMappings.json'
const expected = {
EventSourceMappings: [{ test: 1 }],
ScheduleEvents: []
ScheduleEvents: [],
S3Events: []
}
assert.deepEqual(lambda._eventSourceList(program), expected)
})
Expand All @@ -823,7 +836,18 @@ describe('lib/main', function () {
program.eventSourceFile = 'only_ScheduleEvents.json'
const expected = {
EventSourceMappings: [],
ScheduleEvents: [{ test: 2 }]
ScheduleEvents: [{ test: 2 }],
S3Events: []
}
assert.deepEqual(lambda._eventSourceList(program), expected)
})

it('only S3Events', () => {
program.eventSourceFile = 'only_S3Events.json'
const expected = {
EventSourceMappings: [],
ScheduleEvents: [],
S3Events: [{ test: 3 }]
}
assert.deepEqual(lambda._eventSourceList(program), expected)
})
Expand All @@ -845,6 +869,20 @@ describe('lib/main', function () {
key1: 'value',
key2: 'value'
}
}],
S3Events: [{
Bucket: 'BUCKET_NAME',
Events: [
's3:ObjectCreated:*'
],
Filter: {
Key: {
FilterRules: [{
Name: 'prefix',
Value: 'STRING_VALUE'
}]
}
}
}]
}
assert.deepEqual(lambda._eventSourceList(program), expected)
Expand All @@ -867,7 +905,8 @@ describe('lib/main', function () {
program.eventSourceFile = fileName
const expected = {
EventSourceMappings: oldStyleValue,
ScheduleEvents: []
ScheduleEvents: [],
S3Events: []
}
assert.deepEqual(lambda._eventSourceList(program), expected)
})
Expand Down Expand Up @@ -1015,6 +1054,58 @@ describe('lib/main', function () {
})
})

describe('_updateS3Events', () => {
const S3Events = require(path.join('..', 'lib', 's3_events'))
const eventSourcesJsonValue = {
S3Events: [{
Bucket: 'node-lambda-test-bucket',
Events: ['s3:ObjectCreated:*'],
Filter: null
}]
}

let s3Events = null

before(() => {
fs.writeFileSync(
'event_sources.json',
JSON.stringify(eventSourcesJsonValue)
)
s3Events = new S3Events(aws)
})

after(() => fs.unlinkSync('event_sources.json'))

it('program.eventSourceFile is empty value', () => {
program.eventSourceFile = ''
const eventSourceList = lambda._eventSourceList(program)
return lambda._updateS3Events(
s3Events,
'',
eventSourceList.S3Events
).then(results => {
assert.deepEqual(results, [])
})
})

it('simple test with mock', () => {
program.eventSourceFile = 'event_sources.json'
const eventSourceList = lambda._eventSourceList(program)
const functionArn = 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function'
return lambda._updateS3Events(
s3Events,
functionArn,
eventSourceList.S3Events
).then(results => {
const expected = [Object.assign(
eventSourcesJsonValue.S3Events[0],
{ FunctionArn: functionArn }
)]
assert.deepEqual(results, expected)
})
})
})

describe('_uploadNew', () => {
it('simple test with mock', () => {
const params = lambda._params(program, null)
Expand Down