Search in sources :

Example 1 with ChaosType

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();
}
Also used : Response(javax.ws.rs.core.Response) ShutdownInstanceChaosType(com.netflix.simianarmy.chaos.ShutdownInstanceChaosType) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChaosType(com.netflix.simianarmy.chaos.ChaosType) ShutdownInstanceChaosType(com.netflix.simianarmy.chaos.ShutdownInstanceChaosType) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) POST(javax.ws.rs.POST)

Aggregations

ChaosType (com.netflix.simianarmy.chaos.ChaosType)1 ShutdownInstanceChaosType (com.netflix.simianarmy.chaos.ShutdownInstanceChaosType)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 POST (javax.ws.rs.POST)1 Response (javax.ws.rs.core.Response)1 JsonGenerator (org.codehaus.jackson.JsonGenerator)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1