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

trying to work through eco pr #982

Closed
wants to merge 6 commits into from
Closed
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
19 changes: 10 additions & 9 deletions lib/lambda/deleteIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ export const handler: Handler = async (event, __, callback) => {
};
let errorResponse = null;
try {
if (!event.osDomain) throw "process.env.osDomain cannot be undefined";
const { osDomain, indexNamespace = "" } = event;
if (!osDomain) throw "osDomain cannot be undefined";

const indices: Index[] = [
`${event.indexNamespace}main`,
`${event.indexNamespace}changelog`,
`${event.indexNamespace}insights`,
`${event.indexNamespace}types`,
`${event.indexNamespace}subtypes`,
`${event.indexNamespace}legacyinsights`,
`${event.indexNamespace}cpocs`,
`${indexNamespace}main`,
`${indexNamespace}changelog`,
`${indexNamespace}insights`,
`${indexNamespace}types`,
`${indexNamespace}subtypes`,
`${indexNamespace}legacyinsights`,
`${indexNamespace}cpocs`,
];
for (const index of indices) {
await os.deleteIndex(event.osDomain, index);
await os.deleteIndex(osDomain, index);
}
} catch (error: any) {
response.statusCode = 500;
Expand Down
2 changes: 1 addition & 1 deletion lib/lambda/getAttachmentUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("Lambda Handler", () => {

expect(response).toHaveBeenCalledWith({
statusCode: 500,
body: { message: "ERROR: osDomain env variable is required" },
body: { message: "ERROR: process.env.osDomain must be defined" },
});
});

Expand Down
21 changes: 7 additions & 14 deletions lib/lambda/getAttachmentUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@ import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

import { getStateFilter } from "../libs/api/auth/user";
import { getPackage, getPackageChangelog } from "../libs/api/package";
import { getDomain } from "libs/utils";

// Handler function to get Seatool data
export const handler = async (event: APIGatewayEvent) => {
if (!process.env.osDomain) {
try {
getDomain();
} catch (error) {
return response({
statusCode: 500,
body: { message: "ERROR: osDomain env variable is required" },
body: { message: `ERROR: ${error?.message || error}` },
});
}

if (!event.body) {
return response({
statusCode: 400,
body: { message: "Event body required" },
});
}
if (!process.env.osDomain) {
return response({
statusCode: 500,
body: { message: "Handler is missing process.env.osDomain env var" },
});
}

try {
const body = JSON.parse(event.body);
Expand Down Expand Up @@ -72,12 +70,7 @@ export const handler = async (event: APIGatewayEvent) => {
}

// Now we can generate the presigned url
const url = await generatePresignedUrl(
body.bucket,
body.key,
body.filename,
60,
);
const url = await generatePresignedUrl(body.bucket, body.key, body.filename, 60);

return response<unknown>({
statusCode: 200,
Expand Down
13 changes: 4 additions & 9 deletions lib/lambda/getCpocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { handleOpensearchError } from "./utils";
import { APIGatewayEvent } from "aws-lambda";
import * as os from "libs/opensearch-lib";
import { response } from "libs/handler-lib";
import { getDomainAndNamespace } from "libs/utils";

// type GetCpocsBody = object;

export const queryCpocs = async () => {
if (!process.env.osDomain) {
throw new Error("process.env.osDomain must be defined");
}
const { index, domain } = getDomainAndNamespace("cpocs");

const query = {
size: 1000,
Expand All @@ -20,11 +19,7 @@ export const queryCpocs = async () => {
},
],
};
return await os.search(
process.env.osDomain,
`${process.env.indexNamespace}cpocs`,
query,
);
return await os.search(domain, index, query);
};

export const getCpocs = async (event: APIGatewayEvent) => {
Expand All @@ -47,7 +42,7 @@ export const getCpocs = async (event: APIGatewayEvent) => {
body: result,
});
} catch (err) {
return response(handleOpensearchError(err))
return response(handleOpensearchError(err));
}
};

Expand Down
31 changes: 22 additions & 9 deletions lib/lambda/processEmails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { SESClient, SendEmailCommand, SendEmailCommandInput } from "@aws-sdk/cli
import { EmailAddresses, KafkaEvent, KafkaRecord } from "shared-types";
import { decodeBase64WithUtf8, getSecret } from "shared-utils";
import { Handler } from "aws-lambda";
import { getEmailTemplates, getAllStateUsers } from "libs/email";
import { getEmailTemplates } from "libs/email";
import { getAllStateUsers } from "libs/email/getAllStateUsers";
import * as os from "./../libs/opensearch-lib";
import { EMAIL_CONFIG, getCpocEmail, getSrtEmails } from "libs/email/content/email-components";
import { htmlToText, HtmlToTextOptions } from "html-to-text";
import pLimit from "p-limit";
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
import { getDomain, getNamespace } from "lib/libs/utils";
import { Index } from "lib/packages/shared-types/opensearch";
import { opensearch } from "shared-types";

class TemporaryError extends Error {
constructor(message: string) {
Expand All @@ -20,7 +24,8 @@ interface ProcessEmailConfig {
emailAddressLookupSecretName: string;
applicationEndpointUrl: string;
osDomain: string;
indexNamespace: string;
mainIndex: Index;
cpocIndex: Index;
region: string;
DLQ_URL: string;
userPoolId: string;
Expand Down Expand Up @@ -48,23 +53,26 @@ export const handler: Handler<KafkaEvent> = async (event) => {

const emailAddressLookupSecretName = process.env.emailAddressLookupSecretName!;
const applicationEndpointUrl = process.env.applicationEndpointUrl!;
const osDomain = process.env.osDomain!;
const indexNamespace = process.env.indexNamespace!;
const osDomain = getDomain();
const mainIndex = getNamespace("main");
const cpocIndex = getNamespace("cpocs");
const region = process.env.region!;
const DLQ_URL = process.env.DLQ_URL!;
const userPoolId = process.env.userPoolId!;
const configurationSetName = process.env.configurationSetName!;
const isDev = process.env.isDev!;
const isDev = process.env.isDev === "true";

const config: ProcessEmailConfig = {
emailAddressLookupSecretName,
applicationEndpointUrl,
osDomain: `https://${osDomain}`,
indexNamespace,
mainIndex,
cpocIndex,
region,
DLQ_URL,
userPoolId,
configurationSetName,
isDev: isDev === "true",
isDev,
};

try {
Expand Down Expand Up @@ -164,9 +172,14 @@ export async function processAndSendEmails(record: any, id: string, config: Proc

const sec = await getSecret(config.emailAddressLookupSecretName);

const item = await os.getItem(config.osDomain, `${config.indexNamespace}main`, id);
const item = await os.getItem(config.osDomain, `${config.mainIndex}`, id);
const cpocItem = (await os.getItem(
config.osDomain,
`${config.cpocIndex}`,
id,
)) as unknown as opensearch.cpocs.ItemResult;

const cpocEmail = getCpocEmail(item);
const cpocEmail = getCpocEmail(cpocItem);
const srtEmails = getSrtEmails(item);
const emails: EmailAddresses = JSON.parse(sec);

Expand Down
6 changes: 3 additions & 3 deletions lib/lambda/runReindex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("CloudFormation Custom Resource Handler", () => {
await handler(mockEvent, {} as Context, callback);

expect(stepFunctionSpy).not.toHaveBeenCalled();
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.SUCCESS, {}, "static");
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.SUCCESS);
expect(callback).toHaveBeenCalledWith(null, { statusCode: 200 });
});

Expand All @@ -65,7 +65,7 @@ describe("CloudFormation Custom Resource Handler", () => {
await handler(mockEvent, {} as Context, callback);

expect(stepFunctionSpy).not.toHaveBeenCalled();
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.SUCCESS, {}, "static");
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.SUCCESS);
expect(callback).toHaveBeenCalledWith(null, { statusCode: 200 });
});

Expand All @@ -91,7 +91,7 @@ describe("CloudFormation Custom Resource Handler", () => {
},
}),
);
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.FAILED, {}, "static");
expect(cfnSpy).toHaveBeenCalledWith(mockEvent, {}, cfn.FAILED);
expect(callback).toHaveBeenCalledWith(expect.any(Error), { statusCode: 500 });
});
});
15 changes: 5 additions & 10 deletions lib/lambda/runReindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { send, SUCCESS, FAILED } from "cfn-response-async";
import { SFNClient, StartExecutionCommand } from "@aws-sdk/client-sfn";

export const handler: Handler = async (event, context, callback) => {
console.log("request:", JSON.stringify(event, undefined, 2));
const response = {
statusCode: 200,
};
Expand All @@ -24,21 +23,17 @@ export const handler: Handler = async (event, context, callback) => {
});

const execution = await stepFunctionsClient.send(startExecutionCommand);
console.log(`State machine execution started: ${execution.executionArn}`);
console.log(
"The state machine is now in charge of this resource, and will notify of success or failure upon completion.",
);
console.log(`State machine execution started with ARN: ${execution.executionArn}`);
} else if (event.RequestType == "Update") {
await send(event, context, SUCCESS, {}, "static");
await send(event, context, SUCCESS);
} else if (event.RequestType == "Delete") {
// need to delete all triggers here to do
await send(event, context, SUCCESS, {}, "static");
await send(event, context, SUCCESS);
}
} catch (error) {
console.log(error);
console.error("Reindexing failed with error:", error);
response.statusCode = 500;
errorResponse = error;
await send(event, context, FAILED, {}, "static");
await send(event, context, FAILED);
} finally {
callback(errorResponse, response);
}
Expand Down
Loading
Loading