Skip to content

Commit

Permalink
Adds index action as built-in action (#41592) (#41932)
Browse files Browse the repository at this point in the history
* Adds index action as built-in action

* resolve PR comments

- moved `nullableType` to a module so it can be reused across action types
- changed index action param `body` to `documents`, and it's no longer an
  object or array, it's only an array
- rename index action param `execution_time_field` to `executionTimeField`
- remove index action param `doc_id` as it's no longer relevant (only useful
  in the single document story previously)
  • Loading branch information
pmuellr authored Jul 24, 2019
1 parent ff6f37d commit ec94f28
Show file tree
Hide file tree
Showing 17 changed files with 783 additions and 31 deletions.
22 changes: 22 additions & 0 deletions x-pack/legacy/plugins/actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Kibana ships with a set of built-in action types:
- server log: logs messages to the Kibana log using `server.log()`
- email: send an email
- slack: post a message to a slack channel
- index: index document(s) into elasticsearch

## server log, action id: `.log`

Expand Down Expand Up @@ -247,6 +248,27 @@ This action type interfaces with the [Slack Incoming Webhooks feature](https://a
|---|---|---|
|message|the message text|string|


## index, action id: `.index`

The config and params properties are modelled after the [Watcher Index Action](https://www.elastic.co/guide/en/elastic-stack-overview/master/actions-index.html). The index can be set in the config or params, and if set in config, then the index set in the params will be ignored.

#### config properties

|Property|Description|Type|
|---|---|---|
|index|The Elasticsearch index to index into.|string _(optional)_|

#### params properties

|Property|Description|Type|
|---|---|---|
|index|The Elasticsearch index to index into.|string _(optional)_|
|doc_id|The optional _id of the document.|string _(optional)_|
|execution_time_field|The field that will store/index the action execution time.|string _(optional)_|
|refresh|Setting of the refresh policy for the write request|boolean _(optional)_|
|body|The documument body/bodies to index.|object or object[]|

# Command Line Utility

The [`kbn-action`](https://github.com/pmuellr/kbn-action) tool can be used to send HTTP requests to the Actions plugin. For instance, to create a Slack action from the `.slack` Action Type, use the following command:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ describe('execute()', () => {
message: 'a message to you',
};

const executorOptions: ActionTypeExecutorOptions = { config, params, services };
const id = 'some-id';
const executorOptions: ActionTypeExecutorOptions = { id, config, params, services };
sendEmailMock.mockReset();
await actionType.executor(executorOptions);
expect(sendEmailMock.mock.calls[0][1]).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { schema, TypeOf, Type } from '@kbn/config-schema';
import { schema, TypeOf } from '@kbn/config-schema';
import nodemailerServices from 'nodemailer/lib/well-known/services.json';

import { sendEmail, JSON_TRANSPORT_SERVICE } from './lib/send_email';
import { nullableType } from './lib/nullable';
import { ActionType, ActionTypeExecutorOptions, ActionTypeExecutorResult } from '../types';

const PORT_MAX = 256 * 256 - 1;

function nullableType<V>(type: Type<V>) {
return schema.oneOf([type, schema.literal(null)], { defaultValue: () => null });
}

// config definition

const unencryptedConfigProperties = ['service', 'host', 'port', 'secure', 'from'];
Expand Down Expand Up @@ -108,6 +105,7 @@ export const actionType: ActionType = {
// action executor

async function executor(execOptions: ActionTypeExecutorOptions): Promise<ActionTypeExecutorResult> {
const id = execOptions.id;
const config = execOptions.config as ActionTypeConfigType;
const params = execOptions.params as ActionParamsType;
const services = execOptions.services;
Expand Down Expand Up @@ -146,7 +144,7 @@ async function executor(execOptions: ActionTypeExecutorOptions): Promise<ActionT
} catch (err) {
return {
status: 'error',
message: `error sending email: ${err.message}`,
message: `error in action ${id} sending email: ${err.message}`,
};
}

Expand Down
Loading

0 comments on commit ec94f28

Please sign in to comment.