use of com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException 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.errors.InvalidContentException in project vespa by vespa-engine.
the class DummyStateApi method setUnitState.
@Override
public SetResponse setUnitState(SetUnitStateRequest request) throws StateRestApiException {
checkForInducedException();
String[] path = request.getUnitPath();
if (path.length != 2) {
throw new OperationNotSupportedForUnitException(path, "You can only set states on nodes");
}
DummyBackend.Node n = null;
DummyBackend.Cluster c = backend.getClusters().get(path[0]);
if (c != null) {
n = c.nodes.get(path[1]);
}
if (n == null)
throw new MissingUnitException(path, 2);
Map<String, UnitState> newState = request.getNewState();
if (newState.size() != 1 || !newState.containsKey("current")) {
throw new InvalidContentException("Only state of type 'current' is allowed to be set.");
}
n.state = newState.get("current").getId();
n.reason = newState.get("current").getReason();
String reason = String.format("DummyStateAPI %s call", request.getResponseWait().getName());
return new SetResponse(reason, true);
}
use of com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException in project vespa by vespa-engine.
the class SetNodeStatesForClusterRequest method calculateResult.
@Override
public SetResponse calculateResult(RemoteClusterControllerTask.Context context) throws StateRestApiException {
if (condition != SetUnitStateRequest.Condition.FORCE) {
// go down, etc. This is prohibited in Condition.SAFE.
throw new InvalidContentException("Setting all nodes in a cluster to a state is only supported with FORCE");
}
for (ConfiguredNode configuredNode : context.cluster.getConfiguredNodes().values()) {
Node node = new Node(NodeType.STORAGE, configuredNode.index());
SetResponse setResponse = SetNodeStateRequest.setWantedState(context.cluster, condition, newStates, node, context.nodeStateOrHostInfoChangeHandler, context.currentConsolidatedState);
if (!setResponse.getWasModified()) {
throw new InternalFailure("We have not yet implemented the meaning of " + "failing to set the wanted state for a subset of nodes: " + "condition = " + condition + ", newStates = " + newStates + ", currentConsolidatedState = " + context.currentConsolidatedState);
}
}
// 'true' here means the current state now equals the request's wanted state.
return new SetResponse("ok", true);
}
use of com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException 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