mirror of
https://github.com/grafana/grafana.git
synced 2025-01-26 16:27:02 -06:00
Alerting: Support newer http_config struct (#69452)
This commit is contained in:
parent
bbd83cbaeb
commit
a91de30f99
@ -17,6 +17,7 @@ import { initialAsyncRequestState } from '../../../utils/redux';
|
||||
|
||||
import { ChannelSubForm } from './ChannelSubForm';
|
||||
import { DeletedSubForm } from './fields/DeletedSubform';
|
||||
import { normalizeFormValues } from './util';
|
||||
|
||||
interface Props<R extends ChannelValues> {
|
||||
config: AlertManagerCortexConfig;
|
||||
@ -48,7 +49,10 @@ export function ReceiverForm<R extends ChannelValues>({
|
||||
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: [
|
||||
{
|
||||
|
@ -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<ChannelValues> = {
|
||||
name: 'My Contact Point',
|
||||
items: [
|
||||
{
|
||||
__id: '',
|
||||
type: '',
|
||||
secureSettings: {},
|
||||
secureFields: {},
|
||||
settings: {
|
||||
http_config: {
|
||||
...httpConfig,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
return config;
|
||||
}
|
@ -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<ChannelValues>
|
||||
): ReceiverFormValues<ChannelValues> | 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user