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

Configure plugin for calling Search API #20

Merged
merged 4 commits into from
Dec 12, 2023
Merged
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ 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 it...`)
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