Skip to content

Commit

Permalink
Add functionality to call Search API module defined by user.
Browse files Browse the repository at this point in the history
Execute post-deployment API requests in both sandbox mode and production.
Add @nasa-gcn/architect-functions-search as dependency.
  • Loading branch information
ronitagarwala01 committed Dec 11, 2023
1 parent ac3db4f commit 9e18b21
Show file tree
Hide file tree
Showing 4 changed files with 1,238 additions and 7 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ const client = await search()

Now `client` is an instance of the [OpenSearch JavaScript client](https://opensearch.org/docs/latest/clients/javascript/index/), and you can call any client method on it — for example, [`client.index.create()`](https://opensearch.org/docs/latest/clients/javascript/index/#creating-an-index), [`client.index()`](https://opensearch.org/docs/latest/clients/javascript/index/#indexing-a-document), or [`client.search()`](https://opensearch.org/docs/latest/clients/javascript/index/#searching-for-documents).

## Making post-deployment requests to OpenSearch or ElasticSearch from your application

If you would like to make post-deployment REST API calls to your OpenSearch or ElasticSearch instance, you may optionally add a postdeploy-search.js file in the root directory of your Architect project. This file should by default export a function that takes a configured OpenSearch client instance as its only argument. The function will be called post-deployment in production mode and at the start of sandbox mode.

Here's an sample postdeploy-search.js file for making requests to OpenSearch:

```ts
export default async function (client) {

await client.transport.request({
method: 'PUT',
path: '/_cluster/settings',
body: {
persistent: {
cluster.max_shards_per_node: '500',
},
},
})
}
}
```

## Advanced usage from your application

If you would like to manually connect to OpenSearch from your application, then you will need to sign your requests using [AWS SIG4](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html); the OpenSearch client library provides the [`AwsSigv4Signer`](https://opensearch.org/docs/latest/clients/javascript/index/#authenticating-with-amazon-opensearch-service--aws-sigv4) helper to automate this.
Expand Down
29 changes: 29 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { exists } from './paths'
import { join } from 'path'
import { pathToFileURL } from 'url'
import { launch } from './run.js'
import { populate } from './data.js'
import { search as getSearchClient } from '@nasa-gcn/architect-functions-search'
import {
cloudformationResources as serverlessCloudformationResources,
services as serverlessServices,
Expand Down Expand Up @@ -38,6 +42,25 @@ function getConfig(arc: {
else return {}
}

const searchApiFile = 'postdeploy-search.js'

async function executeSearchRequests(cwd: string) {
//Load api call file and run all api calls to cluster
const apiPath = join(cwd, searchApiFile)
if (await exists(apiPath)) {
console.log(`Found ${searchApiFile} file, running API calls...`)
let result = (await import(pathToFileURL(apiPath).toString())).default
const client = await getSearchClient()

// result should be a function that returns a promise
if (typeof result === 'function') {
result = result(client)
}

await result
}
}

export const deploy = {
// @ts-expect-error: The Architect plugins API has no type definitions.
start({ cloudformation, inventory, arc, stage }) {
Expand All @@ -63,6 +86,10 @@ export const deploy = {
return serverlessServices
}
},
// @ts-expect-error: The Architect plugins API has no type definitions.
async end({ inventory }) {
executeSearchRequests(inventory.inv._project.cwd)
},
}

let local: Awaited<ReturnType<typeof launch>>
Expand All @@ -85,8 +112,10 @@ export const sandbox = {
}) {
const engine = getEngine(getConfig(arc).sandboxEngine)
local = await launch({ engine })
await executeSearchRequests(cwd)
await populate(cwd, { node: local.url })
},

async end() {
await local.stop()
},
Expand Down
Loading

0 comments on commit 9e18b21

Please sign in to comment.