Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 3 | #include <dbus/connection.hpp> |
| 4 | #include <dbus/endpoint.hpp> |
| 5 | #include <dbus/filter.hpp> |
| 6 | #include <dbus/match.hpp> |
| 7 | #include <dbus/message.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 8 | #include <persistent_data_middleware.hpp> |
| 9 | #include <token_authorization_middleware.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 10 | #include <fstream> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 11 | #include <streambuf> |
| 12 | #include <string> |
Ed Tanous | 2a866f8 | 2017-10-25 17:46:24 -0700 | [diff] [blame] | 13 | #include <crow/app.h> |
| 14 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 15 | namespace crow { |
| 16 | namespace redfish { |
| 17 | |
| 18 | template <typename... Middlewares> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 19 | void get_redfish_sub_routes(Crow<Middlewares...>& app, const std::string& url, |
| 20 | nlohmann::json& j) { |
Ed Tanous | 1c74de8 | 2017-10-26 13:58:28 -0700 | [diff] [blame] | 21 | std::vector<const std::string*> routes = app.get_routes(url); |
| 22 | for (auto route : routes) { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 23 | auto redfish_sub_route = |
Ed Tanous | 1c74de8 | 2017-10-26 13:58:28 -0700 | [diff] [blame] | 24 | route->substr(url.size(), route->size() - url.size() - 1); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 25 | // check if the route is at this level, and we didn't find and exact match |
| 26 | // also, filter out resources that start with $ to remove $metadata |
| 27 | if (!redfish_sub_route.empty() && redfish_sub_route[0] != '$' && |
| 28 | redfish_sub_route.find('/') == std::string::npos) { |
Ed Tanous | 1c74de8 | 2017-10-26 13:58:28 -0700 | [diff] [blame] | 29 | j[redfish_sub_route] = nlohmann::json{{"@odata.id", *route}}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 30 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 31 | } |
| 32 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 2a866f8 | 2017-10-25 17:46:24 -0700 | [diff] [blame] | 34 | std::string execute_process(const char* cmd) { |
| 35 | std::array<char, 128> buffer; |
| 36 | std::string result; |
| 37 | std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); |
| 38 | if (!pipe) throw std::runtime_error("popen() failed!"); |
| 39 | while (!feof(pipe.get())) { |
| 40 | if (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) |
| 41 | result += buffer.data(); |
| 42 | } |
| 43 | return result; |
| 44 | } |
| 45 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 46 | template <typename... Middlewares> |
| 47 | void request_routes(Crow<Middlewares...>& app) { |
| 48 | CROW_ROUTE(app, "/redfish/") |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 49 | .methods("GET"_method)([](const crow::request& req, crow::response& res) { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 50 | res.json_value = {{"v1", "/redfish/v1/"}}; |
| 51 | res.end(); |
| 52 | }); |
| 53 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 54 | CROW_ROUTE(app, "/redfish/v1/Chassis/") |
| 55 | .methods("GET"_method)( |
| 56 | [&](const crow::request& req, crow::response& res) { |
| 57 | std::vector<std::string> entities; |
| 58 | /*std::ifstream f("~/system.json"); |
| 59 | |
| 60 | nlohmann::json input = nlohmann::json::parse(f); |
| 61 | for (auto it = input.begin(); it != input.end(); it++) { |
| 62 | auto value = it.value(); |
| 63 | if (value["type"] == "Chassis") { |
| 64 | std::string str = value["name"]; |
| 65 | entities.emplace_back(str); |
| 66 | } |
| 67 | } |
| 68 | */ |
Ed Tanous | 1c74de8 | 2017-10-26 13:58:28 -0700 | [diff] [blame] | 69 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 70 | res.json_value = { |
| 71 | {"@odata.context", |
| 72 | "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"}, |
| 73 | {"@odata.id", "/redfish/v1/Chassis"}, |
| 74 | {"@odata.type", "#ChassisCollection.ChassisCollection"}, |
| 75 | {"Name", "Chassis Collection"}, |
| 76 | {"Members@odata.count", entities.size()}}; |
| 77 | |
| 78 | get_redfish_sub_routes(app, "/redfish/v1/Chassis", res.json_value); |
| 79 | res.end(); |
| 80 | }); |
| 81 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 82 | CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/") |
| 83 | .methods( |
| 84 | "GET"_method)([&](const crow::request& req, crow::response& res) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 85 | boost::asio::io_service io; |
| 86 | auto bus = std::make_shared<dbus::connection>(io, dbus::bus::session); |
| 87 | dbus::endpoint user_list("org.openbmc.UserManager", |
| 88 | "/org/openbmc/UserManager/Users", |
| 89 | "org.openbmc.Enrol", "UserList"); |
| 90 | bus->async_method_call( |
| 91 | [&](const boost::system::error_code ec, |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 92 | const std::vector<std::string>& users) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 93 | if (ec) { |
| 94 | res.code = 500; |
| 95 | } else { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 96 | res.json_value = { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 97 | {"@odata.context", |
| 98 | "/redfish/v1/" |
| 99 | "$metadata#ManagerAccountCollection." |
| 100 | "ManagerAccountCollection"}, |
| 101 | {"@odata.id", "/redfish/v1/AccountService/Accounts"}, |
| 102 | {"@odata.type", |
| 103 | "#ManagerAccountCollection.ManagerAccountCollection"}, |
| 104 | {"Name", "Accounts Collection"}, |
| 105 | {"Description", "BMC User Accounts"}, |
| 106 | {"Members@odata.count", users.size()}}; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 107 | nlohmann::json member_array = nlohmann::json::array(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 108 | int user_index = 0; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 109 | for (int user_index = 0; user_index < users.size(); |
| 110 | user_index++) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 111 | member_array.push_back( |
Kowalski, Kamil | 2b7981f | 2018-01-31 13:24:59 +0100 | [diff] [blame] | 112 | {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + |
| 113 | std::to_string(user_index)}}); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 114 | } |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 115 | res.json_value["Members"] = member_array; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 116 | } |
| 117 | res.end(); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 118 | }, |
| 119 | user_list); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 120 | }); |
| 121 | |
| 122 | CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/<int>/") |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 123 | .methods("GET"_method)([](const crow::request& req, crow::response& res, |
| 124 | int account_index) { |
| 125 | res.json_value = { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 126 | {"@odata.context", |
| 127 | "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"}, |
| 128 | {"@odata.id", "/redfish/v1/AccountService/Accounts/1"}, |
| 129 | {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"}, |
| 130 | {"Id", "1"}, |
| 131 | {"Name", "User Account"}, |
| 132 | {"Description", "User Account"}, |
| 133 | {"Enabled", false}, |
| 134 | {"Password", nullptr}, |
| 135 | {"UserName", "anonymous"}, |
| 136 | {"RoleId", "NoAccess"}, |
| 137 | {"Links", |
| 138 | {{"Role", |
| 139 | {{"@odata.id", "/redfish/v1/AccountService/Roles/NoAccess"}}}}}}; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 140 | res.end(); |
| 141 | }); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 142 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 143 | } // namespace redfish |
| 144 | } // namespace crow |