Skip to content

Commit 4a89fe5

Browse files
committed
Fix data source test on raw edition
Using checkmk raw as a data source showed an error message on data source "test & save", although the raw edition was correctly chosen in the edition dropdown. refs #55
1 parent e59e5b0 commit 4a89fe5

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/DataSource.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { getBackendSrv } from '@grafana/runtime';
1212

1313
import { buildRequestBody, combinedDesc, graphDefinitionRequest } from './graphspecs';
14-
import { MyQuery, defaultQuery } from './types';
14+
import { MyQuery, defaultQuery, MyDataSourceOptions } from './types';
1515

1616
export const buildUrlWithParams = (url: string, params: any) => url + '?' + new URLSearchParams(params).toString();
1717

@@ -34,7 +34,7 @@ function buildMetricDataFrame(response: any, query: MyQuery) {
3434
}
3535

3636
export class DataSource extends DataSourceApi<MyQuery> {
37-
constructor(private instanceSettings: DataSourceInstanceSettings) {
37+
constructor(private instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
3838
super(instanceSettings);
3939
}
4040

@@ -70,21 +70,27 @@ export class DataSource extends DataSourceApi<MyQuery> {
7070
params: { action: 'get_combined_graph_identifications' },
7171
data: buildRequestBody(combinedDesc({ host: { host: 'ARANDOMNAME' } })),
7272
context: {},
73-
}).then((response) => {
74-
if (
75-
response.data.result_code === 1 &&
76-
response.data.result === 'Checkmk exception: Currently not supported with this Checkmk Edition' &&
77-
get(this, 'instanceSettings.jsonData.edition', 'CEE') === 'CEE'
78-
) {
79-
throw new Error('Mismatch between selected checkmk edition and monitoring site edition');
80-
}
81-
82-
return {
83-
status: 'success',
84-
message: 'Data source is working',
85-
title: 'Success',
86-
};
87-
});
73+
})
74+
.catch((error) => {
75+
let firstLineOfError = error.message.split('\n')[0];
76+
if (firstLineOfError === 'Checkmk exception: Currently not supported with this Checkmk Edition') {
77+
if ((this.instanceSettings.jsonData.edition ?? 'CEE') === 'CEE') {
78+
// edition dropdown = cee, so seeing this error means that we speak with a raw edition
79+
throw new Error('Mismatch between selected Checkmk edition and monitoring site edition');
80+
} else {
81+
// edition dropdown = raw, so seeing this error is expected (but auth worked, so we are fine)
82+
return;
83+
}
84+
}
85+
throw error;
86+
})
87+
.then((response) => {
88+
return {
89+
status: 'success',
90+
message: 'Data source is working',
91+
title: 'Success',
92+
};
93+
});
8894
}
8995

9096
async doRequest(options: MyQuery) {
@@ -120,10 +126,7 @@ export class DataSource extends DataSourceApi<MyQuery> {
120126

121127
if (typeof result.data === 'string') {
122128
throw new Error(`${result.data}`);
123-
} else if (
124-
result.data.result_code !== 0 &&
125-
result.data.result !== 'Checkmk exception: Currently not supported with this Checkmk Edition'
126-
) {
129+
} else if (result.data.result_code !== 0) {
127130
throw new Error(`${result.data.result}`);
128131
} else {
129132
return result;

0 commit comments

Comments
 (0)