Search in sources :

Example 16 with JsonNode

use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.

the class EddaLaunchConfigJanitorCrawler method getLaunchConfigResourcesInRegion.

private List<Resource> getLaunchConfigResourcesInRegion(String region, String... launchConfigNames) {
    String url = eddaClient.getBaseUrl(region) + "/aws/launchConfigurations;";
    if (launchConfigNames != null && launchConfigNames.length != 0) {
        url += StringUtils.join(launchConfigNames, ',');
        LOGGER.info(String.format("Getting launch configurations in region %s for %d ids", region, launchConfigNames.length));
    } else {
        LOGGER.info(String.format("Getting all launch configurations in region %s", region));
    }
    url += ";_expand:(launchConfigurationName,createdTime)";
    JsonNode jsonNode = null;
    try {
        jsonNode = eddaClient.getJsonNodeFromUrl(url);
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to get Jason node from edda for instances in region %s.", region), e);
    }
    if (jsonNode == null || !jsonNode.isArray()) {
        throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", url, jsonNode));
    }
    List<Resource> resources = Lists.newArrayList();
    Set<String> usedLCs = getLaunchConfigsInUse(region);
    for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext(); ) {
        JsonNode launchConfiguration = it.next();
        String lcName = launchConfiguration.get("launchConfigurationName").getTextValue();
        Resource lcResource = new AWSResource().withId(lcName).withRegion(region).withResourceType(AWSResourceType.LAUNCH_CONFIG).withLaunchTime(new Date(launchConfiguration.get("createdTime").getLongValue()));
        lcResource.setOwnerEmail(getOwnerEmailForResource(lcResource));
        lcResource.setAdditionalField(LAUNCH_CONFIG_FIELD_USED_BY_ASG, String.valueOf(usedLCs.contains(lcName)));
        resources.add(lcResource);
    }
    return resources;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

Example 17 with JsonNode

use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.

the class EddaLaunchConfigJanitorCrawler method getLaunchConfigsInUse.

/**
     * Gets the launch configs that are currently in use by at least one ASG in a region.
     * @param region the region
     * @return the set of launch config names
     */
private Set<String> getLaunchConfigsInUse(String region) {
    LOGGER.info(String.format("Getting all launch configurations in use in region %s", region));
    String url = eddaClient.getBaseUrl(region) + "/aws/autoScalingGroups;_expand:(launchConfigurationName)";
    JsonNode jsonNode = null;
    try {
        jsonNode = eddaClient.getJsonNodeFromUrl(url);
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to get Jason node from edda for launch configs in use in region %s.", region), e);
    }
    if (jsonNode == null || !jsonNode.isArray()) {
        throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", url, jsonNode));
    }
    Set<String> launchConfigs = Sets.newHashSet();
    for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext(); ) {
        launchConfigs.add(it.next().get("launchConfigurationName").getTextValue());
    }
    return launchConfigs;
}
Also used : JsonNode(org.codehaus.jackson.JsonNode)

Example 18 with JsonNode

use of org.codehaus.jackson.JsonNode in project SimianArmy by Netflix.

the class JanitorMonkeyResource method addEvent.

/**
     * POST /api/v1/janitor will try a add a new event with the information in the url context.
     *
     * @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 resourceId = getStringField(input, "resourceId");
    String region = getStringField(input, "region");
    Response.Status responseStatus;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
    gen.writeStartObject();
    gen.writeStringField("eventType", eventType);
    gen.writeStringField("resourceId", resourceId);
    if (StringUtils.isEmpty(eventType) || StringUtils.isEmpty(resourceId)) {
        responseStatus = Response.Status.BAD_REQUEST;
        gen.writeStringField("message", "eventType and resourceId parameters are all required");
    } else {
        if (eventType.equals("OPTIN")) {
            responseStatus = optInResource(resourceId, true, region, gen);
        } else if (eventType.equals("OPTOUT")) {
            responseStatus = optInResource(resourceId, false, region, 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) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) POST(javax.ws.rs.POST)

Example 19 with JsonNode

use of org.codehaus.jackson.JsonNode in project NabAlive by jcheype.

the class ContesApplication method getRand.

public String getRand() throws IOException {
    InputStream inputStream = getClass().getResourceAsStream("/contes.json");
    JsonNode jsonNodes = mapper.readTree(inputStream);
    Iterator<Map.Entry<String, JsonNode>> fields = jsonNodes.get("fields").get(0).get("values").getFields();
    List<Map.Entry<String, JsonNode>> listFields = new ArrayList<Map.Entry<String, JsonNode>>();
    while (fields.hasNext()) {
        listFields.add(fields.next());
    }
    int sz = listFields.size();
    return listFields.get(rand.nextInt(sz)).getValue().getTextValue();
}
Also used : InputStream(java.io.InputStream) JsonNode(org.codehaus.jackson.JsonNode)

Example 20 with JsonNode

use of org.codehaus.jackson.JsonNode in project pinot by linkedin.

the class IndexingConfigTest method testIgnoreUnknown.

@Test
public void testIgnoreUnknown() throws JSONException, IOException {
    JSONObject json = new JSONObject();
    json.put("invertedIndexColumns", Arrays.asList("a", "b", "c"));
    json.put("sortedColumn", Arrays.asList("d", "e", "f"));
    json.put("loadMode", "MMAP");
    json.put("keyThatIsUnknown", "randomValue");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(json.toString());
    IndexingConfig indexingConfig = mapper.readValue(jsonNode, IndexingConfig.class);
    Assert.assertEquals("MMAP", indexingConfig.getLoadMode());
    List<String> invertedIndexColumns = indexingConfig.getInvertedIndexColumns();
    Assert.assertEquals(3, invertedIndexColumns.size());
    Assert.assertEquals("a", invertedIndexColumns.get(0));
    Assert.assertEquals("b", invertedIndexColumns.get(1));
    Assert.assertEquals("c", invertedIndexColumns.get(2));
    List<String> sortedIndexColumns = indexingConfig.getSortedColumn();
    Assert.assertEquals(3, sortedIndexColumns.size());
    Assert.assertEquals("d", sortedIndexColumns.get(0));
    Assert.assertEquals("e", sortedIndexColumns.get(1));
    Assert.assertEquals("f", sortedIndexColumns.get(2));
}
Also used : JSONObject(org.json.JSONObject) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Aggregations

JsonNode (org.codehaus.jackson.JsonNode)191 Test (org.junit.Test)51 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)47 IOException (java.io.IOException)39 HashMap (java.util.HashMap)20 ArrayList (java.util.ArrayList)19 HTTP (org.neo4j.test.server.HTTP)18 Resource (com.netflix.simianarmy.Resource)17 AWSResource (com.netflix.simianarmy.aws.AWSResource)17 ObjectNode (org.codehaus.jackson.node.ObjectNode)14 Response (org.neo4j.test.server.HTTP.Response)12 Map (java.util.Map)9 ArrayNode (org.codehaus.jackson.node.ArrayNode)9 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)7 Date (java.util.Date)6 List (java.util.List)6 Description (org.hamcrest.Description)6 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)6