use direct db connection datasource name for provisioning, #614

This commit is contained in:
Alexander Zobnin
2018-08-28 22:34:41 +03:00
parent 2e84893279
commit 8096c0a65e
12 changed files with 158 additions and 21 deletions

View File

@@ -237,7 +237,8 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
// Direct DB Connection options
this.enableDirectDBConnection = jsonData.dbConnectionEnable || false;
this.datasourceId = jsonData.dbConnectionDatasourceId;
this.dbConnectionDatasourceId = jsonData.dbConnectionDatasourceId;
this.dbConnectionDatasourceName = jsonData.dbConnectionDatasourceName;
var zabbixOptions = {
url: this.url,
@@ -247,7 +248,8 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
withCredentials: this.withCredentials,
cacheTTL: this.cacheTTL,
enableDirectDBConnection: this.enableDirectDBConnection,
datasourceId: this.datasourceId
dbConnectionDatasourceId: this.dbConnectionDatasourceId,
dbConnectionDatasourceName: this.dbConnectionDatasourceName
};
this.zabbix = new Zabbix(zabbixOptions, backendSrv, datasourceSrv);

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,62 @@
'use strict';
System.register(['../zabbix/connectors/dbConnector'], function (_export, _context) {
"use strict";
var DBConnector;
return {
setters: [function (_zabbixConnectorsDbConnector) {
DBConnector = _zabbixConnectorsDbConnector.default;
}],
execute: function () {
describe('DBConnector', function () {
var ctx = {};
var backendSrvMock = {};
var datasourceSrvMock = {
loadDatasource: jest.fn().mockResolvedValue({ id: 42, name: 'foo', meta: {} }),
getAll: jest.fn().mockReturnValue([{ id: 42, name: 'foo' }])
};
describe('When init DB connector', function () {
beforeEach(function () {
ctx.options = {
datasourceId: 42,
datasourceName: undefined
};
});
it('should load datasource by name by default', function () {
ctx.options = {
datasourceName: 'bar'
};
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
dbConnector.loadDBDataSource();
expect(datasourceSrvMock.getAll).not.toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('bar');
});
it('should load datasource by id if name not present', function () {
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
dbConnector.loadDBDataSource();
expect(datasourceSrvMock.getAll).toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('foo');
});
it('should throw error if no name and id specified', function () {
ctx.options = {};
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source name should be specified');
});
it('should throw error if datasource with given id is not found', function () {
ctx.options.datasourceId = 45;
var dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source with ID 45 not found');
});
});
});
}
};
});
//# sourceMappingURL=dbConnector.test.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/datasource-zabbix/specs/dbConnector.test.js"],"names":["DBConnector","describe","ctx","backendSrvMock","datasourceSrvMock","loadDatasource","jest","fn","mockResolvedValue","id","name","meta","getAll","mockReturnValue","beforeEach","options","datasourceId","datasourceName","undefined","it","dbConnector","loadDBDataSource","expect","not","toHaveBeenCalled","toHaveBeenCalledWith","rejects","toBe"],"mappings":";;;;;;;;AAAOA,iB;;;;AAEPC,eAAS,aAAT,EAAwB,YAAM;AAC5B,YAAIC,MAAM,EAAV;AACA,YAAMC,iBAAiB,EAAvB;AACA,YAAMC,oBAAoB;AACxBC,0BAAgBC,KAAKC,EAAL,GAAUC,iBAAV,CACd,EAAEC,IAAI,EAAN,EAAUC,MAAM,KAAhB,EAAuBC,MAAM,EAA7B,EADc,CADQ;AAIxBC,kBAAQN,KAAKC,EAAL,GAAUM,eAAV,CAA0B,CAChC,EAAEJ,IAAI,EAAN,EAAUC,MAAM,KAAhB,EADgC,CAA1B;AAJgB,SAA1B;;AASAT,iBAAS,wBAAT,EAAmC,YAAM;AACvCa,qBAAW,YAAM;AACfZ,gBAAIa,OAAJ,GAAc;AACZC,4BAAc,EADF;AAEZC,8BAAgBC;AAFJ,aAAd;AAID,WALD;;AAOAC,aAAG,2CAAH,EAAgD,YAAM;AACpDjB,gBAAIa,OAAJ,GAAc;AACZE,8BAAgB;AADJ,aAAd;AAGA,gBAAMG,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACAgB,wBAAYC,gBAAZ;AACAC,mBAAOlB,kBAAkBQ,MAAzB,EAAiCW,GAAjC,CAAqCC,gBAArC;AACAF,mBAAOlB,kBAAkBC,cAAzB,EAAyCoB,oBAAzC,CAA8D,KAA9D;AACD,WARD;;AAUAN,aAAG,kDAAH,EAAuD,YAAM;AAC3D,gBAAMC,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACAgB,wBAAYC,gBAAZ;AACAC,mBAAOlB,kBAAkBQ,MAAzB,EAAiCY,gBAAjC;AACAF,mBAAOlB,kBAAkBC,cAAzB,EAAyCoB,oBAAzC,CAA8D,KAA9D;AACD,WALD;;AAOAN,aAAG,gDAAH,EAAqD,YAAM;AACzDjB,gBAAIa,OAAJ,GAAc,EAAd;AACA,gBAAMK,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACA,mBAAOkB,OAAOF,YAAYC,gBAAZ,EAAP,EAAuCK,OAAvC,CAA+CC,IAA/C,CAAoD,0CAApD,CAAP;AACD,WAJD;;AAMAR,aAAG,6DAAH,EAAkE,YAAM;AACtEjB,gBAAIa,OAAJ,CAAYC,YAAZ,GAA2B,EAA3B;AACA,gBAAMI,cAAc,IAAIpB,WAAJ,CAAgBE,IAAIa,OAApB,EAA6BZ,cAA7B,EAA6CC,iBAA7C,CAApB;AACA,mBAAOkB,OAAOF,YAAYC,gBAAZ,EAAP,EAAuCK,OAAvC,CAA+CC,IAA/C,CAAoD,sCAApD,CAAP;AACD,WAJD;AAKD,SApCD;AAqCD,OAjDD","file":"dbConnector.test.js","sourcesContent":["import DBConnector from '../zabbix/connectors/dbConnector';\n\ndescribe('DBConnector', () => {\n let ctx = {};\n const backendSrvMock = {};\n const datasourceSrvMock = {\n loadDatasource: jest.fn().mockResolvedValue(\n { id: 42, name: 'foo', meta: {} }\n ),\n getAll: jest.fn().mockReturnValue([\n { id: 42, name: 'foo' }\n ])\n };\n\n describe('When init DB connector', () => {\n beforeEach(() => {\n ctx.options = {\n datasourceId: 42,\n datasourceName: undefined\n };\n });\n\n it('should load datasource by name by default', () => {\n ctx.options = {\n datasourceName: 'bar'\n };\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n dbConnector.loadDBDataSource();\n expect(datasourceSrvMock.getAll).not.toHaveBeenCalled();\n expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('bar');\n });\n\n it('should load datasource by id if name not present', () => {\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n dbConnector.loadDBDataSource();\n expect(datasourceSrvMock.getAll).toHaveBeenCalled();\n expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('foo');\n });\n\n it('should throw error if no name and id specified', () => {\n ctx.options = {};\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source name should be specified');\n });\n\n it('should throw error if datasource with given id is not found', () => {\n ctx.options.datasourceId = 45;\n const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);\n return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source with ID 45 not found');\n });\n });\n});\n"]}

View File

@@ -51,16 +51,21 @@ System.register(['lodash'], function (_export, _context) {
value: function loadDBDataSource() {
var _this = this;
if (!this.datasourceName && this.datasourceId !== undefined) {
var ds = _.find(this.datasourceSrv.getAll(), { 'id': this.datasourceId });
if (ds) {
return this.datasourceSrv.loadDatasource(ds.name).then(function (ds) {
_this.datasourceName = ds.name;
if (!ds) {
return Promise.reject('SQL Data Source with ID ' + this.datasourceId + ' not found');
}
this.datasourceName = ds.name;
}
if (this.datasourceName) {
return this.datasourceSrv.loadDatasource(this.datasourceName).then(function (ds) {
_this.datasourceTypeId = ds.meta.id;
_this.datasourceTypeName = ds.meta.name;
return ds;
});
} else {
return Promise.reject('SQL Data Source with ID ' + this.datasourceId + ' not found');
return Promise.reject('SQL Data Source name should be specified');
}
}
}, {

View File

@@ -1 +1 @@
{"version":3,"sources":["../../../../src/datasource-zabbix/zabbix/connectors/dbConnector.js"],"names":["_","DBConnector","options","backendSrv","datasourceSrv","datasourceId","datasourceName","datasourceTypeId","datasourceTypeName","ds","find","getAll","loadDatasource","name","then","meta","id","Promise","reject","ZabbixNotImplemented","methodName","code","message"],"mappings":";;;;;;;;;;;;;;;AAAOA,O;;;;;;;;;;;;;;;;;;;;;AAMcC,iB;AACnB,6BAAYC,OAAZ,EAAqBC,UAArB,EAAiCC,aAAjC,EAAgD;AAAA;;AAC9C,eAAKD,UAAL,GAAkBA,UAAlB;AACA,eAAKC,aAAL,GAAqBA,aAArB;AACA,eAAKC,YAAL,GAAoBH,QAAQG,YAA5B;AACA,eAAKC,cAAL,GAAsBJ,QAAQI,cAA9B;AACA,eAAKC,gBAAL,GAAwB,IAAxB;AACA,eAAKC,kBAAL,GAA0B,IAA1B;AACD;;;;6CAEkB;AAAA;;AACjB,gBAAIC,KAAKT,EAAEU,IAAF,CAAO,KAAKN,aAAL,CAAmBO,MAAnB,EAAP,EAAoC,EAAC,MAAM,KAAKN,YAAZ,EAApC,CAAT;AACA,gBAAII,EAAJ,EAAQ;AACN,qBAAO,KAAKL,aAAL,CAAmBQ,cAAnB,CAAkCH,GAAGI,IAArC,EACNC,IADM,CACD,cAAM;AACV,sBAAKR,cAAL,GAAsBG,GAAGI,IAAzB;AACA,sBAAKN,gBAAL,GAAwBE,GAAGM,IAAH,CAAQC,EAAhC;AACA,sBAAKR,kBAAL,GAA0BC,GAAGM,IAAH,CAAQF,IAAlC;AACA,uBAAOJ,EAAP;AACD,eANM,CAAP;AAOD,aARD,MAQO;AACL,qBAAOQ,QAAQC,MAAR,8BAA0C,KAAKb,YAA/C,gBAAP;AACD;AACF;;;2CAKgB;AACf,kBAAM,IAAIc,oBAAJ,CAAyB,kBAAzB,CAAN;AACD;;;uCAKY;AACX,kBAAM,IAAIA,oBAAJ,CAAyB,cAAzB,CAAN;AACD;;;sCAKW;AACV,kBAAM,IAAIA,oBAAJ,CAAyB,aAAzB,CAAN;AACD;;;;;;yBA5CkBlB,W;;sCAgDRkB,oB;AACX,sCAAYC,UAAZ,EAAwB;AAAA;;AACtB,eAAKC,IAAL,GAAY,IAAZ;AACA,eAAKR,IAAL,GAAY,sBAAZ;AACA,eAAKS,OAAL,2CAAoDF,cAAc,EAAlE;AACD;;;;qCAEU;AACT,mBAAO,KAAKE,OAAZ;AACD","file":"dbConnector.js","sourcesContent":["import _ from 'lodash';\n\n/**\n * Base class for external history database connectors. Subclasses should implement `getHistory()`, `getTrends()` and\n * `testDataSource()` methods, which describe how to fetch data from source other than Zabbix API.\n */\nexport default class DBConnector {\n constructor(options, backendSrv, datasourceSrv) {\n this.backendSrv = backendSrv;\n this.datasourceSrv = datasourceSrv;\n this.datasourceId = options.datasourceId;\n this.datasourceName = options.datasourceName;\n this.datasourceTypeId = null;\n this.datasourceTypeName = null;\n }\n\n loadDBDataSource() {\n let ds = _.find(this.datasourceSrv.getAll(), {'id': this.datasourceId});\n if (ds) {\n return this.datasourceSrv.loadDatasource(ds.name)\n .then(ds => {\n this.datasourceName = ds.name;\n this.datasourceTypeId = ds.meta.id;\n this.datasourceTypeName = ds.meta.name;\n return ds;\n });\n } else {\n return Promise.reject(`SQL Data Source with ID ${this.datasourceId} not found`);\n }\n }\n\n /**\n * Send test request to datasource in order to ensure it's working.\n */\n testDataSource() {\n throw new ZabbixNotImplemented('testDataSource()');\n }\n\n /**\n * Get history data from external sources.\n */\n getHistory() {\n throw new ZabbixNotImplemented('getHistory()');\n }\n\n /**\n * Get trends data from external sources.\n */\n getTrends() {\n throw new ZabbixNotImplemented('getTrends()');\n }\n}\n\n// Define Zabbix DB Connector exception type for non-implemented methods\nexport class ZabbixNotImplemented {\n constructor(methodName) {\n this.code = null;\n this.name = 'ZabbixNotImplemented';\n this.message = `Zabbix DB Connector Error: method ${methodName || ''} should be implemented in subclass of DBConnector`;\n }\n\n toString() {\n return this.message;\n }\n}\n"]}
{"version":3,"sources":["../../../../src/datasource-zabbix/zabbix/connectors/dbConnector.js"],"names":["_","DBConnector","options","backendSrv","datasourceSrv","datasourceId","datasourceName","datasourceTypeId","datasourceTypeName","undefined","ds","find","getAll","Promise","reject","name","loadDatasource","then","meta","id","ZabbixNotImplemented","methodName","code","message"],"mappings":";;;;;;;;;;;;;;;AAAOA,O;;;;;;;;;;;;;;;;;;;;;AAMcC,iB;AACnB,6BAAYC,OAAZ,EAAqBC,UAArB,EAAiCC,aAAjC,EAAgD;AAAA;;AAC9C,eAAKD,UAAL,GAAkBA,UAAlB;AACA,eAAKC,aAAL,GAAqBA,aAArB;AACA,eAAKC,YAAL,GAAoBH,QAAQG,YAA5B;AACA,eAAKC,cAAL,GAAsBJ,QAAQI,cAA9B;AACA,eAAKC,gBAAL,GAAwB,IAAxB;AACA,eAAKC,kBAAL,GAA0B,IAA1B;AACD;;;;6CAEkB;AAAA;;AACjB,gBAAI,CAAC,KAAKF,cAAN,IAAwB,KAAKD,YAAL,KAAsBI,SAAlD,EAA6D;AAC3D,kBAAIC,KAAKV,EAAEW,IAAF,CAAO,KAAKP,aAAL,CAAmBQ,MAAnB,EAAP,EAAoC,EAAC,MAAM,KAAKP,YAAZ,EAApC,CAAT;AACA,kBAAI,CAACK,EAAL,EAAS;AACP,uBAAOG,QAAQC,MAAR,8BAA0C,KAAKT,YAA/C,gBAAP;AACD;AACD,mBAAKC,cAAL,GAAsBI,GAAGK,IAAzB;AACD;AACD,gBAAI,KAAKT,cAAT,EAAyB;AACvB,qBAAO,KAAKF,aAAL,CAAmBY,cAAnB,CAAkC,KAAKV,cAAvC,EACNW,IADM,CACD,cAAM;AACV,sBAAKV,gBAAL,GAAwBG,GAAGQ,IAAH,CAAQC,EAAhC;AACA,sBAAKX,kBAAL,GAA0BE,GAAGQ,IAAH,CAAQH,IAAlC;AACA,uBAAOL,EAAP;AACD,eALM,CAAP;AAMD,aAPD,MAOO;AACL,qBAAOG,QAAQC,MAAR,4CAAP;AACD;AACF;;;2CAKgB;AACf,kBAAM,IAAIM,oBAAJ,CAAyB,kBAAzB,CAAN;AACD;;;uCAKY;AACX,kBAAM,IAAIA,oBAAJ,CAAyB,cAAzB,CAAN;AACD;;;sCAKW;AACV,kBAAM,IAAIA,oBAAJ,CAAyB,aAAzB,CAAN;AACD;;;;;;yBAjDkBnB,W;;sCAqDRmB,oB;AACX,sCAAYC,UAAZ,EAAwB;AAAA;;AACtB,eAAKC,IAAL,GAAY,IAAZ;AACA,eAAKP,IAAL,GAAY,sBAAZ;AACA,eAAKQ,OAAL,2CAAoDF,cAAc,EAAlE;AACD;;;;qCAEU;AACT,mBAAO,KAAKE,OAAZ;AACD","file":"dbConnector.js","sourcesContent":["import _ from 'lodash';\n\n/**\n * Base class for external history database connectors. Subclasses should implement `getHistory()`, `getTrends()` and\n * `testDataSource()` methods, which describe how to fetch data from source other than Zabbix API.\n */\nexport default class DBConnector {\n constructor(options, backendSrv, datasourceSrv) {\n this.backendSrv = backendSrv;\n this.datasourceSrv = datasourceSrv;\n this.datasourceId = options.datasourceId;\n this.datasourceName = options.datasourceName;\n this.datasourceTypeId = null;\n this.datasourceTypeName = null;\n }\n\n loadDBDataSource() {\n if (!this.datasourceName && this.datasourceId !== undefined) {\n let ds = _.find(this.datasourceSrv.getAll(), {'id': this.datasourceId});\n if (!ds) {\n return Promise.reject(`SQL Data Source with ID ${this.datasourceId} not found`);\n }\n this.datasourceName = ds.name;\n }\n if (this.datasourceName) {\n return this.datasourceSrv.loadDatasource(this.datasourceName)\n .then(ds => {\n this.datasourceTypeId = ds.meta.id;\n this.datasourceTypeName = ds.meta.name;\n return ds;\n });\n } else {\n return Promise.reject(`SQL Data Source name should be specified`);\n }\n }\n\n /**\n * Send test request to datasource in order to ensure it's working.\n */\n testDataSource() {\n throw new ZabbixNotImplemented('testDataSource()');\n }\n\n /**\n * Get history data from external sources.\n */\n getHistory() {\n throw new ZabbixNotImplemented('getHistory()');\n }\n\n /**\n * Get trends data from external sources.\n */\n getTrends() {\n throw new ZabbixNotImplemented('getTrends()');\n }\n}\n\n// Define Zabbix DB Connector exception type for non-implemented methods\nexport class ZabbixNotImplemented {\n constructor(methodName) {\n this.code = null;\n this.name = 'ZabbixNotImplemented';\n this.message = `Zabbix DB Connector Error: method ${methodName || ''} should be implemented in subclass of DBConnector`;\n }\n\n toString() {\n return this.message;\n }\n}\n"]}

View File

@@ -174,7 +174,8 @@ System.register(['lodash', '../utils', '../responseHandler', './connectors/zabbi
withCredentials = options.withCredentials,
cacheTTL = options.cacheTTL,
enableDirectDBConnection = options.enableDirectDBConnection,
datasourceId = options.datasourceId;
dbConnectionDatasourceId = options.dbConnectionDatasourceId,
dbConnectionDatasourceName = options.dbConnectionDatasourceName;
this.enableDirectDBConnection = enableDirectDBConnection;
@@ -189,7 +190,10 @@ System.register(['lodash', '../utils', '../responseHandler', './connectors/zabbi
this.zabbixAPI = new ZabbixAPIConnector(url, username, password, basicAuth, withCredentials, backendSrv);
if (enableDirectDBConnection) {
var dbConnectorOptions = { datasourceId: datasourceId };
var dbConnectorOptions = {
datasourceId: dbConnectionDatasourceId,
datasourceName: dbConnectionDatasourceName
};
this.dbConnector = new SQLConnector(dbConnectorOptions, backendSrv, datasourceSrv);
this.getHistoryDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getHistory, 'getHistory', this.dbConnector);
this.getTrendsDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getTrends, 'getTrends', this.dbConnector);

File diff suppressed because one or more lines are too long

View File

@@ -50,7 +50,8 @@ export class ZabbixDatasource {
// Direct DB Connection options
this.enableDirectDBConnection = jsonData.dbConnectionEnable || false;
this.datasourceId = jsonData.dbConnectionDatasourceId;
this.dbConnectionDatasourceId = jsonData.dbConnectionDatasourceId;
this.dbConnectionDatasourceName = jsonData.dbConnectionDatasourceName;
let zabbixOptions = {
url: this.url,
@@ -60,7 +61,8 @@ export class ZabbixDatasource {
withCredentials: this.withCredentials,
cacheTTL: this.cacheTTL,
enableDirectDBConnection: this.enableDirectDBConnection,
datasourceId: this.datasourceId
dbConnectionDatasourceId: this.dbConnectionDatasourceId,
dbConnectionDatasourceName: this.dbConnectionDatasourceName
};
this.zabbix = new Zabbix(zabbixOptions, backendSrv, datasourceSrv);

View File

@@ -0,0 +1,52 @@
import DBConnector from '../zabbix/connectors/dbConnector';
describe('DBConnector', () => {
let ctx = {};
const backendSrvMock = {};
const datasourceSrvMock = {
loadDatasource: jest.fn().mockResolvedValue(
{ id: 42, name: 'foo', meta: {} }
),
getAll: jest.fn().mockReturnValue([
{ id: 42, name: 'foo' }
])
};
describe('When init DB connector', () => {
beforeEach(() => {
ctx.options = {
datasourceId: 42,
datasourceName: undefined
};
});
it('should load datasource by name by default', () => {
ctx.options = {
datasourceName: 'bar'
};
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
dbConnector.loadDBDataSource();
expect(datasourceSrvMock.getAll).not.toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('bar');
});
it('should load datasource by id if name not present', () => {
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
dbConnector.loadDBDataSource();
expect(datasourceSrvMock.getAll).toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('foo');
});
it('should throw error if no name and id specified', () => {
ctx.options = {};
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source name should be specified');
});
it('should throw error if datasource with given id is not found', () => {
ctx.options.datasourceId = 45;
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source with ID 45 not found');
});
});
});

View File

@@ -15,17 +15,22 @@ export default class DBConnector {
}
loadDBDataSource() {
if (!this.datasourceName && this.datasourceId !== undefined) {
let ds = _.find(this.datasourceSrv.getAll(), {'id': this.datasourceId});
if (ds) {
return this.datasourceSrv.loadDatasource(ds.name)
.then(ds => {
if (!ds) {
return Promise.reject(`SQL Data Source with ID ${this.datasourceId} not found`);
}
this.datasourceName = ds.name;
}
if (this.datasourceName) {
return this.datasourceSrv.loadDatasource(this.datasourceName)
.then(ds => {
this.datasourceTypeId = ds.meta.id;
this.datasourceTypeName = ds.meta.name;
return ds;
});
} else {
return Promise.reject(`SQL Data Source with ID ${this.datasourceId} not found`);
return Promise.reject(`SQL Data Source name should be specified`);
}
}

View File

@@ -30,7 +30,8 @@ export class Zabbix {
withCredentials,
cacheTTL,
enableDirectDBConnection,
datasourceId
dbConnectionDatasourceId,
dbConnectionDatasourceName,
} = options;
this.enableDirectDBConnection = enableDirectDBConnection;
@@ -45,7 +46,10 @@ export class Zabbix {
this.zabbixAPI = new ZabbixAPIConnector(url, username, password, basicAuth, withCredentials, backendSrv);
if (enableDirectDBConnection) {
let dbConnectorOptions = { datasourceId };
let dbConnectorOptions = {
datasourceId: dbConnectionDatasourceId,
datasourceName: dbConnectionDatasourceName
};
this.dbConnector = new SQLConnector(dbConnectorOptions, backendSrv, datasourceSrv);
this.getHistoryDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getHistory, 'getHistory', this.dbConnector);
this.getTrendsDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getTrends, 'getTrends', this.dbConnector);