use of com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState in project vespa by vespa-engine.
the class SetNodeStateRequest method getRequestedNodeState.
static NodeState getRequestedNodeState(Map<String, UnitState> newStates, Node n) throws StateRestApiException {
UnitState newState = newStates.get("user");
if (newState == null)
throw new InvalidContentException("No new user state given in request");
State state;
switch(newState.getId().toLowerCase()) {
case "up":
state = State.UP;
break;
case "retired":
state = State.RETIRED;
break;
case "maintenance":
state = State.MAINTENANCE;
break;
case "down":
state = State.DOWN;
break;
default:
throw new InvalidContentException("Invalid user state '" + newState.getId() + "' given.");
}
return new NodeState(n.getType(), state).setDescription(newState.getReason());
}
use of com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState in project vespa by vespa-engine.
the class JsonReader method getStateRequestData.
public SetRequestData getStateRequestData(HttpRequest request) throws Exception {
JSONObject json = new JSONObject(request.getPostContent().toString());
final SetUnitStateRequest.Condition condition;
if (json.has("condition")) {
condition = SetUnitStateRequest.Condition.fromString(json.getString("condition"));
} else {
condition = SetUnitStateRequest.Condition.FORCE;
}
final SetUnitStateRequest.ResponseWait responseWait = json.has("response-wait") ? SetUnitStateRequest.ResponseWait.fromString(json.getString("response-wait")) : SetUnitStateRequest.ResponseWait.WAIT_UNTIL_CLUSTER_ACKED;
Map<String, UnitState> stateMap = new HashMap<>();
if (!json.has("state")) {
throw new InvalidContentException("Set state requests must contain a state object");
}
Object o = json.get("state");
if (!(o instanceof JSONObject)) {
throw new InvalidContentException("value of state is not a json object");
}
JSONObject state = (JSONObject) o;
JSONArray stateTypes = state.names();
for (int i = 0; i < stateTypes.length(); ++i) {
o = stateTypes.get(i);
String type = (String) o;
o = state.get(type);
if (!(o instanceof JSONObject)) {
throw new InvalidContentException("value of state->" + type + " is not a json object");
}
JSONObject userState = (JSONObject) o;
String code = "up";
if (userState.has("state")) {
o = userState.get("state");
if (!(o instanceof String)) {
throw new InvalidContentException("value of state->" + type + "->state is not a string");
}
code = o.toString();
}
String reason = "";
if (userState.has("reason")) {
o = userState.get("reason");
if (!(o instanceof String)) {
throw new InvalidContentException("value of state->" + type + "->reason is not a string");
}
reason = o.toString();
}
stateMap.put(type, new UnitStateImpl(code, reason));
}
return new SetRequestData(stateMap, condition, responseWait);
}
Aggregations