backend: proxy Zabbix API requests through the backend

This commit is contained in:
Alexander Zobnin
2019-09-24 20:45:36 +03:00
parent 0938d06487
commit 6b8c11f4f4
6 changed files with 108 additions and 5 deletions

View File

@@ -10,7 +10,7 @@ import { ZBX_ACK_ACTION_NONE, ZBX_ACK_ACTION_ACK, ZBX_ACK_ACTION_ADD_MESSAGE, MI
* Wraps API calls and provides high-level methods.
*/
export class ZabbixAPIConnector {
constructor(api_url, username, password, version, basicAuth, withCredentials, backendSrv) {
constructor(api_url, username, password, version, basicAuth, withCredentials, backendSrv, datasourceId) {
this.url = api_url;
this.username = username;
this.password = password;
@@ -22,6 +22,9 @@ export class ZabbixAPIConnector {
withCredentials: withCredentials
};
this.datasourceId = datasourceId;
this.backendSrv = backendSrv;
this.loginPromise = null;
this.loginErrorCount = 0;
this.maxLoginAttempts = 3;
@@ -37,6 +40,8 @@ export class ZabbixAPIConnector {
//////////////////////////
request(method, params) {
this.tsdbRequest(method, params);
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
.catch(error => {
if (isNotAuthorized(error.data)) {
@@ -55,6 +60,25 @@ export class ZabbixAPIConnector {
});
}
tsdbRequest(method, params) {
const tsdbRequestData = {
queries: [{
datasourceId: this.datasourceId,
queryType: 'zabbixAPI',
target: {
method,
params,
},
}],
};
return this.backendSrv.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: tsdbRequestData
});
}
/**
* When API unauthenticated or auth token expired each request produce login()
* call. But auth token is common to all requests. This function wraps login() method

View File

@@ -25,7 +25,7 @@ const REQUESTS_TO_BIND = [
];
export class Zabbix {
constructor(options, datasourceSrv, backendSrv) {
constructor(options, datasourceSrv, backendSrv, datasourceId) {
let {
url,
username,
@@ -49,7 +49,7 @@ export class Zabbix {
};
this.cachingProxy = new CachingProxy(cacheOptions);
this.zabbixAPI = new ZabbixAPIConnector(url, username, password, zabbixVersion, basicAuth, withCredentials, backendSrv);
this.zabbixAPI = new ZabbixAPIConnector(url, username, password, zabbixVersion, basicAuth, withCredentials, backendSrv, datasourceId);
this.proxyfyRequests();
this.cacheRequests();