Skip to content

Commit

Permalink
feat:(cb2-11262): upgrade aws-sdk v2 to v3 (#74)
Browse files Browse the repository at this point in the history
* feat(cb2-11262): 1 test suite failing

* feat(cb2-11262): formatting

* feat(cb2-11262): more formatting

* feat(cb2-11262): unit tests passing and aws sdk 2 removed

* feat(cb2-11262): unit tests passing and aws sdk 2 removed

* feat(cb2-11262): fix failing test

* feat(cb2-11262): another lint fix

* feat(cb2-11262): test working

* feat(cb2-11262): refactor changes

* feat(cb2-11262): linting fix

* feat(cb2-11262): refactor

* feat(cb2-11262): remove an any

* feat(cb2-11262): space added
  • Loading branch information
owen-corrigan-bjss authored Apr 18, 2024
1 parent c2e51ae commit 3a029a9
Show file tree
Hide file tree
Showing 10 changed files with 5,739 additions and 1,999 deletions.
7,311 changes: 5,498 additions & 1,813 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.549.0",
"@aws-sdk/client-sqs": "^3.547.0",
"@aws-sdk/util-dynamodb": "^3.549.0",
"@smithy/smithy-client": "^2.5.0",
"aws-lambda": "^1.0.5",
"aws-sdk": "^2.1414.0",
"aws-xray-sdk": "^3.5.0",
"aws-sdk-client-mock": "^4.0.0",
"aws-xray-sdk": "^3.6.0",
"node-yaml": "^3.2.0",
"reflect-metadata": "^0.1.13"
},
Expand Down
13 changes: 6 additions & 7 deletions src/functions/certGenInit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Callback, Context, Handler } from "aws-lambda";
import { AWSError, SQS } from "aws-sdk";
import { ServiceException } from "@smithy/smithy-client";
import { SendMessageCommandOutput, SQSClient } from "@aws-sdk/client-sqs";
import { SQService } from "../services/SQService";
import { PromiseResult } from "aws-sdk/lib/request";
import { SendMessageResult } from "aws-sdk/clients/sqs";
import { StreamService } from "../services/StreamService";
import { Utils } from "../utils/Utils";

Expand All @@ -16,7 +15,7 @@ const certGenInit: Handler = async (
event: any,
context?: Context,
callback?: Callback
): Promise<void | Array<PromiseResult<SendMessageResult, AWSError>>> => {
): Promise<void | Array<SendMessageCommandOutput | any>> => {
if (!event) {
console.error("ERROR: event is not defined.");
return;
Expand All @@ -28,9 +27,9 @@ const certGenInit: Handler = async (
Utils.filterCertificateGenerationRecords(expandedRecords);

// Instantiate the Simple Queue Service
const sqService: SQService = new SQService(new SQS());
const sqService: SQService = new SQService(new SQSClient());
const sendMessagePromises: Array<
Promise<PromiseResult<SendMessageResult, AWSError>>
Promise<SendMessageCommandOutput | ServiceException>
> = [];

certGenFilteredRecords.forEach((record: any) => {
Expand All @@ -39,7 +38,7 @@ const certGenInit: Handler = async (
);
});

return Promise.all(sendMessagePromises).catch((error: AWSError) => {
return Promise.all(sendMessagePromises).catch((error) => {
console.error(error);
console.log("expandedRecords");
console.log(JSON.stringify(expandedRecords));
Expand Down
20 changes: 15 additions & 5 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { certGenInit } from "./functions/certGenInit";
import { config as AWSConfig } from "aws-sdk";
import {
PutSecretValueCommand,
SecretsManagerClient,
} from "@aws-sdk/client-secrets-manager";

const isOffline: boolean =
!process.env.BRANCH || process.env.BRANCH === "local";

if (isOffline) {
AWSConfig.credentials = {
accessKeyId: "offline",
secretAccessKey: "offline",
};
const SMC = new SecretsManagerClient({});

const command = new PutSecretValueCommand({
SecretId: "secretid1",
SecretString: JSON.stringify({
accessKeyId: "offline",
secretAccessKey: "offline",
}),
});

SMC.send(command);
}

export { certGenInit as handler };
76 changes: 43 additions & 33 deletions src/services/SQService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import SQS, {
GetQueueUrlResult,
MessageBodyAttributeMap,
ReceiveMessageResult,
SendMessageResult,
} from "aws-sdk/clients/sqs";
import { ServiceException } from "@smithy/smithy-client";

import {
GetQueueUrlCommand,
GetQueueUrlCommandOutput,
MessageAttributeValue,
ReceiveMessageCommand,
ReceiveMessageCommandOutput,
SQSClient,
SendMessageCommand,
SendMessageCommandInput,
SendMessageCommandOutput,
SetQueueAttributesCommand,
} from "@aws-sdk/client-sqs";

import { Service } from "../models/injector/ServiceDecorator";
import { Configuration } from "../utils/Configuration";
import { PromiseResult } from "aws-sdk/lib/request";
import { AWSError, config as AWSConfig } from "aws-sdk";

/* tslint:disable */
const AWSXRay = require("aws-xray-sdk");
/* tslint:enable */
Expand All @@ -17,29 +25,29 @@ const AWSXRay = require("aws-xray-sdk");
*/
@Service()
class SQService {
public readonly sqsClient: SQS;
public readonly sqsClient: SQSClient;
private readonly config: any;

/**
* Constructor for the ActivityService class
* @param sqsClient - The Simple Queue Service client
*/
constructor(sqsClient: SQS) {
constructor(sqsClient: SQSClient) {
const config: any = Configuration.getInstance().getConfig();
this.sqsClient = AWSXRay.captureAWSClient(sqsClient);

if (!config.sqs) {
throw new Error("SQS config is not defined in the config file.");
}

// Not defining BRANCH will default to local
const env: string =
!process.env.BRANCH || process.env.BRANCH === "local"
? "local"
: "remote";
this.config = config.sqs[env];

AWSConfig.sqs = this.config.params;
this.sqsClient = new SQSClient({ ...sqsClient, ...this.config });

if (!config.sqs) {
throw new Error("SQS config is not defined in the config file.");
}

// AWSConfig.sqs = this.config.params;
}

/**
Expand All @@ -59,12 +67,13 @@ class SQService {
private async sendMessage(
messageBody: string,
queueName: string,
messageAttributes?: MessageBodyAttributeMap
): Promise<PromiseResult<SendMessageResult, AWSError>> {
messageAttributes?: Record<string, MessageAttributeValue>
): Promise<SendMessageCommandOutput | ServiceException> {
// Get the queue URL for the provided queue name
const queueUrlResult: GetQueueUrlResult = await this.sqsClient
.getQueueUrl({ QueueName: queueName })
.promise();
const queueUrlResult: GetQueueUrlCommandOutput = await this.sqsClient.send(
new GetQueueUrlCommand({ QueueName: queueName })
);
// .promise();

const params = {
QueueUrl: queueUrlResult.QueueUrl,
Expand All @@ -76,26 +85,27 @@ class SQService {
}

// Send a message to the queue
return this.sqsClient
.sendMessage(params as SQS.Types.SendMessageRequest)
.promise();
return this.sqsClient.send(
new SendMessageCommand(params as SendMessageCommandInput)
);
}

/**
* Get the messages in the queue
*/
public async getMessages(): Promise<
PromiseResult<ReceiveMessageResult, AWSError>
ReceiveMessageCommandOutput | ServiceException
> {
// Get the queue URL for the provided queue name
const queueUrlResult: GetQueueUrlResult = await this.sqsClient
.getQueueUrl({ QueueName: this.config.queueName[0] })
.promise();

console.log("here in getMessages");
const queueUrlResult: GetQueueUrlCommandOutput = await this.sqsClient.send(
new GetQueueUrlCommand({ QueueName: this.config.queueName[0] })
);
console.log("after get queueurl");
// Get the messages from the queue
return this.sqsClient
.receiveMessage({ QueueUrl: queueUrlResult.QueueUrl! })
.promise();
return this.sqsClient.send(
new ReceiveMessageCommand({ QueueUrl: queueUrlResult.QueueUrl! })
);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/services/StreamService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// import { DynamoDB } from "aws-sdk";
import { DynamoDBRecord } from "aws-lambda";
import { DynamoDB } from "aws-sdk";
import { unmarshall } from "@aws-sdk/util-dynamodb";

/**
* Service class for interpreting and formatting
Expand Down Expand Up @@ -33,10 +34,10 @@ class StreamService {
(record.eventName === "MODIFY" &&
StreamService.isProcessModifyEventsEnabled())
);
}).map((record: DynamoDBRecord) => {
}).map((record: any) => {
// Convert to JS object
if (record.dynamodb && record.dynamodb.NewImage) {
return DynamoDB.Converter.unmarshall(record.dynamodb.NewImage);
return unmarshall(record.dynamodb.NewImage);
}
});

Expand Down
27 changes: 16 additions & 11 deletions tests/models/SQMockClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { AWSError } from "aws-sdk";
import SQS, {
import {
CreateQueueRequest,
DeleteQueueRequest,
} from "aws-sdk/clients/sqs";
GetQueueUrlRequest,
GetQueueUrlResult,
SendMessageRequest,
SendMessageResult,
ReceiveMessageRequest,
ReceiveMessageResult,
} from "@aws-sdk/client-sqs";

interface IQueue {
queueName: string;
queueName?: string;
queueURL: string;
queueMessages: string[];
}
Expand Down Expand Up @@ -45,8 +50,8 @@ class SQMockClient {
* @param callback - optional callback function
*/
public getQueueUrl(
params: SQS.Types.GetQueueUrlRequest,
callback?: (err: AWSError, data: SQS.Types.GetQueueUrlResult) => void
params: GetQueueUrlRequest,
callback?: (err: any, data: GetQueueUrlResult) => void
): any {
return {
promise: () => {
Expand All @@ -71,8 +76,8 @@ class SQMockClient {
* @param callback - optional callback function
*/
public sendMessage(
params: SQS.Types.SendMessageRequest,
callback?: (err: AWSError, data: SQS.Types.SendMessageResult) => void
params: SendMessageRequest,
callback?: (err: any, data: SendMessageResult) => void
): any {
return {
promise: () => {
Expand All @@ -81,7 +86,7 @@ class SQMockClient {
(queue) => queue.queueURL === params.QueueUrl
);

if (foundQueue) {
if (foundQueue && params.MessageBody) {
foundQueue.queueMessages.push(params.MessageBody);
resolve({
MessageId: "mock",
Expand All @@ -100,8 +105,8 @@ class SQMockClient {
* @param callback - optional callback function
*/
public receiveMessage(
params: SQS.Types.ReceiveMessageRequest,
callback?: (err: AWSError, data: SQS.Types.ReceiveMessageResult) => void
params: ReceiveMessageRequest,
callback?: (err: any, data: ReceiveMessageResult) => void
): any {
return {
promise: () => {
Expand Down
Loading

0 comments on commit 3a029a9

Please sign in to comment.