Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
utils: fix potential NPE and error message
This fixes the error message when Redfish doesn't support power related
POST requests. Also fixes an NPE edge case discovered via the emulator.

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
  • Loading branch information
yadvr committed Apr 17, 2025
commit b66f149778a91dabc0535102902fdaa158546870
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;

import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.nio.TrustAllManager;
import com.google.gson.JsonElement;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
Expand Down Expand Up @@ -84,6 +86,7 @@
private final static String ACCEPT = "accept";
private final static String ODATA_ID = "@odata.id";
private final static String MEMBERS = "Members";
private final static String LINKS = "Links";
private final static String EXPECTED_HTTP_STATUS = "2XX";
private final static int WAIT_FOR_REQUEST_RETRY = 2;

Expand Down Expand Up @@ -306,8 +309,8 @@

int statusCode = response.getStatusLine().getStatusCode();
if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
throw new RedfishException(String.format("Failed to get System power state for host '%s' with request '%s: %s'. The expected HTTP status code is '%s' but it got '%s'.",
HttpGet.METHOD_NAME, url, hostAddress, EXPECTED_HTTP_STATUS, statusCode));
throw new RedfishException(String.format("Failed to execute System power command for host by performing '%s' request on URL '%s' and host address '%s'. The expected HTTP status code is '%s' but it got '%s'.",
HttpPost.METHOD_NAME, url, hostAddress, EXPECTED_HTTP_STATUS, statusCode));

Check warning on line 313 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L312-L313

Added lines #L312 - L313 were not covered by tests
}
logger.debug(String.format("Sending ComputerSystem.Reset Command '%s' to host '%s' with request '%s %s'", resetCommand, hostAddress, HttpPost.METHOD_NAME, url));
}
Expand Down Expand Up @@ -341,16 +344,25 @@
try {
in = response.getEntity().getContent();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
jsonString = streamReader.lines().collect(Collectors.joining());

Check warning on line 347 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L347

Added line #L347 was not covered by tests
} catch (UnsupportedOperationException | IOException e) {
throw new RedfishException("Failed to process system Response", e);
}

// retrieving the system ID (e.g. 'System.Embedded.1') via JsonParser:
// (...) Members":[{"@odata.id":"/redfish/v1/Systems/System.Embedded.1"}] (...)
JsonArray jArray = new JsonParser().parse(jsonString).getAsJsonObject().get(MEMBERS).getAsJsonArray();
JsonObject jsonnObject = jArray.get(0).getAsJsonObject();
String jsonObjectAsString = jsonnObject.get(ODATA_ID).getAsString();
JsonArray jArray = null;
JsonElement jsonElement = new JsonParser().parse(jsonString);

Check warning on line 355 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L354-L355

Added lines #L354 - L355 were not covered by tests
if (jsonElement.getAsJsonObject().get(MEMBERS) != null) {
jArray = jsonElement.getAsJsonObject().get(MEMBERS).getAsJsonArray();

Check warning on line 357 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L357

Added line #L357 was not covered by tests
} else if (jsonElement.getAsJsonObject().get(LINKS) != null){
jArray = jsonElement.getAsJsonObject().get(LINKS).getAsJsonObject().get(MEMBERS).getAsJsonArray();

Check warning on line 359 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L359

Added line #L359 was not covered by tests
}
if (jArray == null || jArray.size() < 1) {
throw new CloudRuntimeException("Members not found in the Redfish Systems JSON, unable to determine Redfish system ID");

Check warning on line 362 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L362

Added line #L362 was not covered by tests
}
JsonObject jsonObject = jArray.get(0).getAsJsonObject();
String jsonObjectAsString = jsonObject.get(ODATA_ID).getAsString();

Check warning on line 365 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L364-L365

Added lines #L364 - L365 were not covered by tests
String[] arrayOfStrings = StringUtils.split(jsonObjectAsString, '/');

return arrayOfStrings[arrayOfStrings.length - 1];
Expand Down Expand Up @@ -385,7 +397,7 @@
try {
in = response.getEntity().getContent();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
jsonString = streamReader.lines().collect(Collectors.joining());

Check warning on line 400 in utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java

View check run for this annotation

Codecov / codecov/patch

utils/src/main/java/org/apache/cloudstack/utils/redfish/RedfishClient.java#L400

Added line #L400 was not covered by tests
String powerState = new JsonParser().parse(jsonString).getAsJsonObject().get(POWER_STATE).getAsString();
return RedfishPowerState.valueOf(powerState);
} catch (UnsupportedOperationException | IOException e) {
Expand Down
Loading