Skip to content

Commit

Permalink
[ResponseOps][Alerting] Ability to bulk update API keys for alerting …
Browse files Browse the repository at this point in the history
…rules (#139036)

* Adding bulk update api keys

* Adding tests
  • Loading branch information
doakalexi authored Aug 23, 2022
1 parent 84e8e54 commit 6aa29cb
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.
*/

import * as React from 'react';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { RuleTableItem } from '../../../../types';
import { RuleQuickEditButtonsWithApi as RuleQuickEditButtons } from './rule_quick_edit_buttons';

jest.mock('../../../../common/lib/kibana', () => ({
useKibana: jest.fn().mockReturnValue({
services: {
notifications: { toast: { addDanger: jest.fn() } },
},
}),
}));

describe('rule_quick_edit_buttons', () => {
it('renders buttons', async () => {
const mockRule: RuleTableItem = {
id: '1',
enabled: true,
muteAll: false,
} as RuleTableItem;

const wrapper = mountWithIntl(
<RuleQuickEditButtons
selectedItems={[mockRule]}
onPerformingAction={() => {}}
onActionPerformed={() => {}}
setRulesToDelete={() => {}}
setRulesToUpdateAPIKey={() => {}}
/>
);

expect(wrapper.find('[data-test-subj="muteAll"]').exists()).toBeTruthy();
expect(wrapper.find('[data-test-subj="unmuteAll"]').exists()).toBeFalsy();
expect(wrapper.find('[data-test-subj="enableAll"]').exists()).toBeFalsy();
expect(wrapper.find('[data-test-subj="disableAll"]').exists()).toBeTruthy();
expect(wrapper.find('[data-test-subj="updateAPIKeys"]').exists()).toBeTruthy();
expect(wrapper.find('[data-test-subj="deleteAll"]').exists()).toBeTruthy();
});

it('renders muteAll if rules are all muted', async () => {
const mockRule: RuleTableItem = {
id: '1',
muteAll: true,
} as RuleTableItem;

const wrapper = mountWithIntl(
<RuleQuickEditButtons
selectedItems={[mockRule]}
onPerformingAction={() => {}}
onActionPerformed={() => {}}
setRulesToDelete={() => {}}
setRulesToUpdateAPIKey={() => {}}
/>
);

expect(wrapper.find('[data-test-subj="muteAll"]').exists()).toBeFalsy();
expect(wrapper.find('[data-test-subj="unmuteAll"]').exists()).toBeTruthy();
});

it('renders enableAll if rules are all disabled', async () => {
const mockRule: RuleTableItem = {
id: '1',
enabled: false,
} as RuleTableItem;

const wrapper = mountWithIntl(
<RuleQuickEditButtons
selectedItems={[mockRule]}
onPerformingAction={() => {}}
onActionPerformed={() => {}}
setRulesToDelete={() => {}}
setRulesToUpdateAPIKey={() => {}}
/>
);

expect(wrapper.find('[data-test-subj="enableAll"]').exists()).toBeTruthy();
expect(wrapper.find('[data-test-subj="disableAll"]').exists()).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ComponentOpts = {
onPerformingAction?: () => void;
onActionPerformed?: () => void;
setRulesToDelete: React.Dispatch<React.SetStateAction<string[]>>;
setRulesToUpdateAPIKey: React.Dispatch<React.SetStateAction<string[]>>;
} & BulkOperationsComponentOpts;

export const RuleQuickEditButtons: React.FunctionComponent<ComponentOpts> = ({
Expand All @@ -34,6 +35,7 @@ export const RuleQuickEditButtons: React.FunctionComponent<ComponentOpts> = ({
enableRules,
disableRules,
setRulesToDelete,
setRulesToUpdateAPIKey,
}: ComponentOpts) => {
const {
notifications: { toasts },
Expand All @@ -44,6 +46,7 @@ export const RuleQuickEditButtons: React.FunctionComponent<ComponentOpts> = ({
const [isEnablingRules, setIsEnablingRules] = useState<boolean>(false);
const [isDisablingRules, setIsDisablingRules] = useState<boolean>(false);
const [isDeletingRules, setIsDeletingRules] = useState<boolean>(false);
const [isUpdatingRuleAPIKeys, setIsUpdatingRuleAPIKeys] = useState<boolean>(false);

const allRulesMuted = selectedItems.every(isRuleMuted);
const allRulesDisabled = selectedItems.every(isRuleDisabled);
Expand Down Expand Up @@ -154,6 +157,26 @@ export const RuleQuickEditButtons: React.FunctionComponent<ComponentOpts> = ({
}
}

async function updateAPIKeysClick() {
onPerformingAction();
setIsUpdatingRuleAPIKeys(true);
try {
setRulesToUpdateAPIKey(selectedItems.map((selected: any) => selected.id));
} catch (e) {
toasts.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.rulesList.bulkActionPopover.failedToUpdateRuleAPIKeysMessage',
{
defaultMessage: 'Failed to update API keys for rule(s)',
}
),
});
} finally {
setIsUpdatingRuleAPIKeys(false);
onActionPerformed();
}
}

return (
<EuiFlexGroup alignItems="baseline" direction="column" gutterSize="none">
{!allRulesMuted && (
Expand Down Expand Up @@ -216,6 +239,19 @@ export const RuleQuickEditButtons: React.FunctionComponent<ComponentOpts> = ({
</EuiButtonEmpty>
</EuiFlexItem>
)}
<EuiFlexItem>
<EuiButtonEmpty
onClick={updateAPIKeysClick}
isLoading={isUpdatingRuleAPIKeys}
isDisabled={isPerformingAction}
data-test-subj="updateAPIKeys"
>
<FormattedMessage
id="xpack.triggersActionsUI.sections.rulesList.bulkActionPopover.updateRuleAPIKeysTitle"
defaultMessage="Update API Keys"
/>
</EuiButtonEmpty>
</EuiFlexItem>
<EuiFlexItem>
<EuiButtonEmpty
onClick={deleteSelectedItems}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ export const RulesList = ({
setIsPerformingAction(false);
}}
setRulesToDelete={setRulesToDelete}
setRulesToUpdateAPIKey={setRulesToUpdateAPIKey}
/>
</BulkOperationPopover>
</EuiFlexItem>
Expand Down

0 comments on commit 6aa29cb

Please sign in to comment.