public static <T> Function<HttpToolResponse, T> jsonContents(String element, Class<T> expected) { return jsonContents(new String[] {element}, expected); }
@Test(expectedExceptions=NoSuchElementException.class) public void testJsonContentsGettingMissingElement() throws Exception { assertNull(HttpValueFunctions.jsonContents("wrongkey", String.class).apply(response)); }
@Test public void testJsonContentsGettingElement() throws Exception { assertEquals(HttpValueFunctions.jsonContents(bodyKey, String.class).apply(response), bodyVal); }
public static <T> Function<HttpToolResponse, T> jsonContentsFromPath(String path){ return Functionals.chain(jsonContents(), JsonFunctions.<T>getPath(path)); }
@Test public void testJsonContents() throws Exception { JsonElement json = HttpValueFunctions.jsonContents().apply(response); assertTrue(json.isJsonObject()); assertEquals(json.getAsJsonObject().entrySet(), ImmutableMap.of(bodyKey, new JsonPrimitive(bodyVal)).entrySet()); }
public static <T> Function<HttpToolResponse, T> jsonContents(String[] elements, Class<T> expected) { return Functionals.chain(jsonContents(), JsonFunctions.walk(elements), JsonFunctions.cast(expected)); }
public Optional<JsonElement> getApplicationJson() { MesosFramework framework = getFramework(); String uri = Urls.mergePaths(framework.sensors().get(MarathonFramework.FRAMEWORK_URL), "/v2/apps", sensors().get(APPLICATION_ID)); HttpToolResponse response = HttpTool.httpGet( MesosUtils.buildClient(framework), URI.create(uri), MutableMap.of(HttpHeaders.ACCEPT, "application/json")); if (!HttpTool.isStatusCodeHealthy(response.getResponseCode())) { return Optional.absent(); } else { JsonElement app = HttpValueFunctions.jsonContents().apply(response); if (app.isJsonNull()) { return Optional.absent(); } else { LOG.debug("Application JSON: {}", response.getContentAsString()); return Optional.of(app); } } }
@Override public ManagementNodeState call(ConfigBag parameters) { HighAvailabilityMode mode = parameters.get(MODE); Preconditions.checkNotNull(mode, MODE.getName() + " parameter is required"); EntityHttpClient httpClient = ((BrooklynNode)entity()).http(); HttpToolResponse resp = httpClient.post("/v1/server/ha/state", ImmutableMap.of("Brooklyn-Allow-Non-Master-Access", "true"), ImmutableMap.of("mode", mode.toString())); if (resp.getResponseCode() == HttpStatus.SC_OK) { Function<HttpToolResponse, ManagementNodeState> parseRespone = Functionals.chain( Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(String.class)), Enums.fromStringFunction(ManagementNodeState.class)); return parseRespone.apply(resp); } else { throw new IllegalStateException("Unexpected response code: " + resp.getResponseCode() + "\n" + resp.getContentAsString()); } } }
.baseUri(url) .poll(new HttpPollConfig<String>(SERVER_NAME) .onSuccess(HttpValueFunctions.jsonContents("name", String.class))) .poll(new HttpPollConfig<Integer>(SERVER_STATUS) .onSuccess(HttpValueFunctions.jsonContents("status", Integer.class))) .poll(new HttpPollConfig<Boolean>(SERVER_OK) .onSuccess(HttpValueFunctions.jsonContents("ok", Boolean.class))) .poll(new HttpPollConfig<String>(SERVER_BUILD_TIMESTAMP) .onSuccess(HttpValueFunctions.jsonContents(new String[]{"version", "build_timestamp"}, String.class))) .poll(new HttpPollConfig<String>(SERVER_BUILD_HASH) .onSuccess(HttpValueFunctions.jsonContents(new String[]{"version", "build_hash"}, String.class))) .poll(new HttpPollConfig<Boolean>(SERVER_IS_BUILD_SNAPSHOT) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "build_snapshot"}, Boolean.class))) .poll(new HttpPollConfig<String>(SERVER_LUCENE_VERSION) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "lucene_version"}, String.class))) .poll(new HttpPollConfig<String>(SERVER_ES_VERSION) .onSuccess(HttpValueFunctions.jsonContents(new String[] {"version", "es_version"}, String.class))) .build();
.poll(HttpPollConfig.forSensor(MEMORY_AVAILABLE) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM(new Predicate<JsonElement>() { .poll(HttpPollConfig.forSensor(CPU_AVAILABLE) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM(new Predicate<JsonElement>() { .poll(HttpPollConfig.forSensor(DISK_AVAILABLE) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM(new Predicate<JsonElement>() { .poll(HttpPollConfig.forSensor(MEMORY_USED) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM(new Predicate<JsonElement>() { .poll(HttpPollConfig.forSensor(CPU_USED) .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM(new Predicate<JsonElement>() {
new HttpGet(baseUri + "/mydocuments/docs/1/_source")); assertEquals(getResponse.getResponseCode(), 200); assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar");
new HttpGet(getBaseUri + "/mydocuments/docs/1/_source")); assertEquals(getResponse.getResponseCode(), 200); assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar");
private void connectAuthenticatedSensors() { hostsHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/hosts", ambariUri)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<List<String>>(REGISTERED_HOSTS) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getHosts())) .onFailureOrException(Functions.<List<String>>constant(ImmutableList.<String>of()))) .build(); clusterHttpFeed = HttpFeed.builder() .entity(this) .period(1000, TimeUnit.MILLISECONDS) .baseUri(String.format("%s/api/v1/clusters/%s/requests/%d", ambariUri, getConfig(AmbariCluster.CLUSTER_NAME), 1)) .credentials(usernamePasswordCredentials.getUserName(), usernamePasswordCredentials.getPassword()) .header(HttpHeaders.AUTHORIZATION, HttpTool.toBasicAuthorizationValue(usernamePasswordCredentials)) .poll(new HttpPollConfig<String>(CLUSTER_STATE) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), getRequestState())) .onFailureOrException(Functions.<String>constant(null))) .build(); }
.poll(new HttpPollConfig<Boolean>(SERVICE_UP) .suppressDuplicates(true) .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("tasks"), new Function<JsonElement, Boolean>() { @Override
.poll(new HttpPollConfig<Integer>(REQUEST_COUNT) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("requestCount", Integer.class)) .onFailureOrException(EntityFunctions.attribute(this, REQUEST_COUNT)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(ERROR_COUNT) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("errorCount", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(TOTAL_PROCESSING_TIME) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("processingTime", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Integer>(MAX_PROCESSING_TIME) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("maxTime", Integer.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Long>(BYTES_RECEIVED) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("bytesReceived", Long.class)) .enabled(retrieveUsageMetrics)) .poll(new HttpPollConfig<Long>(BYTES_SENT) .vars(includeRuntimeUriVars) .onSuccess(HttpValueFunctions.jsonContents("bytesSent", Long.class)) .enabled(retrieveUsageMetrics)) .build();
@Override public void connectSensors() { super.connectSensors(); connectServiceUpIsRunning(); HostAndPort hostAndPort = BrooklynAccessUtils.getBrooklynAccessibleAddress(this, sensors().get(DOCKER_REGISTRY_PORT)); sensors().set(Attributes.MAIN_URI, URI.create("https://" + hostAndPort + "/v2")); httpFeed = HttpFeed.builder() .entity(this) .period(Duration.seconds(3)) .baseUri(getAttribute(Attributes.MAIN_URI)) .poll(new HttpPollConfig<Boolean>(Attributes.SERVICE_UP) .onSuccess(Functions.constant(true)) .onFailureOrException(Functions.constant(false))) .poll(new HttpPollConfig<List<String>>(DOCKER_REGISTRY_CATALOG) .suburl("/_catalog") .onSuccess(Functionals.chain( HttpValueFunctions.jsonContents(), JsonFunctions.walk("repositories"), JsonFunctions.forEach(JsonFunctions.cast(String.class)))) .onFailureOrException(Functions.constant(Collections.<String>emptyList()))) .build(); }
@Override public void connectSensors() { super.connectSensors(); HttpFeed.Builder httpFeedBuilder = HttpFeed.builder() .entity(this) .period(2000, TimeUnit.MILLISECONDS) .baseUri(sensors().get(FRAMEWORK_URL)) .credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(MARATHON_APPLICATIONS) .suppressDuplicates(true) .suburl("/v2/apps/") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("apps"), JsonFunctions.forEach(JsonFunctions.<String>getPath("id")))) .onFailureOrException(Functions.constant(Arrays.asList(new String[0])))) .poll(HttpPollConfig.forSensor(MARATHON_VERSION) .suppressDuplicates(true) .suburl("/v2/info/") .onSuccess(HttpValueFunctions.jsonContents("version", String.class)) .onFailureOrException(Functions.constant(""))) .poll(HttpPollConfig.forSensor(SERVICE_UP) .suppressDuplicates(true) .suburl("/ping") .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(Boolean.FALSE))); httpFeed = httpFeedBuilder.build(); }
.poll(new HttpPollConfig<Boolean>(WEB_CONSOLE_ACCESSIBLE) .suburl("/v1/server/healthy") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(Boolean.class))) .poll(new HttpPollConfig<ManagementNodeState>(MANAGEMENT_NODE_STATE) .suburl("/v1/server/ha/state") .onSuccess(Functionals.chain(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(String.class)), Enums.fromStringFunction(ManagementNodeState.class))) .setOnFailureOrException(null))
@Override public void connectSensors() { Duration scanInterval = config().get(MesosCluster.SCAN_INTERVAL); HttpFeed.Builder taskScanBuilder = HttpFeed.builder() .entity(this) .period(scanInterval) .baseUri(config().get(MesosCluster.MESOS_URL)) .credentialsIfNotNull(config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .poll(HttpPollConfig.forSensor(MESOS_TASK_LIST) .description("Scan Tasks") .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("frameworks"), new Function<JsonElement, List<String>>() { @Override public List<String> apply(JsonElement frameworks) { return scanTasks(frameworks.getAsJsonArray()); } })) .onFailureOrException(Functions.<List<String>>constant(null))); taskScan = taskScanBuilder.build(); }
.poll(HttpPollConfig.forSensor(MEMORY_TOTAL_BYTES) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("mem_total_bytes", Long.class)) .onFailureOrException(Functions.constant(-1L))); httpFeed = httpFeedBuilder.build(); .description("Scan Cluster Slaves") .suburl("/master/state.json") .onSuccess(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.walk("slaves"), new Function<JsonElement, List<String>>() { @Override public List<String> apply(JsonElement slaves) {