Merge PR #600 (Implements proposal #598)

This commit is contained in:
Alexander Zobnin
2018-08-28 20:39:16 +03:00
7 changed files with 80 additions and 13 deletions

View File

@@ -10,6 +10,7 @@ module.exports = {
"moduleNameMapper": { "moduleNameMapper": {
"^[./a-zA-Z0-9$_-]+\.css\!?$": "<rootDir>/src/test-setup/cssStub.js", "^[./a-zA-Z0-9$_-]+\.css\!?$": "<rootDir>/src/test-setup/cssStub.js",
}, },
"testRegex": "(\\.|/)(test|spec)\\.(jsx?|tsx?)$",
"transform": { "transform": {
"^.+\\.js$": "babel-jest" "^.+\\.js$": "babel-jest"
}, },

View File

@@ -1,11 +1,16 @@
import _ from 'lodash'; import _ from 'lodash';
import { migrateDSConfig } from './migrations';
const SUPPORTED_SQL_DS = ['mysql', 'postgres']; const SUPPORTED_SQL_DS = ['mysql', 'postgres'];
const defaultConfig = { const defaultConfig = {
dbConnection: { trends: false,
enable: false, dbConnectionEnable: false,
} dbConnectionDatasourceId: null,
alerting: false,
addThresholds: false,
alertingMinSeverity: 3,
disableReadOnlyUsersAck: false
}; };
export class ZabbixDSConfigController { export class ZabbixDSConfigController {
@@ -14,6 +19,7 @@ export class ZabbixDSConfigController {
constructor($scope, $injector, datasourceSrv) { constructor($scope, $injector, datasourceSrv) {
this.datasourceSrv = datasourceSrv; this.datasourceSrv = datasourceSrv;
this.current.jsonData = migrateDSConfig(this.current.jsonData);
_.defaults(this.current.jsonData, defaultConfig); _.defaults(this.current.jsonData, defaultConfig);
this.sqlDataSources = this.getSupportedSQLDataSources(); this.sqlDataSources = this.getSupportedSQLDataSources();
} }

View File

@@ -25,7 +25,7 @@ export class ZabbixDatasource {
this.basicAuth = instanceSettings.basicAuth; this.basicAuth = instanceSettings.basicAuth;
this.withCredentials = instanceSettings.withCredentials; this.withCredentials = instanceSettings.withCredentials;
const jsonData = instanceSettings.jsonData || {}; const jsonData = migrations.migrateDSConfig(instanceSettings.jsonData);
// Zabbix API credentials // Zabbix API credentials
this.username = jsonData.username; this.username = jsonData.username;
@@ -49,9 +49,8 @@ export class ZabbixDatasource {
this.disableReadOnlyUsersAck = jsonData.disableReadOnlyUsersAck; this.disableReadOnlyUsersAck = jsonData.disableReadOnlyUsersAck;
// Direct DB Connection options // Direct DB Connection options
let dbConnectionOptions = jsonData.dbConnection || {}; this.enableDirectDBConnection = jsonData.dbConnectionEnable || false;
this.enableDirectDBConnection = dbConnectionOptions.enable; this.datasourceId = jsonData.dbConnectionDatasourceId;
this.datasourceId = dbConnectionOptions.datasourceId;
let zabbixOptions = { let zabbixOptions = {
url: this.url, url: this.url,

View File

@@ -41,3 +41,25 @@ function convertToRegex(str) {
return '/.*/'; 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;
}

View File

@@ -79,9 +79,9 @@
<h3 class="page-heading">Direct DB Connection</h3> <h3 class="page-heading">Direct DB Connection</h3>
<gf-form-switch class="gf-form" label-class="width-12" <gf-form-switch class="gf-form" label-class="width-12"
label="Enable" label="Enable"
checked="ctrl.current.jsonData.dbConnection.enable"> checked="ctrl.current.jsonData.dbConnectionEnable">
</gf-form-switch> </gf-form-switch>
<div ng-if="ctrl.current.jsonData.dbConnection.enable"> <div ng-if="ctrl.current.jsonData.dbConnectionEnable">
<div class="gf-form max-width-20"> <div class="gf-form max-width-20">
<span class="gf-form-label width-12"> <span class="gf-form-label width-12">
SQL Data Source SQL Data Source
@@ -94,7 +94,7 @@
</info-popover> </info-popover>
</span> </span>
<div class="gf-form-select-wrapper max-width-16"> <div class="gf-form-select-wrapper max-width-16">
<select class="gf-form-input" ng-model="ctrl.current.jsonData.dbConnection.datasourceId" <select class="gf-form-input" ng-model="ctrl.current.jsonData.dbConnectionDatasourceId"
ng-options="ds.id as ds.name for ds in ctrl.sqlDataSources"> ng-options="ds.id as ds.name for ds in ctrl.sqlDataSources">
</select> </select>
</div> </div>

View File

@@ -14,9 +14,7 @@ describe('ZabbixDatasource', () => {
trends: true, trends: true,
trendsFrom: '14d', trendsFrom: '14d',
trendsRange: '7d', trendsRange: '7d',
dbConnection: { dbConnectionEnable: false
enabled: false
}
} }
}; };
ctx.templateSrv = {}; ctx.templateSrv = {};

View File

@@ -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();
});
});
});