Skip to content

Commit

Permalink
[ResponseOps][MW] Fix bug when creating repeating Maintenance Window (e…
Browse files Browse the repository at this point in the history
…lastic#207084)

Closes elastic#198774

## Summary

- There was a bug when submitting `rrule` with a `byweekday` I fixed
that validation to use a more inclusive regex. `byweekday` can be the
expected `MO`, `TU`, etc but also `-1FR` or `+3SA` where the number
corresponds to the week in a month.
- The model version for the maintenance window was incorrect so when
saving the SO the validation was failing. I fixed that and now we are
allowed to save `number[]` as expected.
- I removed some duplicated code and we now use the `rrule` schema from
the `common` folder
  • Loading branch information
adcoelho authored Jan 24, 2025
1 parent 2c62e26 commit 0df78e6
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
validateStartDateV1,
validateEndDateV1,
createValidateRecurrenceByV1,
validateRecurrenceByWeekdayV1,
} from '../../validation';

export const rRuleRequestSchema = schema.object({
Expand Down Expand Up @@ -40,20 +41,9 @@ export const rRuleRequestSchema = schema.object({
})
),
byweekday: schema.maybe(
schema.arrayOf(
schema.oneOf([
schema.literal('MO'),
schema.literal('TU'),
schema.literal('WE'),
schema.literal('TH'),
schema.literal('FR'),
schema.literal('SA'),
schema.literal('SU'),
]),
{
validate: createValidateRecurrenceByV1('byweekday'),
}
)
schema.arrayOf(schema.string(), {
validate: validateRecurrenceByWeekdayV1,
})
),
bymonthday: schema.maybe(
schema.arrayOf(schema.number({ min: 1, max: 31 }), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
export { validateStartDate } from './validate_start_date/latest';
export { validateEndDate } from './validate_end_date/latest';
export { createValidateRecurrenceBy } from './validate_recurrence_by/latest';
export { validateRecurrenceByWeekday } from './validate_recurrence_by_weekday/latest';

export { validateStartDate as validateStartDateV1 } from './validate_start_date/v1';
export { validateEndDate as validateEndDateV1 } from './validate_end_date/v1';
export { createValidateRecurrenceBy as createValidateRecurrenceByV1 } from './validate_recurrence_by/v1';
export { validateRecurrenceByWeekday as validateRecurrenceByWeekdayV1 } from './validate_recurrence_by_weekday/v1';
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@
* 2.0.
*/

export const validateStartDate = (date: string) => {
const parsedValue = Date.parse(date);
if (isNaN(parsedValue)) return `Invalid date: ${date}`;
return;
};
export * from './v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { validateRecurrenceByWeekday } from './v1';

describe('validateRecurrenceByWeekday', () => {
it('validates empty array', () => {
expect(validateRecurrenceByWeekday([])).toEqual('rRule byweekday cannot be empty');
});

it('validates properly formed byweekday strings', () => {
const weekdays = ['+1MO', '+2TU', '+3WE', '+4TH', '-4FR', '-3SA', '-2SU', '-1MO'];

expect(validateRecurrenceByWeekday(weekdays)).toBeUndefined();
});

it('validates improperly formed byweekday strings', () => {
expect(validateRecurrenceByWeekday(['+1MO', 'FOO', '+3WE', 'BAR', '-4FR'])).toEqual(
'invalid byweekday values in rRule byweekday: FOO,BAR'
);
});

it('validates byweekday strings without recurrence', () => {
expect(validateRecurrenceByWeekday(['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'])).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const validateRecurrenceByWeekday = (array: string[]) => {
if (array.length === 0) {
return 'rRule byweekday cannot be empty';
}

const byWeekDayRegex = new RegExp('^(((\\+|-)[1-4])?(MO|TU|WE|TH|FR|SA|SU))$');
const invalidDays: string[] = [];

array.forEach((day) => {
if (!byWeekDayRegex.test(day)) {
invalidDays.push(day);
}
});

if (invalidDays.length > 0) {
return `invalid byweekday values in rRule byweekday: ${invalidDays.join(',')}`;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
*/

import moment from 'moment-timezone';
import { createMaintenanceWindow } from './create_maintenance_window';
import { CreateMaintenanceWindowParams } from './types';

import {
savedObjectsClientMock,
loggingSystemMock,
uiSettingsServiceMock,
} from '@kbn/core/server/mocks';
import { SavedObject } from '@kbn/core/server';
import { FilterStateStore } from '@kbn/es-query';
import { Frequency } from '@kbn/rrule';

import {
MaintenanceWindowClientContext,
MAINTENANCE_WINDOW_SAVED_OBJECT_TYPE,
} from '../../../../../common';
import { getMockMaintenanceWindow } from '../../../../data/maintenance_window/test_helpers';
import type { MaintenanceWindow } from '../../types';
import { FilterStateStore } from '@kbn/es-query';
import { createMaintenanceWindow } from './create_maintenance_window';
import { CreateMaintenanceWindowParams } from './types';

const savedObjectsClient = savedObjectsClientMock.create();
const uiSettings = uiSettingsServiceMock.createClient();
Expand Down Expand Up @@ -57,6 +60,13 @@ describe('MaintenanceWindowClient - create', () => {

const mockMaintenanceWindow = getMockMaintenanceWindow({
expirationDate: moment(new Date()).tz('UTC').add(1, 'year').toISOString(),
rRule: {
tzid: 'UTC',
dtstart: '2023-02-26T00:00:00.000Z',
freq: Frequency.WEEKLY,
byweekday: ['-4MO', 'TU'],
count: 2,
},
});

savedObjectsClient.create.mockResolvedValueOnce({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

import { schema } from '@kbn/config-schema';
import { rRuleRequestSchema } from '../../../../../../common/routes/r_rule';
import { maintenanceWindowCategoryIdsSchema } from '../../../schemas';
import { rRuleRequestSchema } from '../../../../r_rule/schemas';

import { alertsFilterQuerySchema } from '../../../../alerts_filter_query/schemas';

export const createMaintenanceWindowParamsSchema = schema.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { schema } from '@kbn/config-schema';
import { maintenanceWindowCategoryIdsSchema } from '../../../schemas';
import { rRuleRequestSchema } from '../../../../r_rule/schemas';
import { rRuleRequestSchema } from '../../../../../../common/routes/r_rule';
import { alertsFilterQuerySchema } from '../../../../alerts_filter_query/schemas';

export const updateMaintenanceWindowParamsSchema = schema.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const updatedAttributes = {
dtstart: '2023-03-26T00:00:00.000Z',
freq: Frequency.WEEKLY,
count: 2,
byweekday: ['-1MO', 'WE'],
},
};

Expand Down Expand Up @@ -110,8 +111,8 @@ describe('MaintenanceWindowClient - update', () => {
{
...updatedAttributes,
events: [
{ gte: '2023-03-26T00:00:00.000Z', lte: '2023-03-26T02:00:00.000Z' },
{ gte: '2023-04-01T23:00:00.000Z', lte: '2023-04-02T01:00:00.000Z' }, // Daylight savings
{ gte: '2023-03-26T23:00:00.000Z', lte: '2023-03-27T01:00:00.000Z' },
{ gte: '2023-03-28T23:00:00.000Z', lte: '2023-03-29T01:00:00.000Z' }, // Daylight savings
],
expirationDate: moment(new Date(secondTimestamp)).tz('UTC').add(1, 'year').toISOString(),
createdAt: '2023-02-26T00:00:00.000Z',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
*/

export { rRuleSchema } from './r_rule_schema';
export { rRuleRequestSchema } from './r_rule_request_schema';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
*/

export type { RRule } from './r_rule';
export type { RRuleRequest } from './r_rule_request';

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/
import { schema } from '@kbn/config-schema';
import { rRuleRequestSchema } from '../../../../r_rule/schemas';
import { notifyWhenSchema, actionRequestSchema, systemActionRequestSchema } from '../../../schemas';
import { rRuleRequestSchema } from '../../../../../../common/routes/r_rule';
import { validateDuration } from '../../../validation';
import { validateSnoozeSchedule } from '../validation';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const alertsFilterQuerySchema = schema.object({
dsl: schema.maybe(schema.string()),
});

const rRuleSchema = schema.object({
export const rRuleSchema = schema.object({
dtstart: schema.string(),
tzid: schema.string(),
freq: schema.maybe(
Expand Down Expand Up @@ -55,15 +55,17 @@ const rRuleSchema = schema.object({
schema.literal('SU'),
])
),
byweekday: schema.maybe(schema.arrayOf(schema.oneOf([schema.string(), schema.number()]))),
bymonth: schema.maybe(schema.number()),
bysetpos: schema.maybe(schema.number()),
bymonthday: schema.maybe(schema.number()),
byyearday: schema.maybe(schema.number()),
byweekno: schema.maybe(schema.number()),
byhour: schema.maybe(schema.number()),
byminute: schema.maybe(schema.number()),
bysecond: schema.maybe(schema.number()),
byweekday: schema.maybe(
schema.nullable(schema.arrayOf(schema.oneOf([schema.string(), schema.number()])))
),
bymonth: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
bysetpos: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
bymonthday: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
byyearday: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
byweekno: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
byhour: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
byminute: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
bysecond: schema.maybe(schema.nullable(schema.arrayOf(schema.number()))),
});

const rawMaintenanceWindowEventsSchema = schema.object({
Expand Down
Loading

0 comments on commit 0df78e6

Please sign in to comment.