Skip to content

Commit

Permalink
[Watcher] Add Index, HipChat, PagerDuty, and Jira Action types on the…
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga authored and cjcenizal committed Feb 12, 2019
1 parent 8ff5ecb commit a590b2f
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 27 deletions.
8 changes: 8 additions & 0 deletions x-pack/plugins/watcher/public/models/action/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ 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 = {};
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 = () => {
Expand Down
69 changes: 69 additions & 0 deletions x-pack/plugins/watcher/public/models/action/hipchat.action.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
88 changes: 88 additions & 0 deletions x-pack/plugins/watcher/public/models/action/index.action.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
70 changes: 70 additions & 0 deletions x-pack/plugins/watcher/public/models/action/jira.action.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

80 changes: 80 additions & 0 deletions x-pack/plugins/watcher/public/models/action/pagerduty.action.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

33 changes: 6 additions & 27 deletions x-pack/plugins/watcher/public/models/action/webhook.action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = '<watch-email-action></watch-email-action>';
// 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'
// });
}

0 comments on commit a590b2f

Please sign in to comment.