Merge pull request #10618 from Budibase/budi-6932/check_elastic

Check elastic search connection settings
This commit is contained in:
Adria Navarro 2023-05-17 10:16:16 +02:00 committed by GitHub
commit a2e22bda71
2 changed files with 49 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import {
QueryType,
IntegrationBase,
DatasourceFeature,
ConnectionInfo,
} from "@budibase/types"
import { Client, ClientOptions } from "@elastic/elasticsearch"
@ -21,7 +22,7 @@ const SCHEMA: Integration = {
"Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.",
friendlyName: "ElasticSearch",
type: "Non-relational",
features: [],
features: [DatasourceFeature.CONNECTION_CHECKING],
datasource: {
url: {
type: DatasourceFieldType.STRING,
@ -97,7 +98,7 @@ const SCHEMA: Integration = {
class ElasticSearchIntegration implements IntegrationBase {
private config: ElasticsearchConfig
private client: any
private client
constructor(config: ElasticsearchConfig) {
this.config = config
@ -116,6 +117,18 @@ class ElasticSearchIntegration implements IntegrationBase {
this.client = new Client(clientConfig)
}
async testConnection(): Promise<ConnectionInfo> {
try {
await this.client.info()
return { connected: true }
} catch (e: any) {
return {
connected: false,
error: e.message as string,
}
}
}
async create(query: { index: string; json: object }) {
const { index, json } = query

View File

@ -0,0 +1,34 @@
import { ElasticsearchContainer } from "testcontainers"
import elastic from "../../../../packages/server/src/integrations/elasticsearch"
jest.unmock("@elastic/elasticsearch")
describe("datasource validators", () => {
describe("elastic search", () => {
let url: string
beforeAll(async () => {
const container = await new ElasticsearchContainer().start()
url = container.getHttpUrl()
})
it("test valid connection string", async () => {
const integration = new elastic.integration({
url,
})
const result = await integration.testConnection()
expect(result).toEqual({ connected: true })
})
it("test wrong connection string", async () => {
const integration = new elastic.integration({
url: `http://localhost:5656`,
})
const result = await integration.testConnection()
expect(result).toEqual({
connected: false,
error: "connect ECONNREFUSED 127.0.0.1:5656",
})
})
})
})