Commit 620a3f2f by Shavonn Brown Committed by GitHub

Azure Monitor: Datasource Config Type (#20183)

* fix subscriptions getting, move subscriptions and workspace fetching into react

* config typed

* moved ConfigEditor to components, parser for select objs

* AzureDataSourceSettings type on state

* typing datasource
parent b1e0a4d2
...@@ -128,4 +128,25 @@ export default class ResponseParser { ...@@ -128,4 +128,25 @@ export default class ResponseParser {
return list; return list;
} }
static parseSubscriptionsForSelect(result: any): Array<{ label: string; value: string }> {
const list: Array<{ label: string; value: string }> = [];
if (!result) {
return list;
}
const valueFieldName = 'subscriptionId';
const textFieldName = 'displayName';
for (let i = 0; i < result.data.value.length; i++) {
if (!_.find(list, ['value', _.get(result.data.value[i], valueFieldName)])) {
list.push({
label: `${_.get(result.data.value[i], textFieldName)} - ${_.get(result.data.value[i], valueFieldName)}`,
value: _.get(result.data.value[i], valueFieldName),
});
}
}
return list;
}
} }
...@@ -21,6 +21,7 @@ const setup = () => { ...@@ -21,6 +21,7 @@ const setup = () => {
withCredentials: false, withCredentials: false,
isDefault: false, isDefault: false,
jsonData: { jsonData: {
subscriptionId: '',
azureLogAnalyticsSameAs: true, azureLogAnalyticsSameAs: true,
cloudName: 'azuremonitor', cloudName: 'azuremonitor',
}, },
......
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { SelectableValue, DataSourcePluginOptionsEditorProps } from '@grafana/data'; import { SelectableValue, DataSourcePluginOptionsEditorProps, DataSourceSettings } from '@grafana/data';
import { MonitorConfig } from './components/MonitorConfig'; import { MonitorConfig } from './MonitorConfig';
import { AnalyticsConfig } from './components/AnalyticsConfig'; import { AnalyticsConfig } from './AnalyticsConfig';
import { TemplateSrv } from 'app/features/templating/template_srv'; import { TemplateSrv } from 'app/features/templating/template_srv';
import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv'; import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv';
import AzureMonitorDatasource from './azure_monitor/azure_monitor_datasource'; import { InsightsConfig } from './InsightsConfig';
import AzureLogAnalyticsDatasource from './azure_log_analytics/azure_log_analytics_datasource'; import ResponseParser from '../azure_monitor/response_parser';
import { InsightsConfig } from './components/InsightsConfig'; import { AzureDataSourceJsonData, AzureDataSourceSecureJsonData } from '../types';
export type Props = DataSourcePluginOptionsEditorProps<any>; export type Props = DataSourcePluginOptionsEditorProps<AzureDataSourceJsonData>;
type AzureDataSourceSettings = DataSourceSettings<AzureDataSourceJsonData, AzureDataSourceSecureJsonData>;
export interface State { export interface State {
config: any; config: AzureDataSourceSettings;
subscriptions: SelectableValue[]; subscriptions: SelectableValue[];
logAnalyticsSubscriptions: SelectableValue[]; logAnalyticsSubscriptions: SelectableValue[];
logAnalyticsWorkspaces: SelectableValue[]; logAnalyticsWorkspaces: SelectableValue[];
...@@ -145,20 +147,63 @@ export class ConfigEditor extends PureComponent<Props, State> { ...@@ -145,20 +147,63 @@ export class ConfigEditor extends PureComponent<Props, State> {
} }
}; };
getSubscriptions = async () => { loadSubscriptions = async (route?: string) => {
if (!this.hasNecessaryCredentials()) { const url = `/${route || this.state.config.jsonData.cloudName}/subscriptions?api-version=2019-03-01`;
return;
} return this.backendSrv
.datasourceRequest({
url: this.state.config.url + url,
method: 'GET',
})
.then((result: any) => {
return ResponseParser.parseSubscriptionsForSelect(result);
})
.catch((error: any) => {
throw error;
});
};
const azureMonitorDatasource = new AzureMonitorDatasource(this.state.config, this.backendSrv, this.templateSrv); loadWorkspaces = async (subscription: string) => {
const { azureLogAnalyticsSameAs, cloudName, logAnalyticsSubscriptionId } = this.state.config.jsonData;
let azureMonitorUrl = '',
subscriptionId = this.templateSrv.replace(subscription || this.state.config.jsonData.subscriptionId);
let subscriptions = (await azureMonitorDatasource.getSubscriptions()) || []; if (!!subscriptionId || !!azureLogAnalyticsSameAs) {
subscriptions = subscriptions.map((subscription: any) => { const azureCloud = cloudName || 'azuremonitor';
azureMonitorUrl = `/${azureCloud}/subscriptions`;
} else {
subscriptionId = logAnalyticsSubscriptionId;
azureMonitorUrl = `/workspacesloganalytics/subscriptions`;
}
const workspaceListUrl =
azureMonitorUrl +
`/${subscriptionId}/providers/Microsoft.OperationalInsights/workspaces?api-version=2017-04-26-preview`;
return this.backendSrv
.datasourceRequest({
url: this.state.config.url + workspaceListUrl,
method: 'GET',
})
.then((result: any) => {
return result.data.value.map((val: any) => {
return { return {
value: subscription.value, value: val.properties.customerId,
label: subscription.text, label: val.name,
}; };
}); });
})
.catch((error: any) => {
throw error;
});
};
getSubscriptions = async () => {
if (!this.hasNecessaryCredentials()) {
return;
}
const subscriptions = (await this.loadSubscriptions()) || [];
if (subscriptions && subscriptions.length > 0) { if (subscriptions && subscriptions.length > 0) {
this.setState({ subscriptions }); this.setState({ subscriptions });
...@@ -176,15 +221,7 @@ export class ConfigEditor extends PureComponent<Props, State> { ...@@ -176,15 +221,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
return; return;
} }
const azureMonitorDatasource = new AzureMonitorDatasource(this.state.config, this.backendSrv, this.templateSrv); const logAnalyticsSubscriptions = (await this.loadSubscriptions('workspacesloganalytics')) || [];
let logAnalyticsSubscriptions = (await azureMonitorDatasource.getSubscriptions('workspacesloganalytics')) || [];
logAnalyticsSubscriptions = logAnalyticsSubscriptions.map((subscription: any) => {
return {
value: subscription.value,
label: subscription.text,
};
});
if (logAnalyticsSubscriptions && logAnalyticsSubscriptions.length > 0) { if (logAnalyticsSubscriptions && logAnalyticsSubscriptions.length > 0) {
this.setState({ logAnalyticsSubscriptions }); this.setState({ logAnalyticsSubscriptions });
...@@ -204,21 +241,9 @@ export class ConfigEditor extends PureComponent<Props, State> { ...@@ -204,21 +241,9 @@ export class ConfigEditor extends PureComponent<Props, State> {
return; return;
} }
const azureLogAnalyticsDatasource = new AzureLogAnalyticsDatasource( const logAnalyticsWorkspaces = await this.loadWorkspaces(
this.state.config,
this.backendSrv,
this.templateSrv
);
let logAnalyticsWorkspaces = await azureLogAnalyticsDatasource.getWorkspaces(
sameAs ? this.state.config.jsonData.subscriptionId : this.state.config.jsonData.logAnalyticsSubscriptionId sameAs ? this.state.config.jsonData.subscriptionId : this.state.config.jsonData.logAnalyticsSubscriptionId
); );
logAnalyticsWorkspaces = logAnalyticsWorkspaces.map((workspace: any) => {
return {
value: workspace.value,
label: workspace.text,
};
});
if (logAnalyticsWorkspaces.length > 0) { if (logAnalyticsWorkspaces.length > 0) {
this.setState({ logAnalyticsWorkspaces }); this.setState({ logAnalyticsWorkspaces });
......
...@@ -46,7 +46,7 @@ export class MonitorConfig extends PureComponent<Props, State> { ...@@ -46,7 +46,7 @@ export class MonitorConfig extends PureComponent<Props, State> {
...this.state.config, ...this.state.config,
jsonData: { jsonData: {
...this.state.config.jsonData, ...this.state.config.jsonData,
cloudName, cloudName: cloudName.value,
}, },
}); });
}; };
......
import { DataSourcePlugin } from '@grafana/data'; import { DataSourcePlugin } from '@grafana/data';
import { AzureMonitorQueryCtrl } from './query_ctrl'; import { AzureMonitorQueryCtrl } from './query_ctrl';
import Datasource from './datasource'; import Datasource from './datasource';
import { ConfigEditor } from './ConfigEditor'; import { ConfigEditor } from './components/ConfigEditor';
import { AzureMonitorAnnotationsQueryCtrl } from './annotations_query_ctrl'; import { AzureMonitorAnnotationsQueryCtrl } from './annotations_query_ctrl';
import { AzureMonitorQuery, AzureDataSourceJsonData } from './types';
export const plugin = new DataSourcePlugin(Datasource) export const plugin = new DataSourcePlugin<Datasource, AzureMonitorQuery, AzureDataSourceJsonData>(Datasource)
.setConfigEditor(ConfigEditor) .setConfigEditor(ConfigEditor)
.setQueryCtrl(AzureMonitorQueryCtrl) .setQueryCtrl(AzureMonitorQueryCtrl)
.setAnnotationQueryCtrl(AzureMonitorAnnotationsQueryCtrl); .setAnnotationQueryCtrl(AzureMonitorAnnotationsQueryCtrl);
...@@ -21,13 +21,18 @@ export interface AzureDataSourceJsonData extends DataSourceJsonData { ...@@ -21,13 +21,18 @@ export interface AzureDataSourceJsonData extends DataSourceJsonData {
logAnalyticsSubscriptionId?: string; logAnalyticsSubscriptionId?: string;
logAnalyticsTenantId?: string; logAnalyticsTenantId?: string;
logAnalyticsClientId?: string; logAnalyticsClientId?: string;
azureLogAnalyticsSameAs?: string; azureLogAnalyticsSameAs?: boolean;
logAnalyticsDefaultWorkspace?: string; logAnalyticsDefaultWorkspace?: string;
// App Insights // App Insights
appInsightsAppId?: string; appInsightsAppId?: string;
} }
export interface AzureDataSourceSecureJsonData {
clientSecret: string;
logAnalyticsClientSecret: string;
}
export interface AzureMetricQuery { export interface AzureMetricQuery {
resourceGroup: string; resourceGroup: string;
resourceName: string; resourceName: string;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment