Skip to content

Commit 215cd03

Browse files
committed
Add request flyout to Threshold Watch edit form.
- Move action models into common directory. - Remove boom error reporting from action models because this is an unnecessary level of defensiveness since we control the UI that consumes this API. - Make changes to action model valiation which may or may not break the UX. - Add serializeJsonWatch function and consume it within the JSON Watch edit form.
1 parent 2901eab commit 215cd03

File tree

26 files changed

+210
-185
lines changed

26 files changed

+210
-185
lines changed

x-pack/legacy/plugins/watcher/common/constants/watch_types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
export const WATCH_TYPES: { [key: string]: string } = {
88
JSON: 'json',
9-
109
THRESHOLD: 'threshold',
11-
1210
MONITORING: 'monitoring',
1311
};

x-pack/legacy/plugins/watcher/common/lib/serialization/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
export { serializeJsonWatch } from './serialize_json_watch';
78
export { serializeThresholdWatch } from './serialize_threshold_watch';
89
export { buildInput } from './serialization_helpers';
10+
11+
// TODO: Address this stub
12+
export function serializeMonitoringWatch() {
13+
return {};
14+
}

x-pack/legacy/plugins/watcher/common/lib/serialization/serialization_helpers/build_actions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import { forEach } from 'lodash';
8+
import { Action } from '../../../models/action';
89

910
/*
1011
watch.actions
@@ -13,7 +14,8 @@ export function buildActions(actions) {
1314
const result = {};
1415

1516
forEach(actions, (action) => {
16-
Object.assign(result, action.upstreamJson);
17+
const actionModel = Action.fromDownstreamJson(action);
18+
Object.assign(result, actionModel.upstreamJson);
1719
});
1820

1921
return result;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { WATCH_TYPES } from '../../constants';
8+
9+
export function serializeJsonWatch(name, json) {
10+
const serializedWatch = {
11+
...json,
12+
metadata: {
13+
xpack: {
14+
type: WATCH_TYPES.JSON,
15+
}
16+
},
17+
};
18+
19+
if (name) {
20+
serializedWatch.metadata.name = name;
21+
}
22+
23+
return serializedWatch;
24+
}

x-pack/legacy/plugins/watcher/common/lib/serialization/serialize_threshold_watch.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@ import {
1414
buildTrigger,
1515
} from './serialization_helpers';
1616

17-
export function serializeThresholdWatch(watch) {
18-
const {
19-
name,
20-
triggerIntervalSize,
21-
triggerIntervalUnit,
22-
index, timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder,
23-
thresholdComparator, hasTermsAgg, threshold,
24-
actions,
25-
} = watch;
26-
17+
export function serializeThresholdWatch({
18+
name,
19+
triggerIntervalSize,
20+
triggerIntervalUnit,
21+
index,
22+
timeWindowSize,
23+
timeWindowUnit,
24+
timeField,
25+
aggType,
26+
aggField,
27+
termField,
28+
termSize,
29+
termOrder,
30+
thresholdComparator,
31+
hasTermsAgg,
32+
threshold,
33+
actions,
34+
}) {
2735
const serializedWatch = {
2836
trigger: buildTrigger(triggerIntervalSize, triggerIntervalUnit),
2937
input: buildInput({ index, timeWindowSize, timeWindowUnit, timeField, aggType, aggField, termField, termSize, termOrder }),
3038
condition: buildCondition({ aggType, thresholdComparator, hasTermsAgg, threshold }),
3139
transform: buildTransform({ aggType, thresholdComparator, hasTermsAgg, threshold }),
3240
actions: buildActions(actions),
3341
metadata: {
34-
name,
3542
xpack: {
3643
type: WATCH_TYPES.THRESHOLD,
3744
},
@@ -52,5 +59,9 @@ export function serializeThresholdWatch(watch) {
5259
},
5360
};
5461

62+
if (name) {
63+
serializedWatch.metadata.name = name;
64+
}
65+
5566
return serializedWatch;
5667
}

x-pack/legacy/plugins/watcher/server/models/action/action.js renamed to x-pack/legacy/plugins/watcher/common/models/action/action.js

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import { set } from 'lodash';
8-
import { badRequest } from 'boom';
98
import { getActionType } from '../../../common/lib/get_action_type';
109
import { ACTION_TYPES } from '../../../common/constants';
1110
import { LoggingAction } from './logging_action';
@@ -16,7 +15,6 @@ import { WebhookAction } from './webhook_action';
1615
import { PagerDutyAction } from './pagerduty_action';
1716
import { JiraAction } from './jira_action';
1817
import { UnknownAction } from './unknown_action';
19-
import { i18n } from '@kbn/i18n';
2018

2119
const ActionTypes = {};
2220
set(ActionTypes, ACTION_TYPES.LOGGING, LoggingAction);
@@ -34,63 +32,17 @@ export class Action {
3432
}
3533

3634
// From Elasticsearch
37-
static fromUpstreamJson(json, options = { throwExceptions: {} }) {
38-
if (!json.id) {
39-
throw badRequest(
40-
i18n.translate('xpack.watcher.models.actionStatus.idPropertyMissingBadRequestMessage', {
41-
defaultMessage: 'JSON argument must contain an {id} property',
42-
values: {
43-
id: 'id'
44-
}
45-
}),
46-
);
47-
}
48-
49-
if (!json.actionJson) {
50-
throw badRequest(
51-
i18n.translate('xpack.watcher.models.action.actionJsonPropertyMissingBadRequestMessage', {
52-
defaultMessage: 'JSON argument must contain an {actionJson} property',
53-
values: {
54-
actionJson: 'actionJson'
55-
}
56-
}),
57-
);
58-
}
59-
35+
static fromUpstreamJson(json) {
6036
const type = getActionType(json.actionJson);
6137
const ActionType = ActionTypes[type] || UnknownAction;
62-
63-
const { action, errors } = ActionType.fromUpstreamJson(json, options);
64-
const doThrowException = options.throwExceptions.Action !== false;
65-
66-
if (errors && doThrowException) {
67-
this.throwErrors(errors);
68-
}
69-
38+
const { action } = ActionType.fromUpstreamJson(json);
7039
return action;
7140
}
7241

7342
// From Kibana
74-
static fromDownstreamJson(json, options = { throwExceptions: {} }) {
43+
static fromDownstreamJson(json) {
7544
const ActionType = ActionTypes[json.type] || UnknownAction;
76-
77-
const { action, errors } = ActionType.fromDownstreamJson(json);
78-
const doThrowException = options.throwExceptions.Action !== false;
79-
80-
if (errors && doThrowException) {
81-
this.throwErrors(errors);
82-
}
83-
45+
const { action } = ActionType.fromDownstreamJson(json);
8446
return action;
8547
}
86-
87-
static throwErrors(errors) {
88-
const allMessages = errors.reduce((message, error) => {
89-
if (message) {
90-
return `${message}, ${error.message}`;
91-
}
92-
return error.message;
93-
}, '');
94-
throw badRequest(allMessages);
95-
}
9648
}

x-pack/legacy/plugins/watcher/server/models/action/base_action.js renamed to x-pack/legacy/plugins/watcher/common/models/action/base_action.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { badRequest } from 'boom';
8-
import { i18n } from '@kbn/i18n';
9-
107
export class BaseAction {
118
constructor(props, errors) {
129
this.id = props.id;
@@ -35,17 +32,6 @@ export class BaseAction {
3532
}
3633

3734
static getPropsFromUpstreamJson(json) {
38-
if (!json.id) {
39-
throw badRequest(
40-
i18n.translate('xpack.watcher.models.baseAction.idPropertyMissingBadRequestMessage', {
41-
defaultMessage: 'JSON argument must contain an {id} property',
42-
values: {
43-
id: 'id'
44-
}
45-
}),
46-
);
47-
}
48-
4935
return {
5036
id: json.id
5137
};

x-pack/legacy/plugins/watcher/server/models/action/email_action.js renamed to x-pack/legacy/plugins/watcher/common/models/action/email_action.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,9 @@ export class EmailAction extends BaseAction {
108108
code: ERROR_CODES.ERR_PROP_MISSING,
109109
message
110110
});
111-
112-
json.email = {};
113111
}
114112

115-
if (!json.email.to) {
113+
if (json.email && !json.email.to) {
116114
const message = i18n.translate('xpack.watcher.models.emailAction.actionJsonEmailToPropertyMissingBadRequestMessage', {
117115
defaultMessage: 'JSON argument must contain an {actionJsonEmailTo} property',
118116
values: {

0 commit comments

Comments
 (0)