InfluxDB: Move database information into jsondata (#62308)

* Use dbName in jsonData instead of database

* Use dbName in instead of database

* Remove database fields and define dbName instead

* Fix tests

* set database field as empty string
This commit is contained in:
ismail simsek
2023-01-31 14:38:02 +01:00
committed by GitHub
parent 77a186879d
commit 65fbbc06fd
9 changed files with 36 additions and 18 deletions

View File

@@ -76,11 +76,12 @@ datasources:
- name: gdev-influxdb1-influxql - name: gdev-influxdb1-influxql
type: influxdb type: influxdb
access: proxy access: proxy
database: site
user: grafana user: grafana
url: http://localhost:8087 url: http://localhost:8087
secureJsonData: secureJsonData:
password: grafana password: grafana
jsonData:
dbName: site
- name: gdev-influxdb-flux - name: gdev-influxdb-flux
type: influxdb type: influxdb
@@ -98,9 +99,9 @@ datasources:
- name: gdev-influxdb-influxql - name: gdev-influxdb-influxql
type: influxdb type: influxdb
access: proxy access: proxy
database: mybucket
url: http://localhost:8086 url: http://localhost:8086
jsonData: jsonData:
dbName: mybucket
httpHeaderName1: "Authorization" httpHeaderName1: "Authorization"
secureJsonData: secureJsonData:
httpHeaderValue1: "Token mytoken" httpHeaderValue1: "Token mytoken"

View File

@@ -32,11 +32,12 @@ datasources:
- name: gdev-influxdb1-influxql - name: gdev-influxdb1-influxql
type: influxdb type: influxdb
access: proxy access: proxy
database: site
user: grafana user: grafana
url: http://influxdb1:8086 url: http://influxdb1:8086
secureJsonData: secureJsonData:
password: grafana password: grafana
jsonData:
dbName: site
- name: gdev-influxdb-flux - name: gdev-influxdb-flux
type: influxdb type: influxdb
@@ -52,9 +53,9 @@ datasources:
- name: gdev-influxdb-influxql - name: gdev-influxdb-influxql
type: influxdb type: influxdb
access: proxy access: proxy
database: mybucket
url: http://influxdb:8086 url: http://influxdb:8086
jsonData: jsonData:
dbName: mybucket
httpHeaderName1: "Authorization" httpHeaderName1: "Authorization"
secureJsonData: secureJsonData:
httpHeaderValue1: "Token mytoken" httpHeaderValue1: "Token mytoken"

View File

@@ -69,10 +69,14 @@ func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.Inst
if version == "" { if version == "" {
version = influxVersionInfluxQL version = influxVersionInfluxQL
} }
database := jsonData.DbName
if database == "" {
database = settings.Database
}
model := &models.DatasourceInfo{ model := &models.DatasourceInfo{
HTTPClient: client, HTTPClient: client,
URL: settings.URL, URL: settings.URL,
Database: settings.Database, DbName: database,
Version: version, Version: version,
HTTPMode: httpMode, HTTPMode: httpMode,
TimeInterval: jsonData.TimeInterval, TimeInterval: jsonData.TimeInterval,
@@ -178,7 +182,7 @@ func (s *Service) createRequest(ctx context.Context, logger log.Logger, dsInfo *
req.Header.Set("User-Agent", "Grafana") req.Header.Set("User-Agent", "Grafana")
params := req.URL.Query() params := req.URL.Query()
params.Set("db", dsInfo.Database) params.Set("db", dsInfo.DbName)
params.Set("epoch", "ms") params.Set("epoch", "ms")
if httpMode == "GET" { if httpMode == "GET" {

View File

@@ -15,7 +15,7 @@ import (
func TestExecutor_createRequest(t *testing.T) { func TestExecutor_createRequest(t *testing.T) {
datasource := &models.DatasourceInfo{ datasource := &models.DatasourceInfo{
URL: "http://awesome-influxdb:1337", URL: "http://awesome-influxdb:1337",
Database: "awesome-db", DbName: "awesome-db",
HTTPMode: "GET", HTTPMode: "GET",
} }
query := "SELECT awesomeness FROM somewhere" query := "SELECT awesomeness FROM somewhere"

View File

@@ -68,7 +68,7 @@ func (f *fakeInstance) Get(pluginContext backend.PluginContext) (instancemgmt.In
HTTPClient: client, HTTPClient: client,
Token: "sometoken", Token: "sometoken",
URL: "https://awesome-influx.com", URL: "https://awesome-influx.com",
Database: "testdb", DbName: "testdb",
Version: f.version, Version: f.version,
HTTPMode: "GET", HTTPMode: "GET",
TimeInterval: "10s", TimeInterval: "10s",

View File

@@ -9,7 +9,7 @@ type DatasourceInfo struct {
Token string Token string
URL string URL string
Database string `json:"database"` DbName string `json:"dbName"`
Version string `json:"version"` Version string `json:"version"`
HTTPMode string `json:"httpMode"` HTTPMode string `json:"httpMode"`
TimeInterval string `json:"timeInterval"` TimeInterval string `json:"timeInterval"`

View File

@@ -3,13 +3,13 @@ import React, { PureComponent } from 'react';
import { import {
DataSourcePluginOptionsEditorProps, DataSourcePluginOptionsEditorProps,
SelectableValue,
onUpdateDatasourceOption,
updateDatasourcePluginResetOption,
onUpdateDatasourceJsonDataOption, onUpdateDatasourceJsonDataOption,
onUpdateDatasourceJsonDataOptionSelect, onUpdateDatasourceJsonDataOptionSelect,
onUpdateDatasourceOption,
onUpdateDatasourceSecureJsonDataOption, onUpdateDatasourceSecureJsonDataOption,
SelectableValue,
updateDatasourcePluginJsonDataOption, updateDatasourcePluginJsonDataOption,
updateDatasourcePluginResetOption,
} from '@grafana/data'; } from '@grafana/data';
import { import {
Alert, Alert,
@@ -18,15 +18,16 @@ import {
InlineField, InlineField,
InlineFormLabel, InlineFormLabel,
LegacyForms, LegacyForms,
Select,
SecureSocksProxySettings, SecureSocksProxySettings,
Select,
} from '@grafana/ui'; } from '@grafana/ui';
import { config } from 'app/core/config'; import { config } from 'app/core/config';
const { Input, SecretFormField } = LegacyForms;
import { BROWSER_MODE_DISABLED_MESSAGE } from '../constants'; import { BROWSER_MODE_DISABLED_MESSAGE } from '../constants';
import { InfluxOptions, InfluxSecureJsonData, InfluxVersion } from '../types'; import { InfluxOptions, InfluxSecureJsonData, InfluxVersion } from '../types';
const { Input, SecretFormField } = LegacyForms;
const httpModes: SelectableValue[] = [ const httpModes: SelectableValue[] = [
{ label: 'GET', value: 'GET' }, { label: 'GET', value: 'GET' },
{ label: 'POST', value: 'POST' }, { label: 'POST', value: 'POST' },
@@ -199,8 +200,17 @@ export class ConfigEditor extends PureComponent<Props, State> {
<Input <Input
id={`${htmlPrefix}-db`} id={`${htmlPrefix}-db`}
className="width-20" className="width-20"
value={options.database || ''} value={options.jsonData.dbName ?? options.database}
onChange={onUpdateDatasourceOption(this.props, 'database')} onChange={(event) => {
this.props.onOptionsChange({
...options,
database: '',
jsonData: {
...options.jsonData,
dbName: event.target.value,
},
});
}}
/> />
</div> </div>
</div> </div>

View File

@@ -62,11 +62,11 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
this.username = instanceSettings.username ?? ''; this.username = instanceSettings.username ?? '';
this.password = instanceSettings.password ?? ''; this.password = instanceSettings.password ?? '';
this.name = instanceSettings.name; this.name = instanceSettings.name;
this.database = instanceSettings.database;
this.basicAuth = instanceSettings.basicAuth; this.basicAuth = instanceSettings.basicAuth;
this.withCredentials = instanceSettings.withCredentials; this.withCredentials = instanceSettings.withCredentials;
this.access = instanceSettings.access; this.access = instanceSettings.access;
const settingsData = instanceSettings.jsonData || ({} as InfluxOptions); const settingsData = instanceSettings.jsonData || ({} as InfluxOptions);
this.database = settingsData.dbName ?? instanceSettings.database;
this.interval = settingsData.timeInterval; this.interval = settingsData.timeInterval;
this.httpMode = settingsData.httpMode || 'GET'; this.httpMode = settingsData.httpMode || 'GET';
this.responseParser = new ResponseParser(); this.responseParser = new ResponseParser();

View File

@@ -1,4 +1,4 @@
import { DataQuery, DataSourceJsonData, AdHocVariableFilter } from '@grafana/data'; import { AdHocVariableFilter, DataQuery, DataSourceJsonData } from '@grafana/data';
export enum InfluxVersion { export enum InfluxVersion {
InfluxQL = 'InfluxQL', InfluxQL = 'InfluxQL',
@@ -11,6 +11,8 @@ export interface InfluxOptions extends DataSourceJsonData {
timeInterval?: string; timeInterval?: string;
httpMode?: string; httpMode?: string;
dbName?: string;
// With Flux // With Flux
organization?: string; organization?: string;
defaultBucket?: string; defaultBucket?: string;