use of com.netflix.simianarmy.chaos.ChaosType in project SimianArmy by Netflix.
the class ChaosMonkeyResource method addEvent.
/**
* POST /api/v1/chaos will try a add a new event with the information in the url context,
* ignoring the monkey probability and max termination configurations, for a specific instance group.
*
* @param content
* the Json content passed to the http POST request
* @return the response
* @throws IOException
*/
@POST
public Response addEvent(String content) throws IOException {
ObjectMapper mapper = new ObjectMapper();
LOGGER.info(String.format("JSON content: '%s'", content));
JsonNode input = mapper.readTree(content);
String eventType = getStringField(input, "eventType");
String groupType = getStringField(input, "groupType");
String groupName = getStringField(input, "groupName");
String chaosTypeName = getStringField(input, "chaosType");
ChaosType chaosType;
if (!Strings.isNullOrEmpty(chaosTypeName)) {
chaosType = ChaosType.parse(this.monkey.getChaosTypes(), chaosTypeName);
} else {
chaosType = new ShutdownInstanceChaosType(monkey.context().configuration());
}
Response.Status responseStatus;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
gen.writeStartObject();
gen.writeStringField("eventType", eventType);
gen.writeStringField("groupType", groupType);
gen.writeStringField("groupName", groupName);
gen.writeStringField("chaosType", chaosType.getKey());
if (StringUtils.isEmpty(eventType) || StringUtils.isEmpty(groupType) || StringUtils.isEmpty(groupName)) {
responseStatus = Response.Status.BAD_REQUEST;
gen.writeStringField("message", "eventType, groupType, and groupName parameters are all required");
} else {
if (eventType.equals("CHAOS_TERMINATION")) {
responseStatus = addTerminationEvent(groupType, groupName, chaosType, gen);
} else {
responseStatus = Response.Status.BAD_REQUEST;
gen.writeStringField("message", String.format("Unrecognized event type: %s", eventType));
}
}
gen.writeEndObject();
gen.close();
LOGGER.info("entity content is '{}'", baos.toString("UTF-8"));
return Response.status(responseStatus).entity(baos.toString("UTF-8")).build();
}
Aggregations