mirror of https://github.com/Icinga/icinga2.git
parent
7e4953dd35
commit
f570875c8b
|
@ -27,7 +27,7 @@ endif()
|
||||||
|
|
||||||
add_executable(icinga-studio MACOSX_BUNDLE WIN32 icinga-studio.cpp
|
add_executable(icinga-studio MACOSX_BUNDLE WIN32 icinga-studio.cpp
|
||||||
forms.cpp aboutform.cpp connectform.cpp mainform.cpp
|
forms.cpp aboutform.cpp connectform.cpp mainform.cpp
|
||||||
icinga.icns api.cpp ${WindowsSources})
|
icinga.icns apiclient.cpp ${WindowsSources})
|
||||||
|
|
||||||
include_directories(${Boost_INCLUDE_DIRS})
|
include_directories(${Boost_INCLUDE_DIRS})
|
||||||
target_link_libraries(icinga-studio ${Boost_LIBRARIES} ${wxWidgets_LIBRARIES} base remote)
|
target_link_libraries(icinga-studio ${Boost_LIBRARIES} ${wxWidgets_LIBRARIES} base remote)
|
||||||
|
|
|
@ -17,14 +17,13 @@
|
||||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#include "icinga-studio/api.hpp"
|
#include "icinga-studio/apiclient.hpp"
|
||||||
#include "remote/base64.hpp"
|
#include "remote/base64.hpp"
|
||||||
#include "base/json.hpp"
|
#include "base/json.hpp"
|
||||||
#include "base/logger.hpp"
|
#include "base/logger.hpp"
|
||||||
#include "base/exception.hpp"
|
#include "base/exception.hpp"
|
||||||
#include "base/convert.hpp"
|
#include "base/convert.hpp"
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
#include <wx/msgdlg.h>
|
|
||||||
|
|
||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
|
||||||
|
@ -46,13 +45,7 @@ void ApiClient::GetTypes(const TypesCompletionCallback& callback) const
|
||||||
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
||||||
m_Connection->SubmitRequest(req, boost::bind(TypesHttpCompletionCallback, _1, _2, callback));
|
m_Connection->SubmitRequest(req, boost::bind(TypesHttpCompletionCallback, _1, _2, callback));
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
callback(std::vector<ApiType::Ptr>());
|
callback(boost::current_exception(), std::vector<ApiType::Ptr>());
|
||||||
|
|
||||||
std::string message = "HTTP request (" + url->Format() + ") failed.";
|
|
||||||
wxMessageBox(message);
|
|
||||||
|
|
||||||
Log(LogCritical, "ApiClient")
|
|
||||||
<< "HTTP request failed: " << DiagnosticInformation(ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,14 +61,15 @@ void ApiClient::TypesHttpCompletionCallback(HttpRequest& request, HttpResponse&
|
||||||
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
||||||
body += String(buffer, buffer + count);
|
body += String(buffer, buffer + count);
|
||||||
|
|
||||||
std::vector<ApiType::Ptr> types;
|
try {
|
||||||
|
|
||||||
if (response.StatusCode < 200 || response.StatusCode > 299) {
|
if (response.StatusCode < 200 || response.StatusCode > 299) {
|
||||||
std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body;
|
std::string message = "HTTP request failed; Code: " + Convert::ToString(response.StatusCode) + "; Body: " + body;
|
||||||
|
|
||||||
wxMessageBox(message);
|
BOOST_THROW_EXCEPTION(ScriptError(message));
|
||||||
} else {
|
}
|
||||||
try {
|
|
||||||
|
std::vector<ApiType::Ptr> types;
|
||||||
|
|
||||||
result = JsonDecode(body);
|
result = JsonDecode(body);
|
||||||
|
|
||||||
Array::Ptr results = result->Get("results");
|
Array::Ptr results = result->Get("results");
|
||||||
|
@ -91,13 +85,14 @@ void ApiClient::TypesHttpCompletionCallback(HttpRequest& request, HttpResponse&
|
||||||
// TODO: attributes
|
// TODO: attributes
|
||||||
types.push_back(type);
|
types.push_back(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callback(boost::exception_ptr(), types);
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
Log(LogCritical, "ApiClient")
|
Log(LogCritical, "ApiClient")
|
||||||
<< "Error while decoding response: " << DiagnosticInformation(ex);
|
<< "Error while decoding response: " << DiagnosticInformation(ex);
|
||||||
}
|
callback(boost::current_exception(), std::vector<ApiType::Ptr>());
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(types);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ApiClient::GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback,
|
void ApiClient::GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback,
|
||||||
|
@ -129,13 +124,7 @@ void ApiClient::GetObjects(const String& pluralType, const ObjectsCompletionCall
|
||||||
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
req->AddHeader("Authorization", "Basic " + Base64::Encode(m_User + ":" + m_Password));
|
||||||
m_Connection->SubmitRequest(req, boost::bind(ObjectsHttpCompletionCallback, _1, _2, callback));
|
m_Connection->SubmitRequest(req, boost::bind(ObjectsHttpCompletionCallback, _1, _2, callback));
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
callback(std::vector<ApiObject::Ptr>());
|
callback(boost::current_exception(), std::vector<ApiObject::Ptr>());
|
||||||
|
|
||||||
std::string message = "HTTP request (" + pUrl->Format() + ") failed.";
|
|
||||||
wxMessageBox(message);
|
|
||||||
|
|
||||||
Log(LogCritical, "ApiClient")
|
|
||||||
<< "HTTP request failed: " << DiagnosticInformation(ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,13 +140,15 @@ void ApiClient::ObjectsHttpCompletionCallback(HttpRequest& request,
|
||||||
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
while ((count = response.ReadBody(buffer, sizeof(buffer))) > 0)
|
||||||
body += String(buffer, buffer + count);
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<ApiObject::Ptr> objects;
|
std::vector<ApiObject::Ptr> objects;
|
||||||
|
|
||||||
if (response.StatusCode < 200 || response.StatusCode > 299) {
|
|
||||||
Log(LogCritical, "ApiClient")
|
|
||||||
<< "Failed HTTP request; Code: " << response.StatusCode << "; Body: " << body;
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
result = JsonDecode(body);
|
result = JsonDecode(body);
|
||||||
|
|
||||||
Array::Ptr results = result->Get("results");
|
Array::Ptr results = result->Get("results");
|
||||||
|
@ -194,11 +185,11 @@ void ApiClient::ObjectsHttpCompletionCallback(HttpRequest& request,
|
||||||
objects.push_back(object);
|
objects.push_back(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callback(boost::exception_ptr(), objects);
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
Log(LogCritical, "ApiClient")
|
Log(LogCritical, "ApiClient")
|
||||||
<< "Error while decoding response: " << DiagnosticInformation(ex);
|
<< "Error while decoding response: " << DiagnosticInformation(ex);
|
||||||
|
callback(boost::current_exception(), std::vector<ApiObject::Ptr>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(objects);
|
|
||||||
}
|
|
||||||
|
|
|
@ -17,11 +17,12 @@
|
||||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
#ifndef API_H
|
#ifndef APICLIENT_H
|
||||||
#define API_H
|
#define APICLIENT_H
|
||||||
|
|
||||||
#include "remote/httpclientconnection.hpp"
|
#include "remote/httpclientconnection.hpp"
|
||||||
#include "base/value.hpp"
|
#include "base/value.hpp"
|
||||||
|
#include "base/exception.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
|
@ -87,10 +88,10 @@ public:
|
||||||
ApiClient(const String& host, const String& port,
|
ApiClient(const String& host, const String& port,
|
||||||
const String& user, const String& password);
|
const String& user, const String& password);
|
||||||
|
|
||||||
typedef boost::function<void(const std::vector<ApiType::Ptr>&)> TypesCompletionCallback;
|
typedef boost::function<void(boost::exception_ptr, const std::vector<ApiType::Ptr>&)> TypesCompletionCallback;
|
||||||
void GetTypes(const TypesCompletionCallback& callback) const;
|
void GetTypes(const TypesCompletionCallback& callback) const;
|
||||||
|
|
||||||
typedef boost::function<void(const std::vector<ApiObject::Ptr>&)> ObjectsCompletionCallback;
|
typedef boost::function<void(boost::exception_ptr, const std::vector<ApiObject::Ptr>&)> ObjectsCompletionCallback;
|
||||||
void GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback,
|
void GetObjects(const String& pluralType, const ObjectsCompletionCallback& callback,
|
||||||
const std::vector<String>& names = std::vector<String>(),
|
const std::vector<String>& names = std::vector<String>(),
|
||||||
const std::vector<String>& attrs = std::vector<String>()) const;
|
const std::vector<String>& attrs = std::vector<String>()) const;
|
||||||
|
@ -108,4 +109,4 @@ private:
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* API_H */
|
#endif /* APICLIENT_H */
|
||||||
|
|
|
@ -39,7 +39,7 @@ MainForm::MainForm(wxWindow *parent, const Url::Ptr& url)
|
||||||
port = "5665";
|
port = "5665";
|
||||||
|
|
||||||
m_ApiClient = new ApiClient(url->GetHost(), port, url->GetUsername(), url->GetPassword());
|
m_ApiClient = new ApiClient(url->GetHost(), port, url->GetUsername(), url->GetPassword());
|
||||||
m_ApiClient->GetTypes(boost::bind(&MainForm::TypesCompletionHandler, this, _1, true));
|
m_ApiClient->GetTypes(boost::bind(&MainForm::TypesCompletionHandler, this, _2, true));
|
||||||
|
|
||||||
std::string title = url->Format() + " - Icinga Studio";
|
std::string title = url->Format() + " - Icinga Studio";
|
||||||
SetTitle(title);
|
SetTitle(title);
|
||||||
|
@ -101,7 +101,7 @@ void MainForm::OnTypeSelected(wxTreeEvent& event)
|
||||||
std::vector<String> attrs;
|
std::vector<String> attrs;
|
||||||
attrs.push_back(type->Name.ToLower() + ".__name");
|
attrs.push_back(type->Name.ToLower() + ".__name");
|
||||||
|
|
||||||
m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectsCompletionHandler, this, _1, true),
|
m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectsCompletionHandler, this, _2, true),
|
||||||
std::vector<String>(), attrs);
|
std::vector<String>(), attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ void MainForm::OnObjectSelected(wxListEvent& event)
|
||||||
std::vector<String> names;
|
std::vector<String> names;
|
||||||
names.push_back(objectName);
|
names.push_back(objectName);
|
||||||
|
|
||||||
m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectDetailsCompletionHandler, this, _1, true), names);
|
m_ApiClient->GetObjects(type->PluralName, boost::bind(&MainForm::ObjectDetailsCompletionHandler, this, _2, true), names);
|
||||||
}
|
}
|
||||||
|
|
||||||
wxPGProperty *MainForm::ValueToProperty(const String& name, const Value& value)
|
wxPGProperty *MainForm::ValueToProperty(const String& name, const Value& value)
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
#ifndef MAINFORM_H
|
#ifndef MAINFORM_H
|
||||||
#define MAINFORM_H
|
#define MAINFORM_H
|
||||||
|
|
||||||
#include "icinga-studio/api.hpp"
|
#include "icinga-studio/apiclient.hpp"
|
||||||
#include "remote/url.hpp"
|
#include "remote/url.hpp"
|
||||||
#include "icinga-studio/forms.h"
|
#include "icinga-studio/forms.h"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue