Fix API initialization, closes #861
This commit is contained in:
@@ -62,12 +62,16 @@ export class ZabbixAPIConnector {
|
|||||||
|
|
||||||
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
|
return this.zabbixAPICore.request(this.url, method, params, this.requestOptions, this.auth)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (isNotAuthorized(error.data)) {
|
if (isNotInitialized(error.data)) {
|
||||||
|
// If API not initialized yet (auth is empty), login first
|
||||||
|
return this.loginOnce()
|
||||||
|
.then(() => this.request(method, params));
|
||||||
|
} else if (isNotAuthorized(error.data)) {
|
||||||
// Handle auth errors
|
// Handle auth errors
|
||||||
this.loginErrorCount++;
|
this.loginErrorCount++;
|
||||||
if (this.loginErrorCount > this.maxLoginAttempts) {
|
if (this.loginErrorCount > this.maxLoginAttempts) {
|
||||||
this.loginErrorCount = 0;
|
this.loginErrorCount = 0;
|
||||||
return null;
|
return Promise.resolve();
|
||||||
} else {
|
} else {
|
||||||
return this.loginOnce()
|
return this.loginOnce()
|
||||||
.then(() => this.request(method, params));
|
.then(() => this.request(method, params));
|
||||||
@@ -237,7 +241,7 @@ export class ZabbixAPIConnector {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return this.request('item.get', params)
|
return this.request('item.get', params)
|
||||||
.then(utils.expandItems);
|
.then(items => utils.expandItems(items));
|
||||||
}
|
}
|
||||||
|
|
||||||
getMacros(hostids) {
|
getMacros(hostids) {
|
||||||
@@ -662,6 +666,10 @@ function filterTriggersByAcknowledge(triggers, acknowledged) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isNotInitialized(message) {
|
||||||
|
return message === "Not initialized";
|
||||||
|
}
|
||||||
|
|
||||||
function isNotAuthorized(message) {
|
function isNotAuthorized(message) {
|
||||||
return (
|
return (
|
||||||
message === "Session terminated, re-login, please." ||
|
message === "Session terminated, re-login, please." ||
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export class ZabbixAPICore {
|
|||||||
|
|
||||||
if (auth === "") {
|
if (auth === "") {
|
||||||
// Reject immediately if not authenticated
|
// Reject immediately if not authenticated
|
||||||
return Promise.reject(new ZabbixAPIError({data: "Not authorised."}));
|
return Promise.reject(new ZabbixAPIError({data: "Not initialized"}));
|
||||||
} else if (auth) {
|
} else if (auth) {
|
||||||
// Set auth parameter only if it needed
|
// Set auth parameter only if it needed
|
||||||
requestData.auth = auth;
|
requestData.auth = auth;
|
||||||
|
|||||||
@@ -86,10 +86,12 @@ function cacheRequest(func, funcName, funcScope, self) {
|
|||||||
} else {
|
} else {
|
||||||
return func.apply(funcScope, arguments)
|
return func.apply(funcScope, arguments)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
|
if (result !== undefined) {
|
||||||
cacheObject[hash] = {
|
cacheObject[hash] = {
|
||||||
value: result,
|
value: result,
|
||||||
timestamp: Date.now()
|
timestamp: Date.now()
|
||||||
};
|
};
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user