Alerting: Fix support check for export with modifications (#81602)

This commit is contained in:
Gilles De Mey 2024-02-01 14:17:48 +01:00 committed by GitHub
parent 3e01ba0f57
commit eb889c41ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 83 additions and 5 deletions

View File

@ -1,5 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`AlertRule abilities should report no permissions while we are loading data for cloud rule 1`] = `
{
"delete-alert-rule": [
false,
false,
],
"duplicate-alert-rule": [
false,
false,
],
"explore-alert-rule": [
true,
false,
],
"modify-export-rule": [
false,
false,
],
"silence-alert-rule": [
false,
false,
],
"update-alert-rule": [
false,
false,
],
"view-alert-rule": [
true,
false,
],
}
`;
exports[`AlertRule abilities should report that all actions are supported for a Grafana Managed alert rule 1`] = `
{
"delete-alert-rule": [
true,
false,
],
"duplicate-alert-rule": [
true,
false,
],
"explore-alert-rule": [
true,
false,
],
"modify-export-rule": [
true,
false,
],
"silence-alert-rule": [
true,
false,
],
"update-alert-rule": [
true,
false,
],
"view-alert-rule": [
true,
false,
],
}
`;
exports[`alertmanager abilities should report Create / Update / Delete actions aren't supported for external vanilla alertmanager 1`] = `
{
"create-contact-point": [

View File

@ -7,7 +7,7 @@ import { TestProvider } from 'test/helpers/TestProvider';
import { AlertManagerDataSourceJsonData, AlertManagerImplementation } from 'app/plugins/datasource/alertmanager/types';
import { AccessControlAction } from 'app/types';
import { getGrafanaRule, grantUserPermissions, mockDataSource } from '../mocks';
import { getCloudRule, getGrafanaRule, grantUserPermissions, mockDataSource } from '../mocks';
import { AlertmanagerProvider } from '../state/AlertmanagerContext';
import { setupDataSources } from '../testSetup/datasources';
import { DataSourceType, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
@ -136,7 +136,7 @@ describe('alertmanager abilities', () => {
});
});
describe('rule permissions', () => {
describe('AlertRule abilities', () => {
it('should report that all actions are supported for a Grafana Managed alert rule', async () => {
const rule = getGrafanaRule();
@ -149,9 +149,21 @@ describe('rule permissions', () => {
expect(supported).toBe(true);
}
});
expect(abilities.result.current).toMatchSnapshot();
});
it('should report the correct set of supported actions for an external rule with ruler API', async () => {});
it('should report no permissions while we are loading data for cloud rule', async () => {
const rule = getCloudRule();
const abilities = renderHook(() => useAllAlertRuleAbilities(rule), { wrapper: TestProvider });
await waitFor(() => {
expect(abilities.result.current).not.toBeUndefined();
});
expect(abilities.result.current).toMatchSnapshot();
});
it('should not allow certain actions for provisioned rules', () => {});

View File

@ -146,11 +146,11 @@ export function useAllAlertRuleAbilities(rule: CombinedRule): Abilities<AlertRul
const isProvisioned = isGrafanaRulerRule(rule.rulerRule) && Boolean(rule.rulerRule.grafana_alert.provenance);
const isFederated = isFederatedRuleGroup(rule.group);
const isGrafanaManagedAlertRule = isGrafanaRulerRule(rule.rulerRule);
// if a rule is either provisioned or a federated rule, we don't allow it to be removed or edited
const immutableRule = isProvisioned || isFederated;
// TODO refactor this hook maybe
const {
isEditable,
isRemovable,
@ -173,7 +173,7 @@ export function useAllAlertRuleAbilities(rule: CombinedRule): Abilities<AlertRul
[AlertRuleAction.Delete]: [MaybeSupportedUnlessImmutable, isRemovable ?? false],
[AlertRuleAction.Explore]: toAbility(AlwaysSupported, AccessControlAction.DataSourcesExplore),
[AlertRuleAction.Silence]: canSilence,
[AlertRuleAction.ModifyExport]: [MaybeSupported, exportAllowed],
[AlertRuleAction.ModifyExport]: [isGrafanaManagedAlertRule, exportAllowed],
};
return abilities;