2022-04-22 14:33:13 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
|
2022-03-02 06:42:30 -08:00
|
|
|
import { DataSourcePluginOptionsEditorProps, SelectableValue, updateDatasourcePluginOption } from '@grafana/data';
|
2022-06-15 08:59:29 +01:00
|
|
|
import { getBackendSrv, getTemplateSrv, isFetchError, TemplateSrv } from '@grafana/runtime';
|
2023-03-17 07:15:20 -05:00
|
|
|
import { Alert, SecureSocksProxySettings } from '@grafana/ui';
|
|
|
|
|
import { config } from 'app/core/config';
|
2022-03-02 06:42:30 -08:00
|
|
|
|
2019-11-06 09:29:07 -05:00
|
|
|
import ResponseParser from '../azure_monitor/response_parser';
|
2019-12-04 14:35:53 -05:00
|
|
|
import { AzureDataSourceJsonData, AzureDataSourceSecureJsonData, AzureDataSourceSettings } from '../types';
|
2021-07-16 12:47:26 +02:00
|
|
|
import { routeNames } from '../utils/common';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
|
|
|
|
import { MonitorConfig } from './MonitorConfig';
|
2019-08-19 17:35:44 -04:00
|
|
|
|
2019-12-04 14:35:53 -05:00
|
|
|
export type Props = DataSourcePluginOptionsEditorProps<AzureDataSourceJsonData, AzureDataSourceSecureJsonData>;
|
2019-08-19 17:35:44 -04:00
|
|
|
|
2021-08-17 16:46:15 +01:00
|
|
|
interface ErrorMessage {
|
|
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
details?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 17:35:44 -04:00
|
|
|
export interface State {
|
2021-05-11 12:34:41 -07:00
|
|
|
unsaved: boolean;
|
2021-08-17 16:46:15 +01:00
|
|
|
error?: ErrorMessage;
|
2019-08-19 17:35:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ConfigEditor extends PureComponent<Props, State> {
|
2020-07-08 11:05:20 +02:00
|
|
|
templateSrv: TemplateSrv = getTemplateSrv();
|
2021-07-16 12:47:26 +02:00
|
|
|
baseURL: string;
|
2020-07-08 11:05:20 +02:00
|
|
|
|
2019-08-19 17:35:44 -04:00
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2021-05-11 12:34:41 -07:00
|
|
|
unsaved: false,
|
2019-08-19 17:35:44 -04:00
|
|
|
};
|
2021-07-16 12:47:26 +02:00
|
|
|
this.baseURL = `/api/datasources/${this.props.options.id}/resources/${routeNames.azureMonitor}/subscriptions`;
|
2019-08-19 17:35:44 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
private updateOptions = (optionsFunc: (options: AzureDataSourceSettings) => AzureDataSourceSettings): void => {
|
|
|
|
|
const updated = optionsFunc(this.props.options);
|
|
|
|
|
this.props.onOptionsChange(updated);
|
2019-12-04 14:35:53 -05:00
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
this.setState({ unsaved: true });
|
2019-08-19 17:35:44 -04:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
private saveOptions = async (): Promise<void> => {
|
|
|
|
|
if (this.state.unsaved) {
|
|
|
|
|
await getBackendSrv()
|
|
|
|
|
.put(`/api/datasources/${this.props.options.id}`, this.props.options)
|
|
|
|
|
.then((result: { datasource: AzureDataSourceSettings }) => {
|
|
|
|
|
updateDatasourcePluginOption(this.props, 'version', result.datasource.version);
|
|
|
|
|
});
|
2020-01-10 14:26:33 -05:00
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
this.setState({ unsaved: false });
|
|
|
|
|
}
|
2019-12-04 14:35:53 -05:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
private getSubscriptions = async (): Promise<Array<SelectableValue<string>>> => {
|
|
|
|
|
await this.saveOptions();
|
2019-12-04 14:35:53 -05:00
|
|
|
|
2021-07-16 12:47:26 +02:00
|
|
|
const query = `?api-version=2019-03-01`;
|
2021-08-17 16:46:15 +01:00
|
|
|
try {
|
|
|
|
|
const result = await getBackendSrv()
|
|
|
|
|
.fetch({
|
|
|
|
|
url: this.baseURL + query,
|
|
|
|
|
method: 'GET',
|
|
|
|
|
})
|
|
|
|
|
.toPromise();
|
|
|
|
|
|
|
|
|
|
this.setState({ error: undefined });
|
|
|
|
|
return ResponseParser.parseSubscriptionsForSelect(result);
|
|
|
|
|
} catch (err) {
|
2022-06-15 08:59:29 +01:00
|
|
|
if (isFetchError(err)) {
|
|
|
|
|
this.setState({
|
|
|
|
|
error: {
|
|
|
|
|
title: 'Error requesting subscriptions',
|
|
|
|
|
description: 'Could not request subscriptions from Azure. Check your credentials and try again.',
|
|
|
|
|
details: err?.data?.message,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-08-17 16:46:15 +01:00
|
|
|
return Promise.resolve([]);
|
|
|
|
|
}
|
2019-08-19 17:35:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2023-03-17 07:15:20 -05:00
|
|
|
const { options, onOptionsChange } = this.props;
|
2021-08-17 16:46:15 +01:00
|
|
|
const { error } = this.state;
|
2019-08-19 17:35:44 -04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2021-05-11 12:34:41 -07:00
|
|
|
<MonitorConfig options={options} updateOptions={this.updateOptions} getSubscriptions={this.getSubscriptions} />
|
2021-08-17 16:46:15 +01:00
|
|
|
{error && (
|
|
|
|
|
<Alert severity="error" title={error.title}>
|
|
|
|
|
<p>{error.description}</p>
|
|
|
|
|
{error.details && <details style={{ whiteSpace: 'pre-wrap' }}>{error.details}</details>}
|
|
|
|
|
</Alert>
|
|
|
|
|
)}
|
2023-03-17 07:15:20 -05:00
|
|
|
{config.featureToggles.secureSocksDatasourceProxy && (
|
|
|
|
|
<SecureSocksProxySettings options={options} onOptionsChange={onOptionsChange} />
|
|
|
|
|
)}
|
2019-08-19 17:35:44 -04:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ConfigEditor;
|