From 056d9bcd26bd77da86b25e79b67b767fdc33c376 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 28 Aug 2018 17:22:50 +0300 Subject: [PATCH] refactor testDataSource() method --- src/datasource-zabbix/datasource.js | 31 ++++++------- .../zabbix/connectors/dbConnector.js | 28 ++++++++--- .../zabbix/connectors/sql/sqlConnector.js | 2 +- src/datasource-zabbix/zabbix/zabbix.js | 46 +++++++++++++++++++ 4 files changed, 83 insertions(+), 24 deletions(-) diff --git a/src/datasource-zabbix/datasource.js b/src/datasource-zabbix/datasource.js index 972a849..739c6d8 100644 --- a/src/datasource-zabbix/datasource.js +++ b/src/datasource-zabbix/datasource.js @@ -335,28 +335,20 @@ export class ZabbixDatasource { } /** - * Test connection to Zabbix API - * @return {object} Connection status and Zabbix API version + * Test connection to Zabbix API and external history DB. */ testDatasource() { - let zabbixVersion; - return this.zabbix.getVersion() - .then(version => { - zabbixVersion = version; - return this.zabbix.login(); - }) - .then(() => { - if (this.enableDirectDBConnection) { - return this.zabbix.dbConnector.testDataSource(); - } else { - return Promise.resolve(); + return this.zabbix.testDataSource() + .then(result => { + const { zabbixVersion, dbConnectorStatus } = result; + let message = `Zabbix API version: ${zabbixVersion}`; + if (dbConnectorStatus) { + message += `, DB connector type: ${dbConnectorStatus.dsType}`; } - }) - .then(() => { return { status: "success", title: "Success", - message: "Zabbix API version: " + zabbixVersion + message: message }; }) .catch(error => { @@ -372,7 +364,14 @@ export class ZabbixDatasource { title: "Connection failed", message: "Connection failed: " + error.data.message }; + } else if (typeof(error) === 'string') { + return { + status: "error", + title: "Connection failed", + message: "Connection failed: " + error + }; } else { + console.log(error); return { status: "error", title: "Connection failed", diff --git a/src/datasource-zabbix/zabbix/connectors/dbConnector.js b/src/datasource-zabbix/zabbix/connectors/dbConnector.js index 6699b45..d723515 100644 --- a/src/datasource-zabbix/zabbix/connectors/dbConnector.js +++ b/src/datasource-zabbix/zabbix/connectors/dbConnector.js @@ -1,7 +1,5 @@ import _ from 'lodash'; -const NOT_IMPLEMENTED = 'Method should be implemented in subclass of DBConnector'; - /** * Base class for external history database connectors. Subclasses should implement `getHistory()`, `getTrends()` and * `testDataSource()` methods, which describe how to fetch data from source other than Zabbix API. @@ -12,7 +10,8 @@ export default class DBConnector { this.datasourceSrv = datasourceSrv; this.datasourceId = options.datasourceId; this.datasourceName = options.datasourceName; - this.datasourceType = null; + this.datasourceTypeId = null; + this.datasourceTypeName = null; } loadDBDataSource() { @@ -20,7 +19,9 @@ export default class DBConnector { if (ds) { return this.datasourceSrv.loadDatasource(ds.name) .then(ds => { - this.datasourceType = ds.meta.id; + this.datasourceName = ds.name; + this.datasourceTypeId = ds.meta.id; + this.datasourceTypeName = ds.meta.name; return ds; }); } else { @@ -32,20 +33,33 @@ export default class DBConnector { * Send test request to datasource in order to ensure it's working. */ testDataSource() { - throw NOT_IMPLEMENTED; + throw new ZabbixNotImplemented('testDataSource()'); } /** * Get history data from external sources. */ getHistory() { - throw NOT_IMPLEMENTED; + throw new ZabbixNotImplemented('getHistory()'); } /** * Get trends data from external sources. */ getTrends() { - throw NOT_IMPLEMENTED; + throw new ZabbixNotImplemented('getTrends()'); + } +} + +// Define Zabbix DB Connector exception type for non-implemented methods +export class ZabbixNotImplemented { + constructor(methodName) { + this.code = null; + this.name = 'ZabbixNotImplemented'; + this.message = `Zabbix DB Connector Error: method ${methodName || ''} should be implemented in subclass of DBConnector`; + } + + toString() { + return this.message; } } diff --git a/src/datasource-zabbix/zabbix/connectors/sql/sqlConnector.js b/src/datasource-zabbix/zabbix/connectors/sql/sqlConnector.js index 75b5678..f3fbd39 100644 --- a/src/datasource-zabbix/zabbix/connectors/sql/sqlConnector.js +++ b/src/datasource-zabbix/zabbix/connectors/sql/sqlConnector.js @@ -48,7 +48,7 @@ export class SQLConnector extends DBConnector { } loadSQLDialect() { - if (this.datasourceType === supportedDatabases.postgres) { + if (this.datasourceTypeId === supportedDatabases.postgres) { this.sqlDialect = postgres; } else { this.sqlDialect = mysql; diff --git a/src/datasource-zabbix/zabbix/zabbix.js b/src/datasource-zabbix/zabbix/zabbix.js index 7fedd11..6698b0a 100644 --- a/src/datasource-zabbix/zabbix/zabbix.js +++ b/src/datasource-zabbix/zabbix/zabbix.js @@ -4,6 +4,7 @@ import responseHandler from '../responseHandler'; import { ZabbixAPIConnector } from './connectors/zabbix_api/zabbixAPIConnector'; import { SQLConnector } from './connectors/sql/sqlConnector'; import { CachingProxy } from './proxy/cachingProxy'; +import { ZabbixNotImplemented } from './connectors/dbConnector'; const REQUESTS_TO_PROXYFY = [ 'getHistory', 'getTrend', 'getGroups', 'getHosts', 'getApps', 'getItems', 'getMacros', 'getItemsByIDs', @@ -73,6 +74,51 @@ export class Zabbix { } } + /** + * Perform test query for Zabbix API and external history DB. + * @return {object} test result object: + * ``` + { + zabbixVersion, + dbConnectorStatus: { + dsType, + dsName + } + } + ``` + */ + testDataSource() { + let zabbixVersion; + let dbConnectorStatus; + return this.getVersion() + .then(version => { + zabbixVersion = version; + return this.login(); + }) + .then(() => { + if (this.enableDirectDBConnection) { + return this.dbConnector.testDataSource(); + } else { + return Promise.resolve(); + } + }) + .catch(error => { + if (error instanceof ZabbixNotImplemented) { + return Promise.resolve(); + } + return Promise.reject(error); + }) + .then(testResult => { + if (testResult) { + dbConnectorStatus = { + dsType: this.dbConnector.datasourceTypeName, + dsName: this.dbConnector.datasourceName + }; + } + return { zabbixVersion, dbConnectorStatus }; + }); + } + getItemsFromTarget(target, options) { let parts = ['group', 'host', 'application', 'item']; let filters = _.map(parts, p => target[p].filter);