Remove Redfish Node class
Reduces the total number of lines and will allow for easier testing of
the redfish responses.
A main purpose of the node class was to set app.routeDynamic(). However
now app.routeDynamic can handle the complexity that was once in critical
to node. The macro app.routeDynamic() provides a shorter cleaner
interface to the unerlying app.routeDyanic call. The old pattern set
permissions for 6 interfaces (get, head, patch, put, delete_, and post)
even if only one interface is created. That pattern creates unneeded
code that can be safely removed with no effect.
Unit test for the responses would have to mock the node the class in
order to fully test responses.
see https://github.com/openbmc/bmcweb/issues/181
The following files still need node to be extracted.
virtual_media.hpp
account_service.hpp
redfish_sessions.hpp
ethernet.hpp
The files above use a pattern that is not trivial to address. Often their
responses call an async lambda capturing the inherited class. ie
(https://github.com/openbmc/bmcweb/blob/ffed87b5ad1797ca966d030e7f979770
28d258fa/redfish-core/lib/account_service.hpp#L1393)
At a later point I plan to remove node from the files above.
Tested:
I ran the docker unit test with the following command.
WORKSPACE=$(pwd) UNIT_TEST_PKG=bmcweb
./openbmc-build-scripts/run-unit-test-docker.sh
I ran the validator and this change did not create any issues.
python3 RedfishServiceValidator.py -c config.ini
Signed-off-by: John Edward Broadbent <jebr@google.com>
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I147a0289c52cb4198345b1ad9bfe6fdddf57f3df
diff --git a/redfish-core/lib/roles.hpp b/redfish-core/lib/roles.hpp
index 8e00d43..b375ff0 100644
--- a/redfish-core/lib/roles.hpp
+++ b/redfish-core/lib/roles.hpp
@@ -15,7 +15,7 @@
*/
#pragma once
-#include "node.hpp"
+#include <app.hpp>
#include <variant>
@@ -70,114 +70,86 @@
return true;
}
-class Roles : public Node
+inline void requestRoutesRoles(App& app)
{
- public:
- Roles(App& app) :
- Node(app, "/redfish/v1/AccountService/Roles/<str>/", std::string())
- {
- entityPrivileges = {
- {boost::beast::http::verb::get, {{"Login"}}},
- {boost::beast::http::verb::head, {{"Login"}}},
- {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
- {boost::beast::http::verb::put, {{"ConfigureManager"}}},
- {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
- {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
- }
+ BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/<str>/")
+ .privileges({"Login"})
+ .methods(boost::beast::http::verb::get)(
+ [](const crow::Request&,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const std::string& roleId) {
+ nlohmann::json privArray = nlohmann::json::array();
+ if (false == getAssignedPrivFromRole(roleId, privArray))
+ {
+ messages::resourceNotFound(asyncResp->res, "Role", roleId);
- private:
- void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
- const crow::Request&,
- const std::vector<std::string>& params) override
- {
- if (params.size() != 1)
- {
- messages::internalError(asyncResp->res);
+ return;
+ }
- return;
- }
- const std::string& roleId = params[0];
- nlohmann::json privArray = nlohmann::json::array();
- if (false == getAssignedPrivFromRole(roleId, privArray))
- {
- messages::resourceNotFound(asyncResp->res, "Role", roleId);
+ asyncResp->res.jsonValue = {
+ {"@odata.type", "#Role.v1_2_2.Role"},
+ {"Name", "User Role"},
+ {"Description", roleId + " User Role"},
+ {"OemPrivileges", nlohmann::json::array()},
+ {"IsPredefined", true},
+ {"Id", roleId},
+ {"RoleId", roleId},
+ {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
+ {"AssignedPrivileges", std::move(privArray)}};
+ });
+}
- return;
- }
-
- asyncResp->res.jsonValue = {
- {"@odata.type", "#Role.v1_2_2.Role"},
- {"Name", "User Role"},
- {"Description", roleId + " User Role"},
- {"OemPrivileges", nlohmann::json::array()},
- {"IsPredefined", true},
- {"Id", roleId},
- {"RoleId", roleId},
- {"@odata.id", "/redfish/v1/AccountService/Roles/" + roleId},
- {"AssignedPrivileges", std::move(privArray)}};
- }
-};
-
-class RoleCollection : public Node
+inline void requestRoutesRoleCollection(App& app)
{
- public:
- RoleCollection(App& app) : Node(app, "/redfish/v1/AccountService/Roles/")
- {
- entityPrivileges = {
- {boost::beast::http::verb::get, {{"Login"}}},
- {boost::beast::http::verb::head, {{"Login"}}},
- {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
- {boost::beast::http::verb::put, {{"ConfigureManager"}}},
- {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
- {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
- }
+ BMCWEB_ROUTE(app, "/redfish/v1/AccountService/Roles/")
+ .privileges({"Login"})
+ .methods(boost::beast::http::verb::get)(
+ [](const crow::Request&,
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
+ asyncResp->res.jsonValue = {
+ {"@odata.id", "/redfish/v1/AccountService/Roles"},
+ {"@odata.type", "#RoleCollection.RoleCollection"},
+ {"Name", "Roles Collection"},
+ {"Description", "BMC User Roles"}};
- private:
- void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
- const crow::Request&, const std::vector<std::string>&) override
- {
-
- asyncResp->res.jsonValue = {
- {"@odata.id", "/redfish/v1/AccountService/Roles"},
- {"@odata.type", "#RoleCollection.RoleCollection"},
- {"Name", "Roles Collection"},
- {"Description", "BMC User Roles"}};
-
- crow::connections::systemBus->async_method_call(
- [asyncResp](const boost::system::error_code ec,
+ crow::connections::systemBus->async_method_call(
+ [asyncResp](
+ const boost::system::error_code ec,
const std::variant<std::vector<std::string>>& resp) {
- if (ec)
- {
- messages::internalError(asyncResp->res);
- return;
- }
- nlohmann::json& memberArray =
- asyncResp->res.jsonValue["Members"];
- memberArray = nlohmann::json::array();
- const std::vector<std::string>* privList =
- std::get_if<std::vector<std::string>>(&resp);
- if (privList == nullptr)
- {
- messages::internalError(asyncResp->res);
- return;
- }
- for (const std::string& priv : *privList)
- {
- std::string role = getRoleFromPrivileges(priv);
- if (!role.empty())
- {
- memberArray.push_back(
- {{"@odata.id",
- "/redfish/v1/AccountService/Roles/" + role}});
- }
- }
- asyncResp->res.jsonValue["Members@odata.count"] =
- memberArray.size();
- },
- "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
- "org.freedesktop.DBus.Properties", "Get",
- "xyz.openbmc_project.User.Manager", "AllPrivileges");
- }
-};
+ if (ec)
+ {
+ messages::internalError(asyncResp->res);
+ return;
+ }
+ nlohmann::json& memberArray =
+ asyncResp->res.jsonValue["Members"];
+ memberArray = nlohmann::json::array();
+ const std::vector<std::string>* privList =
+ std::get_if<std::vector<std::string>>(&resp);
+ if (privList == nullptr)
+ {
+ messages::internalError(asyncResp->res);
+ return;
+ }
+ for (const std::string& priv : *privList)
+ {
+ std::string role = getRoleFromPrivileges(priv);
+ if (!role.empty())
+ {
+ memberArray.push_back(
+ {{"@odata.id",
+ "/redfish/v1/AccountService/Roles/" +
+ role}});
+ }
+ }
+ asyncResp->res.jsonValue["Members@odata.count"] =
+ memberArray.size();
+ },
+ "xyz.openbmc_project.User.Manager",
+ "/xyz/openbmc_project/user",
+ "org.freedesktop.DBus.Properties", "Get",
+ "xyz.openbmc_project.User.Manager", "AllPrivileges");
+ });
+}
} // namespace redfish