Skip to content

Commit

Permalink
adjust logic that determines if actions are enabled/disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Apr 26, 2019
1 parent 03b58c0 commit 85fc8df
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
*/

import { ELASTIC_WEBSITE_URL, DOC_LINK_VERSION } from 'ui/documentation_links';
import { ACTION_TYPES } from '../../../common/constants';

const esBase = `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/reference/${DOC_LINK_VERSION}`;
const esStackBase = `${ELASTIC_WEBSITE_URL}guide/en/elastic-stack-overview/${DOC_LINK_VERSION}`;

export const putWatchApiUrl = `${esBase}/watcher-api-put-watch.html`;
export const executeWatchApiUrl = `${esBase}/watcher-api-execute-watch.html#watcher-api-execute-watch-action-mode`;
export const watchNotificationSettingsUrl = `${esBase}/notification-settings.html#slack-notification-settings`;

export const watchActionsConfigurationMap = {
[ACTION_TYPES.SLACK]: `${esStackBase}/actions-slack.html#configuring-slack`,
[ACTION_TYPES.PAGERDUTY]: `${esStackBase}/actions-pagerduty.html#configuring-pagerduty`,
[ACTION_TYPES.JIRA]: `${esStackBase}/actions-jira.html#configuring-jira`,
};
14 changes: 10 additions & 4 deletions x-pack/plugins/watcher/public/models/action/email_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ import chrome from 'ui/chrome';
export class EmailAction extends BaseAction {
constructor(props = {}) {
super(props);

const uiSettings = chrome.getUiSettingsClient();
const defaultToEmail = uiSettings.get('xPack:defaultAdminEmail') || undefined;
const toArray = get(props, 'to', defaultToEmail);
this.to = isArray(toArray) ? toArray : toArray && [ toArray ];
this.subject = get(props, 'subject');

const defaultSubject = i18n.translate('xpack.watcher.models.emailAction.defaultSubjectText', {
defaultMessage: 'Watch [{context}] has exceeded the threshold',
values: {
context: '{{ctx.metadata.name}}',
}
});
this.subject = get(props, 'subject', defaultSubject);

this.body = get(props, 'body');
}

Expand Down Expand Up @@ -95,7 +104,4 @@ export class EmailAction extends BaseAction {
static simulatePrompt = i18n.translate('xpack.watcher.models.emailAction.simulateButtonLabel', {
defaultMessage: 'Test fire an email now'
});
static defaults = {
subject: 'Watch [{{ctx.metadata.name}}] has exceeded the threshold'
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class IndexAction extends BaseAction {
return new IndexAction(upstreamAction);
}

static defaults = {};
static typeName = i18n.translate('xpack.watcher.models.indexAction.typeName', {
defaultMessage: 'Index',
});
Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugins/watcher/public/models/action/jira_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ import { i18n } from '@kbn/i18n';
export class JiraAction extends BaseAction {
constructor(props = {}) {
super(props);

const defaultSummary = i18n.translate('xpack.watcher.models.jiraAction.defaultSummaryText', {
defaultMessage: 'Watch [{context}] has exceeded the threshold',
values: {
context: '{{ctx.metadata.name}}',
}
});
this.summary = get(props, 'summary', defaultSummary);

this.account = get(props, 'account');
this.projectKey = get(props, 'projectKey');
this.issueType = get(props, 'issueType');
this.summary = get(props, 'summary');
}

validate() {
Expand Down Expand Up @@ -98,7 +106,4 @@ export class JiraAction extends BaseAction {
static simulatePrompt = i18n.translate('xpack.watcher.models.jiraAction.simulateButtonLabel', {
defaultMessage: 'Create a sample Jira issue now'
});
static defaults = {
summary: 'Watch [{{ctx.metadata.name}}] has exceeded the threshold'
};
}
12 changes: 7 additions & 5 deletions x-pack/plugins/watcher/public/models/action/logging_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ export class LoggingAction extends BaseAction {
constructor(props = {}) {
super(props);

this.text = get(props, 'text');
const defaultText = i18n.translate('xpack.watcher.models.loggingAction.defaultText', {
defaultMessage: 'Watch [{context}] has exceeded the threshold',
values: {
context: '{{ctx.metadata.name}}',
}
});
this.text = get(props, 'text', defaultText);
}

validate() {
Expand Down Expand Up @@ -55,10 +61,6 @@ export class LoggingAction extends BaseAction {
});
}

static defaults = {
text: 'Watch [{{ctx.metadata.name}}] has exceeded the threshold'
};

static fromUpstreamJson(upstreamAction) {
return new LoggingAction(upstreamAction);
}
Expand Down
12 changes: 8 additions & 4 deletions x-pack/plugins/watcher/public/models/action/pagerduty_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import { i18n } from '@kbn/i18n';
export class PagerDutyAction extends BaseAction {
constructor(props = {}) {
super(props);
this.description = get(props, 'description');

const defaultDescription = i18n.translate('xpack.watcher.models.pagerdutyAction.defaultDescriptionText', {
defaultMessage: 'Watch [{context}] has exceeded the threshold',
values: {
context: '{{ctx.metadata.name}}',
}
});
this.description = get(props, 'description', defaultDescription);
}

validate() {
Expand Down Expand Up @@ -69,8 +76,5 @@ export class PagerDutyAction extends BaseAction {
static simulatePrompt = i18n.translate('xpack.watcher.models.pagerDutyAction.simulateButtonLabel', {
defaultMessage: 'Test fire a PagerDuty event'
});
static defaults = {
description: 'Watch [{{ctx.metadata.name}}] has exceeded the threshold'
};
}

13 changes: 8 additions & 5 deletions x-pack/plugins/watcher/public/models/action/slack_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ export class SlackAction extends BaseAction {

const toArray = get(props, 'to');
this.to = isArray(toArray) ? toArray : toArray && [ toArray ];
this.text = props.text;

const defaultText = i18n.translate('xpack.watcher.models.slackAction.defaultText', {
defaultMessage: 'Watch [{context}] has exceeded the threshold',
values: {
context: '{{ctx.metadata.name}}',
}
});
this.text = props.text || defaultText;
}

validate() {
Expand Down Expand Up @@ -66,10 +73,6 @@ export class SlackAction extends BaseAction {
});
}

static defaults = {
text: 'Watch [{{ctx.metadata.name}}] has exceeded the threshold'
}

static fromUpstreamJson(upstreamAction) {
return new SlackAction(upstreamAction);
}
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/watcher/public/models/action/webhook_action.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ export class WebhookAction extends BaseAction {
constructor(props = {}) {
super(props);

const defaultJson = JSON.stringify({ message: 'Watch [{{ctx.metadata.name}}] has exceeded the threshold' }, null, 2);
this.body = get(props, 'body', defaultJson);

this.method = get(props, 'method');
this.host = get(props, 'host');
this.port = get(props, 'port');
this.path = get(props, 'path');
this.body = get(props, 'body');

this.fullPath = `${this.host}${this.port}${this.path}`;
}

Expand Down Expand Up @@ -99,9 +102,6 @@ export class WebhookAction extends BaseAction {
return new WebhookAction(upstreamAction);
}

static defaults = {
body: JSON.stringify({ message: 'Watch [{{ctx.metadata.name}}] has exceeded the threshold' }, null, 2)
};
static typeName = i18n.translate('xpack.watcher.models.webhookAction.typeName', {
defaultMessage: 'Webhook',
});
Expand Down
10 changes: 3 additions & 7 deletions x-pack/plugins/watcher/public/models/watch/base_watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { getSearchValue } from 'plugins/watcher/lib/get_search_value';
import { get, isEqual, remove, map, merge } from 'lodash';
import { get, isEqual, remove, map } from 'lodash';
import { Action } from '../action';
import { WatchStatus } from '../watch_status';
import { WatchErrors } from '../watch_errors';
Expand Down Expand Up @@ -41,7 +41,7 @@ export class BaseWatch {
this.watchStatus = watchStatus;
}

createAction = (type, defaults) => {
createAction = (type) => {
const ActionTypes = Action.getActionTypes();
const ActionType = ActionTypes[type];

Expand All @@ -55,11 +55,7 @@ export class BaseWatch {
}

const id = createActionId(this.actions, type);
const props = merge(
{},
defaults,
{ id, type }
);
const props = { id, type };

const action = new ActionType(props);
this.addAction(action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ interface Props {
editAction: (changedProperty: { key: string; value: any }) => void;
errors: { [key: string]: string[] };
hasErrors: boolean;
children: React.ReactNode;
}

export const JiraActionFields: React.FunctionComponent<Props> = ({
action,
editAction,
errors,
hasErrors,
children,
}) => {
const { account, projectKey, issueType, summary } = action;

return (
<Fragment>
{children}
<EuiFormRow
fullWidth
label={i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ interface Props {
editAction: (changedProperty: { key: string; value: string }) => void;
errors: { [key: string]: string[] };
hasErrors: boolean;
children: React.ReactNode;
}

export const PagerDutyActionFields: React.FunctionComponent<Props> = ({
errors,
hasErrors,
action,
editAction,
children,
}) => {
const { description } = action;
return (
<Fragment>
{children}
<ErrableFormRow
id="pagerDutyDescription"
errorKey="description"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ import { SlackAction } from '../../../../../../common/types/action_types';
interface Props {
action: SlackAction;
editAction: (changedProperty: { key: string; value: any }) => void;
children: React.ReactNode;
}

export const SlackActionFields: React.FunctionComponent<Props> = ({ action, editAction }) => {
export const SlackActionFields: React.FunctionComponent<Props> = ({
action,
editAction,
children,
}) => {
const { text, to } = action;
const toOptions = to ? to.map(label => ({ label })) : [];

return (
<Fragment>
{children}
<EuiFormRow
fullWidth
label={i18n.translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ import {
EuiIcon,
EuiTitle,
EuiForm,
EuiCallOut,
EuiLink,
EuiText,
EuiSpacer,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { ExecuteDetails } from 'plugins/watcher/models/execute_details/execute_details';
import { Action } from 'plugins/watcher/models/action';
import { toastNotifications } from 'ui/notify';
Expand All @@ -33,6 +38,7 @@ import {
JiraActionFields,
} from './action_fields';
import { executeWatch } from '../../../../lib/api';
import { watchActionsConfigurationMap } from '../../../../lib/documentation_links';

const ActionFieldsComponentMap = {
[ACTION_TYPES.LOGGING]: LoggingActionFields,
Expand All @@ -44,7 +50,17 @@ const ActionFieldsComponentMap = {
[ACTION_TYPES.JIRA]: JiraActionFields,
};

export const WatchActionsAccordion: React.FunctionComponent = () => {
interface Props {
settings: {
actionTypes: {
[key: string]: {
enabled: boolean;
};
};
} | null;
}

export const WatchActionsAccordion: React.FunctionComponent<Props> = ({ settings }) => {
const { watch, setWatchProperty } = useContext(WatchContext);
const { actions } = watch;

Expand Down Expand Up @@ -127,7 +143,46 @@ export const WatchActionsAccordion: React.FunctionComponent = () => {
});
setWatchProperty('actions', updatedActions);
}}
/>
>
{settings && settings.actionTypes[action.type].enabled === false ? (
<Fragment>
<EuiCallOut
title={i18n.translate(
'xpack.watcher.sections.watchEdit.threshold.actions.actionConfigurationWarningTitleText',
{
defaultMessage: 'Account may not be configured.',
}
)}
color="warning"
iconType="help"
>
<EuiText>
<p>
<FormattedMessage
id="xpack.watcher.sections.watchEdit.threshold.actions.actionConfigurationWarningDescriptionText"
defaultMessage="To create this action, at least one {accountType} account must be configured. {docLink}"
values={{
accountType: action.typeName,
docLink: (
<EuiLink
href={watchActionsConfigurationMap[action.type]}
target="_blank"
>
<FormattedMessage
id="xpack.watcher.sections.watchEdit.threshold.actions.actionConfigurationWarningHelpLinkText"
defaultMessage="Learn more."
/>
</EuiLink>
),
}}
/>
</p>
</EuiText>
</EuiCallOut>
<EuiSpacer />
</Fragment>
) : null}
</FieldsComponent>

<EuiButton
type="submit"
Expand Down
Loading

0 comments on commit 85fc8df

Please sign in to comment.