2015-08-29 01:16:16 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2015-08-29 01:16:16 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
#include "remote/apiclient.hpp"
|
2017-09-11 16:51:13 +02:00
|
|
|
#include "base/base64.hpp"
|
2015-08-29 01:16:16 +02:00
|
|
|
#include "base/json.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2015-09-29 13:49:55 +02:00
|
|
|
#include "base/convert.hpp"
|
2015-08-29 01:16:16 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ApiClient::ApiClient(const String& host, const String& port,
|
|
|
|
const String& user, const String& password)
|
|
|
|
: m_Connection(new HttpClientConnection(host, port, true)), m_User(user), m_Password(password)
|
|
|
|
{
|
|
|
|
m_Connection->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::GetTypes(const TypesCompletionCallback& callback) const
|
|
|
|
{
|
2015-09-30 14:02:18 +02:00
|
|
|
Url::Ptr url = new Url();
|
|
|
|
url->SetScheme("https");
|
|
|
|
url->SetHost(m_Connection->GetHost());
|
|
|
|
url->SetPort(m_Connection->GetPort());
|
|
|
|
|
|
|
|
std::vector<String> path;
|
|
|
|
path.push_back("v1");
|
|
|
|
path.push_back("types");
|
|
|
|
url->SetPath(path);
|
2015-09-29 13:49:55 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
boost::shared_ptr<HttpRequest> req = m_Connection->NewRequest();
|
|
|
|
req->RequestMethod = "GET";
|
|
|
|
req->RequestUrl = url;
|
|
|
|
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
2015-11-05 15:18:53 +01:00
|
|
|
req->AddHeader("Accept", "application/json");
|
2015-09-29 13:49:55 +02:00
|
|
|
m_Connection->SubmitRequest(req, boost::bind(TypesHttpCompletionCallback, _1, _2, callback));
|
|
|
|
} catch (const std::exception& ex) {
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), std::vector<ApiType::Ptr>());
|
2015-09-29 13:49:55 +02:00
|
|
|
}
|
2015-08-29 01:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::TypesHttpCompletionCallback(HttpRequest& request, HttpResponse& response,
|
|
|
|
const TypesCompletionCallback& callback)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr result;
|
|
|
|
|
|
|
|
String body;
|
|
|
|
char buffer[1024];
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
|
|
|
body += String(buffer, buffer + count);
|
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
try {
|
|
|
|
if (response.StatusCode < 200 || response.StatusCode > 299) {
|
|
|
|
std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body;
|
2015-09-29 10:31:16 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(message));
|
|
|
|
}
|
2015-09-29 13:49:55 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
std::vector<ApiType::Ptr> types;
|
2015-09-29 08:09:48 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
result = JsonDecode(body);
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
Array::Ptr results = result->Get("results");
|
|
|
|
|
|
|
|
ObjectLock olock(results);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Ptr typeInfo : results)
|
2015-09-30 08:49:30 +02:00
|
|
|
{
|
|
|
|
ApiType::Ptr type = new ApiType();;
|
|
|
|
type->Abstract = typeInfo->Get("abstract");
|
|
|
|
type->BaseName = typeInfo->Get("base");
|
|
|
|
type->Name = typeInfo->Get("name");
|
|
|
|
type->PluralName = typeInfo->Get("plural_name");
|
|
|
|
// TODO: attributes
|
|
|
|
types.push_back(type);
|
2015-08-29 01:16:16 +02:00
|
|
|
}
|
2015-09-30 08:49:30 +02:00
|
|
|
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::exception_ptr(), types);
|
2015-09-30 08:49:30 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "ApiClient")
|
|
|
|
<< "Error while decoding response: " << DiagnosticInformation(ex);
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), std::vector<ApiType::Ptr>());
|
2015-08-29 01:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback,
|
2015-11-06 11:04:58 +01:00
|
|
|
const std::vector<String>& names, const std::vector<String>& attrs, const std::vector<String>& joins, bool all_joins) const
|
2015-08-29 01:16:16 +02:00
|
|
|
{
|
2015-09-30 14:02:18 +02:00
|
|
|
Url::Ptr url = new Url();
|
|
|
|
url->SetScheme("https");
|
|
|
|
url->SetHost(m_Connection->GetHost());
|
|
|
|
url->SetPort(m_Connection->GetPort());
|
|
|
|
|
|
|
|
std::vector<String> path;
|
|
|
|
path.push_back("v1");
|
|
|
|
path.push_back("objects");
|
|
|
|
path.push_back(pluralType);
|
|
|
|
url->SetPath(path);
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-09-30 14:02:18 +02:00
|
|
|
std::map<String, std::vector<String> > params;
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& name : names) {
|
2015-09-30 14:02:18 +02:00
|
|
|
params[pluralType.ToLower()].push_back(name);
|
2015-08-29 01:16:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& attr : attrs) {
|
2015-09-30 14:02:18 +02:00
|
|
|
params["attrs"].push_back(attr);
|
2015-08-29 01:16:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& join : joins) {
|
2015-11-06 11:04:58 +01:00
|
|
|
params["joins"].push_back(join);
|
|
|
|
}
|
|
|
|
|
|
|
|
params["all_joins"].push_back(all_joins ? "1" : "0");
|
|
|
|
|
2015-09-30 14:02:18 +02:00
|
|
|
url->SetQuery(params);
|
2015-09-29 13:49:55 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
boost::shared_ptr<HttpRequest> req = m_Connection->NewRequest();
|
|
|
|
req->RequestMethod = "GET";
|
2015-09-30 14:02:18 +02:00
|
|
|
req->RequestUrl = url;
|
2015-09-29 13:49:55 +02:00
|
|
|
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
2015-11-05 15:18:53 +01:00
|
|
|
req->AddHeader("Accept", "application/json");
|
2015-09-29 13:49:55 +02:00
|
|
|
m_Connection->SubmitRequest(req, boost::bind(ObjectsHttpCompletionCallback, _1, _2, callback));
|
|
|
|
} catch (const std::exception& ex) {
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), std::vector<ApiObject::Ptr>());
|
2015-09-29 13:49:55 +02:00
|
|
|
}
|
2015-08-29 01:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::ObjectsHttpCompletionCallback(HttpRequest& request,
|
|
|
|
HttpResponse& response, const ObjectsCompletionCallback& callback)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr result;
|
|
|
|
|
|
|
|
String body;
|
|
|
|
char buffer[1024];
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
|
|
|
body += String(buffer, buffer + count);
|
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
try {
|
|
|
|
if (response.StatusCode < 200 || response.StatusCode > 299) {
|
|
|
|
std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body;
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(message));
|
|
|
|
}
|
2015-09-29 10:31:16 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
std::vector<ApiObject::Ptr> objects;
|
2015-09-29 08:09:48 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
result = JsonDecode(body);
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
Array::Ptr results = result->Get("results");
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
if (results) {
|
|
|
|
ObjectLock olock(results);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Ptr objectInfo : results) {
|
2015-09-30 08:49:30 +02:00
|
|
|
ApiObject::Ptr object = new ApiObject();
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-11-06 11:04:58 +01:00
|
|
|
object->Name = objectInfo->Get("name");
|
|
|
|
object->Type = objectInfo->Get("type");
|
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
Dictionary::Ptr attrs = objectInfo->Get("attrs");
|
|
|
|
|
2015-11-06 11:04:58 +01:00
|
|
|
if (attrs) {
|
2015-09-30 08:49:30 +02:00
|
|
|
ObjectLock olock(attrs);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : attrs) {
|
2015-11-06 11:04:58 +01:00
|
|
|
object->Attrs[object->Type.ToLower() + "." + kv.first] = kv.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr joins = objectInfo->Get("joins");
|
|
|
|
|
|
|
|
if (joins) {
|
|
|
|
ObjectLock olock(joins);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : joins) {
|
2015-11-06 11:04:58 +01:00
|
|
|
Dictionary::Ptr attrs = kv.second;
|
|
|
|
|
|
|
|
if (attrs) {
|
|
|
|
ObjectLock olock(attrs);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv2 : attrs) {
|
2015-11-06 11:04:58 +01:00
|
|
|
object->Attrs[kv.first + "." + kv2.first] = kv2.second;
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 08:09:48 +02:00
|
|
|
}
|
2015-09-30 08:49:30 +02:00
|
|
|
}
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-09-30 08:49:30 +02:00
|
|
|
Array::Ptr used_by = objectInfo->Get("used_by");
|
2015-08-29 01:16:16 +02:00
|
|
|
|
2015-11-06 11:04:58 +01:00
|
|
|
if (used_by) {
|
2015-09-30 08:49:30 +02:00
|
|
|
ObjectLock olock(used_by);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Ptr& refInfo : used_by) {
|
2015-09-30 08:49:30 +02:00
|
|
|
ApiObjectReference ref;
|
|
|
|
ref.Name = refInfo->Get("name");
|
|
|
|
ref.Type = refInfo->Get("type");
|
|
|
|
object->UsedBy.push_back(ref);
|
2015-09-29 08:09:48 +02:00
|
|
|
}
|
2015-09-29 10:31:16 +02:00
|
|
|
}
|
2015-09-30 08:49:30 +02:00
|
|
|
|
|
|
|
objects.push_back(object);
|
2015-09-29 08:09:48 +02:00
|
|
|
}
|
2015-08-29 01:16:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::exception_ptr(), objects);
|
2015-09-30 08:49:30 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "ApiClient")
|
|
|
|
<< "Error while decoding response: " << DiagnosticInformation(ex);
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), std::vector<ApiObject::Ptr>());
|
2015-09-30 08:49:30 +02:00
|
|
|
}
|
2015-09-18 13:04:09 +02:00
|
|
|
}
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
void ApiClient::ExecuteScript(const String& session, const String& command, bool sandboxed,
|
|
|
|
const ExecuteScriptCompletionCallback& callback) const
|
|
|
|
{
|
|
|
|
Url::Ptr url = new Url();
|
|
|
|
url->SetScheme("https");
|
|
|
|
url->SetHost(m_Connection->GetHost());
|
|
|
|
url->SetPort(m_Connection->GetPort());
|
|
|
|
|
|
|
|
std::vector<String> path;
|
|
|
|
path.push_back("v1");
|
|
|
|
path.push_back("console");
|
|
|
|
path.push_back("execute-script");
|
|
|
|
url->SetPath(path);
|
|
|
|
|
|
|
|
std::map<String, std::vector<String> > params;
|
|
|
|
params["session"].push_back(session);
|
|
|
|
params["command"].push_back(command);
|
|
|
|
params["sandboxed"].push_back(sandboxed ? "1" : "0");
|
|
|
|
url->SetQuery(params);
|
|
|
|
|
|
|
|
try {
|
|
|
|
boost::shared_ptr<HttpRequest> req = m_Connection->NewRequest();
|
|
|
|
req->RequestMethod = "POST";
|
|
|
|
req->RequestUrl = url;
|
|
|
|
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
2015-11-05 15:18:53 +01:00
|
|
|
req->AddHeader("Accept", "application/json");
|
2015-11-02 16:35:21 +01:00
|
|
|
m_Connection->SubmitRequest(req, boost::bind(ExecuteScriptHttpCompletionCallback, _1, _2, callback));
|
|
|
|
} catch (const std::exception& ex) {
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), Empty);
|
2015-11-02 16:35:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::ExecuteScriptHttpCompletionCallback(HttpRequest& request,
|
|
|
|
HttpResponse& response, const ExecuteScriptCompletionCallback& callback)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr result;
|
|
|
|
|
|
|
|
String body;
|
|
|
|
char buffer[1024];
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
|
|
|
body += String(buffer, buffer + count);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (response.StatusCode < 200 || response.StatusCode > 299) {
|
|
|
|
std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body;
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
result = JsonDecode(body);
|
|
|
|
|
|
|
|
Array::Ptr results = result->Get("results");
|
|
|
|
Value result;
|
|
|
|
String errorMessage = "Unexpected result from API.";
|
|
|
|
|
|
|
|
if (results && results->GetLength() > 0) {
|
|
|
|
Dictionary::Ptr resultInfo = results->Get(0);
|
|
|
|
errorMessage = resultInfo->Get("status");
|
|
|
|
|
|
|
|
if (resultInfo->Get("code") >= 200 && resultInfo->Get("code") <= 299) {
|
|
|
|
result = resultInfo->Get("result");
|
|
|
|
} else {
|
|
|
|
DebugInfo di;
|
|
|
|
Dictionary::Ptr debugInfo = resultInfo->Get("debug_info");
|
|
|
|
if (debugInfo) {
|
|
|
|
di.Path = debugInfo->Get("path");
|
|
|
|
di.FirstLine = debugInfo->Get("first_line");
|
|
|
|
di.FirstColumn = debugInfo->Get("first_column");
|
|
|
|
di.LastLine = debugInfo->Get("last_line");
|
|
|
|
di.LastColumn = debugInfo->Get("last_column");
|
|
|
|
}
|
|
|
|
bool incompleteExpression = resultInfo->Get("incomplete_expression");
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(errorMessage, di, incompleteExpression));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::exception_ptr(), result);
|
2015-11-02 16:35:21 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), Empty);
|
2015-11-02 16:35:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::AutocompleteScript(const String& session, const String& command, bool sandboxed,
|
|
|
|
const AutocompleteScriptCompletionCallback& callback) const
|
|
|
|
{
|
|
|
|
Url::Ptr url = new Url();
|
|
|
|
url->SetScheme("https");
|
|
|
|
url->SetHost(m_Connection->GetHost());
|
|
|
|
url->SetPort(m_Connection->GetPort());
|
|
|
|
|
|
|
|
std::vector<String> path;
|
|
|
|
path.push_back("v1");
|
|
|
|
path.push_back("console");
|
|
|
|
path.push_back("auto-complete-script");
|
|
|
|
url->SetPath(path);
|
|
|
|
|
|
|
|
std::map<String, std::vector<String> > params;
|
|
|
|
params["session"].push_back(session);
|
|
|
|
params["command"].push_back(command);
|
|
|
|
params["sandboxed"].push_back(sandboxed ? "1" : "0");
|
|
|
|
url->SetQuery(params);
|
|
|
|
|
|
|
|
try {
|
|
|
|
boost::shared_ptr<HttpRequest> req = m_Connection->NewRequest();
|
|
|
|
req->RequestMethod = "POST";
|
|
|
|
req->RequestUrl = url;
|
|
|
|
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
2015-11-05 15:18:53 +01:00
|
|
|
req->AddHeader("Accept", "application/json");
|
2015-11-02 16:35:21 +01:00
|
|
|
m_Connection->SubmitRequest(req, boost::bind(AutocompleteScriptHttpCompletionCallback, _1, _2, callback));
|
|
|
|
} catch (const std::exception& ex) {
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), Array::Ptr());
|
2015-11-02 16:35:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiClient::AutocompleteScriptHttpCompletionCallback(HttpRequest& request,
|
|
|
|
HttpResponse& response, const AutocompleteScriptCompletionCallback& callback)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr result;
|
|
|
|
|
|
|
|
String body;
|
|
|
|
char buffer[1024];
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
|
|
|
body += String(buffer, buffer + count);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (response.StatusCode < 200 || response.StatusCode > 299) {
|
|
|
|
std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body;
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
result = JsonDecode(body);
|
|
|
|
|
|
|
|
Array::Ptr results = result->Get("results");
|
|
|
|
Array::Ptr suggestions;
|
|
|
|
String errorMessage = "Unexpected result from API.";
|
|
|
|
|
|
|
|
if (results && results->GetLength() > 0) {
|
|
|
|
Dictionary::Ptr resultInfo = results->Get(0);
|
|
|
|
errorMessage = resultInfo->Get("status");
|
|
|
|
|
|
|
|
if (resultInfo->Get("code") >= 200 && resultInfo->Get("code") <= 299)
|
|
|
|
suggestions = resultInfo->Get("suggestions");
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(errorMessage));
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::exception_ptr(), suggestions);
|
2015-11-02 16:35:21 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2016-08-31 13:43:14 +02:00
|
|
|
callback(boost::current_exception(), Array::Ptr());
|
2015-11-02 16:35:21 +01:00
|
|
|
}
|
|
|
|
}
|