2019-08-19 17:35:44 -04:00
|
|
|
import React, { PureComponent } from 'react';
|
2019-12-04 14:35:53 -05:00
|
|
|
import {
|
|
|
|
|
DataSourcePluginOptionsEditorProps,
|
2021-05-11 12:34:41 -07:00
|
|
|
SelectableValue,
|
|
|
|
|
updateDatasourcePluginJsonDataOption,
|
2019-12-04 14:35:53 -05:00
|
|
|
updateDatasourcePluginOption,
|
2020-01-10 14:26:33 -05:00
|
|
|
updateDatasourcePluginResetOption,
|
|
|
|
|
updateDatasourcePluginSecureJsonDataOption,
|
2019-12-04 14:35:53 -05:00
|
|
|
} from '@grafana/data';
|
2019-11-06 09:29:07 -05:00
|
|
|
import { MonitorConfig } from './MonitorConfig';
|
|
|
|
|
import { AnalyticsConfig } from './AnalyticsConfig';
|
2021-05-11 12:34:41 -07:00
|
|
|
import { getBackendSrv, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
|
2019-11-06 09:29:07 -05:00
|
|
|
import { InsightsConfig } from './InsightsConfig';
|
|
|
|
|
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 { isAppInsightsConfigured } from '../credentials';
|
|
|
|
|
import { routeNames } from '../utils/common';
|
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
|
|
|
|
|
|
|
|
export interface State {
|
2021-05-11 12:34:41 -07:00
|
|
|
unsaved: boolean;
|
2021-05-27 20:28:36 +01:00
|
|
|
appInsightsInitiallyConfigured: boolean;
|
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,
|
2021-05-27 20:28:36 +01:00
|
|
|
appInsightsInitiallyConfigured: isAppInsightsConfigured(props.options),
|
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-05-11 12:34:41 -07:00
|
|
|
const result = await getBackendSrv().datasourceRequest({
|
2021-07-16 12:47:26 +02:00
|
|
|
url: this.baseURL + query,
|
2021-05-11 12:34:41 -07:00
|
|
|
method: 'GET',
|
2019-12-13 11:02:39 -05:00
|
|
|
});
|
2019-08-19 17:35:44 -04:00
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
return ResponseParser.parseSubscriptionsForSelect(result);
|
2019-08-19 17:35:44 -04:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
private getLogAnalyticsSubscriptions = async (): Promise<Array<SelectableValue<string>>> => {
|
|
|
|
|
await this.saveOptions();
|
2019-08-19 17:35:44 -04:00
|
|
|
|
2021-07-16 12:47:26 +02:00
|
|
|
const query = `?api-version=2019-03-01`;
|
2020-01-21 09:08:07 +00:00
|
|
|
const result = await getBackendSrv().datasourceRequest({
|
2021-07-16 12:47:26 +02:00
|
|
|
url: this.baseURL + query,
|
2019-12-04 14:35:53 -05:00
|
|
|
method: 'GET',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ResponseParser.parseSubscriptionsForSelect(result);
|
2019-11-06 09:29:07 -05:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
private getWorkspaces = async (subscriptionId: string): Promise<Array<SelectableValue<string>>> => {
|
|
|
|
|
await this.saveOptions();
|
2019-11-06 09:29:07 -05:00
|
|
|
|
2021-07-16 12:47:26 +02:00
|
|
|
const workspaceURL = `/${subscriptionId}/providers/Microsoft.OperationalInsights/workspaces?api-version=2017-04-26-preview`;
|
2020-01-21 09:08:07 +00:00
|
|
|
const result = await getBackendSrv().datasourceRequest({
|
2021-07-16 12:47:26 +02:00
|
|
|
url: this.baseURL + workspaceURL,
|
2019-12-04 14:35:53 -05:00
|
|
|
method: 'GET',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ResponseParser.parseWorkspacesForSelect(result);
|
2019-11-06 09:29:07 -05:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
// TODO: Used only by InsightsConfig
|
|
|
|
|
private onUpdateJsonDataOption = (key: keyof AzureDataSourceJsonData) => (
|
|
|
|
|
event: React.SyntheticEvent<HTMLInputElement | HTMLSelectElement>
|
|
|
|
|
) => {
|
|
|
|
|
updateDatasourcePluginJsonDataOption(this.props, key, event.currentTarget.value);
|
2019-08-19 17:35:44 -04:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
// TODO: Used only by InsightsConfig
|
|
|
|
|
private onUpdateSecureJsonDataOption = (key: keyof AzureDataSourceSecureJsonData) => (
|
|
|
|
|
event: React.SyntheticEvent<HTMLInputElement | HTMLSelectElement>
|
|
|
|
|
) => {
|
|
|
|
|
updateDatasourcePluginSecureJsonDataOption(this.props, key, event.currentTarget.value);
|
2019-08-19 17:35:44 -04:00
|
|
|
};
|
|
|
|
|
|
2021-05-11 12:34:41 -07:00
|
|
|
// TODO: Used only by InsightsConfig
|
|
|
|
|
private resetSecureKey = (key: keyof AzureDataSourceSecureJsonData) => {
|
|
|
|
|
updateDatasourcePluginResetOption(this.props, key);
|
2019-08-19 17:35:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-12-04 14:35:53 -05:00
|
|
|
const { options } = this.props;
|
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} />
|
2019-08-19 17:35:44 -04:00
|
|
|
|
|
|
|
|
<AnalyticsConfig
|
2019-12-04 14:35:53 -05:00
|
|
|
options={options}
|
2021-05-11 12:34:41 -07:00
|
|
|
updateOptions={this.updateOptions}
|
|
|
|
|
getSubscriptions={this.getLogAnalyticsSubscriptions}
|
|
|
|
|
getWorkspaces={this.getWorkspaces}
|
2019-08-19 17:35:44 -04:00
|
|
|
/>
|
|
|
|
|
|
2021-05-27 20:28:36 +01:00
|
|
|
{this.state.appInsightsInitiallyConfigured && (
|
|
|
|
|
<InsightsConfig
|
|
|
|
|
options={options}
|
|
|
|
|
onUpdateJsonDataOption={this.onUpdateJsonDataOption}
|
|
|
|
|
onUpdateSecureJsonDataOption={this.onUpdateSecureJsonDataOption}
|
|
|
|
|
onResetOptionKey={this.resetSecureKey}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2019-08-19 17:35:44 -04:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ConfigEditor;
|