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

[RAM][HTTP Versioning] Version Internal and Public Find Rules Route #180654

Merged
merged 10 commits into from
Apr 30, 2024
15 changes: 15 additions & 0 deletions x-pack/plugins/alerting/common/routes/rule/apis/find/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 { findRulesRequestQuerySchema } from './schemas/latest';
export type { FindRulesRequestQuery, FindRulesResponse } from './types/latest';

export { findRulesRequestQuerySchema as findRulesRequestQuerySchemaV1 } from './schemas/v1';
export type {
FindRulesRequestQuery as FindRulesRequestQueryV1,
FindRulesResponse as FindRulesResponseV1,
} from './types/latest';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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 * from './v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { schema } from '@kbn/config-schema';

export const findRulesRequestQuerySchema = schema.object({
per_page: schema.number({ defaultValue: 10, min: 0 }),
page: schema.number({ defaultValue: 1, min: 1 }),
search: schema.maybe(schema.string()),
default_search_operator: schema.oneOf([schema.literal('OR'), schema.literal('AND')], {
defaultValue: 'OR',
}),
search_fields: schema.maybe(schema.oneOf([schema.arrayOf(schema.string()), schema.string()])),
sort_field: schema.maybe(schema.string()),
sort_order: schema.maybe(schema.oneOf([schema.literal('asc'), schema.literal('desc')])),
has_reference: schema.maybe(
// use nullable as maybe is currently broken
// in config-schema
schema.nullable(
schema.object({
type: schema.string(),
id: schema.string(),
})
)
),
fields: schema.maybe(schema.arrayOf(schema.string())),
filter: schema.maybe(schema.string()),
filter_consumers: schema.maybe(schema.arrayOf(schema.string())),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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 * from './v1';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 type { TypeOf } from '@kbn/config-schema';
import { RuleParamsV1, RuleResponseV1 } from '../../../response';
import { findRulesRequestQuerySchemaV1 } from '..';

export type FindRulesRequestQuery = TypeOf<typeof findRulesRequestQuerySchemaV1>;

export interface FindRulesResponse<Params extends RuleParamsV1 = never> {
body: {
page: number;
per_page: number;
total: number;
data: Array<Partial<RuleResponseV1<Params>>>;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,38 @@
* 2.0.
*/

import { RulesClient, ConstructorOptions } from '../rules_client';
import { RulesClient, ConstructorOptions } from '../../../../rules_client/rules_client';
import {
savedObjectsClientMock,
loggingSystemMock,
savedObjectsRepositoryMock,
uiSettingsServiceMock,
} from '@kbn/core/server/mocks';
import { taskManagerMock } from '@kbn/task-manager-plugin/server/mocks';
import { ruleTypeRegistryMock } from '../../rule_type_registry.mock';
import { alertingAuthorizationMock } from '../../authorization/alerting_authorization.mock';
import { ruleTypeRegistryMock } from '../../../../rule_type_registry.mock';
import { alertingAuthorizationMock } from '../../../../authorization/alerting_authorization.mock';
import { nodeTypes, fromKueryExpression } from '@kbn/es-query';
import { encryptedSavedObjectsMock } from '@kbn/encrypted-saved-objects-plugin/server/mocks';
import { actionsAuthorizationMock } from '@kbn/actions-plugin/server/mocks';
import { AlertingAuthorization } from '../../authorization/alerting_authorization';
import { AlertingAuthorization } from '../../../../authorization/alerting_authorization';
import { ActionsAuthorization } from '@kbn/actions-plugin/server';
import { auditLoggerMock } from '@kbn/security-plugin/server/audit/mocks';
import { getBeforeSetup, setGlobalDate } from './lib';
import { RecoveredActionGroup } from '../../../common';
import { RegistryRuleType } from '../../rule_type_registry';
import { getBeforeSetup, setGlobalDate } from '../../../../rules_client/tests/lib';
import { RecoveredActionGroup } from '../../../../../common';
import { RegistryRuleType } from '../../../../rule_type_registry';
import { schema } from '@kbn/config-schema';
import { enabledRule1, enabledRule2, siemRule1, siemRule2 } from './test_helpers';
import { formatLegacyActions } from '../lib';
import { ConnectorAdapterRegistry } from '../../connector_adapters/connector_adapter_registry';
import { RULE_SAVED_OBJECT_TYPE } from '../../saved_objects';
import { backfillClientMock } from '../../backfill_client/backfill_client.mock';
import {
enabledRule1,
enabledRule2,
siemRule1,
siemRule2,
} from '../../../../rules_client/tests/test_helpers';
import { formatLegacyActions } from '../../../../rules_client/lib';
import { ConnectorAdapterRegistry } from '../../../../connector_adapters/connector_adapter_registry';
import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects';
import { backfillClientMock } from '../../../../backfill_client/backfill_client.mock';

jest.mock('../lib/siem_legacy_actions/format_legacy_actions', () => {
jest.mock('../../../../rules_client/lib/siem_legacy_actions/format_legacy_actions', () => {
return {
formatLegacyActions: jest.fn(),
};
Expand Down Expand Up @@ -82,7 +87,7 @@ beforeEach(() => {

setGlobalDate();

jest.mock('../common/map_sort_field', () => ({
jest.mock('../../../../rules_client/common/map_sort_field', () => ({
mapSortField: jest.fn(),
}));

Expand Down Expand Up @@ -127,6 +132,10 @@ describe('find()', () => {
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
notifyWhen: 'onActiveAlert',
actions: [
{
Expand Down Expand Up @@ -197,6 +206,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -244,6 +257,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -303,6 +320,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -350,6 +371,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -400,6 +425,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -441,7 +470,9 @@ describe('find()', () => {
test('calls mapSortField', async () => {
const rulesClient = new RulesClient(rulesClientParams);
await rulesClient.find({ options: { sortField: 'name' } });
expect(jest.requireMock('../common/map_sort_field').mapSortField).toHaveBeenCalledWith('name');
expect(
jest.requireMock('../../../../rules_client/common/map_sort_field').mapSortField
).toHaveBeenCalledWith('name');
});

test('should translate filter/sort/search on params to mapped_params', async () => {
Expand Down Expand Up @@ -559,6 +590,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -591,6 +626,10 @@ describe('find()', () => {
bar: true,
parameterThatIsSavedObjectRef: 'soRef_0',
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -649,6 +688,10 @@ describe('find()', () => {
],
"alertTypeId": "myType",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "1",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand All @@ -675,6 +718,10 @@ describe('find()', () => {
],
"alertTypeId": "123",
"createdAt": 2019-02-12T21:01:22.479Z,
"executionStatus": Object {
"lastExecutionDate": 2019-02-12T21:01:22.000Z,
"status": "pending",
},
"id": "2",
"notifyWhen": "onActiveAlert",
"params": Object {
Expand Down Expand Up @@ -779,6 +826,10 @@ describe('find()', () => {
params: {
bar: true,
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -811,6 +862,10 @@ describe('find()', () => {
bar: true,
parameterThatIsSavedObjectRef: 'soRef_0',
},
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notifyWhen: 'onActiveAlert',
Expand Down Expand Up @@ -891,6 +946,10 @@ describe('find()', () => {
type: RULE_SAVED_OBJECT_TYPE,
attributes: {
actions: [],
executionStatus: {
status: 'pending',
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
},
alertTypeId: 'myType',
consumer: 'myApp',
tags: ['myTag'],
Expand Down
Loading