[Watcher] Preserve the watch active status after updates (#61999)

* 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>
This commit is contained in:
Jean-Louis Leysens 2020-04-10 12:28:55 +02:00 committed by GitHub
parent 34b1d0a10d
commit 93971dbfab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 17 deletions

View file

@ -108,6 +108,7 @@ describe('<JsonWatchEdit /> create route', () => {
name: watch.name,
type: watch.type,
isNew: true,
isActive: true,
actions: [
{
id: DEFAULT_LOGGING_ACTION_ID,
@ -185,6 +186,7 @@ describe('<JsonWatchEdit /> create route', () => {
id,
type,
isNew: true,
isActive: true,
actions: [],
watch: defaultWatchJson,
};
@ -246,6 +248,7 @@ describe('<JsonWatchEdit /> create route', () => {
id,
type,
isNew: true,
isActive: true,
actions: [],
watch: defaultWatchJson,
};

View file

@ -244,6 +244,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'logging_1',
@ -314,6 +315,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'index_1',
@ -376,6 +378,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'slack_1',
@ -448,6 +451,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'email_1',
@ -540,6 +544,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'webhook_1',
@ -629,6 +634,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'jira_1',
@ -709,6 +715,7 @@ describe('<ThresholdWatchEdit /> create route', () => {
name: WATCH_NAME,
type: WATCH_TYPES.THRESHOLD,
isNew: true,
isActive: true,
actions: [
{
id: 'pagerduty_1',
@ -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,

View file

@ -98,6 +98,7 @@ describe('<WatchEdit />', () => {
name: EDITED_WATCH_NAME,
type: watch.type,
isNew: false,
isActive: true,
actions: [
{
id: DEFAULT_LOGGING_ACTION_ID,
@ -191,6 +192,7 @@ describe('<WatchEdit />', () => {
name: EDITED_WATCH_NAME,
type,
isNew: false,
isActive: true,
actions: [],
timeField,
triggerIntervalSize,

View file

@ -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);
@ -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),
};
}

View file

@ -174,6 +174,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%>',

View file

@ -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';
@ -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(
{
@ -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: {
@ -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