Skip to content

Commit

Permalink
Added UI support for the default action group for Alert Type Model (#…
Browse files Browse the repository at this point in the history
…57603)

* Added UI support for the default action group for Alert Type Model

* Fixed set default on  alert type select

* Fixed type check

* Moved setting of default alert type to the server api

* Added default value for actionGroups if it is empty in register alert type functions

* Fixed type check

* Fixed due to comments	aed89377b9	Yuliia Naumenko <yuliia.naumenko@elastic.com>	Feb 20, 2020 at 12:40 PM

* Renamed defaultActionGroup to defaultActionGroupId

* Fixed failing tests
  • Loading branch information
YulNaumenko authored Feb 21, 2020
1 parent d61ef26 commit 1eb0176
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const getLicenseExpiration = (
}),
},
],
defaultActionGroupId: 'default',
async executor({
services,
params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const signalRulesAlertType = ({
}),
},
],
defaultActionGroupId: 'default',
validate: {
params: schema.object({
description: schema.string(),
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
export * from './alert';
export * from './alert_instance';
export * from './alert_task_instance';

export interface ActionGroup {
id: string;
name: string;
}
50 changes: 44 additions & 6 deletions x-pack/plugins/alerting/server/alert_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ describe('has()', () => {
registry.register({
id: 'foo',
name: 'Foo',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
executor: jest.fn(),
});
expect(registry.has('foo')).toEqual(true);
Expand All @@ -39,7 +45,13 @@ describe('register()', () => {
const alertType = {
id: 'test',
name: 'Test',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
executor: jest.fn(),
};
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -64,14 +76,26 @@ describe('register()', () => {
registry.register({
id: 'test',
name: 'Test',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
executor: jest.fn(),
});
expect(() =>
registry.register({
id: 'test',
name: 'Test',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
executor: jest.fn(),
})
).toThrowErrorMatchingInlineSnapshot(`"Alert type \\"test\\" is already registered."`);
Expand All @@ -84,13 +108,25 @@ describe('get()', () => {
registry.register({
id: 'test',
name: 'Test',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
executor: jest.fn(),
});
const alertType = registry.get('test');
expect(alertType).toMatchInlineSnapshot(`
Object {
"actionGroups": Array [],
"actionGroups": Array [
Object {
"id": "default",
"name": "Default",
},
],
"defaultActionGroupId": "default",
"executor": [MockFunction],
"id": "test",
"name": "Test",
Expand Down Expand Up @@ -124,6 +160,7 @@ describe('list()', () => {
name: 'Test Action Group',
},
],
defaultActionGroupId: 'testActionGroup',
executor: jest.fn(),
});
const result = registry.list();
Expand All @@ -136,6 +173,7 @@ describe('list()', () => {
"name": "Test Action Group",
},
],
"defaultActionGroupId": "testActionGroup",
"id": "test",
"name": "Test",
},
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/alert_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class AlertTypeRegistry {
id: alertTypeId,
name: alertType.name,
actionGroups: alertType.actionGroups,
defaultActionGroupId: alertType.defaultActionGroupId,
}));
}
}
12 changes: 11 additions & 1 deletion x-pack/plugins/alerting/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('create()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
async executor() {},
});
});
Expand Down Expand Up @@ -522,7 +523,13 @@ describe('create()', () => {
alertTypeRegistry.get.mockReturnValue({
id: '123',
name: 'Test',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
validate: {
params: schema.object({
param1: schema.string(),
Expand Down Expand Up @@ -1885,6 +1892,7 @@ describe('update()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
async executor() {},
});
});
Expand Down Expand Up @@ -2415,6 +2423,7 @@ describe('update()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
validate: {
params: schema.object({
param1: schema.string(),
Expand Down Expand Up @@ -2647,6 +2656,7 @@ describe('update()', () => {
id: '123',
name: 'Test',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
async executor() {},
});
savedObjectsClient.bulkGet.mockResolvedValueOnce({
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type AlertsClient = PublicMethodsOf<AlertsClientClass>;

export {
AlertType,
ActionGroup,
AlertingPlugin,
AlertExecutorOptions,
AlertActionParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ test('should return passed in params when validation not defined', () => {
{
id: 'my-alert-type',
name: 'My description',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
async executor() {},
},
{
Expand All @@ -27,7 +33,13 @@ test('should validate and apply defaults when params is valid', () => {
{
id: 'my-alert-type',
name: 'My description',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
validate: {
params: schema.object({
param1: schema.string(),
Expand All @@ -50,7 +62,13 @@ test('should validate and throw error when params is invalid', () => {
{
id: 'my-alert-type',
name: 'My description',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
validate: {
params: schema.object({
param1: schema.string(),
Expand Down
24 changes: 21 additions & 3 deletions x-pack/plugins/alerting/server/routes/list_alert_types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ describe('listAlertTypesRoute', () => {
{
id: '1',
name: 'name',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
},
];

Expand All @@ -50,7 +56,13 @@ describe('listAlertTypesRoute', () => {
Object {
"body": Array [
Object {
"actionGroups": Array [],
"actionGroups": Array [
Object {
"id": "default",
"name": "Default",
},
],
"defaultActionGroupId": "default",
"id": "1",
"name": "name",
},
Expand Down Expand Up @@ -128,7 +140,13 @@ describe('listAlertTypesRoute', () => {
{
id: '1',
name: 'name',
actionGroups: [],
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const alertType: AlertType = {
{ id: 'default', name: 'Default' },
{ id: 'other-group', name: 'Other Group' },
],
defaultActionGroupId: 'default',
executor: jest.fn(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const alertType = {
id: 'test',
name: 'My test alert',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
executor: jest.fn(),
};
let fakeTimer: sinon.SinonFakeTimers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const alertType = {
id: 'test',
name: 'My test alert',
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
executor: jest.fn(),
};
let fakeTimer: sinon.SinonFakeTimers;
Expand Down
8 changes: 2 additions & 6 deletions x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AlertInstance } from './alert_instance';
import { AlertTypeRegistry as OrigAlertTypeRegistry } from './alert_type_registry';
import { PluginSetupContract, PluginStartContract } from './plugin';
import { SavedObjectAttributes, SavedObjectsClientContract } from '../../../../src/core/server';
import { Alert, AlertActionParams } from '../common';
import { Alert, AlertActionParams, ActionGroup } from '../common';
import { AlertsClient } from './alerts_client';
export * from '../common';

Expand Down Expand Up @@ -52,18 +52,14 @@ export interface AlertExecutorOptions {
updatedBy: string | null;
}

export interface ActionGroup {
id: string;
name: string;
}

export interface AlertType {
id: string;
name: string;
validate?: {
params?: { validate: (object: any) => any };
};
actionGroups: ActionGroup[];
defaultActionGroupId: ActionGroup['id'];
executor: ({ services, params, state }: AlertExecutorOptions) => Promise<State | void>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('loadAlertTypes', () => {
name: 'Test',
actionVariables: ['var1'],
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
},
];
http.get.mockResolvedValueOnce(resolvedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('alert_form', () => {
alertTypeRegistry.has.mockReturnValue(true);
actionTypeRegistry.list.mockReturnValue([actionType]);
actionTypeRegistry.has.mockReturnValue(true);
actionTypeRegistry.get.mockReturnValue(actionType);

const initialAlert = ({
name: 'test',
Expand Down
Loading

0 comments on commit 1eb0176

Please sign in to comment.