Skip to content

Commit b66f149

Browse files
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 <[email protected]>
1 parent 566ea2a commit b66f149

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import javax.net.ssl.SSLSession;
3838
import javax.net.ssl.TrustManager;
3939

40+
import com.cloud.utils.exception.CloudRuntimeException;
4041
import com.cloud.utils.nio.TrustAllManager;
42+
import com.google.gson.JsonElement;
4143
import org.apache.commons.httpclient.HttpStatus;
4244
import org.apache.commons.lang3.StringUtils;
4345
import org.apache.http.HttpResponse;
@@ -84,6 +86,7 @@ public class RedfishClient {
8486
private final static String ACCEPT = "accept";
8587
private final static String ODATA_ID = "@odata.id";
8688
private final static String MEMBERS = "Members";
89+
private final static String LINKS = "Links";
8790
private final static String EXPECTED_HTTP_STATUS = "2XX";
8891
private final static int WAIT_FOR_REQUEST_RETRY = 2;
8992

@@ -306,8 +309,8 @@ public void executeComputerSystemReset(String hostAddress, RedfishResetCmd reset
306309

307310
int statusCode = response.getStatusLine().getStatusCode();
308311
if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) {
309-
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'.",
310-
HttpGet.METHOD_NAME, url, hostAddress, EXPECTED_HTTP_STATUS, statusCode));
312+
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'.",
313+
HttpPost.METHOD_NAME, url, hostAddress, EXPECTED_HTTP_STATUS, statusCode));
311314
}
312315
logger.debug(String.format("Sending ComputerSystem.Reset Command '%s' to host '%s' with request '%s %s'", resetCommand, hostAddress, HttpPost.METHOD_NAME, url));
313316
}
@@ -348,9 +351,18 @@ protected String processGetSystemIdResponse(CloseableHttpResponse response) {
348351

349352
// retrieving the system ID (e.g. 'System.Embedded.1') via JsonParser:
350353
// (...) Members":[{"@odata.id":"/redfish/v1/Systems/System.Embedded.1"}] (...)
351-
JsonArray jArray = new JsonParser().parse(jsonString).getAsJsonObject().get(MEMBERS).getAsJsonArray();
352-
JsonObject jsonnObject = jArray.get(0).getAsJsonObject();
353-
String jsonObjectAsString = jsonnObject.get(ODATA_ID).getAsString();
354+
JsonArray jArray = null;
355+
JsonElement jsonElement = new JsonParser().parse(jsonString);
356+
if (jsonElement.getAsJsonObject().get(MEMBERS) != null) {
357+
jArray = jsonElement.getAsJsonObject().get(MEMBERS).getAsJsonArray();
358+
} else if (jsonElement.getAsJsonObject().get(LINKS) != null){
359+
jArray = jsonElement.getAsJsonObject().get(LINKS).getAsJsonObject().get(MEMBERS).getAsJsonArray();
360+
}
361+
if (jArray == null || jArray.size() < 1) {
362+
throw new CloudRuntimeException("Members not found in the Redfish Systems JSON, unable to determine Redfish system ID");
363+
}
364+
JsonObject jsonObject = jArray.get(0).getAsJsonObject();
365+
String jsonObjectAsString = jsonObject.get(ODATA_ID).getAsString();
354366
String[] arrayOfStrings = StringUtils.split(jsonObjectAsString, '/');
355367

356368
return arrayOfStrings[arrayOfStrings.length - 1];

0 commit comments

Comments
 (0)