Skip to content

Commit

Permalink
[control-plane] [sdk] Add a helper method to check app's existence
Browse files Browse the repository at this point in the history
  • Loading branch information
mcopik committed Oct 15, 2024
1 parent 38f2494 commit ddbbef2
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ endforeach()
if(PRAAS_WITH_TESTING)

set(TESTS
#tests/integration/invocation.cpp
tests/integration/invocation.cpp
tests/integration/allocation_invocation.cpp
tests/integration/state_swap.cpp
)
Expand Down
6 changes: 6 additions & 0 deletions control-plane/include/praas/control-plane/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace praas::control_plane {

METHOD_LIST_BEGIN
ADD_METHOD_TO(HttpServer::create_app, "/apps/{1}", drogon::Put);
ADD_METHOD_TO(HttpServer::get_app, "/apps/{1}", drogon::Get);
ADD_METHOD_TO(HttpServer::delete_app, "/apps/{1}/delete", drogon::Post);
ADD_METHOD_TO(HttpServer::create_process, "/apps/{1}/processes/{2}", drogon::Put);
ADD_METHOD_TO(HttpServer::delete_process, "/apps/{1}/processes/{2}/delete", drogon::Post);
Expand All @@ -47,6 +48,11 @@ namespace praas::control_plane {
std::function<void(const drogon::HttpResponsePtr&)>&& callback, const std::string& app_name
);

void get_app(
const drogon::HttpRequestPtr& request,
std::function<void(const drogon::HttpResponsePtr&)>&& callback, const std::string& app_name
);

void delete_app(
const drogon::HttpRequestPtr& request,
std::function<void(const drogon::HttpResponsePtr&)>&& callback, const std::string& app_name
Expand Down
2 changes: 2 additions & 0 deletions control-plane/include/praas/control-plane/worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ namespace praas::control_plane::worker {
////////////////////////////////////////////////////////////////////////////////
bool create_application(const std::string& app_name, ApplicationResources&& cloud_resources);

bool get_application(const std::string& app_name);

bool delete_application(const std::string& app_name);

////////////////////////////////////////////////////////////////////////////////
Expand Down
23 changes: 23 additions & 0 deletions control-plane/src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ namespace praas::control_plane {
});
}

void HttpServer::get_app(
const drogon::HttpRequestPtr& request,
std::function<void(const drogon::HttpResponsePtr&)>&& callback, const std::string& app_name
)
{

_logger->info("Get application {}", app_name);
_workers.add_task([=, callback = std::move(callback), this]() {

auto response = _workers.get_application(app_name);
if (!response) {
callback(failed_response("Application does not exist!", drogon::HttpStatusCode::k404NotFound));
}

Json::Value json;
json["application"] = app_name;

auto resp = drogon::HttpResponse::newHttpJsonResponse(json);
resp->setStatusCode(drogon::HttpStatusCode::k200OK);
callback(resp);
});
}

void HttpServer::delete_app(
const drogon::HttpRequestPtr& request,
std::function<void(const drogon::HttpResponsePtr&)>&& callback, const std::string& app_name
Expand Down
9 changes: 9 additions & 0 deletions control-plane/src/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ namespace praas::control_plane::worker {
return true;
}

bool
Workers::get_application(const std::string& app_name)
{
Resources::ROAccessor acc;
_resources.get_application(app_name, acc);

return !acc.empty();
}

bool
Workers::delete_application(const std::string& app_name)
{
Expand Down
2 changes: 2 additions & 0 deletions sdk/include/praas/sdk/praas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace praas::sdk {

bool create_application(const std::string& application, const std::string& cloud_resource_name);

bool get_application(const std::string& application);

bool delete_application(const std::string& application);

std::optional<Process> create_process(
Expand Down
22 changes: 22 additions & 0 deletions sdk/src/praas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ namespace praas::sdk {
return p.get_future().get();
}

bool
PraaS::get_application(const std::string& application)
{
auto req = drogon::HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath(fmt::format("/apps/{}", application));

std::promise<bool> p;

_http_client->sendRequest(
req,
[&](drogon::ReqResult result, const drogon::HttpResponsePtr& response) {
if (result == drogon::ReqResult::Ok) {
p.set_value(response->getStatusCode() == drogon::k200OK);
} else {
p.set_value(false);
}
}
);
return p.get_future().get();
}

bool PraaS::delete_application(const std::string& application)
{
auto req = drogon::HttpRequest::newHttpRequest();
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/invocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ TEST_F(IntegrationLocalInvocation, Invoke)
{
praas::common::http::HTTPClientFactory::initialize(1);
{
praas::sdk::PraaS praas{fmt::format("http://127.0.0.1:{}", 8000)};
praas::sdk::PraaS praas{fmt::format("http://127.0.0.1:{}", 9000)};

ASSERT_TRUE(praas.create_application("test_invoc", "spcleth/praas-examples:hello-world-cpp"));
if(!praas.get_application("test_invoc")) {
ASSERT_TRUE(praas.create_application("test_invoc", "spcleth/praas-examples:hello-world-cpp"));
}

auto invoc = praas.invoke("test_invoc", "hello-world", "");

Expand Down

0 comments on commit ddbbef2

Please sign in to comment.