Skip to content

Commit

Permalink
Alerting: Support newer http_config struct (#69452)
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesdemey committed Jun 7, 2023
1 parent bbd83cb commit a91de30
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit a91de30

Please sign in to comment.