Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 3 | #include <crow/app.h> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame^] | 4 | |
| 5 | #include <dbus/connection.hpp> |
| 6 | #include <dbus/endpoint.hpp> |
| 7 | #include <dbus/filter.hpp> |
| 8 | #include <dbus/match.hpp> |
| 9 | #include <dbus/message.hpp> |
| 10 | #include <fstream> |
| 11 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 12 | namespace crow { |
| 13 | namespace redfish { |
| 14 | |
| 15 | template <typename... Middlewares> |
| 16 | void request_routes(Crow<Middlewares...>& app) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame^] | 17 | |
| 18 | // noop for now |
| 19 | return; |
| 20 | |
| 21 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 22 | CROW_ROUTE(app, "/redfish/").methods("GET"_method)([]() { |
| 23 | return nlohmann::json{{"v1", "/redfish/v1/"}}; |
| 24 | }); |
| 25 | CROW_ROUTE(app, "/redfish/v1/").methods("GET"_method)([]() { |
| 26 | return nlohmann::json{ |
| 27 | {"@odata.context", "/redfish/v1/$metadata#ServiceRoot.ServiceRoot"}, |
| 28 | {"@odata.id", "/redfish/v1/"}, |
| 29 | {"@odata.type", "#ServiceRoot.v1_1_1.ServiceRoot"}, |
| 30 | {"Id", "RootService"}, |
| 31 | {"Name", "Root Service"}, |
| 32 | {"RedfishVersion", "1.1.0"}, |
| 33 | {"UUID", "bdfc5c6d-07a9-4e67-972f-bd2b30c6a0e8"}, |
| 34 | //{"Systems", {{"@odata.id", "/redfish/v1/Systems"}}}, |
| 35 | //{"Chassis", {{"@odata.id", "/redfish/v1/Chassis"}}}, |
| 36 | //{"Managers", {{"@odata.id", "/redfish/v1/Managers"}}}, |
| 37 | //{"SessionService", {{"@odata.id", "/redfish/v1/SessionService"}}}, |
| 38 | {"AccountService", {{"@odata.id", "/redfish/v1/AccountService"}}}, |
| 39 | //{"UpdateService", {{"@odata.id", "/redfish/v1/UpdateService"}}}, |
| 40 | /*{"Links", |
| 41 | {{"Sessions", |
| 42 | {{"@odata.id", "/redfish/v1/SessionService/Sessions"}}}}}*/ |
| 43 | }; |
| 44 | }); |
| 45 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame^] | 46 | CROW_ROUTE(app, "/redfish/v1/Chassis").methods("GET"_method)([]() { |
| 47 | std::vector<std::string> entities; |
| 48 | std::ifstream f("~/system.json"); |
| 49 | nlohmann::json input; |
| 50 | input << f; |
| 51 | for (auto it = input.begin(); it != input.end(); it++) { |
| 52 | auto value = it.value(); |
| 53 | if (value["type"] == "Chassis") { |
| 54 | std::string str = value["name"]; |
| 55 | entities.emplace_back(str); |
| 56 | } |
| 57 | } |
| 58 | auto ret = nlohmann::json{ |
| 59 | {"@odata.context", |
| 60 | "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"}, |
| 61 | {"@odata.id", "/redfish/v1/Chassis"}, |
| 62 | {"@odata.type", "#ChassisCollection.ChassisCollection"}, |
| 63 | {"Name", "Chassis Collection"}, |
| 64 | {"Members@odata.count", entities.size()}}; |
| 65 | return ret; |
| 66 | }); |
| 67 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 68 | CROW_ROUTE(app, "/redfish/v1/AccountService").methods("GET"_method)([]() { |
| 69 | return nlohmann::json{ |
| 70 | {"@odata.context", |
| 71 | "/redfish/v1/$metadata#AccountService.AccountService"}, |
| 72 | {"@odata.id", "/redfish/v1/AccountService"}, |
| 73 | {"@odata.type", "#AccountService.v1_1_0.AccountService"}, |
| 74 | {"Id", "AccountService"}, |
| 75 | {"Name", "Account Service"}, |
| 76 | {"Description", "BMC User Accounts"}, |
| 77 | {"Status", |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame^] | 78 | // TODO(ed) health rollup |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 79 | {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}}, |
| 80 | {"ServiceEnabled", true}, |
| 81 | {"MinPasswordLength", 1}, |
| 82 | {"MaxPasswordLength", 20}, |
| 83 | {"Accounts", {{"@odata.id", "/redfish/v1/AccountService/Accounts"}}}, |
| 84 | //{"Roles", {{"@odata.id", "/redfish/v1/AccountService/Roles"}}} |
| 85 | }; |
| 86 | }); |
| 87 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame^] | 88 | CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts") |
| 89 | .methods("GET"_method)([](const crow::request& req, crow::response& res) { |
| 90 | boost::asio::io_service io; |
| 91 | auto bus = std::make_shared<dbus::connection>(io, dbus::bus::session); |
| 92 | dbus::endpoint user_list("org.openbmc.UserManager", |
| 93 | "/org/openbmc/UserManager/Users", |
| 94 | "org.openbmc.Enrol", "UserList"); |
| 95 | bus->async_method_call( |
| 96 | [&](const boost::system::error_code ec, |
| 97 | const std::vector<std::string>& users) { |
| 98 | if (ec) { |
| 99 | res.code = 500; |
| 100 | } else { |
| 101 | nlohmann::json return_json{ |
| 102 | {"@odata.context", |
| 103 | "/redfish/v1/" |
| 104 | "$metadata#ManagerAccountCollection." |
| 105 | "ManagerAccountCollection"}, |
| 106 | {"@odata.id", "/redfish/v1/AccountService/Accounts"}, |
| 107 | {"@odata.type", |
| 108 | "#ManagerAccountCollection.ManagerAccountCollection"}, |
| 109 | {"Name", "Accounts Collection"}, |
| 110 | {"Description", "BMC User Accounts"}, |
| 111 | {"Members@odata.count", users.size()}}; |
| 112 | nlohmann::json member_array; |
| 113 | int user_index = 0; |
| 114 | for (auto& user : users) { |
| 115 | member_array.push_back( |
| 116 | {{"@odata.id", "/redfish/v1/AccountService/Accounts/" + |
| 117 | std::to_string(++user_index)}}); |
| 118 | } |
| 119 | return_json["Members"] = member_array; |
| 120 | } |
| 121 | res.end(); |
| 122 | }, user_list); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 123 | }); |
| 124 | |
| 125 | CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/<int>/") |
| 126 | .methods("GET"_method)([](int account_index) { |
| 127 | return nlohmann::json{ |
| 128 | {"@odata.context", |
| 129 | "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"}, |
| 130 | {"@odata.id", "/redfish/v1/AccountService/Accounts/1"}, |
| 131 | {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"}, |
| 132 | {"Id", "1"}, |
| 133 | {"Name", "User Account"}, |
| 134 | {"Description", "User Account"}, |
| 135 | {"Enabled", false}, |
| 136 | {"Password", nullptr}, |
| 137 | {"UserName", "anonymous"}, |
| 138 | {"RoleId", "NoAccess"}, |
| 139 | {"Links", |
| 140 | {{"Role", |
| 141 | {{"@odata.id", "/redfish/v1/AccountService/Roles/NoAccess"}}}}}}; |
| 142 | }); |
| 143 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame^] | 144 | } // namespace redfish |
| 145 | } // namespace crow |