[Response Ops] Fixing ES index connector so that it can index into data streams as well as indices. (#136011)

* Adding op_type: create

* Adding functional test

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Ying Mao 2022-07-13 12:30:47 -04:00 committed by GitHub
parent 951359e23b
commit 8bcc50e953
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 144 additions and 65 deletions

View file

@ -188,7 +188,9 @@ describe('execute()', () => {
Object {
"body": Array [
Object {
"index": Object {},
"index": Object {
"op_type": "create",
},
},
Object {
"jim": "bob",
@ -228,7 +230,9 @@ describe('execute()', () => {
Object {
"body": Array [
Object {
"index": Object {},
"index": Object {
"op_type": "create",
},
},
Object {
"jimbob": "jr",
@ -262,7 +266,9 @@ describe('execute()', () => {
Object {
"body": Array [
Object {
"index": Object {},
"index": Object {
"op_type": "create",
},
},
Object {
"jim": "bob",
@ -295,13 +301,17 @@ describe('execute()', () => {
Object {
"body": Array [
Object {
"index": Object {},
"index": Object {
"op_type": "create",
},
},
Object {
"a": 1,
},
Object {
"index": Object {},
"index": Object {
"op_type": "create",
},
},
Object {
"b": 2,

View file

@ -89,7 +89,7 @@ async function executor(
document[timeField] = new Date();
}
bulkBody.push({ index: {} });
bulkBody.push({ index: { op_type: 'create' } });
bulkBody.push(document);
}

View file

@ -9,7 +9,10 @@ import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
const ES_TEST_INDEX_NAME = 'functional-test-actions-index';
const ES_TEST_INDEX_NAME = 'functional-test-connectors-index';
const ES_TEST_DATASTREAM_PREFIX = 'functional-test-connectors-ds';
const ES_TEST_DATASTREAM_PATTERN_NAME = `${ES_TEST_DATASTREAM_PREFIX}-*`;
const ES_TEST_DATASTREAM_INDEX_NAME = `${ES_TEST_DATASTREAM_PREFIX}-00001`;
// eslint-disable-next-line import/no-default-export
export default function indexTest({ getService }: FtrProviderContext) {
@ -17,62 +20,76 @@ export default function indexTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esDeleteAllIndices = getService('esDeleteAllIndices');
describe('index action', () => {
beforeEach(() => esDeleteAllIndices(ES_TEST_INDEX_NAME));
describe('index connector', () => {
beforeEach(() => {
esDeleteAllIndices(ES_TEST_INDEX_NAME);
esDeleteAllIndices(ES_TEST_DATASTREAM_INDEX_NAME);
});
let createdActionID: string;
let createdActionIDWithIndex: string;
after(async () => {
await es.transport.request({
method: 'DELETE',
path: `/_data_stream/${ES_TEST_DATASTREAM_INDEX_NAME}`,
});
await es.transport.request({
method: 'DELETE',
path: `/_index_template/${ES_TEST_DATASTREAM_PREFIX}`,
});
});
let createdConnectorID: string;
let createdConnectorIDWithIndex: string;
it('should be created successfully', async () => {
// create action with no config
const { body: createdAction } = await supertest
.post('/api/actions/action')
const { body: createdConnector } = await supertest
.post('/api/actions/connector')
.set('kbn-xsrf', 'foo')
.send({
name: 'An index action',
actionTypeId: '.index',
name: 'An index connector',
connector_type_id: '.index',
config: { index: ES_TEST_INDEX_NAME },
secrets: {},
})
.expect(200);
expect(createdAction).to.eql({
id: createdAction.id,
isPreconfigured: false,
isDeprecated: false,
name: 'An index action',
actionTypeId: '.index',
isMissingSecrets: false,
expect(createdConnector).to.eql({
id: createdConnector.id,
is_preconfigured: false,
is_deprecated: false,
name: 'An index connector',
connector_type_id: '.index',
is_missing_secrets: false,
config: {
index: ES_TEST_INDEX_NAME,
refresh: false,
executionTimeField: null,
},
});
createdActionID = createdAction.id;
expect(typeof createdActionID).to.be('string');
createdConnectorID = createdConnector.id;
expect(typeof createdConnectorID).to.be('string');
const { body: fetchedAction } = await supertest
.get(`/api/actions/action/${createdActionID}`)
const { body: fetchedConnector } = await supertest
.get(`/api/actions/connector/${createdConnectorID}`)
.expect(200);
expect(fetchedAction).to.eql({
id: fetchedAction.id,
isPreconfigured: false,
isDeprecated: false,
isMissingSecrets: false,
name: 'An index action',
actionTypeId: '.index',
expect(fetchedConnector).to.eql({
id: fetchedConnector.id,
is_preconfigured: false,
is_deprecated: false,
is_missing_secrets: false,
name: 'An index connector',
connector_type_id: '.index',
config: { index: ES_TEST_INDEX_NAME, refresh: false, executionTimeField: null },
});
// create action with all config props
const { body: createdActionWithIndex } = await supertest
.post('/api/actions/action')
// create connector with all config props
const { body: createdConnectorWithIndex } = await supertest
.post('/api/actions/connector')
.set('kbn-xsrf', 'foo')
.send({
name: 'An index action with index config',
actionTypeId: '.index',
name: 'An index connector with index config',
connector_type_id: '.index',
config: {
index: ES_TEST_INDEX_NAME,
refresh: true,
@ -81,33 +98,33 @@ export default function indexTest({ getService }: FtrProviderContext) {
})
.expect(200);
expect(createdActionWithIndex).to.eql({
id: createdActionWithIndex.id,
isPreconfigured: false,
isDeprecated: false,
name: 'An index action with index config',
actionTypeId: '.index',
isMissingSecrets: false,
expect(createdConnectorWithIndex).to.eql({
id: createdConnectorWithIndex.id,
is_preconfigured: false,
is_deprecated: false,
name: 'An index connector with index config',
connector_type_id: '.index',
is_missing_secrets: false,
config: {
index: ES_TEST_INDEX_NAME,
refresh: true,
executionTimeField: 'test',
},
});
createdActionIDWithIndex = createdActionWithIndex.id;
expect(typeof createdActionIDWithIndex).to.be('string');
createdConnectorIDWithIndex = createdConnectorWithIndex.id;
expect(typeof createdConnectorIDWithIndex).to.be('string');
const { body: fetchedActionWithIndex } = await supertest
.get(`/api/actions/action/${createdActionIDWithIndex}`)
const { body: fetchedConnectorWithIndex } = await supertest
.get(`/api/actions/connector/${createdConnectorIDWithIndex}`)
.expect(200);
expect(fetchedActionWithIndex).to.eql({
id: fetchedActionWithIndex.id,
isPreconfigured: false,
isDeprecated: false,
name: 'An index action with index config',
actionTypeId: '.index',
isMissingSecrets: false,
expect(fetchedConnectorWithIndex).to.eql({
id: fetchedConnectorWithIndex.id,
is_preconfigured: false,
is_deprecated: false,
name: 'An index connector with index config',
connector_type_id: '.index',
is_missing_secrets: false,
config: {
index: ES_TEST_INDEX_NAME,
refresh: true,
@ -116,13 +133,13 @@ export default function indexTest({ getService }: FtrProviderContext) {
});
});
it('should execute successly when expected for a single body', async () => {
const { body: createdAction } = await supertest
.post('/api/actions/action')
it('should execute successfully when expected for a single body', async () => {
const { body: createdConnector } = await supertest
.post('/api/actions/connector')
.set('kbn-xsrf', 'foo')
.send({
name: 'An index action',
actionTypeId: '.index',
name: 'An index connector',
connector_type_id: '.index',
config: {
index: ES_TEST_INDEX_NAME,
refresh: true,
@ -131,7 +148,7 @@ export default function indexTest({ getService }: FtrProviderContext) {
})
.expect(200);
const { body: result } = await supertest
.post(`/api/actions/action/${createdAction.id}/_execute`)
.post(`/api/actions/connector/${createdConnector.id}/_execute`)
.set('kbn-xsrf', 'foo')
.send({
params: {
@ -141,16 +158,68 @@ export default function indexTest({ getService }: FtrProviderContext) {
.expect(200);
expect(result.status).to.eql('ok');
const items = await getTestIndexItems(es);
const items = await getTestIndexItems(es, ES_TEST_INDEX_NAME);
expect(items.length).to.eql(1);
expect(items[0]._source).to.eql({ testing: [1, 2, 3] });
});
it('should execute successfully into data stream', async () => {
await es.transport.request(
{
method: 'PUT',
path: `/_index_template/${ES_TEST_DATASTREAM_PREFIX}`,
body: {
index_patterns: [ES_TEST_DATASTREAM_PATTERN_NAME],
template: {
mappings: {
properties: {
'@timestamp': {
type: 'date',
},
},
},
},
data_stream: {},
},
},
{ meta: true }
);
const { body: createdConnector } = await supertest
.post('/api/actions/connector')
.set('kbn-xsrf', 'foo')
.send({
name: 'An index connector',
connector_type_id: '.index',
config: {
index: ES_TEST_DATASTREAM_INDEX_NAME,
refresh: true,
},
secrets: {},
})
.expect(200);
const timestamp = new Date().toISOString();
const { body: result } = await supertest
.post(`/api/actions/connector/${createdConnector.id}/_execute`)
.set('kbn-xsrf', 'foo')
.send({
params: {
documents: [{ '@timestamp': timestamp, testing_ds: [1, 2, 3] }],
},
})
.expect(200);
expect(result.status).to.eql('ok');
const items = await getTestIndexItems(es, ES_TEST_DATASTREAM_INDEX_NAME);
expect(items.length).to.eql(1);
expect(items[0]._source).to.eql({ '@timestamp': timestamp, testing_ds: [1, 2, 3] });
});
});
}
async function getTestIndexItems(es: Client) {
async function getTestIndexItems(es: Client, indexName: string) {
const result = await es.search({
index: ES_TEST_INDEX_NAME,
index: indexName,
});
return result.hits.hits;