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

@@ -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() {
let ds = _.find(this.datasourceSrv.getAll(), {'id': this.datasourceId});
if (ds) {
return this.datasourceSrv.loadDatasource(ds.name)
if (!this.datasourceName && this.datasourceId !== undefined) {
let ds = _.find(this.datasourceSrv.getAll(), {'id': this.datasourceId});
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.datasourceName = ds.name;
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);