Update most resources to use urlFromPieces
Only id in event_service and account_service have not been updated due
to the risk of it breaking the username/id. It will require further
testing to verify.
Use urlFromPieces wherever that is needed to insert a variable in the
URI. Don't use urlFromPieces when it is hardcoded values. This allow us
to control all resource URIs that is dynamically added and to sync with
the current recommanded method for `@odata.id`. The goal is to have a
common place to manage the url created from dbus-paths in order to
manage/update it easily when needed.
Tested:
RedfishValidtor Passed for all resource including the sensors with the
fragments.
Change-Id: I95cdfaaee58fc7f21c95f5944e1e5c813b3215f2
Signed-off-by: Willy Tu <wltu@google.com>
Signed-off-by: Ed Tanous <edtanous@google.com>
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index e6d7a7a..3ff4842 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -559,8 +559,8 @@
}
populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
- asyncResp->res.jsonValue["@odata.id"] =
- "/redfish/v1/Chassis/" + chassisIdStr + "/" + chassisSubNode;
+ asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
+ "redfish", "v1", "Chassis", chassisIdStr, chassisSubNode);
// Get the list of all sensors for this Chassis element
std::string sensorPath = *chassisPath + "/all_sensors";
@@ -1128,10 +1128,12 @@
.jsonValue["Redundancy"];
nlohmann::json::object_t redundancy;
- redundancy["@odata.id"] =
- "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
- "/" + sensorsAsyncResp->chassisSubNode +
- "#/Redundancy/" + std::to_string(jResp.size());
+ boost::urls::url url = crow::utility::urlFromPieces(
+ "redfish", "v1", "Chassis", sensorsAsyncResp->chassisId,
+ sensorsAsyncResp->chassisSubNode);
+ url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
+ .to_string());
+ redundancy["@odata.id"] = std::move(url);
redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
redundancy["MinNumNeeded"] = minNumNeeded;
redundancy["MemberId"] = name;
@@ -1179,7 +1181,7 @@
std::string* value = odata->get_ptr<std::string*>();
if (value != nullptr)
{
- *value += std::to_string(count);
+ *value += "/" + std::to_string(count);
count++;
sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
}
@@ -2199,8 +2201,10 @@
// Add new PowerSupply object to JSON array
powerSupplyArray.push_back({});
nlohmann::json& powerSupply = powerSupplyArray.back();
- powerSupply["@odata.id"] =
- "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
+ boost::urls::url url = crow::utility::urlFromPieces(
+ "redfish", "v1", "Chassis", chassisId, "Power");
+ url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
+ powerSupply["@odata.id"] = std::move(url);
powerSupply["MemberId"] = inventoryItem.name;
powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
powerSupply["Manufacturer"] = inventoryItem.manufacturer;
@@ -2383,11 +2387,13 @@
// PowerControl. Follows MemberId naming and
// naming in power.hpp.
nlohmann::json::object_t power;
- power["@odata.id"] =
- "/redfish/v1/Chassis/" +
- sensorsAsyncResp->chassisId + "/" +
- sensorsAsyncResp->chassisSubNode + "#/" +
- fieldName + "/0";
+ boost::urls::url url = crow::utility::urlFromPieces(
+ "redfish", "v1", "Chassis",
+ sensorsAsyncResp->chassisId,
+ sensorsAsyncResp->chassisSubNode);
+ url.set_fragment((""_json_pointer / fieldName / "0")
+ .to_string());
+ power["@odata.id"] = std::move(url);
tempArray.push_back(std::move(power));
}
sensorJson = &(tempArray.back());
@@ -2423,11 +2429,13 @@
else
{
nlohmann::json::object_t member;
- member["@odata.id"] = "/redfish/v1/Chassis/" +
- sensorsAsyncResp->chassisId +
- "/" +
- sensorsAsyncResp->chassisSubNode +
- "#/" + fieldName + "/";
+ boost::urls::url url = crow::utility::urlFromPieces(
+ "redfish", "v1", "Chassis",
+ sensorsAsyncResp->chassisId,
+ sensorsAsyncResp->chassisSubNode);
+ url.set_fragment(
+ (""_json_pointer / fieldName).to_string());
+ member["@odata.id"] = std::move(url);
tempArray.push_back(std::move(member));
sensorJson = &(tempArray.back());
}