|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
import React from 'react'; |
|
|
|
|
|
|
|
|
|
import { Button, ConfirmModal, Field, Form, HorizontalGroup, TextArea } from '@grafana/ui'; |
|
|
|
|
import { Button, CodeEditor, ConfirmModal, Field, Form, HorizontalGroup } from '@grafana/ui'; |
|
|
|
|
|
|
|
|
|
import { GRAFANA_RULES_SOURCE_NAME } from '../../utils/datasource'; |
|
|
|
|
|
|
|
|
@ -31,33 +31,46 @@ export const ConfigEditor = ({ |
|
|
|
|
}: ConfigEditorProps) => { |
|
|
|
|
return ( |
|
|
|
|
<Form defaultValues={defaultValues} onSubmit={onSubmit} key={defaultValues.configJSON}> |
|
|
|
|
{({ register, errors }) => ( |
|
|
|
|
<> |
|
|
|
|
{!readOnly && ( |
|
|
|
|
<> |
|
|
|
|
<Field |
|
|
|
|
disabled={loading} |
|
|
|
|
label="Configuration" |
|
|
|
|
invalid={!!errors.configJSON} |
|
|
|
|
error={errors.configJSON?.message} |
|
|
|
|
> |
|
|
|
|
<TextArea |
|
|
|
|
{...register('configJSON', { |
|
|
|
|
required: { value: true, message: 'Required.' }, |
|
|
|
|
validate: (v) => { |
|
|
|
|
try { |
|
|
|
|
JSON.parse(v); |
|
|
|
|
return true; |
|
|
|
|
} catch (e) { |
|
|
|
|
return e instanceof Error ? e.message : 'Invalid JSON.'; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
})} |
|
|
|
|
id="configuration" |
|
|
|
|
rows={25} |
|
|
|
|
/> |
|
|
|
|
</Field> |
|
|
|
|
{({ errors, setValue, register }) => { |
|
|
|
|
register('configJSON', { |
|
|
|
|
required: { value: true, message: 'Required' }, |
|
|
|
|
validate: (value: string) => { |
|
|
|
|
try { |
|
|
|
|
JSON.parse(value); |
|
|
|
|
return true; |
|
|
|
|
} catch (e) { |
|
|
|
|
return e instanceof Error ? e.message : 'JSON is invalid'; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<> |
|
|
|
|
<Field |
|
|
|
|
disabled={loading} |
|
|
|
|
label="Configuration" |
|
|
|
|
invalid={!!errors.configJSON} |
|
|
|
|
error={errors.configJSON?.message} |
|
|
|
|
data-testid={readOnly ? 'readonly-config' : 'config'} |
|
|
|
|
> |
|
|
|
|
<CodeEditor |
|
|
|
|
language="json" |
|
|
|
|
width="100%" |
|
|
|
|
height={500} |
|
|
|
|
showLineNumbers={true} |
|
|
|
|
value={defaultValues.configJSON} |
|
|
|
|
showMiniMap={false} |
|
|
|
|
onSave={(value) => { |
|
|
|
|
setValue('configJSON', value); |
|
|
|
|
}} |
|
|
|
|
onBlur={(value) => { |
|
|
|
|
setValue('configJSON', value); |
|
|
|
|
}} |
|
|
|
|
readOnly={readOnly} |
|
|
|
|
/> |
|
|
|
|
</Field> |
|
|
|
|
|
|
|
|
|
{!readOnly && ( |
|
|
|
|
<HorizontalGroup> |
|
|
|
|
<Button type="submit" variant="primary" disabled={loading}> |
|
|
|
|
Save configuration |
|
|
|
@ -68,29 +81,25 @@ export const ConfigEditor = ({ |
|
|
|
|
</Button> |
|
|
|
|
)} |
|
|
|
|
</HorizontalGroup> |
|
|
|
|
</> |
|
|
|
|
)} |
|
|
|
|
{readOnly && ( |
|
|
|
|
<Field label="Configuration"> |
|
|
|
|
<pre data-testid="readonly-config">{defaultValues.configJSON}</pre> |
|
|
|
|
</Field> |
|
|
|
|
)} |
|
|
|
|
{Boolean(showConfirmDeleteAMConfig) && onConfirmReset && onDismiss && ( |
|
|
|
|
<ConfirmModal |
|
|
|
|
isOpen={true} |
|
|
|
|
title="Reset Alertmanager configuration" |
|
|
|
|
body={`Are you sure you want to reset configuration ${ |
|
|
|
|
alertManagerSourceName === GRAFANA_RULES_SOURCE_NAME |
|
|
|
|
? 'for the Grafana Alertmanager' |
|
|
|
|
: `for "${alertManagerSourceName}"` |
|
|
|
|
}? Contact points and notification policies will be reset to their defaults.`}
|
|
|
|
|
confirmText="Yes, reset configuration" |
|
|
|
|
onConfirm={onConfirmReset} |
|
|
|
|
onDismiss={onDismiss} |
|
|
|
|
/> |
|
|
|
|
)} |
|
|
|
|
</> |
|
|
|
|
)} |
|
|
|
|
)} |
|
|
|
|
|
|
|
|
|
{Boolean(showConfirmDeleteAMConfig) && onConfirmReset && onDismiss && ( |
|
|
|
|
<ConfirmModal |
|
|
|
|
isOpen={true} |
|
|
|
|
title="Reset Alertmanager configuration" |
|
|
|
|
body={`Are you sure you want to reset configuration ${ |
|
|
|
|
alertManagerSourceName === GRAFANA_RULES_SOURCE_NAME |
|
|
|
|
? 'for the Grafana Alertmanager' |
|
|
|
|
: `for "${alertManagerSourceName}"` |
|
|
|
|
}? Contact points and notification policies will be reset to their defaults.`}
|
|
|
|
|
confirmText="Yes, reset configuration" |
|
|
|
|
onConfirm={onConfirmReset} |
|
|
|
|
onDismiss={onDismiss} |
|
|
|
|
/> |
|
|
|
|
)} |
|
|
|
|
</> |
|
|
|
|
); |
|
|
|
|
}} |
|
|
|
|
</Form> |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|