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: Add export drawer when exporting all Grafana managed alerts #74846

Merged
merged 22 commits into from Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8fcf079
Add export formats drawer when exporting contact points
soniaAguilarPeiron Sep 6, 2023
3a6282e
Add 'export by format' drawer in policies (root policy)
soniaAguilarPeiron Sep 7, 2023
73e03f7
Add test for showing export policies button
soniaAguilarPeiron Sep 8, 2023
ecae1fb
Add tests for Policy.tsx
soniaAguilarPeiron Sep 8, 2023
593779a
Add tests for export functionality in receivers
soniaAguilarPeiron Sep 8, 2023
22ca529
Add exporter drawer for receivers
soniaAguilarPeiron Sep 12, 2023
d1fd855
Fix conflicts
soniaAguilarPeiron Sep 12, 2023
488f4e4
Fix prettier warnings
soniaAguilarPeiron Sep 12, 2023
c91360c
Fix conflicts
soniaAguilarPeiron Sep 12, 2023
d83892e
Allow HCL only for alert rules exports
soniaAguilarPeiron Sep 12, 2023
75c333b
Add tests for Policies
soniaAguilarPeiron Sep 13, 2023
61b23c3
Fix tests
soniaAguilarPeiron Sep 13, 2023
63802dc
Merge remote-tracking branch 'origin/main' into alerting/add-export-d…
soniaAguilarPeiron Sep 13, 2023
9b9e910
Merge remote-tracking branch 'origin/main' into alerting/add-export-d…
soniaAguilarPeiron Sep 13, 2023
4fc2699
Refactor: Update ExportProviders types for limiting the avaliable exp…
soniaAguilarPeiron Sep 13, 2023
2bc8abf
Merge remote-tracking branch 'origin/main' into alerting/add-export-d…
soniaAguilarPeiron Sep 13, 2023
268e6aa
Delete unused shouldShowExportOption method and tests
soniaAguilarPeiron Sep 13, 2023
b7f910a
Add exporter drawer when clicking export all Grafana managed alerts
soniaAguilarPeiron Sep 13, 2023
e1d9ef7
Fix test
soniaAguilarPeiron Sep 14, 2023
06194d9
fix prettier
soniaAguilarPeiron Sep 14, 2023
1d870d0
Fix conflicts
soniaAguilarPeiron Sep 18, 2023
13658be
lint
gillesdemey Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions public/app/features/alerting/unified/MoreActionsRuleButtons.tsx
@@ -1,18 +1,20 @@
import React from 'react';
import { useLocation } from 'react-router-dom';
import { useToggle } from 'react-use';

import { urlUtil } from '@grafana/data';
import { Button, Dropdown, Icon, LinkButton, Menu, MenuItem } from '@grafana/ui';

import { logInfo, LogMessages } from './Analytics';
import { GrafanaRulesExporter } from './components/export/GrafanaRulesExporter';
import { useRulesAccess } from './utils/accessControlHooks';
import { createUrl } from './utils/url';

interface Props {}

export function MoreActionsRuleButtons({}: Props) {
const { canCreateGrafanaRules, canCreateCloudRules, canReadProvisioning } = useRulesAccess();
const location = useLocation();
const [showExportDrawer, toggleShowExportDrawer] = useToggle(false);
const newMenu = (
<Menu>
{(canCreateGrafanaRules || canCreateCloudRules) && (
Expand All @@ -23,16 +25,7 @@ export function MoreActionsRuleButtons({}: Props) {
label="New recording rule"
/>
)}
{canReadProvisioning && (
<MenuItem
url={createUrl('/api/v1/provisioning/alert-rules/export', {
download: 'true',
format: 'yaml',
})}
label="Export all Grafana-managed rules"
target="_blank"
/>
)}
{canReadProvisioning && <MenuItem onClick={toggleShowExportDrawer} label="Export all Grafana-managed rules" />}
</Menu>
);

Expand All @@ -54,6 +47,7 @@ export function MoreActionsRuleButtons({}: Props) {
<Icon name="angle-down" />
</Button>
</Dropdown>
{showExportDrawer && <GrafanaRulesExporter onClose={toggleShowExportDrawer} />}
</>
);
}
4 changes: 3 additions & 1 deletion public/app/features/alerting/unified/RuleList.test.tsx
Expand Up @@ -117,7 +117,9 @@ const ui = {
editCloudGroupIcon: byTestId('edit-group'),
newRuleButton: byRole('link', { name: 'New alert rule' }),
moreButton: byRole('button', { name: 'More' }),
exportButton: byRole('link', { name: /export/i }),
exportButton: byRole('menuitem', {
name: /export all grafana\-managed rules/i,
}),
editGroupModal: {
dialog: byRole('dialog'),
namespaceInput: byRole('textbox', { name: /^Namespace/ }),
Expand Down
7 changes: 7 additions & 0 deletions public/app/features/alerting/unified/api/alertRuleApi.ts
Expand Up @@ -192,6 +192,13 @@ export const alertRuleApi = alertingApi.injectEndpoints({
responseType: 'text',
}),
}),
exportRules: build.query<string, { format: ExportFormats }>({
query: ({ format }) => ({
url: `/api/v1/provisioning/alert-rules/export`,
params: { format: format },
responseType: 'text',
}),
}),
exportReceiver: build.query<string, { receiverName: string; decrypt: boolean; format: ExportFormats }>({
query: ({ receiverName, decrypt, format }) => ({
url: `/api/v1/provisioning/contact-points/export/`,
Expand Down
@@ -0,0 +1,54 @@
import React, { useState } from 'react';

import { LoadingPlaceholder } from '@grafana/ui';

import { alertRuleApi } from '../../api/alertRuleApi';

import { FileExportPreview } from './FileExportPreview';
import { GrafanaExportDrawer } from './GrafanaExportDrawer';
import { allGrafanaExportProviders, ExportFormats } from './providers';

interface GrafanaRulesExporterProps {
onClose: () => void;
}

export function GrafanaRulesExporter({ onClose }: GrafanaRulesExporterProps) {
const [activeTab, setActiveTab] = useState<ExportFormats>('yaml');

return (
<GrafanaExportDrawer
activeTab={activeTab}
onTabChange={setActiveTab}
onClose={onClose}
formatProviders={Object.values(allGrafanaExportProviders)}
>
<GrafanaRulesExportPreview exportFormat={activeTab} onClose={onClose} />
</GrafanaExportDrawer>
);
}

interface GrafanaRulesExportPreviewProps {
exportFormat: ExportFormats;
onClose: () => void;
}

function GrafanaRulesExportPreview({ exportFormat, onClose }: GrafanaRulesExportPreviewProps) {
const { currentData: rulesDefinition = '', isFetching } = alertRuleApi.useExportRulesQuery({
format: exportFormat,
});

const downloadFileName = `alert-rules-${new Date().getTime()}`;

if (isFetching) {
return <LoadingPlaceholder text="Loading...." />;
}

return (
<FileExportPreview
format={exportFormat}
textDefinition={rulesDefinition}
downloadFileName={downloadFileName}
onClose={onClose}
/>
);
}