Create Redfish specific setProperty call
There are currently 78 sdbusplus::asio::setProperty calls in
redfish-core. The error handler for nearly all of them looks something
like:
```
if (ec)
{
const sd_bus_error* dbusError = msg.get_error();
if ((dbusError != nullptr) &&
(dbusError->name ==
std::string_view(
"xyz.openbmc_project.Common.Error.InvalidArgument")))
{
BMCWEB_LOG_WARNING("DBUS response error: {}", ec);
messages::propertyValueIncorrect(asyncResp->res, "<PropertyName>", <PropertyValue>);
return;
}
messages::internalError(asyncResp->res);
return;
}
messages::success(asyncResp->res);
```
In some cases there are more errors handled that translate to more error
messages, but the vast majority only handle InvalidArgument. Many of
these, like the ones in account_service.hpp, do the error handling in a
lambda, which causes readability problems. This commit starts to make
things more consistent, and easier for trivial property sets.
This commit invents a setDbusProperty method in the redfish namespace
that tries to handle all DBus errors in a consistent manner. Looking
for input on whether this will work before changing over the other 73
calls. Overall this is less code, fewer inline lambdas, and defaults
that should work for MOST use cases of calling an OpenBMC daemon, and
fall back to more generic errors when calling a "normal" dbus daemon.
As part of this, I've ported over several examples. Some things that
might be up in the air:
1. Do we always return 204 no_content on property sets? Today there's a
mix of 200, with a Base::Success message, and 204, with an empty body.
2. Do all DBus response codes map to the same error? A majority are
covered by xyz.openbmc_project.Common.Error.InvalidArgument, but there
are likely differences. If we allow any daemon to return any return
code, does that cause compatibility problems later?
Tested:
```
curl -k --user "root:0penBmc" -H "Content-Type: application/json" -X PATCH -d '{"HostName":"openbmc@#"}' https://192.168.7.2/redfish/v1/Managers/bmc/EthernetInterfaces/eth0
```
Returns the appropriate error in the response
Base.1.16.0.PropertyValueIncorrect
Change-Id: If033a1112ba516792c9386c997d090c8f9094f3a
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/redfish-core/lib/ethernet.hpp b/redfish-core/lib/ethernet.hpp
index d1d47f5..b73da3e 100644
--- a/redfish-core/lib/ethernet.hpp
+++ b/redfish-core/lib/ethernet.hpp
@@ -729,18 +729,12 @@
const std::string& ifaceId, const std::string& gateway,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
- sdbusplus::asio::setProperty(
- *crow::connections::systemBus, "xyz.openbmc_project.Network",
- "/xyz/openbmc_project/network/" + ifaceId,
+ setDbusProperty(
+ asyncResp, "xyz.openbmc_project.Network",
+ sdbusplus::message::object_path("/xyz/openbmc_project/network") /
+ ifaceId,
"xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
- gateway, [asyncResp](const boost::system::error_code& ec) {
- if (ec)
- {
- messages::internalError(asyncResp->res);
- return;
- }
- asyncResp->res.result(boost::beast::http::status::no_content);
- });
+ "Gateway", gateway);
}
/**
* @brief Creates a static IPv4 entry
@@ -1224,33 +1218,22 @@
"HostName");
return;
}
- sdbusplus::asio::setProperty(
- *crow::connections::systemBus, "xyz.openbmc_project.Network",
- "/xyz/openbmc_project/network/config",
- "xyz.openbmc_project.Network.SystemConfiguration", "HostName", hostname,
- [asyncResp](const boost::system::error_code& ec) {
- if (ec)
- {
- messages::internalError(asyncResp->res);
- }
- });
+ setDbusProperty(
+ asyncResp, "xyz.openbmc_project.Network",
+ sdbusplus::message::object_path("/xyz/openbmc_project/network/config"),
+ "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
+ "HostName", hostname);
}
inline void
handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
- sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
- ifaceId;
- sdbusplus::asio::setProperty(
- *crow::connections::systemBus, "xyz.openbmc_project.Network", objPath,
- "xyz.openbmc_project.Network.EthernetInterface", "MTU", mtuSize,
- [asyncResp](const boost::system::error_code& ec) {
- if (ec)
- {
- messages::internalError(asyncResp->res);
- }
- });
+ sdbusplus::message::object_path objPath("/xyz/openbmc_project/network");
+ objPath /= ifaceId;
+ setDbusProperty(asyncResp, "xyz.openbmc_project.Network", objPath,
+ "xyz.openbmc_project.Network.EthernetInterface", "MTU",
+ "MTUSize", mtuSize);
}
inline void
@@ -1259,16 +1242,12 @@
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
std::vector<std::string> vectorDomainname = {domainname};
- sdbusplus::asio::setProperty(
- *crow::connections::systemBus, "xyz.openbmc_project.Network",
- "/xyz/openbmc_project/network/" + ifaceId,
- "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
- vectorDomainname, [asyncResp](const boost::system::error_code& ec) {
- if (ec)
- {
- messages::internalError(asyncResp->res);
- }
- });
+ setDbusProperty(
+ asyncResp, "xyz.openbmc_project.Network",
+ sdbusplus::message::object_path("/xyz/openbmc_project/network") /
+ ifaceId,
+ "xyz.openbmc_project.Network.EthernetInterface", "DomainName", "FQDN",
+ vectorDomainname);
}
inline bool isHostnameValid(const std::string& hostname)
@@ -1335,32 +1314,12 @@
const std::string& macAddress,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
- static constexpr std::string_view dbusNotAllowedError =
- "xyz.openbmc_project.Common.Error.NotAllowed";
-
- sdbusplus::asio::setProperty(
- *crow::connections::systemBus, "xyz.openbmc_project.Network",
- "/xyz/openbmc_project/network/" + ifaceId,
- "xyz.openbmc_project.Network.MACAddress", "MACAddress", macAddress,
- [asyncResp](const boost::system::error_code& ec,
- const sdbusplus::message_t& msg) {
- if (ec)
- {
- const sd_bus_error* err = msg.get_error();
- if (err == nullptr)
- {
- messages::internalError(asyncResp->res);
- return;
- }
- if (err->name == dbusNotAllowedError)
- {
- messages::propertyNotWritable(asyncResp->res, "MACAddress");
- return;
- }
- messages::internalError(asyncResp->res);
- return;
- }
- });
+ setDbusProperty(
+ asyncResp, "xyz.openbmc_project.Network",
+ sdbusplus::message::object_path("/xyz/openbmc_project/network") /
+ ifaceId,
+ "xyz.openbmc_project.Network.MACAddress", "MACAddress", "MACAddress",
+ macAddress);
}
inline void setDHCPEnabled(const std::string& ifaceId,
@@ -1369,37 +1328,12 @@
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
- sdbusplus::asio::setProperty(
- *crow::connections::systemBus, "xyz.openbmc_project.Network",
- "/xyz/openbmc_project/network/" + ifaceId,
- "xyz.openbmc_project.Network.EthernetInterface", propertyName, dhcp,
- [asyncResp](const boost::system::error_code& ec) {
- if (ec)
- {
- BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
- messages::internalError(asyncResp->res);
- return;
- }
- messages::success(asyncResp->res);
- });
-}
-
-inline void setEthernetInterfaceBoolProperty(
- const std::string& ifaceId, const std::string& propertyName,
- const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
-{
- sdbusplus::asio::setProperty(
- *crow::connections::systemBus, "xyz.openbmc_project.Network",
- "/xyz/openbmc_project/network/" + ifaceId,
- "xyz.openbmc_project.Network.EthernetInterface", propertyName, value,
- [asyncResp](const boost::system::error_code& ec) {
- if (ec)
- {
- BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
- messages::internalError(asyncResp->res);
- return;
- }
- });
+ setDbusProperty(
+ asyncResp, "xyz.openbmc_project.Network",
+ sdbusplus::message::object_path("/xyz/openbmc_project/network") /
+ ifaceId,
+ "xyz.openbmc_project.Network.EthernetInterface", propertyName, "DHCPv4",
+ dhcp);
}
enum class NetworkType
@@ -2434,8 +2368,13 @@
if (interfaceEnabled)
{
- setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
- *interfaceEnabled, asyncResp);
+ setDbusProperty(asyncResp, "xyz.openbmc_project.Network",
+ sdbusplus::message::object_path(
+ "/xyz/openbmc_project/network") /
+ ifaceId,
+ "xyz.openbmc_project.Network.EthernetInterface",
+ "NICEnabled", "InterfaceEnabled",
+ *interfaceEnabled);
}
if (mtuSize)