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

[Alerting UI][License] Disable alert types in UI when the license doesn't support it. #85496

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,6 @@ function getAlertType(actionVariables: ActionVariables): AlertType {
authorizedConsumers: {},
producer: ALERTS_FEATURE_ID,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('loadAlertTypes', () => {
defaultActionGroupId: 'default',
authorizedConsumers: {},
minimumLicenseRequired: 'basic',
enabledInLicense: true,
},
];
http.get.mockResolvedValueOnce(resolvedValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { AlertTypeModel } from '../../types';
import { IsEnabledResult, IsDisabledResult } from './check_alert_type_enabled';

export function alertTypeSolutionCompare(
a: [
string,
Array<{
id: string;
name: string;
checkEnabledResult: IsEnabledResult | IsDisabledResult;
alertTypeItem: AlertTypeModel;
}>
],
b: [
string,
Array<{
id: string;
name: string;
checkEnabledResult: IsEnabledResult | IsDisabledResult;
alertTypeItem: AlertTypeModel;
}>
],
solutions: Map<string, string> | undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't quite clear what a solution is.
Is there a more descriptive name we can use here?

Additionally, can we add some unit tests around this please?

) {
// .sort(([a], [b]) =>
// solutions ? solutions.get(a)!.localeCompare(solutions.get(b)!) : a.localeCompare(b)
// )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code

const solutionA = a[0];
const solutionB = b[0];
const alertTypeItemsA = a[1].find((alertTypeItem) => alertTypeItem.checkEnabledResult.isEnabled);
const alertTypeItemsB = b[1].find((alertTypeItem) => alertTypeItem.checkEnabledResult.isEnabled);

if (!!alertTypeItemsA && !alertTypeItemsB) {
return -1;
}
if (!alertTypeItemsA && !!alertTypeItemsB) {
return 1;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mix of !! and ! makes this a little hard to follow... can we make this a little clearer?
A couple of suggestions that might help:

  1. Can we rename A and B to something more descriptive? Perhaps AlertTypesListA? It's common to use Left and Right rather than A and B. So perhaps LeftAlertTypesList and RightAlertTypesList?
  2. change alertTypeItemsA to a more descriptive name perhaps? Maybe enabledAlertTypeInList?
  3. perhaps, to avoid the mix of !! with !, we do something like:
 const hasEnabledAlertTypeInListA = a[1].find((alertTypeItem) => alertTypeItem.checkEnabledResult.isEnabled) != undefined;

Something like that?


return solutions
? solutions.get(solutionA)!.localeCompare(solutions.get(solutionB)!)
: solutionA.localeCompare(solutionB);
}

export function alertTypeCompare(
a: {
id: string;
name: string;
checkEnabledResult: IsEnabledResult | IsDisabledResult;
alertTypeItem: AlertTypeModel;
},
b: {
id: string;
name: string;
checkEnabledResult: IsEnabledResult | IsDisabledResult;
alertTypeItem: AlertTypeModel;
}
) {
if (a.checkEnabledResult.isEnabled === true && b.checkEnabledResult.isEnabled === false) {
return -1;
}
if (a.checkEnabledResult.isEnabled === false && b.checkEnabledResult.isEnabled === true) {
return 1;
}
return a.name.localeCompare(b.name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { AlertType } from '../../types';
import { checkAlertTypeEnabled } from './check_alert_type_enabled';

describe('checkAlertTypeEnabled', () => {
test(`returns isEnabled:true when alert type isn't provided`, async () => {
expect(checkAlertTypeEnabled()).toMatchInlineSnapshot(`
Object {
"isEnabled": true,
}
`);
});

test('returns isEnabled:true when alert type is enabled', async () => {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionVariables: {
context: [{ name: 'var1', description: 'val1' }],
state: [{ name: 'var2', description: 'val2' }],
params: [{ name: 'var3', description: 'val3' }],
},
producer: 'test',
actionGroups: [{ id: 'default', name: 'Default' }],
recoveryActionGroup: { id: 'recovered', name: 'Recovered' },
defaultActionGroupId: 'default',
authorizedConsumers: {},
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};
expect(checkAlertTypeEnabled(alertType)).toMatchInlineSnapshot(`
Object {
"isEnabled": true,
}
`);
});

test('returns isEnabled:false when alert type is disabled by license', async () => {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionVariables: {
context: [{ name: 'var1', description: 'val1' }],
state: [{ name: 'var2', description: 'val2' }],
params: [{ name: 'var3', description: 'val3' }],
},
producer: 'test',
actionGroups: [{ id: 'default', name: 'Default' }],
recoveryActionGroup: { id: 'recovered', name: 'Recovered' },
defaultActionGroupId: 'default',
authorizedConsumers: {},
minimumLicenseRequired: 'gold',
enabledInLicense: false,
};
expect(checkAlertTypeEnabled(alertType)).toMatchInlineSnapshot(`
Object {
"isEnabled": false,
"message": "This alert type requires a Gold license.",
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { upperFirst } from 'lodash';
import { i18n } from '@kbn/i18n';
import { AlertType } from '../../types';

export interface IsEnabledResult {
isEnabled: true;
}
export interface IsDisabledResult {
isEnabled: false;
message: string;
}

const getLicenseCheckResult = (alertType: AlertType) => {
return {
isEnabled: false,
message: i18n.translate(
'xpack.triggersActionsUI.checkAlertTypeEnabled.alertTypeDisabledByLicenseMessage',
{
defaultMessage: 'This alert type requires a {minimumLicenseRequired} license.',
values: {
minimumLicenseRequired: upperFirst(alertType.minimumLicenseRequired),
},
}
),
};
};

export function checkAlertTypeEnabled(alertType?: AlertType): IsEnabledResult | IsDisabledResult {
if (alertType?.enabledInLicense === false) {
return getLicenseCheckResult(alertType);
}

return { isEnabled: true };
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('alert_details', () => {
minimumLicenseRequired: 'basic',
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
enabledInLicense: true,
};

expect(
Expand Down Expand Up @@ -91,6 +92,7 @@ describe('alert_details', () => {
minimumLicenseRequired: 'basic',
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
enabledInLicense: true,
};

expect(
Expand Down Expand Up @@ -121,6 +123,7 @@ describe('alert_details', () => {
minimumLicenseRequired: 'basic',
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
enabledInLicense: true,
};

expect(
Expand Down Expand Up @@ -157,6 +160,7 @@ describe('alert_details', () => {
minimumLicenseRequired: 'basic',
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
enabledInLicense: true,
};

const actionTypes: ActionType[] = [
Expand Down Expand Up @@ -213,6 +217,7 @@ describe('alert_details', () => {
producer: ALERTS_FEATURE_ID,
minimumLicenseRequired: 'basic',
authorizedConsumers,
enabledInLicense: true,
};
const actionTypes: ActionType[] = [
{
Expand Down Expand Up @@ -274,6 +279,7 @@ describe('alert_details', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

expect(
Expand All @@ -296,6 +302,7 @@ describe('alert_details', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

expect(
Expand Down Expand Up @@ -327,6 +334,7 @@ describe('disable button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const enableButton = shallow(
Expand Down Expand Up @@ -357,6 +365,7 @@ describe('disable button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const enableButton = shallow(
Expand Down Expand Up @@ -387,6 +396,7 @@ describe('disable button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const disableAlert = jest.fn();
Expand Down Expand Up @@ -426,6 +436,7 @@ describe('disable button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const enableAlert = jest.fn();
Expand Down Expand Up @@ -468,6 +479,7 @@ describe('mute button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const enableButton = shallow(
Expand Down Expand Up @@ -499,6 +511,7 @@ describe('mute button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const enableButton = shallow(
Expand Down Expand Up @@ -530,6 +543,7 @@ describe('mute button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const muteAlert = jest.fn();
Expand Down Expand Up @@ -570,6 +584,7 @@ describe('mute button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const unmuteAlert = jest.fn();
Expand Down Expand Up @@ -610,6 +625,7 @@ describe('mute button', () => {
producer: ALERTS_FEATURE_ID,
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

const enableButton = shallow(
Expand Down Expand Up @@ -677,6 +693,7 @@ describe('edit button', () => {
producer: 'alerting',
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

expect(
Expand Down Expand Up @@ -721,6 +738,7 @@ describe('edit button', () => {
producer: 'alerting',
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

expect(
Expand Down Expand Up @@ -758,6 +776,7 @@ describe('edit button', () => {
producer: 'alerting',
authorizedConsumers,
minimumLicenseRequired: 'basic',
enabledInLicense: true,
};

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const AlertDetails: React.FunctionComponent<AlertDetailsProps> = ({
iconType="pencil"
onClick={() => setEditFlyoutVisibility(true)}
name="edit"
disabled={!alertType.enabledInLicense}
>
<FormattedMessage
id="xpack.triggersActionsUI.sections.alertDetails.editAlertButtonLabel"
Expand Down Expand Up @@ -210,7 +211,7 @@ export const AlertDetails: React.FunctionComponent<AlertDetailsProps> = ({
<EuiFlexItem grow={false}>
<EuiSwitch
name="disable"
disabled={!canSaveAlert}
disabled={!canSaveAlert || !alertType.enabledInLicense}
checked={!isEnabled}
data-test-subj="disableSwitch"
onChange={async () => {
Expand All @@ -235,7 +236,7 @@ export const AlertDetails: React.FunctionComponent<AlertDetailsProps> = ({
<EuiSwitch
name="mute"
checked={isMuted}
disabled={!canSaveAlert || !isEnabled}
disabled={!canSaveAlert || !isEnabled || !alertType.enabledInLicense}
data-test-subj="muteSwitch"
onChange={async () => {
if (isMuted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ function mockAlertType(overloads: Partial<AlertType> = {}): AlertType {
authorizedConsumers: {},
producer: 'alerts',
minimumLicenseRequired: 'basic',
enabledInLicense: true,
...overloads,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function mockAlertType(overloads: Partial<AlertType> = {}): AlertType {
authorizedConsumers: {},
producer: 'alerts',
minimumLicenseRequired: 'basic',
enabledInLicense: true,
...overloads,
};
}
Expand Down
Loading