From 7e0a41ed450c4e8abea3e0750c22dc837ff46468 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sat, 23 Jan 2016 12:31:29 +0300 Subject: [PATCH] Working on ZabbixAPIService for general api calls. --- plugins/datasource-zabbix/zabbixAPI.js | 99 ------------------- plugins/datasource-zabbix/zabbixAPIService.js | 89 +++++++++++++++++ 2 files changed, 89 insertions(+), 99 deletions(-) delete mode 100644 plugins/datasource-zabbix/zabbixAPI.js create mode 100644 plugins/datasource-zabbix/zabbixAPIService.js diff --git a/plugins/datasource-zabbix/zabbixAPI.js b/plugins/datasource-zabbix/zabbixAPI.js deleted file mode 100644 index 5e041f2..0000000 --- a/plugins/datasource-zabbix/zabbixAPI.js +++ /dev/null @@ -1,99 +0,0 @@ -define([ - 'angular', -], -function (angular) { - 'use strict'; - - var module = angular.module('grafana.services'); - - module.service('ZabbixAPI', function($q, backendSrv) { - - this.init = function () {}; - - /** - * Request data from Zabbix API - * @return {object} response.result - */ - this._request = function(url, method, params, auth) { - var options = { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - url: url, - data: { - jsonrpc: '2.0', - method: method, - params: params, - auth: auth, - id: 1 - } - }; - - if (this.basicAuth || this.withCredentials) { - options.withCredentials = true; - } - if (this.basicAuth) { - options.headers.Authorization = this.basicAuth; - } - - var self = this; - return backendSrv.datasourceRequest(options).then(function (response) { - if (!response.data) { - return []; - } - // Handle Zabbix API errors - else if (response.data.error) { - - // Handle auth errors - if (response.data.error.data === "Session terminated, re-login, please." || - response.data.error.data === "Not authorised." || - response.data.error.data === "Not authorized") { - return self.performZabbixAPILogin().then(function (response) { - self.auth = response; - return self.performZabbixAPIRequest(method, params); - }); - } - } - return response.data.result; - }); - }; - - /** - * Get authentication token. - * @return {string} auth token - */ - this.login = function(url, username, password) { - var options = { - url : this.url, - method : 'POST', - data: { - jsonrpc: '2.0', - method: 'user.login', - params: { - user: this.username, - password: this.password - }, - auth: null, - id: 1 - } - }; - - if (this.basicAuth || this.withCredentials) { - options.withCredentials = true; - } - if (this.basicAuth) { - options.headers = options.headers || {}; - options.headers.Authorization = this.basicAuth; - } - - return backendSrv.datasourceRequest(options).then(function (result) { - if (!result.data) { - return null; - } - return result.data.result; - }); - }; - - }); -}); \ No newline at end of file diff --git a/plugins/datasource-zabbix/zabbixAPIService.js b/plugins/datasource-zabbix/zabbixAPIService.js new file mode 100644 index 0000000..21c9ec7 --- /dev/null +++ b/plugins/datasource-zabbix/zabbixAPIService.js @@ -0,0 +1,89 @@ +/** + * General Zabbix API methods + */ + +define([ + 'angular', +], +function (angular) { + 'use strict'; + + var module = angular.module('grafana.services'); + + module.service('ZabbixAPIService', function($q, backendSrv) { + + /** + * Request data from Zabbix API + * @return {object} response.result + */ + this._request = function(api_url, method, params, options, auth) { + var requestData = { + jsonrpc: '2.0', + method: method, + params: params, + id: 1 + }; + + // Set auth parameter only if it needed + if (auth) { + requestData.auth = auth; + } + + var requestOptions = { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + url: api_url, + data: requestData + }; + + // Set request options for basic auth + if (options.basicAuth || options.withCredentials) { + requestOptions.withCredentials = true; + } + if (options.basicAuth) { + requestOptions.headers.Authorization = options.basicAuth; + } + + return backendSrv.datasourceRequest(requestOptions).then(function (response) { + // General connection issues + if (!response.data) { + return []; + } + + // Handle Zabbix API errors + else if (response.data.error) { + throw new ZabbixException(response.data.error); + } + + return response.data.result; + }); + }; + + /** + * Get authentication token. + * @return {string} auth token + */ + this.login = function(api_url, username, password, options) { + var params = { + user: username, + password: password + }; + return this._request(api_url, 'user.login', params, options, null); + }; + + }); + + // Define zabbix API exception type + function ZabbixException(error) { + this.code = error.code; + this.message = error.message; + this.data = error.data; + } + + ZabbixException.prototype.toString = function() { + return this.name + " " + this.message; + }; + +}); \ No newline at end of file