SwarmScanner { protected ObjectNode flattenTask(JsonNode n) { ObjectNode out = new ObjectMapper().createObjectNode(); out.put("swarmClusterId", getSwarmClusterId().get()); out.put("taskId", n.path("ID").asText()); out.put("versionIndex", n.path("Version").path("Index").asText()); out.put("createdAt", n.path("CreatedAt").asText()); out.put("updatedAt", n.path("UpdatedAt").asText()); n.path("Labels").fields().forEachRemaining(it -> { out.put(formatLabelKey(it.getKey()), it.getValue().asText()); }); n.path("Spec").path("ContainerSepc"); out.put("serviceId", n.path("ServiceID").asText()); out.put("swarmNodeId", n.path("NodeID").asText()); out.put("state", n.path("Status").path("State").asText()); out.put("message", n.path("Status").path("Message").asText()); out.put("containerId", n.path("Status").path("ContainerStatus").path("ContainerID").asText()); out.put("desiredState", n.path("DesiredState").asText()); n.path("Status").path("PortStatus").path("Ports").forEach(it -> { String mode = it.path("PublishMode").asText(); String protocol = it.path("Protocol").asText(); if (mode.equals("host") && protocol.equals("tcp")) { int targetPort = it.path("TargetPort").asInt(); int publishedPort = it.path("PublishedPort").asInt(); String key = String.format("hostTcpPortMap_%d", targetPort); out.put(key, publishedPort); } }); ; return out; } SwarmScanner(DockerScanner ds); DockerRestClient getRestClient(); static WebTarget extractWebTarget(DockerClient c); void scan(); void scanServicesForSwarm(String swarmClusterId); void scanNode(String nodeId); void scanTask(String taskId); void deleteTask(String id); void deleteNode(String id); void scanTasksForSwarm(String swarmClusterId); Optional<String> getSwarmClusterId(); void scanService(String id); void deleteService(String id);  }
@Test public void testFlattenTask() throws Exception { String taskJson = " {\n" + " \"ID\" : \"kz0xbugh9xnf4bqcxjqc97lo3\",\n" + " \"Version\" : {\n" + " \"Index\" : 112\n" + " },\n" + " \"CreatedAt\" : \"2017-09-08T21:24:42.408242959Z\",\n" + " \"UpdatedAt\" : \"2017-09-09T02:12:26.443758461Z\",\n" + " \"Labels\" : { },\n" + " \"Spec\" : {\n" + " \"ContainerSpec\" : {\n" + " \"Image\" : \"trident-echo:latest@sha256:8c850603e5b67909da2409003454d91a154b5dacb2d9add580355f8a42ea2ae0\",\n" + " \"Env\" : [ \"path=/api/foo\", \"app=foo\" ],\n" + " \"DNSConfig\" : { }\n" + " },\n" + " \"Resources\" : {\n" + " \"Limits\" : { },\n" + " \"Reservations\" : { }\n" + " },\n" + " \"Placement\" : {\n" + " \"Platforms\" : [ {\n" + " \"Architecture\" : \"amd64\",\n" + " \"OS\" : \"linux\"\n" + " } ]\n" + " },\n" + " \"ForceUpdate\" : 0\n" + " },\n" + " \"ServiceID\" : \"sx6r9h4zsyosjqgfx2x66zv7a\",\n" + " \"Slot\" : 2,\n" + " \"NodeID\" : \"un7e89zvlnb4c2l11yvmsvlio\",\n" + " \"Status\" : {\n" + " \"Timestamp\" : \"2017-09-09T02:12:26.350558639Z\",\n" + " \"State\" : \"shutdown\",\n" + " \"Message\" : \"shutdown\",\n" + " \"ContainerStatus\" : {\n" + " \"ContainerID\" : \"1812bf07dea79f54c22313d4fe3de263abfc0bfebee9014b09a748208c00aa39\",\n" + " \"ExitCode\" : 143\n" + " },\n" + " \"PortStatus\" : { }\n" + " },\n" + " \"DesiredState\" : \"shutdown\"\n" + "}"; ObjectNode n = (ObjectNode) mapper.readTree(taskJson); SwarmScanner scanner = new SwarmScanner(null); scanner.swarmClusterId = "abcd123"; JsonNode flattened = scanner.flattenTask(n); JsonLogger.logInfo("foo", flattened); Assertions.assertThat(flattened.get("swarmClusterId").asText()).isEqualTo("abcd123"); Assertions.assertThat(flattened.get("taskId").asText()).isEqualTo("kz0xbugh9xnf4bqcxjqc97lo3"); Assertions.assertThat(flattened.get("serviceId").asText()).isEqualTo("sx6r9h4zsyosjqgfx2x66zv7a"); Assertions.assertThat(flattened.get("swarmNodeId").asText()).isEqualTo("un7e89zvlnb4c2l11yvmsvlio"); Assertions.assertThat(flattened.get("state").asText()).isEqualTo("shutdown"); Assertions.assertThat(flattened.get("message").asText()).isEqualTo("shutdown"); Assertions.assertThat(flattened.get("desiredState").asText()).isEqualTo("shutdown"); Assertions.assertThat(flattened.get("containerId").asText()) .isEqualTo("1812bf07dea79f54c22313d4fe3de263abfc0bfebee9014b09a748208c00aa39"); }