Skip to content

Commit

Permalink
Add support for programmable post-deployment API calls (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronitagarwala01 authored Dec 12, 2023
1 parent ac3db4f commit 80196c3
Show file tree
Hide file tree
Showing 4 changed files with 1,236 additions and 7 deletions.
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

0 comments on commit 80196c3

Please sign in to comment.