From a590b2fd961b79188289081f14385abf0548831f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Loix?= Date: Tue, 12 Feb 2019 17:46:49 +0100 Subject: [PATCH] [Watcher] Add Index, HipChat, PagerDuty, and Jira Action types on the client (#30043) (#30620) --- .../watcher/public/models/action/action.js | 8 ++ .../public/models/action/hipchat.action.js | 69 +++++++++++++++ .../public/models/action/index.action.js | 88 +++++++++++++++++++ .../public/models/action/jira.action.js | 70 +++++++++++++++ .../public/models/action/pagerduty.action.js | 80 +++++++++++++++++ .../public/models/action/webhook.action.js | 33 ++----- 6 files changed, 321 insertions(+), 27 deletions(-) create mode 100644 x-pack/plugins/watcher/public/models/action/hipchat.action.js create mode 100644 x-pack/plugins/watcher/public/models/action/index.action.js create mode 100644 x-pack/plugins/watcher/public/models/action/jira.action.js create mode 100644 x-pack/plugins/watcher/public/models/action/pagerduty.action.js diff --git a/x-pack/plugins/watcher/public/models/action/action.js b/x-pack/plugins/watcher/public/models/action/action.js index 6d15c6c0cd8ec..f3cdfeef7fab7 100644 --- a/x-pack/plugins/watcher/public/models/action/action.js +++ b/x-pack/plugins/watcher/public/models/action/action.js @@ -10,6 +10,10 @@ import { EmailAction } from './email_action'; import { LoggingAction } from './logging_action'; import { SlackAction } from './slack_action'; import { WebhookAction } from './webhook.action'; +import { IndexAction } from './index.action'; +import { HipchatAction } from './hipchat.action'; +import { PagerDutyAction } from './pagerduty.action'; +import { JiraAction } from './jira.action'; import { UnknownAction } from './unknown_action'; const ActionTypes = {}; @@ -17,6 +21,10 @@ set(ActionTypes, ACTION_TYPES.EMAIL, EmailAction); set(ActionTypes, ACTION_TYPES.LOGGING, LoggingAction); set(ActionTypes, ACTION_TYPES.SLACK, SlackAction); set(ActionTypes, ACTION_TYPES.WEBHOOK, WebhookAction); +set(ActionTypes, ACTION_TYPES.INDEX, IndexAction); +set(ActionTypes, ACTION_TYPES.HIPCHAT, HipchatAction); +set(ActionTypes, ACTION_TYPES.PAGERDUTY, PagerDutyAction); +set(ActionTypes, ACTION_TYPES.JIRA, JiraAction); export class Action { static getActionTypes = () => { diff --git a/x-pack/plugins/watcher/public/models/action/hipchat.action.js b/x-pack/plugins/watcher/public/models/action/hipchat.action.js new file mode 100644 index 0000000000000..bf4eb0d039342 --- /dev/null +++ b/x-pack/plugins/watcher/public/models/action/hipchat.action.js @@ -0,0 +1,69 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + + +import { get } from 'lodash'; +import { BaseAction } from './base_action'; +import { i18n } from '@kbn/i18n'; + +const requiredFields = ['message']; +const optionalFields = ['account', 'proxy']; + +const allFields = [...requiredFields, ...optionalFields]; + +export class HipchatAction extends BaseAction { + constructor(props = {}) { + super(props); + + this.fields = {}; + allFields.forEach((field) => { + this.fields[field] = get(props, field); + }); + } + + get upstreamJson() { + // Add all required fields to the request body + let result = requiredFields.reduce((acc, field) => { + acc[field] = this.fields[field]; + return acc; + }, super.upstreamJson); + + // If optional fields have been set, add them to the body + result = optionalFields.reduce((acc, field) => { + if (this[field]) { + acc[field] = this.fields[field]; + } + return acc; + }, result); + + return result; + } + + get description() { + return i18n.translate('xpack.watcher.models.hipchatAction.description', { + defaultMessage: '{body} will be sent through Hipchat', + values: { + body: this.fields.message && this.fields.message.body || '' + } + }); + } + + get simulateMessage() { + return i18n.translate('xpack.watcher.models.hipchatAction.simulateMessage', { + defaultMessage: 'Hipchat message has been sent.', + }); + } + + get simulateFailMessage() { + return i18n.translate('xpack.watcher.models.hipchatAction.simulateFailMessage', { + defaultMessage: 'Failed to send Hipchat message.', + }); + } + + static fromUpstreamJson(upstreamAction) { + return new HipchatAction(upstreamAction); + } +} diff --git a/x-pack/plugins/watcher/public/models/action/index.action.js b/x-pack/plugins/watcher/public/models/action/index.action.js new file mode 100644 index 0000000000000..b4a311501df8c --- /dev/null +++ b/x-pack/plugins/watcher/public/models/action/index.action.js @@ -0,0 +1,88 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + + +import { get } from 'lodash'; +import { BaseAction } from './base_action'; +import { i18n } from '@kbn/i18n'; + +const requiredFields = ['host', 'port']; +const optionalFields = [ + 'scheme', + 'path', + 'method', + 'headers', + 'params', + 'auth', + 'body', + 'proxy', + 'connection_timeout', + 'read_timeout', + 'url' +]; + +const allFields = [...requiredFields, ...optionalFields]; + +export class IndexAction extends BaseAction { + constructor(props = {}) { + super(props); + + this.fields = {}; + allFields.forEach((field) => { + this.fields[field] = get(props, field); + }); + } + + get upstreamJson() { + // Add all required fields to the request body + let result = requiredFields.reduce((acc, field) => { + acc[field] = this.fields[field]; + return acc; + }, super.upstreamJson); + + // If optional fields have been set, add them to the body + result = optionalFields.reduce((acc, field) => { + if (this[field]) { + acc[field] = this.fields[field]; + } + return acc; + }, result); + + return result; + } + + get description() { + return i18n.translate('xpack.watcher.models.indexAction.description', { + defaultMessage: 'The {index} will be indexed as {docType}', + values: { + index: this.fields.index, + docType: this.fields.doc_type, + } + }); + } + + get simulateMessage() { + return i18n.translate('xpack.watcher.models.indexAction.simulateMessage', { + defaultMessage: 'Index {index} has been indexed.', + values: { + index: this.index, + } + }); + } + + get simulateFailMessage() { + return i18n.translate('xpack.watcher.models.indexAction.simulateFailMessage', { + defaultMessage: 'Failed to index {index}.', + values: { + index: this.index + } + }); + } + + static fromUpstreamJson(upstreamAction) { + return new IndexAction(upstreamAction); + } +} diff --git a/x-pack/plugins/watcher/public/models/action/jira.action.js b/x-pack/plugins/watcher/public/models/action/jira.action.js new file mode 100644 index 0000000000000..e5b8c1a2ab7d9 --- /dev/null +++ b/x-pack/plugins/watcher/public/models/action/jira.action.js @@ -0,0 +1,70 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + + +import { get } from 'lodash'; +import { BaseAction } from './base_action'; +import { i18n } from '@kbn/i18n'; + +const requiredFields = ['fields']; +const optionalFields = ['account', 'proxy']; + +const allFields = [...requiredFields, ...optionalFields]; + +export class JiraAction extends BaseAction { + constructor(props = {}) { + super(props); + + this.fields = {}; + allFields.forEach((field) => { + this.fields[field] = get(props, field); + }); + } + + get upstreamJson() { + // Add all required fields to the request body + let result = requiredFields.reduce((acc, field) => { + acc[field] = this.fields[field]; + return acc; + }, super.upstreamJson); + + // If optional fields have been set, add them to the body + result = optionalFields.reduce((acc, field) => { + if (this[field]) { + acc[field] = this.fields[field]; + } + return acc; + }, result); + + return result; + } + + get description() { + return i18n.translate('xpack.watcher.models.jiraAction.description', { + defaultMessage: '{issueName} will be created in Jira', + values: { + issueName: get(this.fields, 'fields.issue.issuetype.name', ''), + } + }); + } + + get simulateMessage() { + return i18n.translate('xpack.watcher.models.jiraAction.simulateMessage', { + defaultMessage: 'Jira issue has been created.', + }); + } + + get simulateFailMessage() { + return i18n.translate('xpack.watcher.models.jiraAction.simulateFailMessage', { + defaultMessage: 'Failed to create Jira issue.', + }); + } + + static fromUpstreamJson(upstreamAction) { + return new JiraAction(upstreamAction); + } +} + diff --git a/x-pack/plugins/watcher/public/models/action/pagerduty.action.js b/x-pack/plugins/watcher/public/models/action/pagerduty.action.js new file mode 100644 index 0000000000000..d8a497cb3236f --- /dev/null +++ b/x-pack/plugins/watcher/public/models/action/pagerduty.action.js @@ -0,0 +1,80 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + + +import { get } from 'lodash'; +import { BaseAction } from './base_action'; +import { i18n } from '@kbn/i18n'; + +const requiredFields = ['description', 'type']; +const optionalFields = [ + 'event_type', + 'incident_key', + 'client', + 'client_url', + 'attach_payload', + 'contexts', + 'proxy', + 'href', + 'src', +]; + +const allFields = [...requiredFields, ...optionalFields]; + +export class PagerDutyAction extends BaseAction { + constructor(props = {}) { + super(props); + + this.fields = {}; + allFields.forEach((field) => { + this.fields[field] = get(props, field); + }); + } + + get upstreamJson() { + // Add all required fields to the request body + let result = requiredFields.reduce((acc, field) => { + acc[field] = this.fields[field]; + return acc; + }, super.upstreamJson); + + // If optional fields have been set, add them to the body + result = optionalFields.reduce((acc, field) => { + if (this.fields[field]) { + acc[field] = this.fields[field]; + } + return acc; + }, result); + + return result; + } + + get description() { + return i18n.translate('xpack.watcher.models.pagerDutyAction.description', { + defaultMessage: '{description} will be sent to PagerDuty', + values: { + description: this.fields.description, + } + }); + } + + get simulateMessage() { + return i18n.translate('xpack.watcher.models.pagerDutyAction.simulateMessage', { + defaultMessage: 'PagerDuty event has been sent.', + }); + } + + get simulateFailMessage() { + return i18n.translate('xpack.watcher.models.pagerDutyAction.simulateFailMessage', { + defaultMessage: 'Failed to send Hipchat event.', + }); + } + + static fromUpstreamJson(upstreamAction) { + return new PagerDutyAction(upstreamAction); + } +} + diff --git a/x-pack/plugins/watcher/public/models/action/webhook.action.js b/x-pack/plugins/watcher/public/models/action/webhook.action.js index 1ca2a3f1f3c43..8c530c9913538 100644 --- a/x-pack/plugins/watcher/public/models/action/webhook.action.js +++ b/x-pack/plugins/watcher/public/models/action/webhook.action.js @@ -30,24 +30,26 @@ export class WebhookAction extends BaseAction { constructor(props = {}) { super(props); + this.fields = {}; allFields.forEach((field) => { - this[field] = get(props, field); + this.fields[field] = get(props, field); }); - this.fullPath = this.url ? this.url : this.host + this.port + this.path; + const { url, host, port, path } = this.fields; + this.fullPath = url ? url : host + port + path; } get upstreamJson() { // Add all required fields to the request body let result = requiredFields.reduce((acc, field) => { - acc[field] = this[field]; + acc[field] = this.fields[field]; return acc; }, super.upstreamJson); // If optional fields have been set, add them to the body result = optionalFields.reduce((acc, field) => { if (this[field]) { - acc[field] = this[field]; + acc[field] = this.fields[field]; } return acc; }, result); @@ -86,27 +88,4 @@ export class WebhookAction extends BaseAction { static fromUpstreamJson(upstreamAction) { return new WebhookAction(upstreamAction); } - - /** - * NOTE: - * - * I don't seem to find in the UI where those static properties are actuall used. - * It looks like we used to have a UI to create an action and that currently we only have - * the "advanced watcher" creation through the JSON editor. - * - * ---> ./components/watch_actions/components/watch_action/watch_actions.html - * is where it seems that this is read. But I can't access that component navigatint the UI - * - */ - // static typeName = i18n.translate('xpack.watcher.models.webhookAction.typeName', { - // defaultMessage: 'E-mail', - // }); - // static iconClass = 'kuiIcon fa-envelope-o'; - // static template = ''; - // static selectMessage = i18n.translate('xpack.watcher.models.webhookAction.selectMessageText', { - // defaultMessage: 'Send out an e-mail from your server.', - // }); - // static simulatePrompt = i18n.translate('xpack.watcher.models.webhookAction.simulateButtonLabel', { - // defaultMessage: 'Test fire an e-mail now' - // }); }