var YugabyteDB = {
	params: {},
	clusters: [],
	error_msg: '',

	setParams: function (params) {
		['account_id', 'project_id', 'token', 'cluster_id'].forEach(function (field) {
			if (typeof params !== 'object' || typeof params[field] === 'undefined' || params[field] === '') {
				throw 'Required param is not set: ' + field + '.';
			}
		});

		YugabyteDB.params = params;
	},


	request: function (url, data) {
		if (typeof data === 'undefined' || data === null) {
			data = '';
		}

		var response, request = new HttpRequest();
		if (typeof YugabyteDB.params.proxy !== 'undefined' && YugabyteDB.params.proxy !== '') {
			request.setProxy(YugabyteDB.params.proxy);
		}
		if (YugabyteDB.params.token) {
			request.addHeader('Accept: application/json');
			request.addHeader('Authorization: Bearer ' + YugabyteDB.params.token);
		}

		Zabbix.log(4, '[ YugabyteDB ] Sending request: ' + url);

		response = request.get(url);

		Zabbix.log(4, '[ YugabyteDB ] Received response with status code ' + request.getStatus() + ': ' + response);

		if (request.getStatus() !== 200 || response === null) {
			throw 'Request failed with status code ' + request.getStatus() + ': ' + response;
		}

		try {
			return JSON.parse(response);
		}
		catch (error) {
			throw 'Failed to parse response received from API:' + error;
		}
	}

};

try {
	YugabyteDB.setParams(JSON.parse(value));

	cluster = YugabyteDB.request(
		'https://cloud.yugabyte.com/api/public/v1/accounts/' + YugabyteDB.params.account_id + '/projects/' + YugabyteDB.params.project_id + '/clusters/' + YugabyteDB.params.cluster_id
	)

	YugabyteDB.clusters = cluster.data;
}
catch (error) {
	Zabbix.log(3, '[ YugabyteDB ] ERROR: ' + error);
	YugabyteDB.error_msg = 'Failed to get data from API:' + error;
}

return JSON.stringify({'data': YugabyteDB.clusters, 'error': YugabyteDB.error_msg});