Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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%>',
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