Skip to content

Commit

Permalink
[Watcher] Preserve the watch active status after updates (elastic#61999
Browse files Browse the repository at this point in the history
…) (elastic#63232)

* Preserve the watch active status after updates

* Use route validation params to set isActive

* Fix Jest test

* Implement PR feedback

- Make the isActive flag required on the save watch endpoint
- Move the isActive state to base_watch and serialize from
  there.

* Fix Jest tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
jloleysens and elasticmachine committed Apr 10, 2020
1 parent f855736 commit 89d24a0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('<JsonWatchEdit /> create route', () => {
name: watch.name,
type: watch.type,
isNew: true,
isActive: true,
actions: [
{
id: DEFAULT_LOGGING_ACTION_ID,
Expand Down Expand Up @@ -185,6 +186,7 @@ describe('<JsonWatchEdit /> create route', () => {
id,
type,
isNew: true,
isActive: true,
actions: [],
watch: defaultWatchJson,
};
Expand Down Expand Up @@ -246,6 +248,7 @@ describe('<JsonWatchEdit /> create route', () => {
id,
type,
isNew: true,
isActive: true,
actions: [],
watch: defaultWatchJson,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'logging_1',
Expand Down Expand Up @@ -314,6 +315,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'index_1',
Expand Down Expand Up @@ -376,6 +378,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'slack_1',
Expand Down Expand Up @@ -448,6 +451,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'email_1',
Expand Down Expand Up @@ -540,6 +544,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'webhook_1',
Expand Down Expand Up @@ -629,6 +634,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'jira_1',
Expand Down Expand Up @@ -709,6 +715,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'pagerduty_1',
Expand Down Expand Up @@ -772,6 +779,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [],
index: MATCH_INDICES,
timeField: WATCH_TIME_FIELD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('<WatchEdit />', () => {
name: EDITED_WATCH_NAME,
type: watch.type,
isNew: false,
isActive: true,
actions: [
{
id: DEFAULT_LOGGING_ACTION_ID,
Expand Down Expand Up @@ -191,6 +192,7 @@ describe('<WatchEdit />', () => {
name: EDITED_WATCH_NAME,
type,
isNew: false,
isActive: true,
actions: [],
timeField,
triggerIntervalSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class BaseWatch {
this.isSystemWatch = Boolean(get(props, 'isSystemWatch'));
this.watchStatus = WatchStatus.fromUpstreamJson(get(props, 'watchStatus'));
this.watchErrors = WatchErrors.fromUpstreamJson(get(props, 'watchErrors'));
this.isActive = this.watchStatus.isActive ?? true;

const actions = get(props, 'actions', []);
this.actions = actions.map(Action.fromUpstreamJson);
Expand Down Expand Up @@ -115,6 +116,7 @@ export class BaseWatch {
name: this.name,
type: this.type,
isNew: this.isNew,
isActive: this.isActive,
actions: map(this.actions, action => action.upstreamJson),
};
}
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/watcher/server/lib/elasticsearch_js_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
name: 'master_timeout',
type: 'duration',
},
active: {
name: 'active',
type: 'boolean',
},
},
url: {
fmt: '/_watcher/watch/<%=id%>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { schema } from '@kbn/config-schema';
import { IScopedClusterClient } from 'kibana/server';
import { i18n } from '@kbn/i18n';
import { WATCH_TYPES } from '../../../../common/constants';
import { serializeJsonWatch, serializeThresholdWatch } from '../../../../common/lib/serialization';
Expand All @@ -21,23 +20,11 @@ const bodySchema = schema.object(
{
type: schema.string(),
isNew: schema.boolean(),
isActive: schema.boolean(),
},
{ unknowns: 'allow' }
);

function fetchWatch(dataClient: IScopedClusterClient, watchId: string) {
return dataClient.callAsCurrentUser('watcher.getWatch', {
id: watchId,
});
}

function saveWatch(dataClient: IScopedClusterClient, id: string, body: any) {
return dataClient.callAsCurrentUser('watcher.putWatch', {
id,
body,
});
}

export function registerSaveRoute(deps: RouteDependencies) {
deps.router.put(
{
Expand All @@ -49,12 +36,16 @@ export function registerSaveRoute(deps: RouteDependencies) {
},
licensePreRoutingFactory(deps, async (ctx, request, response) => {
const { id } = request.params;
const { type, isNew, ...watchConfig } = request.body;
const { type, isNew, isActive, ...watchConfig } = request.body;

const dataClient = ctx.watcher!.client;

// For new watches, verify watch with the same ID doesn't already exist
if (isNew) {
try {
const existingWatch = await fetchWatch(ctx.watcher!.client, id);
const existingWatch = await dataClient.callAsCurrentUser('watcher.getWatch', {
id,
});
if (existingWatch.found) {
return response.conflict({
body: {
Expand Down Expand Up @@ -92,7 +83,11 @@ export function registerSaveRoute(deps: RouteDependencies) {
try {
// Create new watch
return response.ok({
body: await saveWatch(ctx.watcher!.client, id, serializedWatch),
body: await dataClient.callAsCurrentUser('watcher.putWatch', {
id,
active: isActive,
body: serializedWatch,
}),
});
} catch (e) {
// Case: Error from Elasticsearch JS client
Expand Down

0 comments on commit 89d24a0

Please sign in to comment.