import React, { PureComponent } from 'react'; import { InlineFormLabel, LegacyForms, Button } from '@grafana/ui'; const { Select, Input } = LegacyForms; import { AppEvents, SelectableValue, DataSourcePluginOptionsEditorProps, onUpdateDatasourceJsonDataOptionSelect, onUpdateDatasourceResetOption, onUpdateDatasourceJsonDataOption, onUpdateDatasourceSecureJsonDataOption, } from '@grafana/data'; import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; import { CloudWatchDatasource } from '../datasource'; import { CloudWatchJsonData, CloudWatchSecureJsonData } from '../types'; import { CancelablePromise, makePromiseCancelable } from 'app/core/utils/CancelablePromise'; import { appEvents } from 'app/core/core'; const authProviderOptions = [ { label: 'AWS SDK Default', value: 'default' }, { label: 'Access & secret key', value: 'keys' }, { label: 'Credentials file', value: 'credentials' }, ] as SelectableValue[]; export type Props = DataSourcePluginOptionsEditorProps; export interface State { regions: SelectableValue[]; } export class ConfigEditor extends PureComponent { constructor(props: Props) { super(props); this.state = { regions: [], }; } loadRegionsPromise: CancelablePromise | null = null; componentDidMount() { this.loadRegionsPromise = makePromiseCancelable(this.loadRegions()); this.loadRegionsPromise.promise.catch(({ isCanceled }) => { if (isCanceled) { console.warn('Cloud Watch ConfigEditor has unmounted, initialization was canceled'); } }); if (this.props.options.jsonData.authType === 'arn') { appEvents.emit(AppEvents.alertWarning, [ 'Since grafana 7.3 authentication type "arn" is deprecated, falling back to default SDK provider', ]); } else if ( this.props.options.jsonData.authType === 'credentials' && !this.props.options.jsonData.profile && !this.props.options.jsonData.database ) { appEvents.emit(AppEvents.alertWarning, [ 'As of grafana 7.3 authentication type "credentials" should be used only for shared file credentials. \ If you don\'t have a credentials file, switch to the default SDK provider for extracting credentials \ from environment variables or IAM roles', ]); } } componentWillUnmount() { if (this.loadRegionsPromise) { this.loadRegionsPromise.cancel(); } } async loadRegions() { await getDatasourceSrv() .loadDatasource(this.props.options.name) .then((ds: CloudWatchDatasource) => ds.getRegions()) .then( (regions: any) => { this.setState({ regions: regions.map((region: any) => { return { value: region.value, label: region.text, }; }), }); }, (err: any) => { const regions = [ 'af-south-1', 'ap-east-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3', 'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', 'cn-north-1', 'cn-northwest-1', 'eu-central-1', 'eu-north-1', 'eu-south-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'me-south-1', 'sa-east-1', 'us-east-1', 'us-east-2', 'us-gov-east-1', 'us-gov-west-1', 'us-iso-east-1', 'us-isob-east-1', 'us-west-1', 'us-west-2', ]; this.setState({ regions: regions.map((region: string) => ({ value: region, label: region, })), }); // expected to fail when creating new datasource // console.error('failed to get latest regions', err); } ); } render() { const { regions } = this.state; const { options } = this.props; const secureJsonData = (options.secureJsonData || {}) as CloudWatchSecureJsonData; let profile = options.jsonData.profile; if (profile === undefined) { profile = options.database; } return ( <>

CloudWatch Details

Authentication Provider
)} {options.jsonData.authType === 'keys' && (
{options.secureJsonFields?.accessKey ? (
Access Key ID
) : (
Access Key ID
)} {options.secureJsonFields?.secretKey ? (
Secret Access Key
) : (
Secret Access Key
)}
)}
Assume Role ARN
External ID
Default Region
Endpoint
); } } export default ConfigEditor;