From 8ae0f7d9adf483fc961fb609343a6d076d0cd107 Mon Sep 17 00:00:00 2001 From: abetomo Date: Sun, 15 Dec 2024 10:52:08 +0900 Subject: [PATCH] sdk-v3: Use sdk-v3 with _updateEventSources() GH-641 --- lib/main.js | 33 +++++++++++---------------------- test/main.js | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/lib/main.js b/lib/main.js index 8752968..adc9246 100644 --- a/lib/main.js +++ b/lib/main.js @@ -29,9 +29,12 @@ const { createNamespace } = require('continuation-local-storage') */ const { LambdaClient, + CreateEventSourceMappingCommand, CreateFunctionCommand, - ListEventSourceMappingsCommand, + DeleteEventSourceMappingCommand, GetFunctionCommand, + ListEventSourceMappingsCommand, + UpdateEventSourceMappingCommand, UpdateFunctionCodeCommand, UpdateFunctionConfigurationCommand } = require('@aws-sdk/client-lambda') @@ -840,30 +843,16 @@ they may not work as expected in the Lambda environment. switch (updateEventSource.type) { case 'create': delete updateEventSource.type - return new Promise((resolve, reject) => { - lambda.createEventSourceMapping(updateEventSource, (err, data) => { - if (err) return reject(err) - resolve(data) - }) - }) + return lambda.send(new CreateEventSourceMappingCommand(updateEventSource)) case 'update': delete updateEventSource.type - return new Promise((resolve, reject) => { - lambda.updateEventSourceMapping(updateEventSource, (err, data) => { - if (err) return reject(err) - resolve(data) - }) - }) + return lambda.send(new UpdateEventSourceMappingCommand(updateEventSource)) case 'delete': delete updateEventSource.type - return new Promise((resolve, reject) => { - lambda.deleteEventSourceMapping(updateEventSource, (err, data) => { - if (err) return reject(err) - resolve(data) - }) - }) + return lambda.send(new DeleteEventSourceMappingCommand(updateEventSource)) + default: + return Promise.resolve() } - return Promise.resolve() })) } @@ -1033,7 +1022,7 @@ they may not work as expected in the Lambda environment. ]) }), this._updateEventSources( - lambda, + lambdaClient, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings @@ -1056,7 +1045,7 @@ they may not work as expected in the Lambda environment. return Promise.all([ this._updateEventSources( - lambda, + lambdaClient, params.FunctionName, [], eventSourceList.EventSourceMappings diff --git a/test/main.js b/test/main.js index 4f60a85..ae29500 100644 --- a/test/main.js +++ b/test/main.js @@ -19,9 +19,12 @@ awsMock.setSDK(path.resolve('node_modules/aws-sdk')) const { mockClient } = require('aws-sdk-client-mock') const { LambdaClient, + CreateEventSourceMappingCommand, CreateFunctionCommand, - ListEventSourceMappingsCommand, + DeleteEventSourceMappingCommand, GetFunctionCommand, + ListEventSourceMappingsCommand, + UpdateEventSourceMappingCommand, UpdateFunctionCodeCommand, UpdateFunctionConfigurationCommand } = require('@aws-sdk/client-lambda') @@ -172,11 +175,14 @@ describe('lib/main', function () { // for sdk v3 mockLambdaClient.reset() + mockLambdaClient.on(CreateEventSourceMappingCommand).resolves(lambdaMockSettings.createEventSourceMapping) mockLambdaClient.on(CreateFunctionCommand).resolves(lambdaMockSettings.createFunction) + mockLambdaClient.on(DeleteEventSourceMappingCommand).resolves(lambdaMockSettings.deleteEventSourceMapping) mockLambdaClient.on(GetFunctionCommand).resolves(lambdaMockSettings.getFunction) + mockLambdaClient.on(ListEventSourceMappingsCommand).resolves(lambdaMockSettings.listEventSourceMappings) + mockLambdaClient.on(UpdateEventSourceMappingCommand).resolves(lambdaMockSettings.updateEventSourceMapping) mockLambdaClient.on(UpdateFunctionCodeCommand).resolves(lambdaMockSettings.updateFunctionCode) mockLambdaClient.on(UpdateFunctionConfigurationCommand).resolves(lambdaMockSettings.updateFunctionConfiguration) - mockLambdaClient.on(ListEventSourceMappingsCommand).resolves(lambdaMockSettings.listEventSourceMappings) }) after(() => { _awsRestore() @@ -1424,7 +1430,7 @@ describe('lib/main', function () { program.eventSourceFile = '' const eventSourceList = lambda._eventSourceList(program) return lambda._updateEventSources( - awsLambda, + lambdaClient, '', [], eventSourceList.EventSourceMappings @@ -1437,7 +1443,7 @@ describe('lib/main', function () { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) return lambda._updateEventSources( - awsLambda, + lambdaClient, 'functionName', [], eventSourceList.EventSourceMappings @@ -1448,7 +1454,7 @@ describe('lib/main', function () { it('simple test with mock (In case of deletion)', () => { return lambda._updateEventSources( - awsLambda, + lambdaClient, 'functionName', lambdaMockSettings.listEventSourceMappings.EventSourceMappings, {} @@ -1461,7 +1467,7 @@ describe('lib/main', function () { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) return lambda._updateEventSources( - awsLambda, + lambdaClient, 'functionName', lambdaMockSettings.listEventSourceMappings.EventSourceMappings, eventSourceList.EventSourceMappings