PublicDashboards: Allow disabling an existent public dashboard if it has template variables (#55778)

This commit is contained in:
Ezequiel Victorero 2022-09-28 15:34:53 -03:00 committed by GitHub
parent a281c26580
commit b370a8e366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 21 deletions

View File

@ -96,16 +96,12 @@ func (pd *PublicDashboardServiceImpl) GetPublicDashboardConfig(ctx context.Conte
// SavePublicDashboardConfig is a helper method to persist the sharing config
// to the database. It handles validations for sharing config and persistence
func (pd *PublicDashboardServiceImpl) SavePublicDashboardConfig(ctx context.Context, u *user.SignedInUser, dto *SavePublicDashboardConfigDTO) (*PublicDashboard, error) {
// validate if the dashboard exists
dashboard, err := pd.GetDashboard(ctx, dto.DashboardUid)
if err != nil {
return nil, err
}
err = validation.ValidateSavePublicDashboard(dto, dashboard)
if err != nil {
return nil, err
}
// set default value for time settings
if dto.PublicDashboard.TimeSettings == nil {
dto.PublicDashboard.TimeSettings = &TimeSettings{}
@ -120,6 +116,10 @@ func (pd *PublicDashboardServiceImpl) SavePublicDashboardConfig(ctx context.Cont
// save changes
var pubdashUid string
if existingPubdash == nil {
err = validation.ValidateSavePublicDashboard(dto, dashboard)
if err != nil {
return nil, err
}
pubdashUid, err = pd.savePublicDashboardConfig(ctx, dto)
} else {
pubdashUid, err = pd.updatePublicDashboardConfig(ctx, dto)

View File

@ -13,14 +13,12 @@ import { getTimeRange } from 'app/features/dashboard/utils/timeRange';
export const Configuration = ({
disabled,
isPubDashEnabled,
hasTemplateVariables,
onToggleEnabled,
dashboard,
}: {
disabled: boolean;
isPubDashEnabled?: boolean;
onToggleEnabled: () => void;
hasTemplateVariables: boolean;
dashboard: DashboardModel;
}) => {
const selectors = e2eSelectors.pages.ShareDashboardModal.PublicDashboard;
@ -41,7 +39,6 @@ export const Configuration = ({
<Layout orientation={isDesktop ? 0 : 1} spacing="xs" justify="space-between">
<Label description="Configures whether current dashboard can be available publicly">Enabled</Label>
<Switch
disabled={hasTemplateVariables}
data-testid={selectors.EnableSwitch}
value={isPubDashEnabled}
onChange={() => {

View File

@ -5,8 +5,6 @@ import { GrafanaTheme2 } from '@grafana/data/src';
import { selectors as e2eSelectors } from '@grafana/e2e-selectors/src';
import { reportInteraction } from '@grafana/runtime/src';
import { Alert, Button, ClipboardButton, Field, HorizontalGroup, Input, useStyles2, Spinner } from '@grafana/ui/src';
import { notifyApp } from 'app/core/actions';
import { createErrorNotification } from 'app/core/copy/appNotification';
import { contextSrv } from 'app/core/services/context_srv';
import { useGetConfigQuery, useSaveConfigMutation } from 'app/features/dashboard/api/publicDashboardApi';
import { AcknowledgeCheckboxes } from 'app/features/dashboard/components/ShareModal/SharePublicDashboard/AcknowledgeCheckboxes';
@ -20,7 +18,6 @@ import {
} from 'app/features/dashboard/components/ShareModal/SharePublicDashboard/SharePublicDashboardUtils';
import { ShareModalTabProps } from 'app/features/dashboard/components/ShareModal/types';
import { isOrgAdmin } from 'app/features/plugins/admin/permissions';
import { dispatch } from 'app/store/store';
import { AccessControlAction } from 'app/types';
interface Props extends ShareModalTabProps {}
@ -81,13 +78,6 @@ export const SharePublicDashboard = (props: Props) => {
const onSavePublicConfig = () => {
reportInteraction('grafana_dashboards_public_create_clicked');
if (dashboardHasTemplateVariables(dashboardVariables)) {
dispatch(
notifyApp(createErrorNotification('This dashboard cannot be made public because it has template variables'))
);
return;
}
saveConfig({
dashboard: props.dashboard,
payload: { ...publicDashboard!, isEnabled: enabledSwitch.isEnabled },
@ -111,7 +101,7 @@ export const SharePublicDashboard = (props: Props) => {
{isFetchingLoading && <Spinner />}
</HorizontalGroup>
<div className={styles.content}>
{dashboardHasTemplateVariables(dashboardVariables) ? (
{dashboardHasTemplateVariables(dashboardVariables) && !publicDashboardPersisted(publicDashboard) ? (
<Alert
severity="warning"
title="dashboard cannot be public"
@ -140,7 +130,6 @@ export const SharePublicDashboard = (props: Props) => {
onToggleEnabled={() =>
setEnabledSwitch((prevState) => ({ isEnabled: !prevState.isEnabled, wasTouched: true }))
}
hasTemplateVariables={dashboardHasTemplateVariables(dashboardVariables)}
/>
{publicDashboardPersisted(publicDashboard) && enabledSwitch.isEnabled && (
<Field label="Link URL" className={styles.publicUrl}>
@ -163,11 +152,18 @@ export const SharePublicDashboard = (props: Props) => {
</Field>
)}
{hasWritePermissions ? (
props.dashboard.hasUnsavedChanges() && (
props.dashboard.hasUnsavedChanges() ? (
<Alert
title="Please save your dashboard changes before updating the public configuration"
severity="warning"
/>
) : (
dashboardHasTemplateVariables(dashboardVariables) && (
<Alert
title="This public dashboard may not work since it uses template variables"
severity="warning"
/>
)
)
) : (
<Alert title="You don't have permissions to create or update a public dashboard" severity="warning" />