Skip to content

Commit

Permalink
[Watcher] Support scheme field when creating a Threshold alert with a…
Browse files Browse the repository at this point in the history
… Webhook action (#53757) (#54459)

Co-authored-by: Jimmy Kuang <[email protected]>
  • Loading branch information
alisonelizabeth and jkelastic authored Jan 10, 2020
1 parent 5d5e5b8 commit 4560a79
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ export type TestSubjects =
| 'webhookPathInput'
| 'webhookPortInput'
| 'webhookMethodSelect'
| 'webhookSchemeSelect'
| 'webhookUsernameInput';
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
const METHOD = 'put';
const HOST = 'localhost';
const PORT = '9200';
const SCHEME = 'http';
const PATH = '/test';
const USERNAME = 'test_user';
const PASSWORD = 'test_password';
Expand All @@ -510,6 +511,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
form.setInputValue('webhookMethodSelect', METHOD);
form.setInputValue('webhookHostInput', HOST);
form.setInputValue('webhookPortInput', PORT);
form.setInputValue('webhookSchemeSelect', SCHEME);
form.setInputValue('webhookPathInput', PATH);
form.setInputValue('webhookUsernameInput', USERNAME);
form.setInputValue('webhookPasswordInput', PASSWORD);
Expand All @@ -534,6 +536,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
method: METHOD,
host: HOST,
port: Number(PORT),
scheme: SCHEME,
path: PATH,
body:
'{\n "message": "Watch [{{ctx.metadata.name}}] has exceeded the threshold"\n}', // Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class WebhookAction extends BaseAction {
this.method = props.method;
this.host = props.host;
this.port = props.port;
this.scheme = props.scheme;
this.path = props.path;
this.body = props.body;
this.contentType = props.contentType;
Expand All @@ -30,6 +31,7 @@ export class WebhookAction extends BaseAction {
method: this.method,
host: this.host,
port: this.port,
scheme: this.scheme,
path: this.path,
body: this.body,
contentType: this.contentType,
Expand All @@ -47,6 +49,7 @@ export class WebhookAction extends BaseAction {
method: json.method,
host: json.host,
port: json.port,
scheme: json.scheme,
path: json.path,
body: json.body,
contentType: json.contentType,
Expand All @@ -72,6 +75,10 @@ export class WebhookAction extends BaseAction {
optionalFields.method = this.method;
}

if (this.scheme) {
optionalFields.scheme = this.scheme;
}

if (this.body) {
optionalFields.body = this.body;
}
Expand Down Expand Up @@ -108,7 +115,7 @@ export class WebhookAction extends BaseAction {
const webhookJson = json && json.actionJson && json.actionJson.webhook;
const { errors } = this.validateJson(json.actionJson);

const { path, method, body, auth, headers } = webhookJson;
const { path, method, scheme, body, auth, headers } = webhookJson;

const optionalFields = {};

Expand All @@ -120,6 +127,10 @@ export class WebhookAction extends BaseAction {
optionalFields.method = method;
}

if (scheme) {
optionalFields.scheme = scheme;
}

if (body) {
optionalFields.body = body;
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/watcher/common/types/action_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface WebhookAction extends BaseAction {
method?: 'head' | 'get' | 'post' | 'put' | 'delete';
host: string;
port: number;
scheme?: 'http' | 'https';
path?: string;
body?: string;
username?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@ import { i18n } from '@kbn/i18n';
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', props.ignoreDefaults ? null : defaultJson);

this.method = get(props, 'method');
this.host = get(props, 'host');
this.port = get(props, 'port');
this.scheme = get(props, 'scheme', 'http');
this.path = get(props, 'path');
this.username = get(props, 'username');
this.password = get(props, 'password');
this.contentType = get(props, 'contentType');

this.fullPath = `${this.host}:${this.port}${this.path}`;
this.fullPath = `${this.host}:${this.port}${this.path ? '/' + this.path : ''}`;
}

validate() {
Expand Down Expand Up @@ -112,6 +110,7 @@ export class WebhookAction extends BaseAction {
method: this.method,
host: this.host,
port: this.port,
scheme: this.scheme,
path: this.path,
body: this.body,
username: this.username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ interface Props {

const HTTP_VERBS = ['head', 'get', 'post', 'put', 'delete'];

const SCHEME = ['http', 'https'];

export const WebhookActionFields: React.FunctionComponent<Props> = ({
action,
editAction,
errors,
hasErrors,
}) => {
const { method, host, port, path, body, username, password } = action;
const { method, host, port, scheme, path, body, username, password } = action;

useEffect(() => {
editAction({ key: 'contentType', value: 'application/json' }); // set content-type for threshold watch to json by default
Expand Down Expand Up @@ -65,6 +67,27 @@ export const WebhookActionFields: React.FunctionComponent<Props> = ({
</EuiFormRow>
</EuiFlexItem>

<EuiFlexItem>
<EuiFormRow
label={i18n.translate(
'xpack.watcher.sections.watchEdit.threshold.webhookAction.schemeFieldLabel',
{
defaultMessage: 'Scheme',
}
)}
>
<EuiSelect
name="scheme"
value={scheme}
data-test-subj="webhookSchemeSelect"
options={SCHEME.map(verb => ({ text: verb, value: verb }))}
onChange={e => {
editAction({ key: 'scheme', value: e.target.value });
}}
/>
</EuiFormRow>
</EuiFlexItem>

<EuiFlexItem>
<ErrableFormRow
id="webhookHost"
Expand Down

0 comments on commit 4560a79

Please sign in to comment.