-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alerting: Support newer http_config struct (#69452)
- Loading branch information
1 parent
bbd83cb
commit a91de30
Showing
3 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
public/app/features/alerting/unified/components/receivers/form/util.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
54 changes: 54 additions & 0 deletions
54
public/app/features/alerting/unified/components/receivers/form/util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |