Skip to content

Commit

Permalink
[Watcher] Add missing Action types on client (elastic#29818)
Browse files Browse the repository at this point in the history
* Add missing Webhook action on client
  • Loading branch information
sebelga committed Feb 4, 2019
1 parent 49706cb commit c085ffa
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/watcher/public/models/action/action.js
Expand Up @@ -9,12 +9,14 @@ import { ACTION_TYPES } from 'plugins/watcher/../common/constants';
import { EmailAction } from './email_action';
import { LoggingAction } from './logging_action';
import { SlackAction } from './slack_action';
import { WebhookAction } from './webhook.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);

export class Action {
static getActionTypes = () => {
Expand Down
112 changes: 112 additions & 0 deletions x-pack/plugins/watcher/public/models/action/webhook.action.js
@@ -0,0 +1,112 @@
/*
* 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 WebhookAction extends BaseAction {
constructor(props = {}) {
super(props);

allFields.forEach((field) => {
this[field] = get(props, field);
});

this.fullPath = this.url ? this.url : this.host + this.port + this.path;
}

get upstreamJson() {
// Add all required fields to the request body
let result = requiredFields.reduce((acc, field) => {
acc[field] = this[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];
}
return acc;
}, result);

return result;
}

get description() {
return i18n.translate('xpack.watcher.models.webhookAction.description', {
defaultMessage: 'Webhook will trigger a {method} request on {fullPath}',
values: {
method: this.method,
fullPath: this.fullPath
}
});
}

get simulateMessage() {
return i18n.translate('xpack.watcher.models.webhookAction.simulateMessage', {
defaultMessage: 'Sample request sent to {fullPath}',
values: {
fullPath: this.fullPath
}
});
}

get simulateFailMessage() {
return i18n.translate('xpack.watcher.models.webhookAction.simulateFailMessage', {
defaultMessage: 'Failed to send request to {fullPath}.',
values: {
fullPath: this.fullPath
}
});
}

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 c085ffa

Please sign in to comment.