Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align defaults for datasource settings #236

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import RestApiBackend from './backend/rest';
import { Backend } from './backend/types';
import WebApiBackend from './backend/web';
import { Settings } from './settings';
import { Backend as BackendType, CmkQuery, DataSourceOptions, Edition, ResponseDataAutocomplete } from './types';
import { AutoCompleteParams } from './ui/autocomplete';
import { createCmkContext } from './utils';
Expand All @@ -20,11 +21,13 @@
export class DataSource extends DataSourceApi<CmkQuery> {
webBackend: WebApiBackend;
restBackend: RestApiBackend;
settings: Settings;

constructor(private instanceSettings: DataSourceInstanceSettings<DataSourceOptions>) {
super(instanceSettings);
this.webBackend = new WebApiBackend(this);
this.restBackend = new RestApiBackend(this);
this.settings = new Settings(instanceSettings.jsonData);
}

async query(dataQueryRequest: DataQueryRequest<CmkQuery>): Promise<DataQueryResponse> {
Expand All @@ -34,7 +37,7 @@
return this.getBackend().query(dataQueryRequest);
}

async metricFindQuery(query: MetricFindQuery, options?: any): Promise<MetricFindValue[]> {

Check warning on line 40 in src/DataSource.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 40 in src/DataSource.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 40 in src/DataSource.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
if (query.objectType === 'site') {
// rest-api site endpoint were added in 2.2.0 so we have to use the web-api here
// TODO: clean up (remove filterSites from Backend) with end of 2.1.0
Expand Down Expand Up @@ -94,14 +97,12 @@
return this.instanceSettings.url;
}

// TODO: Move config default values to a central place instead of scattering it in getEdition and getBackendType

getEdition(): Edition {
return this.instanceSettings.jsonData.edition ?? 'RAW';
return this.settings.edition;
}

getBackendType(): BackendType {
return this.instanceSettings.jsonData.backend ?? 'rest';
return this.settings.backend;
}

getBackend(): Backend {
Expand All @@ -112,10 +113,6 @@
}

getUsername(): string {
const username = this.instanceSettings.jsonData.username;
if (typeof username === 'string') {
return username;
}
throw Error('Impossible');
return this.settings.username;
}
}
4 changes: 2 additions & 2 deletions src/backend/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class RestApiBackend implements Backend {
}
return {
status: 'success',
message: `Data source is working, reached version ${checkMkVersion} of checkmk`,
message: `Data source is working, reached version ${checkMkVersion} of Checkmk`,
title: 'Success',
};
}
Expand Down Expand Up @@ -224,7 +224,7 @@ export default class RestApiBackend implements Backend {
// but we may have a more detailed error message
if (error.status === 404) {
throw new Error(
'REST API graph endpoints are unavailable. Choose correct checkmk version in data source settings.'
'REST API graph endpoint is unavailable. Choose correct Checkmk edition and version in data source settings.'
);
}

Expand Down
27 changes: 27 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Backend as BackendType, DataSourceOptions, Edition } from './types';

export class Settings {
protected settings: DataSourceOptions;

constructor(settings: DataSourceOptions) {
this.settings = settings;
}

get edition(): Edition {
// cloud instances of this plugin don't save the edition and use this default
return this.settings.edition ?? 'CEE';
}

get backend(): BackendType {
// cloud instances of this plugin don't save the backend and use this default
return this.settings.backend ?? 'rest';
}

get url(): string | undefined {
return this.settings.url;
}

get username(): string {
return this.settings.username ?? '';
}
}
18 changes: 6 additions & 12 deletions src/ui/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DataSourcePluginOptionsEditorProps, SelectableValue } from '@grafana/da
import { Alert, FieldSet, InlineField, LegacyForms, Select } from '@grafana/ui';
import React, { ChangeEvent, useCallback } from 'react';

import { Settings } from '../settings';
import { Backend, DataSourceOptions, Edition, SecureJsonData } from '../types';

const { SecretFormField, FormField } = LegacyForms;
Expand Down Expand Up @@ -107,14 +108,7 @@ export const ConfigEditor = (props: Props) => {
const { options } = props;
const { jsonData, secureJsonFields } = options;
const secureJsonData = options.secureJsonData || {};

if (!jsonData.edition) {
onEditionChange(cmkEditions[0]);
}

if (!jsonData.backend) {
onBackendChange(cmkBackends[0]);
}
const settings = new Settings(jsonData);

return (
<>
Expand All @@ -125,7 +119,7 @@ export const ConfigEditor = (props: Props) => {
labelWidth={6}
inputWidth={20}
onChange={onUrlChange}
value={jsonData.url || ''}
value={settings.url || ''}
tooltip="Which Checkmk Server to connect to. (Example: https://checkmk.server/site)"
data-test-id="checkmk-url"
/>
Expand All @@ -137,7 +131,7 @@ export const ConfigEditor = (props: Props) => {
width={32}
options={cmkEditions}
onChange={onEditionChange}
value={jsonData.edition}
value={settings.edition}
placeholder="Select your checkmk edition"
inputId="checkmk-edition"
/>
Expand All @@ -151,7 +145,7 @@ export const ConfigEditor = (props: Props) => {
width={32}
options={cmkBackends}
onChange={onBackendChange}
value={jsonData.backend}
value={settings.backend}
placeholder="Select your checkmk version"
inputId="checkmk-version"
/>
Expand All @@ -177,7 +171,7 @@ export const ConfigEditor = (props: Props) => {
labelWidth={6}
inputWidth={20}
onChange={onUsernameChange}
value={jsonData.username || ''}
value={settings.username}
tooltip="A checkmk monitoring user. Don't use 'automation' user, because it has admin rights."
data-test-id="checkmk-username"
/>
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/CloudEdition.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ describe('Cloud Edition Restrictions', () => {
expect(screen.queryByLabelText('Edition')).toBeNull();
expect(screen.queryByLabelText('Version')).toBeNull();
});

it('sets the non configurable values to the defaults', () => {
expect(onOptionsChange).toHaveBeenCalledWith(expect.objectContaining({ jsonData: { backend: 'rest' } }));
expect(onOptionsChange).toHaveBeenCalledWith(expect.objectContaining({ jsonData: { edition: 'CEE' } }));
});
});

describe('RestApiBackend', () => {
Expand Down