add migrations for datasource config
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
41
src/datasource-zabbix/specs/migrations.test.js
Normal file
41
src/datasource-zabbix/specs/migrations.test.js
Normal 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user