Skip to content

Commit

Permalink
[ResponseOps][Rules] Create the rule params package (elastic#196971)
Browse files Browse the repository at this point in the history
## Summary

This PR creates a package containing the schema of the params of all
rule types. It starts as `schema.recordOf(schema.string(),
schema.maybe(schema.any()))` which is the current one. In subsequent
PRs, the schema will be updated to `schema.oneOf([apmRuleType,
esQueryRuleType, ....])`. I also substituted the definition of `params`
in the alerting plugin with the `params` exported from the package.

Towards: elastic#187356
Fixes: elastic#195183

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)
- [ ] This will appear in the **Release Notes** and follow the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and neptunian committed Oct 23, 2024
1 parent 24f17a2 commit f7eb6fb
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 32 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ packages/kbn-resizable-layout @elastic/kibana-data-discovery
examples/resizable_layout_examples @elastic/kibana-data-discovery
x-pack/test/plugin_functional/plugins/resolver_test @elastic/security-solution
packages/response-ops/feature_flag_service @elastic/response-ops
packages/response-ops/rule_params @elastic/response-ops
examples/response_stream @elastic/ml-ui
packages/kbn-rison @elastic/kibana-operations
x-pack/packages/rollup @elastic/kibana-management
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@
"@kbn/resizable-layout-examples-plugin": "link:examples/resizable_layout_examples",
"@kbn/resolver-test-plugin": "link:x-pack/test/plugin_functional/plugins/resolver_test",
"@kbn/response-ops-feature-flag-service": "link:packages/response-ops/feature_flag_service",
"@kbn/response-ops-rule-params": "link:packages/response-ops/rule_params",
"@kbn/response-stream-plugin": "link:examples/response_stream",
"@kbn/rison": "link:packages/kbn-rison",
"@kbn/rollup": "link:x-pack/packages/rollup",
Expand Down
3 changes: 3 additions & 0 deletions packages/response-ops/rule_params/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/response-ops-rule-params

The package is responsible for the parameters' schema of all rule types. The alerting plugin uses this package to generate OAS documentation for the `params` property in the rule in requests and responses.
23 changes: 23 additions & 0 deletions packages/response-ops/rule_params/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export { ruleParamsSchema, ruleParamsSchemaWithDefaultValue } from './latest';

export {
ruleParamsSchema as ruleParamsSchemaV1,
ruleParamsSchemaWithDefaultValue as ruleParamsSchemaWithDefaultValueV1,
} from './v1';

export type { RuleParams } from './latest';
export type { RuleParamsWithDefaultValue } from './latest';

export type {
RuleParams as RuleParamsV1,
RuleParamsWithDefaultValue as RuleParamsWithDefaultValueV1,
} from './v1';
14 changes: 14 additions & 0 deletions packages/response-ops/rule_params/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../../..',
roots: ['<rootDir>/packages/response-ops/rule_params'],
};
5 changes: 5 additions & 0 deletions packages/response-ops/rule_params/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/response-ops-rule-params",
"owner": "@elastic/response-ops"
}
10 changes: 10 additions & 0 deletions packages/response-ops/rule_params/latest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export * from './v1';
6 changes: 6 additions & 0 deletions packages/response-ops/rule_params/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/response-ops-rule-params",
"private": true,
"version": "1.0.0",
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0"
}
19 changes: 19 additions & 0 deletions packages/response-ops/rule_params/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node"
]
},
"include": [
"**/*.ts",
],
"exclude": [
"target/**/*"
],
"kbn_references": [
"@kbn/config-schema",
]
}
26 changes: 26 additions & 0 deletions packages/response-ops/rule_params/v1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { TypeOf, schema } from '@kbn/config-schema';

export const ruleParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()), {
meta: { description: 'The parameters for the rule.' },
});

export const ruleParamsSchemaWithDefaultValue = schema.recordOf(
schema.string(),
schema.maybe(schema.any()),
{
defaultValue: {},
meta: { description: 'The parameters for the rule.' },
}
);

export type RuleParams = TypeOf<typeof ruleParamsSchema>;
export type RuleParamsWithDefaultValue = TypeOf<typeof ruleParamsSchemaWithDefaultValue>;
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,8 @@
"@kbn/resolver-test-plugin/*": ["x-pack/test/plugin_functional/plugins/resolver_test/*"],
"@kbn/response-ops-feature-flag-service": ["packages/response-ops/feature_flag_service"],
"@kbn/response-ops-feature-flag-service/*": ["packages/response-ops/feature_flag_service/*"],
"@kbn/response-ops-rule-params": ["packages/response-ops/rule_params"],
"@kbn/response-ops-rule-params/*": ["packages/response-ops/rule_params/*"],
"@kbn/response-stream-plugin": ["examples/response_stream"],
"@kbn/response-stream-plugin/*": ["examples/response_stream/*"],
"@kbn/rison": ["packages/kbn-rison"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchemaV1 } from '@kbn/response-ops-rule-params';
import { adHocRunStatus } from '../../../../constants';

export const statusSchema = schema.oneOf([
Expand All @@ -26,7 +27,7 @@ export const backfillResponseSchema = schema.object({
name: schema.string(),
tags: schema.arrayOf(schema.string()),
rule_type_id: schema.string(),
params: schema.recordOf(schema.string(), schema.maybe(schema.any())),
params: ruleParamsSchemaV1,
api_key_owner: schema.nullable(schema.string()),
api_key_created_by_user: schema.maybe(schema.nullable(schema.boolean())),
consumer: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchemaWithDefaultValueV1 } from '@kbn/response-ops-rule-params';
import { validateDurationV1, validateHoursV1, validateTimezoneV1 } from '../../../validation';
import { notifyWhenSchemaV1, alertDelaySchemaV1 } from '../../../response';
import { alertsFilterQuerySchemaV1 } from '../../../../alerts_filter_query';
Expand Down Expand Up @@ -166,10 +167,7 @@ export const createBodySchema = schema.object({
})
)
),
params: schema.recordOf(schema.string(), schema.maybe(schema.any()), {
defaultValue: {},
meta: { description: 'The parameters for the rule.' },
}),
params: ruleParamsSchemaWithDefaultValueV1,
schedule: schema.object(
{
interval: schema.string({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchemaWithDefaultValueV1 } from '@kbn/response-ops-rule-params';
import { validateDurationV1, validateHoursV1, validateTimezoneV1 } from '../../../validation';
import { notifyWhenSchemaV1, alertDelaySchemaV1 } from '../../../response';
import { alertsFilterQuerySchemaV1 } from '../../../../alerts_filter_query';
Expand Down Expand Up @@ -152,10 +153,7 @@ export const updateBodySchema = schema.object({
})
)
),
params: schema.recordOf(schema.string(), schema.any(), {
defaultValue: {},
meta: { description: 'The parameters for the rule.' },
}),
params: ruleParamsSchemaWithDefaultValueV1,
actions: schema.arrayOf(actionSchema, { defaultValue: [] }),
notify_when: schema.maybe(schema.nullable(notifyWhenSchemaV1)),
alert_delay: schema.maybe(alertDelaySchemaV1),
Expand Down
17 changes: 7 additions & 10 deletions x-pack/plugins/alerting/common/routes/rule/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

export {
ruleParamsSchema,
actionParamsSchema,
mappedParamsSchema,
ruleExecutionStatusSchema,
Expand All @@ -18,16 +17,9 @@ export {
scheduleIdsSchema,
} from './schemas/latest';

export type {
RuleParams,
RuleResponse,
RuleSnoozeSchedule,
RuleLastRun,
Monitoring,
} from './types/latest';
export type { RuleResponse, RuleSnoozeSchedule, RuleLastRun, Monitoring } from './types/latest';

export {
ruleParamsSchema as ruleParamsSchemaV1,
actionParamsSchema as actionParamsSchemaV1,
mappedParamsSchema as mappedParamsSchemaV1,
ruleExecutionStatusSchema as ruleExecutionStatusSchemaV1,
Expand All @@ -41,9 +33,14 @@ export {
} from './schemas/v1';

export type {
RuleParams as RuleParamsV1,
RuleResponse as RuleResponseV1,
RuleSnoozeSchedule as RuleSnoozeScheduleV1,
RuleLastRun as RuleLastRunV1,
Monitoring as MonitoringV1,
} from './types/v1';

export { ruleParamsSchemaV1 } from '@kbn/response-ops-rule-params';
export { ruleParamsSchema } from '@kbn/response-ops-rule-params';

export type { RuleParamsV1 } from '@kbn/response-ops-rule-params';
export type { RuleParams } from '@kbn/response-ops-rule-params';
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchemaV1 } from '@kbn/response-ops-rule-params';
import { rRuleResponseSchemaV1 } from '../../../r_rule';
import { alertsFilterQuerySchemaV1 } from '../../../alerts_filter_query';
import {
Expand All @@ -18,9 +19,6 @@ import {
import { validateNotifyWhenV1 } from '../../validation';
import { flappingSchemaV1 } from '../../common';

export const ruleParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()), {
meta: { description: 'The parameters for the rule.' },
});
export const actionParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()), {
meta: {
description:
Expand Down Expand Up @@ -497,7 +495,7 @@ export const ruleResponseSchema = schema.object({
}),
schedule: intervalScheduleSchema,
actions: schema.arrayOf(actionSchema),
params: ruleParamsSchema,
params: ruleParamsSchemaV1,
mapped_params: schema.maybe(mappedParamsSchema),
scheduled_task_id: schema.maybe(
schema.string({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@
*/

import type { TypeOf } from '@kbn/config-schema';
import { RuleParamsV1 } from '@kbn/response-ops-rule-params';
import {
ruleParamsSchemaV1,
ruleResponseSchemaV1,
ruleSnoozeScheduleSchemaV1,
ruleLastRunSchemaV1,
monitoringSchemaV1,
} from '..';

export type RuleParams = TypeOf<typeof ruleParamsSchemaV1>;
export type RuleSnoozeSchedule = TypeOf<typeof ruleSnoozeScheduleSchemaV1>;
export type RuleLastRun = TypeOf<typeof ruleLastRunSchemaV1>;
export type Monitoring = TypeOf<typeof monitoringSchemaV1>;

type RuleResponseSchemaType = TypeOf<typeof ruleResponseSchemaV1>;

export interface RuleResponse<Params extends RuleParams = never> {
export interface RuleResponse<Params extends RuleParamsV1 = never> {
id: RuleResponseSchemaType['id'];
enabled: RuleResponseSchemaType['enabled'];
name: RuleResponseSchemaType['name'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchema } from '@kbn/response-ops-rule-params';
import { adHocRunStatus } from '../../../../../common/constants';

export const statusSchema = schema.oneOf([
Expand All @@ -32,7 +33,7 @@ export const backfillSchema = schema.object({
name: schema.string(),
tags: schema.arrayOf(schema.string()),
alertTypeId: schema.string(),
params: schema.recordOf(schema.string(), schema.maybe(schema.any())),
params: ruleParamsSchema,
apiKeyOwner: schema.nullable(schema.string()),
apiKeyCreatedByUser: schema.maybe(schema.nullable(schema.boolean())),
consumer: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchemaWithDefaultValue } from '@kbn/response-ops-rule-params';
import { validateDuration } from '../../../validation';
import {
notifyWhenSchema,
Expand All @@ -23,7 +24,7 @@ export const createRuleDataSchema = schema.object(
consumer: schema.string(),
tags: schema.arrayOf(schema.string(), { defaultValue: [] }),
throttle: schema.maybe(schema.nullable(schema.string({ validate: validateDuration }))),
params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { defaultValue: {} }),
params: ruleParamsSchemaWithDefaultValue,
schedule: schema.object({
interval: schema.string({ validate: validateDuration }),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchemaWithDefaultValue } from '@kbn/response-ops-rule-params';
import { validateDuration } from '../../../validation';
import {
notifyWhenSchema,
Expand All @@ -23,7 +24,7 @@ export const updateRuleDataSchema = schema.object(
interval: schema.string({ validate: validateDuration }),
}),
throttle: schema.maybe(schema.nullable(schema.string({ validate: validateDuration }))),
params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { defaultValue: {} }),
params: ruleParamsSchemaWithDefaultValue,
actions: schema.arrayOf(actionRequestSchema, { defaultValue: [] }),
systemActions: schema.maybe(schema.arrayOf(systemActionRequestSchema, { defaultValue: [] })),
notifyWhen: schema.maybe(schema.nullable(notifyWhenSchema)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { ruleParamsSchema } from '@kbn/response-ops-rule-params';
import {
ruleLastRunOutcomeValues,
ruleExecutionStatusValues,
Expand All @@ -18,7 +19,6 @@ import { notifyWhenSchema } from './notify_when_schema';
import { actionSchema, systemActionSchema } from './action_schemas';
import { flappingSchema } from './flapping_schema';

export const ruleParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()));
export const mappedParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()));

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

import { TypeOf } from '@kbn/config-schema';
import { ruleParamsSchema } from '@kbn/response-ops-rule-params';
import {
ruleNotifyWhen,
ruleLastRunOutcomeValues,
Expand All @@ -14,7 +15,6 @@ import {
ruleExecutionStatusWarningReason,
} from '../constants';
import {
ruleParamsSchema,
snoozeScheduleSchema,
ruleExecutionStatusSchema,
ruleLastRunSchema,
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/alerting/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"@kbn/core-security-server",
"@kbn/core-http-server",
"@kbn/zod",
"@kbn/core-saved-objects-base-server-internal"
"@kbn/core-saved-objects-base-server-internal",
"@kbn/response-ops-rule-params"
],
"exclude": [
"target/**/*"
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6169,6 +6169,10 @@
version "0.0.0"
uid ""

"@kbn/response-ops-rule-params@link:packages/response-ops/rule_params":
version "0.0.0"
uid ""

"@kbn/response-stream-plugin@link:examples/response_stream":
version "0.0.0"
uid ""
Expand Down

0 comments on commit f7eb6fb

Please sign in to comment.