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

[Response Ops][Rule Form V2] Move dependencies from triggers actions UI to shared package #184977

Merged
34 changes: 34 additions & 0 deletions packages/kbn-alerting-types/alerting_framework_health_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export enum HealthStatus {
OK = 'ok',
Warning = 'warn',
Error = 'error',
}

export interface AlertsHealth {
decryptionHealth: {
status: HealthStatus;
timestamp: string;
};
executionHealth: {
status: HealthStatus;
timestamp: string;
};
readHealth: {
status: HealthStatus;
timestamp: string;
};
}

export interface AlertingFrameworkHealth {
isSufficientlySecure: boolean;
hasPermanentEncryptionKey: boolean;
alertingFrameworkHealth: AlertsHealth;
}
1 change: 1 addition & 0 deletions packages/kbn-alerting-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export * from './alert_type';
export * from './rule_notify_when_type';
export * from './r_rule_types';
export * from './rule_types';
export * from './alerting_framework_health_types';
export * from './action_variable';
13 changes: 12 additions & 1 deletion packages/kbn-alerting-types/rule_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
* Side Public License, v 1.
*/

import type { SavedObjectAttributes } from '@kbn/core/server';
import type {
SavedObjectAttribute,
SavedObjectAttributes,
SavedObjectsResolveResponse,
} from '@kbn/core/server';
import type { Filter } from '@kbn/es-query';
import type { RuleNotifyWhenType, RRuleParams } from '.';

export type RuleTypeParams = Record<string, unknown>;
export type RuleActionParams = SavedObjectAttributes;
export type RuleActionParam = SavedObjectAttribute;

export const ISO_WEEKDAYS = [1, 2, 3, 4, 5, 6, 7] as const;
export type IsoWeekday = typeof ISO_WEEKDAYS[number];
Expand Down Expand Up @@ -239,3 +244,9 @@ export type SanitizedRule<Params extends RuleTypeParams = never> = Omit<
Rule<Params>,
'apiKey' | 'actions'
> & { actions: SanitizedRuleAction[] };

export type ResolvedSanitizedRule<Params extends RuleTypeParams = never> = SanitizedRule<Params> &
Omit<SavedObjectsResolveResponse, 'saved_object'> & {
outcome: string;
alias_target_id?: string;
};
2 changes: 1 addition & 1 deletion packages/kbn-alerts-ui-shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export type { AlertsSearchBarProps } from './src/alerts_search_bar/types';

export * from './src/alert_fields_table';
export * from './src/alert_filter_controls/types';
export * from './src/common/hooks';
export * from './src/common/types';
Original file line number Diff line number Diff line change
@@ -1,13 +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.
* 2.0 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 or the Server
* Side Public License, v 1.
*/

import { httpServiceMock } from '@kbn/core/public/mocks';
import { RuleUpdates } from '../../../types';
import { createRule } from './create';
import { RuleTypeParams } from '../../types';
import { createRule } from './create_rule';
import { CreateRuleBody } from './types';

const http = httpServiceMock.createStartContract();

Expand Down Expand Up @@ -62,10 +64,7 @@ describe('createRule', () => {
},
};

const ruleToCreate: Omit<
RuleUpdates,
'createdBy' | 'updatedBy' | 'muteAll' | 'mutedInstanceIds' | 'executionStatus'
> = {
const ruleToCreate: CreateRuleBody<RuleTypeParams> = {
params: {
aggType: 'count',
termSize: 5,
Expand Down Expand Up @@ -106,17 +105,14 @@ describe('createRule', () => {
actionTypeId: '.system-action',
},
],
createdAt: new Date('2021-04-01T21:33:13.247Z'),
updatedAt: new Date('2021-04-01T21:33:13.247Z'),
apiKeyOwner: '',
revision: 0,
notifyWhen: 'onActionGroupChange',
alertDelay: {
active: 10,
},
};
http.post.mockResolvedValueOnce(resolvedValue);

const result = await createRule({ http, rule: ruleToCreate });
const result = await createRule({ http, rule: ruleToCreate as CreateRuleBody });
expect(result).toEqual({
actions: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import type { HttpSetup } from '@kbn/core/public';
import type { AsApiContract } from '@kbn/actions-types';
import type { Rule } from '../../types';
import { CreateRuleBody, transformCreateRuleBody } from '.';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { transformRule } from '../../transformations';

export async function createRule({
http,
rule,
}: {
http: HttpSetup;
rule: CreateRuleBody;
}): Promise<Rule> {
const res = await http.post<AsApiContract<Rule>>(`${BASE_ALERTING_API_PATH}/rule`, {
body: JSON.stringify(transformCreateRuleBody(rule)),
});
return transformRule(res);
}
11 changes: 11 additions & 0 deletions packages/kbn-alerts-ui-shared/src/common/apis/create_rule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export * from './types';
export * from './create_rule';
export * from './transform_create_rule_body';
Original file line number Diff line number Diff line change
@@ -1,26 +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.
* 2.0 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 or the Server
* Side Public License, v 1.
*/
import { HttpSetup } from '@kbn/core/public';
import { AsApiContract, RewriteResponseCase } from '@kbn/actions-plugin/common';
import { Rule, RuleUpdates } from '../../../types';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { transformRule } from './common_transformations';

type RuleCreateBody = Omit<
RuleUpdates,
| 'createdBy'
| 'updatedBy'
| 'muteAll'
| 'mutedInstanceIds'
| 'executionStatus'
| 'lastRun'
| 'nextRun'
>;
export const rewriteBodyRequest: RewriteResponseCase<RuleCreateBody> = ({
import { RewriteResponseCase } from '@kbn/actions-types';
import { CreateRuleBody } from './types';

export const transformCreateRuleBody: RewriteResponseCase<CreateRuleBody> = ({
ruleTypeId,
actions,
alertDelay,
Expand Down Expand Up @@ -56,16 +45,3 @@ export const rewriteBodyRequest: RewriteResponseCase<RuleCreateBody> = ({
}),
...(alertDelay ? { alert_delay: alertDelay } : {}),
});

export async function createRule({
http,
rule,
}: {
http: HttpSetup;
rule: RuleCreateBody;
}): Promise<Rule> {
const res = await http.post<AsApiContract<Rule>>(`${BASE_ALERTING_API_PATH}/rule`, {
body: JSON.stringify(rewriteBodyRequest(rule)),
});
return transformRule(res);
}
23 changes: 23 additions & 0 deletions packages/kbn-alerts-ui-shared/src/common/apis/create_rule/types.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 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 or the Server
* Side Public License, v 1.
*/

import { Rule, RuleTypeParams } from '../../types';

export interface CreateRuleBody<Params extends RuleTypeParams = RuleTypeParams> {
name: Rule<Params>['name'];
ruleTypeId: Rule<Params>['ruleTypeId'];
enabled: Rule<Params>['enabled'];
consumer: Rule<Params>['consumer'];
tags: Rule<Params>['tags'];
throttle?: Rule<Params>['throttle'];
params: Rule<Params>['params'];
schedule: Rule<Params>['schedule'];
actions: Rule<Params>['actions'];
notifyWhen?: Rule<Params>['notifyWhen'];
alertDelay?: Rule<Params>['alertDelay'];
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/*
* 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.
* 2.0 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 or the Server
* Side Public License, v 1.
*/

import { httpServiceMock } from '@kbn/core/public/mocks';
import { alertingFrameworkHealth } from './health';
import { fetchAlertingFrameworkHealth } from '.';

describe('alertingFrameworkHealth', () => {
describe('fetchAlertingFrameworkHealth', () => {
const http = httpServiceMock.createStartContract();
test('should call alertingFrameworkHealth API', async () => {
test('should call fetchAlertingFrameworkHealth API', async () => {
http.get.mockResolvedValueOnce({
is_sufficiently_secure: true,
has_permanent_encryption_key: true,
Expand All @@ -20,7 +21,7 @@ describe('alertingFrameworkHealth', () => {
read_health: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
},
});
const result = await alertingFrameworkHealth({ http });
const result = await fetchAlertingFrameworkHealth({ http });
expect(result).toEqual({
alertingFrameworkHealth: {
decryptionHealth: { status: 'ok', timestamp: '2021-04-01T21:29:22.991Z' },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { HttpSetup } from '@kbn/core/public';
import { AsApiContract } from '@kbn/actions-types';
import { AlertingFrameworkHealth, AlertsHealth } from '@kbn/alerting-types';
import { BASE_ALERTING_API_PATH } from '../../constants';
import { transformAlertingFrameworkHealthResponse, transformAlertsHealthResponse } from '.';

export async function fetchAlertingFrameworkHealth({
http,
}: {
http: HttpSetup;
}): Promise<AlertingFrameworkHealth> {
const res = await http.get<AsApiContract<AlertingFrameworkHealth>>(
`${BASE_ALERTING_API_PATH}/_health`
);
const alertingFrameworkHealthRewrited = transformAlertsHealthResponse(
res.alerting_framework_health as unknown as AsApiContract<AlertsHealth>
);
return {
...transformAlertingFrameworkHealthResponse(res),
alertingFrameworkHealth: alertingFrameworkHealthRewrited,
};
}
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 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 or the Server
* Side Public License, v 1.
*/

export * from './fetch_alerting_framework_health';
export * from './transform_alerting_framework_health_response';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { AsApiContract, RewriteRequestCase } from '@kbn/actions-types';
import { AlertingFrameworkHealth, AlertsHealth } from '@kbn/alerting-types';

export const transformAlertsHealthResponse: RewriteRequestCase<AlertsHealth> = ({
decryption_health: decryptionHealth,
execution_health: executionHealth,
read_health: readHealth,
...res
}: AsApiContract<AlertsHealth>) => ({
decryptionHealth,
executionHealth,
readHealth,
...res,
});

export const transformAlertingFrameworkHealthResponse: RewriteRequestCase<
AlertingFrameworkHealth
> = ({
is_sufficiently_secure: isSufficientlySecure,
has_permanent_encryption_key: hasPermanentEncryptionKey,

alerting_framework_health: alertingFrameworkHealth,
...res
}: AsApiContract<AlertingFrameworkHealth>) => ({
isSufficientlySecure,
hasPermanentEncryptionKey,
alertingFrameworkHealth,
...res,
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* 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.
* 2.0 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 or the Server
* Side Public License, v 1.
*/

import { httpServiceMock } from '@kbn/core/public/mocks';
import { triggersActionsUiConfig } from './config_api';
import { fetchUiConfig } from '.';

describe('triggersActionsUiConfig', () => {
describe('fetchUiConfig', () => {
const http = httpServiceMock.createStartContract();

test('should call triggersActionsUiConfig API', async () => {
const result = await triggersActionsUiConfig({ http });
test('should call fetchUiConfig API', async () => {
const result = await fetchUiConfig({ http });
expect(result).toEqual(undefined);
expect(http.get.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down
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 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 or the Server
* Side Public License, v 1.
*/

import type { HttpStart } from '@kbn/core-http-browser';
import { BASE_TRIGGERS_ACTIONS_UI_API_PATH } from '../../constants';
import { UiConfig } from '.';

export const fetchUiConfig = async ({ http }: { http: HttpStart }): Promise<UiConfig> => {
return http.get<UiConfig>(`${BASE_TRIGGERS_ACTIONS_UI_API_PATH}/_config`);
};
Loading