blob: 52b9410dcb2a42b7ce3e2039bfe6e0c208818421 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous911ac312017-08-15 09:37:42 -07003#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 Tanousba9f9a62017-10-11 16:40:35 -07008#include <persistent_data_middleware.hpp>
9#include <token_authorization_middleware.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070010#include <fstream>
Ed Tanousba9f9a62017-10-11 16:40:35 -070011#include <streambuf>
12#include <string>
Ed Tanous2a866f82017-10-25 17:46:24 -070013#include <crow/app.h>
14#include <boost/algorithm/string.hpp>
Ed Tanous3dac7492017-08-02 13:46:20 -070015namespace crow {
16namespace redfish {
17
18template <typename... Middlewares>
Ed Tanousba9f9a62017-10-11 16:40:35 -070019void get_redfish_sub_routes(Crow<Middlewares...>& app, const std::string& url,
20 nlohmann::json& j) {
Ed Tanous1c74de82017-10-26 13:58:28 -070021 std::vector<const std::string*> routes = app.get_routes(url);
22 for (auto route : routes) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070023 auto redfish_sub_route =
Ed Tanous1c74de82017-10-26 13:58:28 -070024 route->substr(url.size(), route->size() - url.size() - 1);
Ed Tanousba9f9a62017-10-11 16:40:35 -070025 // 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 Tanous1c74de82017-10-26 13:58:28 -070029 j[redfish_sub_route] = nlohmann::json{{"@odata.id", *route}};
Ed Tanous911ac312017-08-15 09:37:42 -070030 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070031 }
32}
Ed Tanous911ac312017-08-15 09:37:42 -070033
Ed Tanous2a866f82017-10-25 17:46:24 -070034std::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 Tanousba9f9a62017-10-11 16:40:35 -070046template <typename... Middlewares>
47void request_routes(Crow<Middlewares...>& app) {
48 CROW_ROUTE(app, "/redfish/")
Ed Tanous911ac312017-08-15 09:37:42 -070049 .methods("GET"_method)([](const crow::request& req, crow::response& res) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070050 res.json_value = {{"v1", "/redfish/v1/"}};
51 res.end();
52 });
53
Ed Tanousba9f9a62017-10-11 16:40:35 -070054 CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
55 .methods(
56 "GET"_method)([&](const crow::request& req, crow::response& res) {
Ed Tanous911ac312017-08-15 09:37:42 -070057 boost::asio::io_service io;
58 auto bus = std::make_shared<dbus::connection>(io, dbus::bus::session);
59 dbus::endpoint user_list("org.openbmc.UserManager",
60 "/org/openbmc/UserManager/Users",
61 "org.openbmc.Enrol", "UserList");
62 bus->async_method_call(
63 [&](const boost::system::error_code ec,
Ed Tanousba9f9a62017-10-11 16:40:35 -070064 const std::vector<std::string>& users) {
Ed Tanous911ac312017-08-15 09:37:42 -070065 if (ec) {
66 res.code = 500;
67 } else {
Ed Tanousba9f9a62017-10-11 16:40:35 -070068 res.json_value = {
Ed Tanous911ac312017-08-15 09:37:42 -070069 {"@odata.context",
70 "/redfish/v1/"
71 "$metadata#ManagerAccountCollection."
72 "ManagerAccountCollection"},
73 {"@odata.id", "/redfish/v1/AccountService/Accounts"},
74 {"@odata.type",
75 "#ManagerAccountCollection.ManagerAccountCollection"},
76 {"Name", "Accounts Collection"},
77 {"Description", "BMC User Accounts"},
78 {"Members@odata.count", users.size()}};
Ed Tanousba9f9a62017-10-11 16:40:35 -070079 nlohmann::json member_array = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -070080 int user_index = 0;
Ed Tanousc963aa42017-10-27 16:00:19 -070081 for (int user_index = 0; user_index < users.size();
82 user_index++) {
Ed Tanous911ac312017-08-15 09:37:42 -070083 member_array.push_back(
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +010084 {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
85 std::to_string(user_index)}});
Ed Tanous911ac312017-08-15 09:37:42 -070086 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070087 res.json_value["Members"] = member_array;
Ed Tanous911ac312017-08-15 09:37:42 -070088 }
89 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -070090 },
91 user_list);
Ed Tanous3dac7492017-08-02 13:46:20 -070092 });
93
94 CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/<int>/")
Ed Tanousba9f9a62017-10-11 16:40:35 -070095 .methods("GET"_method)([](const crow::request& req, crow::response& res,
96 int account_index) {
97 res.json_value = {
Ed Tanous3dac7492017-08-02 13:46:20 -070098 {"@odata.context",
99 "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
100 {"@odata.id", "/redfish/v1/AccountService/Accounts/1"},
101 {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
102 {"Id", "1"},
103 {"Name", "User Account"},
104 {"Description", "User Account"},
105 {"Enabled", false},
106 {"Password", nullptr},
107 {"UserName", "anonymous"},
108 {"RoleId", "NoAccess"},
109 {"Links",
110 {{"Role",
111 {{"@odata.id", "/redfish/v1/AccountService/Roles/NoAccess"}}}}}};
Ed Tanousba9f9a62017-10-11 16:40:35 -0700112 res.end();
113 });
Ed Tanous3dac7492017-08-02 13:46:20 -0700114}
Ed Tanous911ac312017-08-15 09:37:42 -0700115} // namespace redfish
116} // namespace crow