All files / lib/lambda deleteIndex.ts

100% Statements 23/23
71.42% Branches 5/7
100% Functions 1/1
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52        1x 4x       4x 4x 4x 4x 4x   3x                     3x 27x 27x   2x 2x 2x   1x 1x 1x         3x 1x     1x 1x   4x      
import { Handler } from "aws-lambda";
import * as os from "libs/opensearch-lib";
import { Index } from "shared-types/opensearch";
 
export const handler: Handler = async (event, __, callback) => {
  const response = {
    statusCode: 200,
  };
  // Using a Set so that it doesn't duplicate errors
  const errorMessages: Set<string> = new Set<string>();
  let errorResponse = null;
  try {
    const { osDomain, indexNamespace = "" } = event;
    if (!osDomain) throw "osDomain cannot be undefined";
 
    const indices: Index[] = [
      `${indexNamespace}main`,
      `${indexNamespace}changelog`,
      `${indexNamespace}insights`,
      `${indexNamespace}types`,
      `${indexNamespace}subtypes`,
      `${indexNamespace}legacyinsights`,
      `${indexNamespace}cpocs`,
      `${indexNamespace}users`,
      `${indexNamespace}roles`,
    ];
    for (const index of indices) {
      try {
        await os.deleteIndex(osDomain, index);
      } catch (error: any) {
        try {
          console.log(`Failed to delete index ${index}, error ${error}, trying again...`);
          await os.deleteIndex(osDomain, index);
        } catch (error2) {
          console.log(`Failed to delete index ${index} again, error, ${error2}`);
          response.statusCode = 500;
          errorMessages.add(`Error deleting index ${index}: ${error2.message || error2}`);
        }
      }
    }
 
    if (errorMessages.size > 0) {
      errorResponse = new Error([...errorMessages].join("\n\n"));
    }
  } catch (error: any) {
    response.statusCode = 500;
    errorResponse = error;
  } finally {
    callback(errorResponse, response);
  }
};