Skip to content

Commit

Permalink
Merge pull request #265 from Enterprise-CMCS/bruce
Browse files Browse the repository at this point in the history
feat(OY-26537): OS changelog package activity
  • Loading branch information
pkim-gswell authored Dec 19, 2023
2 parents 147b66e + 0956f64 commit e66cb71
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 216 deletions.
34 changes: 11 additions & 23 deletions src/libs/opensearch-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as aws4 from "aws4";
import axios from "axios";
import { aws4Interceptor } from "aws4-axios";
import { STSClient, AssumeRoleCommand } from "@aws-sdk/client-sts";
import { OsIndex } from "shared-types";
let client: Client;

export async function getClient(host: string) {
Expand Down Expand Up @@ -39,7 +40,7 @@ export async function updateData(host: string, indexObject: any) {

export async function bulkUpdateData(
host: string,
index: string,
index: OsIndex,
arrayOfDocuments: any
) {
client = client || (await getClient(host));
Expand All @@ -59,7 +60,7 @@ export async function bulkUpdateData(
console.log(response);
}

export async function deleteIndex(host: string, index: string) {
export async function deleteIndex(host: string, index: OsIndex) {
client = client || (await getClient(host));
var response = await client.indices.delete({ index });
}
Expand Down Expand Up @@ -110,7 +111,7 @@ export async function mapRole(
}
}

export async function search(host: string, index: string, query: any) {
export async function search(host: string, index: OsIndex, query: any) {
client = client || (await getClient(host));
try {
const response = await client.search({
Expand All @@ -123,7 +124,7 @@ export async function search(host: string, index: string, query: any) {
}
}

export async function getItem(host: string, index: string, id: string) {
export async function getItem(host: string, index: OsIndex, id: string) {
client = client || (await getClient(host));
try {
const response = await client.get({ id, index });
Expand All @@ -133,27 +134,14 @@ export async function getItem(host: string, index: string, id: string) {
}
}

export async function indexExists(host: string, index: string) {
// check it exists - then create
export async function createIndex(host: string, index: OsIndex) {
client = client || (await getClient(host));
try {
const indexExists = await client.indices.exists({ index });
if (indexExists.body) {
return true;
} else {
return false;
}
} catch (error) {
console.error("Error creating index:", error);
throw error;
}
}
const exists = await client.indices.exists({ index });
if(!!exists.body) return;

export async function createIndex(host: string, index: string) {
client = client || (await getClient(host));
try {
const createResponse = await client.indices.create({
index,
});
await client.indices.create({ index });
} catch (error) {
console.error("Error creating index:", error);
throw error;
Expand All @@ -162,7 +150,7 @@ export async function createIndex(host: string, index: string) {

export async function updateFieldMapping(
host: string,
index: string,
index: OsIndex,
properties: object
) {
client = client || (await getClient(host));
Expand Down
2 changes: 2 additions & 0 deletions src/packages/shared-types/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ export type OsExportHeaderOptions<TData> = {
transform: (data: TData) => string;
name: string;
};

export type OsIndex = "main" | "seatool" | "changelog";
54 changes: 33 additions & 21 deletions src/services/data/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Handler } from "aws-lambda";
import { send, SUCCESS, FAILED } from "cfn-response-async";
type ResponseStatus = typeof SUCCESS | typeof FAILED;
import * as os from "./../../../libs/opensearch-lib";
import { OsIndex } from "shared-types";

export const customResourceWrapper: Handler = async (event, context) => {
console.log("request:", JSON.stringify(event, undefined, 2));
Expand All @@ -24,29 +25,40 @@ export const handler: Handler = async () => {
await manageIndex();
};

const manageIndexResource = async (resource: {
index: OsIndex;
update?: object;
}) => {
if (!process.env.osDomain) {
throw new Error("process.env.osDomain must be defined");
}

const createIndex = await os.createIndex(
process.env.osDomain,
resource.index
);
console.log({ createIndex });

if (!resource.update) return;

const updateFieldMapping = await os.updateFieldMapping(
process.env.osDomain,
resource.index,
resource.update
);
console.log(updateFieldMapping);
};

async function manageIndex() {
try {
if (!process.env.osDomain)
throw new Error("process.env.osDomain must be defined");

if (!(await os.indexExists(process.env.osDomain, "main"))) {
const createIndexReponse = await os.createIndex(
process.env.osDomain,
"main"
);
console.log(createIndexReponse);
const updateFieldMappingResponse = await os.updateFieldMapping(
process.env.osDomain,
"main",
{
rais: {
type: "object",
enabled: false,
},
}
);
console.log(updateFieldMappingResponse);
}
await manageIndexResource({
index: "main",
update: { rais: { type: "object", enabled: false } },
});

await manageIndexResource({
index: "changelog",
});
} catch (error) {
console.log(error);
throw "ERROR: Error occured during index management.";
Expand Down
Loading

0 comments on commit e66cb71

Please sign in to comment.