diff --git a/jest.config.js b/jest.config.js index df52939..9b32b5f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,6 +10,7 @@ module.exports = { "moduleNameMapper": { "^[./a-zA-Z0-9$_-]+\.css\!?$": "/src/test-setup/cssStub.js", }, + "testRegex": "(\\.|/)(test|spec)\\.(jsx?|tsx?)$", "transform": { "^.+\\.js$": "babel-jest" }, diff --git a/src/datasource-zabbix/config.controller.js b/src/datasource-zabbix/config.controller.js index 703c13c..0996865 100644 --- a/src/datasource-zabbix/config.controller.js +++ b/src/datasource-zabbix/config.controller.js @@ -1,11 +1,16 @@ import _ from 'lodash'; +import { migrateDSConfig } from './migrations'; const SUPPORTED_SQL_DS = ['mysql', 'postgres']; const defaultConfig = { - dbConnection: { - enable: false, - } + trends: false, + dbConnectionEnable: false, + dbConnectionDatasourceId: null, + alerting: false, + addThresholds: false, + alertingMinSeverity: 3, + disableReadOnlyUsersAck: false }; export class ZabbixDSConfigController { @@ -14,6 +19,7 @@ export class ZabbixDSConfigController { constructor($scope, $injector, datasourceSrv) { this.datasourceSrv = datasourceSrv; + this.current.jsonData = migrateDSConfig(this.current.jsonData); _.defaults(this.current.jsonData, defaultConfig); this.sqlDataSources = this.getSupportedSQLDataSources(); } diff --git a/src/datasource-zabbix/datasource.js b/src/datasource-zabbix/datasource.js index e68a1a7..df7544a 100644 --- a/src/datasource-zabbix/datasource.js +++ b/src/datasource-zabbix/datasource.js @@ -25,7 +25,7 @@ export class ZabbixDatasource { this.basicAuth = instanceSettings.basicAuth; this.withCredentials = instanceSettings.withCredentials; - const jsonData = instanceSettings.jsonData || {}; + const jsonData = migrations.migrateDSConfig(instanceSettings.jsonData); // Zabbix API credentials this.username = jsonData.username; @@ -49,9 +49,8 @@ export class ZabbixDatasource { this.disableReadOnlyUsersAck = jsonData.disableReadOnlyUsersAck; // Direct DB Connection options - let dbConnectionOptions = jsonData.dbConnection || {}; - this.enableDirectDBConnection = dbConnectionOptions.enable || jsonData.dbConnectionEnable || false; - this.datasourceId = dbConnectionOptions.datasourceId || jsonData.dbConnectionDatasourceId; + this.enableDirectDBConnection = jsonData.dbConnectionEnable || false; + this.datasourceId = jsonData.dbConnectionDatasourceId; let zabbixOptions = { url: this.url, diff --git a/src/datasource-zabbix/migrations.js b/src/datasource-zabbix/migrations.js index 238c627..8b75025 100644 --- a/src/datasource-zabbix/migrations.js +++ b/src/datasource-zabbix/migrations.js @@ -41,3 +41,25 @@ function convertToRegex(str) { return '/.*/'; } } + +export const DS_CONFIG_SCHEMA = 2; +export function migrateDSConfig(jsonData) { + if (!jsonData) { + jsonData = {}; + } + const oldVersion = jsonData.schema || 1; + jsonData.schema = DS_CONFIG_SCHEMA; + + if (oldVersion === DS_CONFIG_SCHEMA) { + return jsonData; + } + + if (oldVersion < 2) { + const dbConnectionOptions = jsonData.dbConnection || {}; + jsonData.dbConnectionEnable = dbConnectionOptions.enable || false; + jsonData.dbConnectionDatasourceId = dbConnectionOptions.datasourceId || null; + delete jsonData.dbConnection; + } + + return jsonData; +} diff --git a/src/datasource-zabbix/specs/migrations.test.js b/src/datasource-zabbix/specs/migrations.test.js new file mode 100644 index 0000000..b11aadc --- /dev/null +++ b/src/datasource-zabbix/specs/migrations.test.js @@ -0,0 +1,41 @@ +import { migrateDSConfig, DS_CONFIG_SCHEMA } from '../migrations'; + +describe('Migrations', () => { + let ctx = {}; + + describe('When migrating datasource config', () => { + beforeEach(() => { + ctx.jsonData = { + dbConnection: { + enable: true, + datasourceId: 1 + } + }; + }); + + it('should change direct DB connection setting to flat style', () => { + migrateDSConfig(ctx.jsonData); + expect(ctx.jsonData).toMatchObject({ + dbConnectionEnable: true, + dbConnectionDatasourceId: 1, + schema: DS_CONFIG_SCHEMA + }); + }); + + it('should not touch anything if schema is up to date', () => { + ctx.jsonData = { + futureOptionOne: 'foo', + futureOptionTwo: 'bar', + schema: DS_CONFIG_SCHEMA + }; + migrateDSConfig(ctx.jsonData); + expect(ctx.jsonData).toMatchObject({ + futureOptionOne: 'foo', + futureOptionTwo: 'bar', + schema: DS_CONFIG_SCHEMA + }); + expect(ctx.jsonData.dbConnectionEnable).toBeUndefined(); + expect(ctx.jsonData.dbConnectionDatasourceId).toBeUndefined(); + }); + }); +});