-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from 5 commits
d03cf87
60dfef8
24dfe8b
1bb9310
2b6082e
291a830
f204384
e3555dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
) { | ||
// .sort(([a], [b]) => | ||
// solutions ? solutions.get(a)!.localeCompare(solutions.get(b)!) : a.localeCompare(b) | ||
// ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mix of
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 }; | ||
} |
There was a problem hiding this comment.
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?