mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[Connector] Use Connector API to update configuration (#183470)
## Summary Use Connector API to handle configuration updates for connectors. Update unit tests.
This commit is contained in:
parent
433b67d112
commit
3965bb6313
5 changed files with 52 additions and 67 deletions
|
@ -8,52 +8,73 @@
|
|||
|
||||
import { ElasticsearchClient } from '@kbn/core/server';
|
||||
|
||||
import { CONNECTORS_INDEX } from '..';
|
||||
import { fetchConnectorById } from './fetch_connectors';
|
||||
import { ConnectorStatus } from '../types/connectors';
|
||||
import { errors } from '@elastic/elasticsearch';
|
||||
|
||||
import { updateConnectorConfiguration } from './update_connector_configuration';
|
||||
import { fetchConnectorById } from './fetch_connectors';
|
||||
|
||||
jest.mock('./fetch_connectors', () => ({ fetchConnectorById: jest.fn() }));
|
||||
|
||||
describe('updateConnectorConfiguration lib function', () => {
|
||||
const mockClient = {
|
||||
update: jest.fn(),
|
||||
transport: {
|
||||
request: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
(fetchConnectorById as jest.Mock).mockResolvedValue({
|
||||
configuration: { test: { label: 'haha', value: 'this' } },
|
||||
id: 'connectorId',
|
||||
status: ConnectorStatus.NEEDS_CONFIGURATION,
|
||||
});
|
||||
});
|
||||
|
||||
it('should update configuration', async () => {
|
||||
(fetchConnectorById as jest.Mock).mockResolvedValue({
|
||||
configuration: { test: { value: 'haha' } },
|
||||
});
|
||||
|
||||
mockClient.transport.request.mockResolvedValueOnce({ result: 'updated' });
|
||||
|
||||
await expect(
|
||||
updateConnectorConfiguration(mockClient as unknown as ElasticsearchClient, 'connectorId', {
|
||||
test: 'newValue',
|
||||
test: 'haha',
|
||||
})
|
||||
).resolves.toEqual({ test: { label: 'haha', value: 'newValue' } });
|
||||
expect(mockClient.update).toHaveBeenCalledWith({
|
||||
doc: {
|
||||
configuration: { test: { label: 'haha', value: 'newValue' } },
|
||||
status: ConnectorStatus.CONFIGURED,
|
||||
).resolves.toEqual({ test: { value: 'haha' } });
|
||||
expect(mockClient.transport.request).toHaveBeenCalledWith({
|
||||
body: {
|
||||
values: {
|
||||
test: 'haha',
|
||||
},
|
||||
},
|
||||
id: 'connectorId',
|
||||
index: CONNECTORS_INDEX,
|
||||
method: 'PUT',
|
||||
path: '/_connector/connectorId/_configuration',
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject if connector does not exist', async () => {
|
||||
(fetchConnectorById as jest.Mock).mockImplementation(() => undefined);
|
||||
|
||||
mockClient.transport.request.mockImplementationOnce(() => {
|
||||
return Promise.reject(
|
||||
new errors.ResponseError({
|
||||
statusCode: 404,
|
||||
body: {
|
||||
error: {
|
||||
type: `document_missing_exception`,
|
||||
},
|
||||
},
|
||||
} as any)
|
||||
);
|
||||
});
|
||||
await expect(
|
||||
updateConnectorConfiguration(mockClient as unknown as ElasticsearchClient, 'connectorId', {
|
||||
test: 'newValue',
|
||||
test: 'haha',
|
||||
})
|
||||
).rejects.toEqual(new Error('Could not find connector'));
|
||||
expect(mockClient.update).not.toHaveBeenCalled();
|
||||
).rejects.toEqual(
|
||||
new errors.ResponseError({
|
||||
statusCode: 404,
|
||||
body: {
|
||||
error: {
|
||||
type: `document_missing_exception`,
|
||||
},
|
||||
},
|
||||
} as any)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -8,54 +8,21 @@
|
|||
|
||||
import { ElasticsearchClient } from '@kbn/core/server';
|
||||
|
||||
import { i18n } from '@kbn/i18n';
|
||||
|
||||
import { CONNECTORS_INDEX } from '..';
|
||||
|
||||
import { Result } from '@elastic/elasticsearch/lib/api/types';
|
||||
import { fetchConnectorById } from './fetch_connectors';
|
||||
import { ConnectorConfiguration, ConnectorDocument, ConnectorStatus } from '../types/connectors';
|
||||
import { isConfigEntry } from '../utils/is_category_entry';
|
||||
import { isNotNullish } from '../utils/is_not_nullish';
|
||||
|
||||
export const updateConnectorConfiguration = async (
|
||||
client: ElasticsearchClient,
|
||||
connectorId: string,
|
||||
configuration: Record<string, string | number | boolean>
|
||||
) => {
|
||||
await client.transport.request<Result>({
|
||||
method: 'PUT',
|
||||
path: `/_connector/${connectorId}/_configuration`,
|
||||
body: {
|
||||
values: configuration,
|
||||
},
|
||||
});
|
||||
const connector = await fetchConnectorById(client, connectorId);
|
||||
if (connector) {
|
||||
const status =
|
||||
connector.status === ConnectorStatus.NEEDS_CONFIGURATION ||
|
||||
connector.status === ConnectorStatus.CREATED
|
||||
? ConnectorStatus.CONFIGURED
|
||||
: connector.status;
|
||||
const updatedConfig: ConnectorConfiguration = Object.keys(connector.configuration)
|
||||
.map((key) => {
|
||||
const configEntry = connector.configuration[key];
|
||||
return isConfigEntry(configEntry)
|
||||
? {
|
||||
...configEntry, // ugly but needed because typescript refuses to believe this is defined
|
||||
key,
|
||||
value: configuration[key] ?? configEntry.value,
|
||||
}
|
||||
: undefined;
|
||||
})
|
||||
.filter(isNotNullish)
|
||||
.reduce((prev: ConnectorConfiguration, curr) => {
|
||||
const { key, ...config } = curr;
|
||||
return { ...prev, [curr.key]: config };
|
||||
}, {});
|
||||
await client.update<ConnectorDocument>({
|
||||
doc: { configuration: updatedConfig, status },
|
||||
id: connectorId,
|
||||
index: CONNECTORS_INDEX,
|
||||
});
|
||||
return updatedConfig;
|
||||
} else {
|
||||
throw new Error(
|
||||
i18n.translate('searchConnectors.server.connectors.configuration.error', {
|
||||
defaultMessage: 'Could not find connector',
|
||||
})
|
||||
);
|
||||
}
|
||||
return connector?.configuration;
|
||||
};
|
||||
|
|
|
@ -5777,7 +5777,6 @@
|
|||
"searchConnectors.searchIndices.identitySync.columnTitle": "Identités synchronisées",
|
||||
"searchConnectors.searchIndices.syncJobType.columnTitle": "Type de synchronisation de contenu",
|
||||
"searchConnectors.searchIndices.syncStatus.columnTitle": "Statut",
|
||||
"searchConnectors.server.connectors.configuration.error": "Connecteur introuvable",
|
||||
"searchConnectors.server.connectors.scheduling.error": "Document introuvable",
|
||||
"searchConnectors.syncJobs.flyout.canceledTitle": "Synchronisation annulée",
|
||||
"searchConnectors.syncJobs.flyout.completedTitle": "Synchronisation terminée",
|
||||
|
|
|
@ -5770,7 +5770,6 @@
|
|||
"searchConnectors.searchIndices.identitySync.columnTitle": "IDが同期されました",
|
||||
"searchConnectors.searchIndices.syncJobType.columnTitle": "コンテンツ同期タイプ",
|
||||
"searchConnectors.searchIndices.syncStatus.columnTitle": "ステータス",
|
||||
"searchConnectors.server.connectors.configuration.error": "コネクターが見つかりませんでした",
|
||||
"searchConnectors.server.connectors.scheduling.error": "ドキュメントが見つかりませんでした",
|
||||
"searchConnectors.syncJobs.flyout.canceledTitle": "同期がキャンセルされました",
|
||||
"searchConnectors.syncJobs.flyout.completedTitle": "同期完了",
|
||||
|
|
|
@ -5781,7 +5781,6 @@
|
|||
"searchConnectors.searchIndices.identitySync.columnTitle": "身份已同步",
|
||||
"searchConnectors.searchIndices.syncJobType.columnTitle": "内容同步类型",
|
||||
"searchConnectors.searchIndices.syncStatus.columnTitle": "状态",
|
||||
"searchConnectors.server.connectors.configuration.error": "找不到连接器",
|
||||
"searchConnectors.server.connectors.scheduling.error": "无法找到文档",
|
||||
"searchConnectors.syncJobs.flyout.canceledTitle": "同步已取消",
|
||||
"searchConnectors.syncJobs.flyout.completedTitle": "同步已完成",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue