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

View File

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

View File

@@ -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;
this.datasourceId = dbConnectionOptions.datasourceId;
this.enableDirectDBConnection = jsonData.dbConnectionEnable || false;
this.datasourceId = jsonData.dbConnectionDatasourceId;
let zabbixOptions = {
url: this.url,

View File

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

View File

@@ -79,9 +79,9 @@
<h3 class="page-heading">Direct DB Connection</h3>
<gf-form-switch class="gf-form" label-class="width-12"
label="Enable"
checked="ctrl.current.jsonData.dbConnection.enable">
checked="ctrl.current.jsonData.dbConnectionEnable">
</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">
<span class="gf-form-label width-12">
SQL Data Source
@@ -94,7 +94,7 @@
</info-popover>
</span>
<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">
</select>
</div>

View File

@@ -14,9 +14,7 @@ describe('ZabbixDatasource', () => {
trends: true,
trendsFrom: '14d',
trendsRange: '7d',
dbConnection: {
enabled: false
}
dbConnectionEnable: false
}
};
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();
});
});
});