-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Search][ES3] Auto generate connector name and index_name (#202149)
## Summary Adds Connector name and index_name auto-generation to ES3. This is taken from the [ESS implementation here](https://github.com/elastic/kibana/blob/main/x-pack/plugins/enterprise_search/server/lib/connectors/generate_connector_name.ts). The ES3 implementation functions a little differently, because the ES3 Connector creation flow is different. For ES3, the auto-generated Connector `name` and `index_name` are automatically saved to the Connector document when a `service_type` is selected. This is because the selection of a `service_type` already creates the Connector record, so it made the most sense to piggyback on that process. If the user defines a name before selecting a service type, the user-defined name is kept.
- Loading branch information
1 parent
2ed1fdf
commit 1749c88
Showing
7 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
|
||
export const indexOrAliasExists = async ( | ||
client: ElasticsearchClient, | ||
index: string | ||
): Promise<boolean> => | ||
(await client.indices.exists({ index })) || (await client.indices.existsAlias({ name: index })); |
72 changes: 72 additions & 0 deletions
72
packages/kbn-search-connectors/lib/generate_connector_name.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
|
||
import { toAlphanumeric } from '../utils/to_alphanumeric'; | ||
import { indexOrAliasExists } from './exists_index'; | ||
import { MANAGED_CONNECTOR_INDEX_PREFIX } from '../constants'; | ||
|
||
const GENERATE_INDEX_NAME_ERROR = 'generate_index_name_error'; | ||
|
||
export const generateConnectorName = async ( | ||
client: ElasticsearchClient, | ||
connectorType: string, | ||
isNative: boolean, | ||
userConnectorName?: string | ||
): Promise<{ connectorName: string; indexName: string }> => { | ||
const prefix = toAlphanumeric(connectorType); | ||
if (!prefix || prefix.length === 0) { | ||
throw new Error('Connector type or connectorName is required'); | ||
} | ||
|
||
const nativePrefix = isNative ? MANAGED_CONNECTOR_INDEX_PREFIX : ''; | ||
|
||
if (userConnectorName) { | ||
let indexName = `${nativePrefix}connector-${userConnectorName}`; | ||
const resultSameName = await indexOrAliasExists(client, indexName); | ||
// index with same name doesn't exist | ||
if (!resultSameName) { | ||
return { | ||
connectorName: userConnectorName, | ||
indexName, | ||
}; | ||
} | ||
// if the index name already exists, we will generate until it doesn't for 20 times | ||
for (let i = 0; i < 20; i++) { | ||
indexName = `${nativePrefix}connector-${userConnectorName}-${uuidv4() | ||
.split('-')[1] | ||
.slice(0, 4)}`; | ||
|
||
const result = await indexOrAliasExists(client, indexName); | ||
if (!result) { | ||
return { | ||
connectorName: userConnectorName, | ||
indexName, | ||
}; | ||
} | ||
} | ||
} else { | ||
for (let i = 0; i < 20; i++) { | ||
const connectorName = `${prefix}-${uuidv4().split('-')[1].slice(0, 4)}`; | ||
const indexName = `${nativePrefix}connector-${connectorName}`; | ||
|
||
const result = await indexOrAliasExists(client, indexName); | ||
if (!result) { | ||
return { | ||
connectorName, | ||
indexName, | ||
}; | ||
} | ||
} | ||
} | ||
throw new Error(GENERATE_INDEX_NAME_ERROR); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/kbn-search-connectors/utils/to_alphanumeric.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { toAlphanumeric } from './to_alphanumeric'; | ||
|
||
describe('toAlphanumeric', () => { | ||
it('replaces non-alphanumeric characters with dashes', () => { | ||
expect(toAlphanumeric('f1 &&o$ 1 2 *&%da')).toEqual('f1-o-1-2-da'); | ||
}); | ||
|
||
it('strips leading and trailing non-alphanumeric characters', () => { | ||
expect(toAlphanumeric('$$hello world**')).toEqual('hello-world'); | ||
}); | ||
|
||
it('strips leading and trailing whitespace', () => { | ||
expect(toAlphanumeric(' test ')).toEqual('test'); | ||
}); | ||
|
||
it('lowercases text', () => { | ||
expect(toAlphanumeric('SomeName')).toEqual('somename'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
export const toAlphanumeric = (input: string) => | ||
input | ||
.trim() | ||
.replace(/[^a-zA-Z0-9]+/g, '-') // Replace all special/non-alphanumerical characters with dashes | ||
.replace(/^[-]+|[-]+$/g, '') // Strip all leading and trailing dashes | ||
.toLowerCase(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters