blob: 86623f715ec2a0f1d286d3555e8125dfe1e83c33 [file] [log] [blame]
#include "tlbmc/redfish/routes/task_service.h"
#include <filesystem> // NOLINT
#include <fstream>
#include <ios>
#include <iterator>
#include <string>
#include "absl/functional/bind_front.h"
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "nlohmann/json.hpp"
#include "json_utils.h"
#include "tlbmc/redfish/app.h"
#include "tlbmc/redfish/request.h"
#include "tlbmc/redfish/response.h"
namespace milotic_tlbmc::task_service {
namespace {
constexpr absl::string_view kInstallOngoingFile = "run/install/install_ongoing";
constexpr absl::string_view kProgressStatusPath =
"var/google/install/progress_status.json";
void HandleFirmwareUpdateTask(const std::string& root_path,
const RedfishRequest& req,
RedfishResponse& resp) {
resp.SetKeyInJsonBody("/@odata.type", "#Task.v1_4_2.Task");
resp.SetKeyInJsonBody("/@odata.id",
"/redfish/v1/TaskService/Tasks/FirmwareBundleUpdate");
resp.SetKeyInJsonBody("/Id", "FirmwareBundleUpdate");
resp.SetKeyInJsonBody("/Name", "Firmware Bundle Update");
// Set default values for the task state and percent completed.
resp.SetKeyInJsonBody("/TaskState", "New");
const std::filesystem::path status_file_path =
std::filesystem::path(root_path) / std::string(kProgressStatusPath);
std::ifstream status_file(status_file_path, std::ios::binary);
std::string content((std::istreambuf_iterator<char>(status_file)),
std::istreambuf_iterator<char>());
if (!status_file.good()) {
resp.SetToInternalError(absl::StrCat(
"Failed to read status file: ", status_file_path.c_str()));
return;
}
nlohmann::json status_json = nlohmann::json::parse(content, nullptr, false);
if (status_json.is_discarded()) {
resp.SetToInternalError(absl::StrCat(
"Failed to parse status file as JSON: ", kProgressStatusPath));
return;
}
// LINT.IfChange
if (const auto* percent_complete =
milotic::authz::GetValueAsInt(status_json, "percent_complete");
percent_complete != nullptr) {
resp.SetKeyInJsonBody("/PercentComplete", *percent_complete);
}
if (const auto* start_time =
milotic::authz::GetValueAsString(status_json, "start_time");
start_time != nullptr) {
resp.SetKeyInJsonBody("/StartTime", *start_time);
}
if (const auto* task_state =
milotic::authz::GetValueAsString(status_json, "status");
task_state != nullptr) {
resp.SetKeyInJsonBody("/TaskState", *task_state);
}
// LINT.ThenChange(//depot/google3/third_party/milotic/external/cc/bloom_install/installer.cc)
}
void HandleTaskService(const RedfishRequest& req, RedfishResponse& resp) {
resp.SetKeyInJsonBody("/@odata.id", "/redfish/v1/TaskService");
resp.SetKeyInJsonBody("/@odata.type", "#TaskService.v1_2_0.TaskService");
resp.SetKeyInJsonBody("/Tasks", "/redfish/v1/TaskService/Tasks");
}
void HandleTaskCollection(const RedfishRequest& req, RedfishResponse& resp) {
resp.SetKeyInJsonBody("/@odata.id", "/redfish/v1/TaskService/Tasks");
resp.SetKeyInJsonBody("/@odata.type", "#TaskCollection.TaskCollection");
resp.SetKeyInJsonBody("/Members@odata.count", 1);
resp.SetKeyInJsonBody("/Members",
nlohmann::json::array({"/redfish/v1/TaskService/Tasks/"
"FirmwareBundleUpdate"}));
}
} // namespace
void RegisterRoutes(RedfishApp& app) {
TLBMC_ROUTE(app, "/redfish/v1/TaskService/")
.methods(boost::beast::http::verb::get)(HandleTaskService);
TLBMC_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
.methods(boost::beast::http::verb::get)(HandleTaskCollection);
TLBMC_ROUTE(app, "/redfish/v1/TaskService/Tasks/FirmwareBundleUpdate/")
.methods(boost::beast::http::verb::get)(
absl::bind_front(HandleFirmwareUpdateTask, "/"));
}
void RegisterRoutesForUnitTest(
RedfishApp& app, const std::string& install_inventory_path_prefix) {
TLBMC_ROUTE(app, "/redfish/v1/TaskService/")
.methods(boost::beast::http::verb::get)(HandleTaskService);
TLBMC_ROUTE(app, "/redfish/v1/TaskService/Tasks/")
.methods(boost::beast::http::verb::get)(HandleTaskCollection);
TLBMC_ROUTE(app, "/redfish/v1/TaskService/Tasks/FirmwareBundleUpdate/")
.methods(boost::beast::http::verb::get)(
absl::bind_front(HandleFirmwareUpdateTask,
std::string(install_inventory_path_prefix)));
}
} // namespace milotic_tlbmc::task_service