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

chore(cb2-11271): upgrade aws-sdk v2 to v3 #402

Merged
merged 26 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5038c93
chore(cb2-11271): initial pass client exception
owen-corrigan Apr 11, 2024
3fa08a5
chore(cb2-11271): fix int tests
owen-corrigan Apr 11, 2024
100180d
chore(cb2-11271): linting
owen-corrigan Apr 11, 2024
bc1ee52
chore(cb2-11271): fix failing test
owen-corrigan Apr 11, 2024
7f6060e
chore(cb2-11271): linting
owen-corrigan Apr 11, 2024
465f20a
chore(cb2-11271): parsing
owen-corrigan Apr 12, 2024
66846e6
chore(cb2-11271): parsing result
owen-corrigan Apr 12, 2024
a4ed9d4
chore(cb2-11271): lint fix
owen-corrigan Apr 12, 2024
2f575c3
chore(cb2-11271): parsing returned result
owen-corrigan Apr 12, 2024
7e5f21c
chore(cb2-11271): parsing attempt
owen-corrigan Apr 12, 2024
55ea583
chore(cb2-11271): console logging
owen-corrigan Apr 12, 2024
4b38232
chore(cb2-11271): linting
owen-corrigan Apr 12, 2024
a9cf91a
feat(cb2-11271): add logging
owen-corrigan Apr 12, 2024
ee28711
chore(cb2-11271): marshall options
owen-corrigan Apr 12, 2024
2e42992
chore(cb2-11271): linting
owen-corrigan Apr 12, 2024
f764206
feat(cb2-11271): add console logging
owen-corrigan Apr 15, 2024
086e822
feat(cb2-11271): another log
owen-corrigan Apr 15, 2024
90ec181
chore(cb2-11271): more logging
owen-corrigan Apr 15, 2024
1941ae9
chore(cb2-11271): change parsing
owen-corrigan Apr 15, 2024
8317e33
chore(cb2-11271): change lambda service return
owen-corrigan Apr 15, 2024
1aefb83
chore(cb2-11271): remove logging
owen-corrigan Apr 15, 2024
54d56c9
chore(cb2-11271): refactor translate config
owen-corrigan Apr 16, 2024
1eca1bc
chore(cb2-11271): down npm version
owen-corrigan Apr 16, 2024
7e3eb76
chore(cb2-11271): npm version
owen-corrigan Apr 16, 2024
75b0dd7
chore(cb2-11271): remove npm from dependencies
owen-corrigan Apr 16, 2024
0088826
chore(cb2-11271): remove npm packages
owen-corrigan Apr 16, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ npm run test-i # for integration tests
```

install and build if not done before

- install deps; `npm install`
- install local dynamo-db: `npm run tools-setup`
- build: project: `npm run build`
Expand Down
2,934 changes: 1,830 additions & 1,104 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@types/supertest": "^2.0.8",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"aws-sdk-client-mock": "^4.0.0",
"commitlint-plugin-function-rules": "^1.7.1",
"eslint": "^8.28.0",
"eslint-config-airbnb-base": "^15.0.0",
Expand All @@ -79,9 +80,14 @@
"typescript": "^3.8.3"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.552.0",
"@aws-sdk/client-lambda": "^3.552.0",
"@aws-sdk/lib-dynamodb": "^3.552.0",
"@aws-sdk/smithy-client": "^3.374.0",
"@dvsa/cvs-type-definitions": "6.0.0",
"@smithy/smithy-client": "^2.5.1",
"@smithy/util-utf8": "^2.3.0",
"aws-lambda": "^1.0.7",
"aws-sdk": "^2.1354.0",
"aws-xray-sdk": "^3.3.4",
"joi": "^17.12.1",
"js-yaml": "^3.14.1",
Expand Down
12 changes: 7 additions & 5 deletions scripts/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const SERVER_OK = `Server ready: http://localhost:3006 🚀`;
// we force throwing an error so we always start from a clean slate if java.io.IOException: Failed to bind to 0.0.0.0/0.0.0.0:8006
const DYNAMO_LOCAL_ERROR_THREAD = `Exception in thread "main"`;

const setupServer = (process: any) => new Promise((resolve, reject) => {
const setupServer = (process: any) =>
new Promise((resolve, reject) => {
process.stdout.setEncoding('utf-8').on('data', (stream: any) => {
console.log(`stdout: ${stream}`);
if (stream.includes(SERVER_OK)) {
Expand All @@ -19,15 +20,16 @@ const setupServer = (process: any) => new Promise((resolve, reject) => {
console.log(`stderr: ${stream}`);
if (stream.includes(DYNAMO_LOCAL_ERROR_THREAD)) {
throw new Error('Internal Java process crashed');
}
else if (stream.includes(SERVER_OK)) {
} else if (stream.includes(SERVER_OK)) {
resolve(process);
}
});

process.on('exit', (code: any, signal: any) => {
if (code !== 137) {
console.info(`process terminated with code: ${code} and signal: ${signal}`);
console.info(
`process terminated with code: ${code} and signal: ${signal}`,
);
}
});
});
Expand All @@ -36,7 +38,7 @@ const server = exec('npm run start &', (error) => {
if (error) {
console.error(`error starting server: ${error}`);
}
});
});

module.exports = async () => {
console.log(`\nSetting up Integration tests...\n\n`);
Expand Down
74 changes: 44 additions & 30 deletions src/models/TestResultsDAO.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
import { PromiseResult } from 'aws-sdk/lib/request';
import { DocumentClient } from 'aws-sdk/lib/dynamodb/document_client';
import { AWSError } from 'aws-sdk/lib/error';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import {
BatchWriteCommand,
BatchWriteCommandOutput,
DynamoDBDocumentClient,
PutCommand,
QueryCommand,
QueryCommandOutput,
TransactWriteCommand,
TransactWriteCommandInput,
TransactWriteCommandOutput,
} from '@aws-sdk/lib-dynamodb';
import { ServiceException } from '@smithy/smithy-client';
import { fromUtf8 } from '@smithy/util-utf8';
import * as models from '.';
import { Configuration } from '../utils/Configuration';
import { LambdaService } from '../services/LambdaService';

/* tslint:disable */
let AWS: { DynamoDB: { DocumentClient: new (arg0: any) => DocumentClient } };
if (process.env._X_AMZN_TRACE_ID) {
AWS = require('aws-xray-sdk').captureAWS(require('aws-sdk'));
} else {
console.log('Serverless Offline detected; skipping AWS X-Ray setup');
AWS = require('aws-sdk');
}
/* tslint:enable */
import { Configuration } from '../utils/Configuration';

export class TestResultsDAO {
private readonly tableName: string;

private static docClient: DocumentClient;
private static docClient: DynamoDBDocumentClient;

private static lambdaInvokeEndpoints: any;

constructor() {
const config: models.IDBConfig =
Configuration.getInstance().getDynamoDBConfig();
const config = Configuration.getInstance().getDynamoDBConfig();
const translateConfig = {
marshallOptions: { removeUndefinedValues: true },
};

this.tableName = config.table;
if (!TestResultsDAO.docClient) {
TestResultsDAO.docClient = new AWS.DynamoDB.DocumentClient(config.params);
const client = new DynamoDBClient(config.params);
if (process.env._X_AMZN_TRACE_ID) {
TestResultsDAO.docClient = require('aws-xray-sdk').captureAWSv3Client(
DynamoDBDocumentClient.from(client, translateConfig),
);
} else {
console.log('Serverless Offline detected; skipping AWS X-Ray setup');
TestResultsDAO.docClient = DynamoDBDocumentClient.from(
client,
translateConfig,
);
}
}
if (!TestResultsDAO.lambdaInvokeEndpoints) {
TestResultsDAO.lambdaInvokeEndpoints =
Expand Down Expand Up @@ -100,12 +114,12 @@ export class TestResultsDAO {
':testResultIdVal': payload.testResultId,
},
};
return TestResultsDAO.docClient.put(query).promise();
return TestResultsDAO.docClient.send(new PutCommand(query));
}

public createMultiple(
testResultsItems: models.ITestResult[],
): Promise<PromiseResult<DocumentClient.BatchWriteItemOutput, AWSError>> {
): Promise<BatchWriteCommandOutput | ServiceException> {
const params = this.generateBatchWritePartialParams();

testResultsItems.forEach((testResultItem: models.ITestResult) => {
Expand All @@ -116,12 +130,12 @@ export class TestResultsDAO {
});
});

return TestResultsDAO.docClient.batchWrite(params).promise();
return TestResultsDAO.docClient.send(new BatchWriteCommand(params));
}

public deleteMultiple(
systemNumberIdPairsToBeDeleted: any[],
): Promise<PromiseResult<DocumentClient.BatchWriteItemOutput, AWSError>> {
): Promise<BatchWriteCommandOutput | ServiceException> {
const params = this.generateBatchWritePartialParams();

systemNumberIdPairsToBeDeleted.forEach(
Expand All @@ -143,7 +157,7 @@ export class TestResultsDAO {
},
);

return TestResultsDAO.docClient.batchWrite(params).promise();
return TestResultsDAO.docClient.send(new BatchWriteCommand(params));
}

public generateBatchWritePartialParams(): any {
Expand Down Expand Up @@ -193,7 +207,7 @@ export class TestResultsDAO {
TestResultsDAO.lambdaInvokeEndpoints.functions.getTestTypesById.name;
try {
console.log('queryString for get Test: ', event);
const lambdaResult = LambdaService.invoke(lambdaName, event);
const lambdaResult = await LambdaService.invoke(lambdaName, event);

return lambdaResult;
} catch (error) {
Expand Down Expand Up @@ -221,7 +235,7 @@ export class TestResultsDAO {
TestResultsDAO.lambdaInvokeEndpoints.functions.getTestNumber.name;
try {
const lambdaResult = LambdaService.invoke(lambdaName, event);
return lambdaResult;
return await lambdaResult;
} catch (error) {
console.error(
`error during lambda invocation: ${lambdaName} and ${event}, \nwith error:${error}`,
Expand All @@ -240,8 +254,8 @@ export class TestResultsDAO {

public updateTestResult(
updatedTestResult: models.ITestResult,
): Promise<PromiseResult<DocumentClient.TransactWriteItemsOutput, AWSError>> {
const query: DocumentClient.TransactWriteItemsInput = {
): Promise<TransactWriteCommandOutput | ServiceException> {
const query: TransactWriteCommandInput = {
TransactItems: [
{
Put: {
Expand All @@ -258,15 +272,15 @@ export class TestResultsDAO {
},
],
};
return TestResultsDAO.docClient.transactWrite(query).promise();
return TestResultsDAO.docClient.send(new TransactWriteCommand(query));
}

private async queryAllData(
params: any,
allData: models.ITestResult[] = [],
): Promise<models.ITestResult[]> {
const data: PromiseResult<DocumentClient.QueryOutput, AWSError> =
await TestResultsDAO.docClient.query(params).promise();
const data: QueryCommandOutput | ServiceException =
await TestResultsDAO.docClient.send(new QueryCommand(params));

if (data.Items && data.Items.length > 0) {
allData = [...allData, ...(data.Items as models.ITestResult[])];
Expand Down
46 changes: 21 additions & 25 deletions src/services/LambdaService.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
/* tslint:disable */
/* tslint:enable */
import { InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda';
import { toUint8Array } from '@smithy/util-utf8';
import { Configuration } from '../utils/Configuration';
import { validateInvocationResponse } from '../utils/validateInvocationResponse';

let AWS: any;
if (process.env._X_AMZN_TRACE_ID) {
AWS = require('aws-xray-sdk').captureAWS(require('aws-sdk'));
} else {
console.log('Serverless Offline detected; skipping AWS X-Ray setup');
AWS = require('aws-sdk');
}

const lambdaInvokeEndpoints = Configuration.getInstance().getEndpoints();

/**
* Helper service for interactions with other lambdas
*/
export class LambdaService {
public static invoke(lambdaName: any, lambdaEvent: any) {
const lambda = new AWS.Lambda(lambdaInvokeEndpoints.params);
public static async invoke(lambdaName: any, lambdaEvent: any) {
const lambdaInvokeEndpoints = Configuration.getInstance().getEndpoints();
let AWS: any;
if (process.env._X_AMZN_TRACE_ID) {
AWS = require('aws-xray-sdk').captureAWSv3Client(
new LambdaClient(lambdaInvokeEndpoints.params),
);
} else {
console.log('Serverless Offline detected; skipping AWS X-Ray setup');
AWS = new LambdaClient(lambdaInvokeEndpoints.params);
}

return lambda
.invoke({
const returned = await AWS.send(
new InvokeCommand({
FunctionName: lambdaName,
InvocationType: 'RequestResponse',
Payload: JSON.stringify(lambdaEvent),
})
.promise()
.then((data: any) => {
const payload = validateInvocationResponse(data);
const body = JSON.parse(payload.body);
return body;
});
Payload: toUint8Array(JSON.stringify(lambdaEvent)),
}),
);
const payload = validateInvocationResponse(returned);
const body = JSON.parse(payload.body);
return body;
}
}
5 changes: 2 additions & 3 deletions src/utils/validateInvocationResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HTTPError } from '../models/HTTPError';
*/
export const validateInvocationResponse = (response: any) => {
if (
(!response.Payload || response.Payload === '') &&
(!response.Payload || Buffer.from(response.Payload).toString() === '') &&
response.StatusCode &&
response.StatusCode < 400
) {
Expand All @@ -18,9 +18,8 @@ export const validateInvocationResponse = (response: any) => {
let payload: any;

try {
payload = JSON.parse(response.Payload);
payload = JSON.parse(Buffer.from(response.Payload).toString());
} catch (error) {
console.log('validateInvocationResponse response parse error', response);
throw new HTTPError(
500,
`Lambda invocation returned bad data: ${response.Payload}`,
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/test-results-post.json
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@
"reasonForCancellation": null,
"make": "test make",
"model": "test model",
"bodyType": { "code": "test code", "description": "test description" }
"bodyType": { "code": "test code", "description": "test description" }
},
{
"vin": "1B7GG36N12S678410",
Expand Down
Loading
Loading