Skip to content

Commit

Permalink
Issue #316 - add code/status code to action response
Browse files Browse the repository at this point in the history
  • Loading branch information
gondor committed Apr 8, 2015
1 parent 1b2e70a commit 1ebf183
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import org.openstack4j.core.transport.HttpResponse;
import org.openstack4j.model.compute.ActionResponse;

import com.google.common.base.Function;
Expand All @@ -13,9 +14,13 @@
*/
public class ParseActionResponseFromJsonMap implements Function<Map<String, Object>, ActionResponse>{

public static final ParseActionResponseFromJsonMap INSTANCE = new ParseActionResponseFromJsonMap();
private static final String KEY_MESSAGE = "message";
private static final String NEUTRON_ERROR = "NeutronError";
private HttpResponse response;

public ParseActionResponseFromJsonMap(HttpResponse response) {
this.response = response;
}

/**
* Parses the JSON Map for an Error message. An OpenStack error response typically is a Map of Map containing a single key
Expand All @@ -35,11 +40,11 @@ public ActionResponse apply(Map<String, Object> map) {
Map<String, Object> inner = (Map<String, Object>) map.get(key);
if (inner.containsKey(KEY_MESSAGE)) {
String msg = String.valueOf(inner.get(KEY_MESSAGE));
return ActionResponse.actionFailed(msg);
return ActionResponse.actionFailed(msg, response.getStatus());
}
if (inner.containsKey(NEUTRON_ERROR)) {
String msg = String.valueOf(inner.get(NEUTRON_ERROR));
return ActionResponse.actionFailed(msg);
return ActionResponse.actionFailed(msg, response.getStatus());
}
}
}
Expand All @@ -50,13 +55,13 @@ public ActionResponse apply(Map<String, Object> map) {
// "error_code": XXX }
if (map.containsKey("error_message")) {
String msg = String.valueOf(map.get("error_message"));
return ActionResponse.actionFailed(msg);
return ActionResponse.actionFailed(msg, response.getStatus());
}

// Neutron error handling when just a message is present
if (map.containsKey(NEUTRON_ERROR)) {
String msg = String.valueOf(map.get(NEUTRON_ERROR));
return ActionResponse.actionFailed(msg);
return ActionResponse.actionFailed(msg, response.getStatus());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public ActionResponse apply(HttpResponse response) {
public ActionResponse apply(HttpResponse response, boolean returnNullIfNotMapped) {
@SuppressWarnings("unchecked")
Map<String, Object> map = response.readEntity(Map.class);
ActionResponse ar = ParseActionResponseFromJsonMap.INSTANCE.apply(map);
ActionResponse ar = new ParseActionResponseFromJsonMap(response).apply(map);
if (ar != null)
return ar;

if (ar == null && returnNullIfNotMapped)
return null;

return ActionResponse.actionFailed(String.format("Status: %d, Reason: %s", response.getStatus(), response.getStatusMessage()));
return ActionResponse.actionFailed(String.format("Status: %d, Reason: %s", response.getStatus(), response.getStatusMessage()), response.getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@ public class ActionResponse implements Serializable {
private static final long serialVersionUID = 1L;

String message;
int code;

private ActionResponse() { }
private ActionResponse(String message) {
private ActionResponse(int code) {
this.code = code;
}
private ActionResponse(String message, int code) {
this(code);
this.message = message;
}

public static ActionResponse actionSuccess() {
return new ActionResponse();
return new ActionResponse(200);
}

public static ActionResponse actionFailed(String message, int code) {
return new ActionResponse(message, code);
}

public static ActionResponse actionFailed(String message) {
return new ActionResponse(message);
/**
* Returns the underlying error code (status code)
*
* @return the error code
*/
public int getCode() {
return code;
}

/**
Expand All @@ -44,7 +57,7 @@ public String getFault() {
}
@Override
public String toString() {
return Objects.toStringHelper(this).omitNullValues().add("success", message == null).add("fault", message).toString();
return Objects.toStringHelper(this).omitNullValues().add("success", message == null).add("fault", message).add("code", code).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public ActionResponse apply(HttpResponse response, String action) {

LOG.error(response.getStatus() + COMMA + response.getStatusMessage());
if (action == null)
return ActionResponse.actionFailed("Instance currently is in build state");
return ActionResponse.actionFailed(String.format(FAILED_MSG, action, action));
return ActionResponse.actionFailed("Instance currently is in build state", 409);
return ActionResponse.actionFailed(String.format(FAILED_MSG, action, action), 409);
}
if (response.getStatus() >= 400 && response.getStatus() < 409) {
return ResponseToActionResponse.INSTANCE.apply(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public ActionResponse action(String serverId, Action action) {

ServerAction instance = BasicActions.actionInstanceFor(action);
if (instance == null)
return ActionResponse.actionFailed(String.format("Action %s was not found in the list of invokable actions", action));
return ActionResponse.actionFailed(String.format("Action %s was not found in the list of invokable actions", action), 412);

return invokeAction(serverId, instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Volume create(Volume volume) {
public ActionResponse update(String volumeId, String name, String description) {
checkNotNull(volumeId);
if (name == null && description == null)
return ActionResponse.actionFailed("Name and Description are both required");
return ActionResponse.actionFailed("Name and Description are both required", 412);

return put(ActionResponse.class, uri("/volumes/%s", volumeId))
.entity(Builders.volume().name(name).description(description).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ActionResponse delete(String snapshotId) {
public ActionResponse update(String snapshotId, String name, String description) {
checkNotNull(snapshotId);
if (name == null && description == null)
return ActionResponse.actionFailed("Both Name and Description are required");
return ActionResponse.actionFailed("Both Name and Description are required", 412);

return put(ActionResponse.class, uri("/snapshots/%s", snapshotId))
.entity(Builders.volumeSnapshot().name(name).description(description).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public ActionResponse delete(String name) {
checkNotNull(name);
HttpResponse resp = delete(Void.class, URI_SEP, name).executeWithResponse();
if (resp.getStatus() == 409)
return ActionResponse.actionFailed(String.format("Container %s is not empty", name));
return ActionResponse.actionFailed(String.format("Container %s is not empty", name), 409);

return ToActionResponseFunction.INSTANCE.apply(resp);
}
Expand Down

0 comments on commit 1ebf183

Please sign in to comment.