From de3c8b4422f439826738556b13671ed9a8821612 Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Sun, 9 Jun 2024 18:29:50 -0500 Subject: [PATCH] Bind groupName to SchedulerClient --- examples/schedule.js | 10 ++++++---- lib/clients/scheduler.doc.js | 1 + lib/clients/scheduler.js | 16 ++++++++++++---- lib/clients/scheduler.spec.js | 35 +++++++++++++++++++++++------------ 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/examples/schedule.js b/examples/schedule.js index a9d7293..def847d 100644 --- a/examples/schedule.js +++ b/examples/schedule.js @@ -4,13 +4,13 @@ export const createSchedule = ({ log }) => async (scheduleName, groupName, arn, roleArn) => { const client = new SchedulerClient({ + groupName, log }) return client.createSchedule(scheduleName, { scheduleExpression: 'rate(1 minute)', flexibleTimeWindow: { mode: 'OFF' }, input: { foo: 'bar' }, - groupName, target: { arn, roleArn, @@ -26,22 +26,23 @@ export const deleteSchedule = ({ log }) => async (scheduleName, groupName) => { const client = new SchedulerClient({ + groupName, log }) - return client.deleteSchedule(scheduleName, { groupName }) + return client.deleteSchedule(scheduleName) } export const updateSchedule = ({ log }) => async (scheduleName, groupName, arn, roleArn) => { const client = new SchedulerClient({ + groupName, log }) return client.updateSchedule(scheduleName, { scheduleExpression: 'rate(2 minutes)', flexibleTimeWindow: { mode: 'OFF' }, input: { foo: 'bar' }, - groupName, target: { arn, roleArn, @@ -57,7 +58,8 @@ export const getSchedule = ({ log }) => async (scheduleName, groupName) => { const client = new SchedulerClient({ + groupName, log }) - return client.getSchedule(scheduleName, { groupName }) + return client.getSchedule(scheduleName) } diff --git a/lib/clients/scheduler.doc.js b/lib/clients/scheduler.doc.js index c2cc820..e63aa53 100644 --- a/lib/clients/scheduler.doc.js +++ b/lib/clients/scheduler.doc.js @@ -3,6 +3,7 @@ * @class SchedulerClient * @see {@link https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-eventbridge/index.html|@aws-sdk/client-scheduler} * @param {Object} parameters + * @param {string} [parameters.groupName=default] Schedule group name. * @param {string} [parameters.name=scheduler] Client name. * @param {string} [parameters.reqId=] Request id. * @param {Object} [parameters.log=] Pino compatible logger. diff --git a/lib/clients/scheduler.js b/lib/clients/scheduler.js index 9ede284..e9dc7f6 100644 --- a/lib/clients/scheduler.js +++ b/lib/clients/scheduler.js @@ -23,20 +23,24 @@ import { keysToCamelCase, keysToPascalCase } from '../case.js' const createClient = createCache() export class SchedulerClient { + #groupName #client #reqId #log constructor({ + groupName = 'default', name = 'scheduler', reqId = uuidv4(), log = createLogger(), AwsSchedulerClient = AwsSdkSchedulerClient, params = {} }) { + this.#groupName = groupName this.#client = createClient(name, () => new AwsSchedulerClient(params)) this.#reqId = reqId this.#log = log.child({ + groupName, params, client: name, class: SchedulerClient.name, @@ -52,7 +56,7 @@ export class SchedulerClient { }) try { log.info('start') - const req = formatReq({ ...params, name: scheduleName }) + const req = this.#formatReq({ ...params, name: scheduleName }) const command = new GetScheduleCommand(req) const res = await this.#client.send(command) @@ -76,7 +80,7 @@ export class SchedulerClient { }) try { log.info('start') - const req = formatReq({ ...params, name: scheduleName }) + const req = this.#formatReq({ ...params, name: scheduleName }) const command = new CreateScheduleCommand(req) const res = await this.#client.send(command) @@ -100,7 +104,7 @@ export class SchedulerClient { }) try { log.info('start') - const req = formatReq({ ...params, name: scheduleName }) + const req = this.#formatReq({ ...params, name: scheduleName }) const command = new DeleteScheduleCommand(req) const res = await this.#client.send(command) @@ -125,7 +129,7 @@ export class SchedulerClient { try { log.info('start') - const req = formatReq({ ...params, name: scheduleName }) + const req = this.#formatReq({ ...params, name: scheduleName }) const command = new UpdateScheduleCommand(req) const res = await this.#client.send(command) @@ -140,6 +144,10 @@ export class SchedulerClient { throw err } } + + #formatReq = (input) => { + return formatReq({ ...input, GroupName: this.#groupName }) + } } const formatReq = pipe( diff --git a/lib/clients/scheduler.spec.js b/lib/clients/scheduler.spec.js index 95c838b..7c92422 100644 --- a/lib/clients/scheduler.spec.js +++ b/lib/clients/scheduler.spec.js @@ -23,6 +23,7 @@ test.beforeEach((t) => { t.context.createClient = (t, options) => { const client = new SchedulerClient({ name: uuidv4(), + groupName, AwsSchedulerClient: t.context.AwsSchedulerClient, reqId, log: createLogger({ t }), @@ -53,7 +54,7 @@ test('getSchedule: returns response', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new GetScheduleCommand({ Name: scheduleName }) + new GetScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenResolve(getScheduleResponse) @@ -70,12 +71,16 @@ test('getSchedule: passes params', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new GetScheduleCommand({ Name: scheduleName, GroupName: groupName }) + new GetScheduleCommand({ + Name: scheduleName, + GroupName: groupName, + Foo: 'bar' + }) ) ) ).thenResolve(getScheduleResponse) - const data = await client.getSchedule(scheduleName, { groupName }) + const data = await client.getSchedule(scheduleName, { foo: 'bar' }) t.deepEqual(data, getScheduleResponseFormatted) }) @@ -88,7 +93,7 @@ test('getSchedule: throws error from client', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new GetScheduleCommand({ Name: scheduleName }) + new GetScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenReject(err) @@ -103,7 +108,7 @@ test('deleteSchedule: returns response', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new DeleteScheduleCommand({ Name: scheduleName }) + new DeleteScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenResolve({}) @@ -120,12 +125,16 @@ test('deleteSchedule: passes params', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new DeleteScheduleCommand({ Name: scheduleName, GroupName: groupName }) + new DeleteScheduleCommand({ + Name: scheduleName, + GroupName: groupName, + Foo: 'bar' + }) ) ) ).thenResolve({}) - const data = await client.deleteSchedule(scheduleName, { groupName }) + const data = await client.deleteSchedule(scheduleName, { foo: 'bar' }) t.deepEqual(data, {}) }) @@ -138,7 +147,7 @@ test('deleteSchedule: throws error from client', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new DeleteScheduleCommand({ Name: scheduleName }) + new DeleteScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenReject(err) @@ -153,7 +162,7 @@ test('createSchedule: returns response', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new CreateScheduleCommand({ Name: scheduleName }) + new CreateScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenResolve(createScheduleResponse) @@ -174,6 +183,7 @@ test('createSchedule: passes params', async (t) => { Arn: 'mock-schedule-arn', FlexibleTimeWindow: { Mode: 'OFF' }, Name: scheduleName, + GroupName: groupName, ScheduleExpression: 'rate(1 minute)', Target: { Arn: 'mock-arn', @@ -214,7 +224,7 @@ test('createSchedule: throws error from client', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new CreateScheduleCommand({ Name: scheduleName }) + new CreateScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenReject(err) @@ -229,7 +239,7 @@ test('updateSchedule: returns response', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new UpdateScheduleCommand({ Name: scheduleName }) + new UpdateScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenResolve(updateScheduleResponse) @@ -250,6 +260,7 @@ test('updateSchedule: passes params', async (t) => { Arn: 'mock-schedule-arn', FlexibleTimeWindow: { Mode: 'OFF' }, Name: scheduleName, + GroupName: groupName, ScheduleExpression: 'rate(1 minute)', Target: { Arn: 'mock-arn', @@ -290,7 +301,7 @@ test('updateSchedule: throws error from client', async (t) => { td.when( AwsSchedulerClient.prototype.send( td.matchers.isAwsSdkCommand( - new UpdateScheduleCommand({ Name: scheduleName }) + new UpdateScheduleCommand({ Name: scheduleName, GroupName: groupName }) ) ) ).thenReject(err)