diff --git a/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx b/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx index b8f847a86b4..f84f7a8bb3e 100644 --- a/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx +++ b/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx @@ -17,6 +17,7 @@ import { initialAsyncRequestState } from '../../../utils/redux'; import { ChannelSubForm } from './ChannelSubForm'; import { DeletedSubForm } from './fields/DeletedSubform'; +import { normalizeFormValues } from './util'; interface Props { config: AlertManagerCortexConfig; @@ -48,7 +49,10 @@ export function ReceiverForm({ const notifyApp = useAppNotification(); const styles = useStyles2(getStyles); - const defaultValues = initialValues || { + // normalize deprecated and new config values + const normalizedConfig = normalizeFormValues(initialValues); + + const defaultValues = normalizedConfig ?? { name: '', items: [ { diff --git a/public/app/features/alerting/unified/components/receivers/form/util.test.ts b/public/app/features/alerting/unified/components/receivers/form/util.test.ts new file mode 100644 index 00000000000..f941939ea13 --- /dev/null +++ b/public/app/features/alerting/unified/components/receivers/form/util.test.ts @@ -0,0 +1,58 @@ +import { ChannelValues, ReceiverFormValues } from '../../../types/receiver-form'; + +import { DeprecatedAuthHTTPConfig, HTTPAuthConfig, normalizeFormValues } from './util'; + +describe('normalizeFormValues', () => { + it('should leave the older config alone', () => { + const config = createContactPoint({ bearer_token: 'token' }); + expect(normalizeFormValues(config)).toEqual(config); + }); + + it('should leave the older config alone', () => { + const config = createContactPoint({ bearer_token_file: 'file' }); + expect(normalizeFormValues(config)).toEqual(config); + }); + + it('should normalize newer config', () => { + const config = createContactPoint({ + authorization: { + type: 'bearer', + credentials: 'token', + }, + }); + + expect(normalizeFormValues(config)).toEqual(createContactPoint({ bearer_token: 'token' })); + }); + + it('should normalize newer config', () => { + const config = createContactPoint({ + authorization: { + type: 'bearer', + credentials_file: 'file', + }, + }); + + expect(normalizeFormValues(config)).toEqual(createContactPoint({ bearer_token_file: 'file' })); + }); +}); + +function createContactPoint(httpConfig: DeprecatedAuthHTTPConfig | HTTPAuthConfig) { + const config: ReceiverFormValues = { + name: 'My Contact Point', + items: [ + { + __id: '', + type: '', + secureSettings: {}, + secureFields: {}, + settings: { + http_config: { + ...httpConfig, + }, + }, + }, + ], + }; + + return config; +} diff --git a/public/app/features/alerting/unified/components/receivers/form/util.ts b/public/app/features/alerting/unified/components/receivers/form/util.ts new file mode 100644 index 00000000000..ec4279257c0 --- /dev/null +++ b/public/app/features/alerting/unified/components/receivers/form/util.ts @@ -0,0 +1,54 @@ +import { omit } from 'lodash'; + +import { ChannelValues, ReceiverFormValues } from '../../../types/receiver-form'; + +export interface DeprecatedAuthHTTPConfig { + bearer_token?: string; + bearer_token_file?: string; +} + +export interface HTTPAuthConfig { + authorization: { + type: string; + credentials?: string; + credentials_file?: string; + }; +} + +// convert the newer http_config to the older (deprecated) format +export function normalizeFormValues( + values?: ReceiverFormValues +): ReceiverFormValues | undefined { + if (!values) { + return; + } + + return { + ...values, + items: values.items.map((item) => ({ + ...item, + settings: { + ...item.settings, + http_config: item.settings?.http_config ? normalizeHTTPConfig(item.settings?.http_config) : undefined, + }, + })), + }; +} + +function normalizeHTTPConfig(config: HTTPAuthConfig | DeprecatedAuthHTTPConfig): DeprecatedAuthHTTPConfig { + if (isDeprecatedHTTPAuthConfig(config)) { + return config; + } + + return { + ...omit(config, 'authorization'), + bearer_token: config.authorization.credentials, + bearer_token_file: config.authorization.credentials_file, + }; +} + +function isDeprecatedHTTPAuthConfig( + config: HTTPAuthConfig | DeprecatedAuthHTTPConfig +): config is DeprecatedAuthHTTPConfig { + return ['bearer_token', 'bearer_token_file'].some((prop) => prop in config); +}