Skip to content
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
2 changes: 1 addition & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ they may not work as expected in the Lambda environment.
const lambdaClient = new LambdaClient(config)
// Migrating to v3.
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
const s3Events = new S3Events(aws.sdk, region)
const s3Events = new S3Events(config)
const cloudWatchLogs = new CloudWatchLogs(config)

const existsFunction = await (async () => {
Expand Down
43 changes: 16 additions & 27 deletions lib/s3_events.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
'use strict'

const { S3Client, PutBucketNotificationConfigurationCommand } = require('@aws-sdk/client-s3')
const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')

/**
* Do not create S3 bucket.
* Put the Notification Configuration in the existing Bucket.
*/
class S3Events {
constructor (aws, region) {
// Authenticated `aws` object in `lib/main.js`
this.lambda = new aws.Lambda({
region,
apiVersion: '2015-03-31'
})
this.s3 = new aws.S3({
region,
apiVersion: '2006-03-01'
})
constructor (config) {
this.s3Client = new S3Client(config)
this.lambdaClient = new LambdaClient(config)
}

_functionName (params) {
Expand All @@ -36,17 +32,14 @@ class S3Events {
}

_addPermission (params) {
return new Promise((resolve, reject) => {
const _params = this._addPermissionParams(params)
this.lambda.addPermission(_params, (err, data) => {
if (err) {
if (err.code !== 'ResourceConflictException') reject(err)
// If it exists it will result in an error but there is no problem.
resolve('Permission already set')
}
resolve(data)
})
})
const _params = this._addPermissionParams(params)
try {
return this.lambdaClient.send(new AddPermissionCommand(_params))
} catch (err) {
if (err.code !== 'ResourceConflictException') throw err
// If it exists it will result in an error but there is no problem.
return 'Permission already set'
}
}

_lambdaFunctionConfiguration (params) {
Expand Down Expand Up @@ -86,12 +79,8 @@ class S3Events {
}

_putBucketNotificationConfiguration (putBucketNotificationConfigurationParams) {
return new Promise((resolve, reject) => {
this.s3.putBucketNotificationConfiguration(putBucketNotificationConfigurationParams, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
return this.s3Client.send(
new PutBucketNotificationConfigurationCommand(putBucketNotificationConfigurationParams))
}

add (paramsList) {
Expand Down
7 changes: 4 additions & 3 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const { mockClient } = require('aws-sdk-client-mock')

const {
LambdaClient,
AddPermissionCommand,
CreateEventSourceMappingCommand,
CreateFunctionCommand,
DeleteEventSourceMappingCommand,
Expand All @@ -45,6 +46,7 @@ const mockCloudWatchLogsClient = mockClient(CloudWatchLogsClient)
const {
S3Client,
CreateBucketCommand,
PutBucketNotificationConfigurationCommand,
PutObjectCommand
} = require('@aws-sdk/client-s3')
const mockS3Client = mockClient(S3Client)
Expand Down Expand Up @@ -142,9 +144,6 @@ const _mockSetting = () => {
awsMock.mock('CloudWatchEvents', 'putTargets', (params, callback) => {
callback(null, {})
})
awsMock.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
callback(null, {})
})

Object.keys(lambdaMockSettings).forEach((method) => {
awsMock.mock('Lambda', method, (params, callback) => {
Expand Down Expand Up @@ -181,6 +180,7 @@ describe('lib/main', function () {

// for sdk v3
mockLambdaClient.reset()
mockLambdaClient.on(AddPermissionCommand).resolves(lambdaMockSettings.addPermission)
mockLambdaClient.on(CreateEventSourceMappingCommand).resolves(lambdaMockSettings.createEventSourceMapping)
mockLambdaClient.on(CreateFunctionCommand).resolves(lambdaMockSettings.createFunction)
mockLambdaClient.on(DeleteEventSourceMappingCommand).resolves(lambdaMockSettings.deleteEventSourceMapping)
Expand All @@ -199,6 +199,7 @@ describe('lib/main', function () {

mockS3Client.reset()
mockS3Client.on(CreateBucketCommand).resolves({})
mockS3Client.on(PutBucketNotificationConfigurationCommand).resolves({})
mockS3Client.on(PutObjectCommand).resolves({})
})
after(() => {
Expand Down
27 changes: 11 additions & 16 deletions test/s3_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ let assert
import('chai').then(chai => {
assert = chai.assert
})
const path = require('path')
const aws = require('aws-sdk-mock')
aws.setSDK(path.resolve('node_modules/aws-sdk'))
const { S3Client, PutBucketNotificationConfigurationCommand } = require('@aws-sdk/client-s3')
const { LambdaClient, AddPermissionCommand } = require('@aws-sdk/client-lambda')
const { mockClient } = require('aws-sdk-client-mock')
const mockS3Client = mockClient(S3Client)
const mockLambdaClient = mockClient(LambdaClient)
const S3Events = require('../lib/s3_events')

const params = {
Expand All @@ -33,22 +35,15 @@ const mockResponse = {

let s3Events = null

/* global before, after, describe, it */
/* global before, describe, it */
describe('lib/s3_events', () => {
before(() => {
aws.mock('Lambda', 'addPermission', (params, callback) => {
callback(null, mockResponse.addPermission)
})
aws.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
callback(null, mockResponse.putBucketNotificationConfiguration)
})

s3Events = new S3Events(require('aws-sdk'))
})
mockS3Client.reset()
mockS3Client.on(PutBucketNotificationConfigurationCommand).resolves(mockResponse.putBucketNotificationConfiguration)
mockLambdaClient.reset()
mockLambdaClient.on(AddPermissionCommand).resolves(mockResponse.addPermission)

after(() => {
aws.restore('Lambda')
aws.restore('S3')
s3Events = new S3Events({ region: 'us-west-1' })
})

describe('_functionName', () => {
Expand Down
Loading