From 3193bd9acff987c6bed194c5b7b073171d7d6380 Mon Sep 17 00:00:00 2001 From: Emily Casey Date: Fri, 15 May 2020 16:27:32 -0400 Subject: [PATCH] Wavefront Support [resolves #37] Also fixes failure when there is no provider in the binding metadata. Signed-off-by: Emily Casey --- README.md | 10 +++ .../cloud/bindings/Bindings.java | 2 +- .../WavefrontBindingsPropertiesProcessor.java | 50 ++++++++++++++ src/main/resources/META-INF/spring.factories | 3 +- ...gSpecificEnvironmentPostProcessorTest.java | 2 +- .../WavefrontPropertiesProcessorTest.java | 68 +++++++++++++++++++ 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/springframework/cloud/bindings/boot/WavefrontBindingsPropertiesProcessor.java create mode 100644 src/test/java/org/springframework/cloud/bindings/boot/WavefrontPropertiesProcessorTest.java diff --git a/README.md b/README.md index 7f311d6..2afa3e7 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,16 @@ Disable Property: `org.springframework.cloud.bindings.boot.sqlserver.enable` | `spring.datasource.url` | `jdbc:sqlserver://{secret/host}:{secret/port}/{secret/database}` | `spring.datasource.username` | `{secret/username}` +### Wavefront + +Kind: `Wavefront` +Disable Property: `org.springframework.cloud.bindings.boot.wavefront.enable` + +| Property | Value +| -------- | ------------------ +| `management.metrics.export.wavefront.api-token` | `{secret/api-token}` +| `management.metrics.export.wavefront.uri` | `{secret/uri}` + ## License This buildpack is released under version 2.0 of the [Apache License][a]. diff --git a/src/main/java/org/springframework/cloud/bindings/Bindings.java b/src/main/java/org/springframework/cloud/bindings/Bindings.java index b597bb8..356028e 100644 --- a/src/main/java/org/springframework/cloud/bindings/Bindings.java +++ b/src/main/java/org/springframework/cloud/bindings/Bindings.java @@ -134,7 +134,7 @@ public final class Bindings { return bindings.stream() .filter(binding -> (kind == null || binding.getKind().equalsIgnoreCase(kind)) && - (provider == null) || binding.getProvider().equalsIgnoreCase(provider)) + (provider == null || binding.getProvider().equalsIgnoreCase(provider))) .collect(Collectors.toList()); } diff --git a/src/main/java/org/springframework/cloud/bindings/boot/WavefrontBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/WavefrontBindingsPropertiesProcessor.java new file mode 100644 index 0000000..9d1e1c7 --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/WavefrontBindingsPropertiesProcessor.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.bindings.boot; + +import org.springframework.cloud.bindings.Binding; +import org.springframework.cloud.bindings.Bindings; +import org.springframework.core.env.Environment; + +import java.util.Map; + +import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled; + +/** + * An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}. + */ +public final class WavefrontBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "Wavefront"; + + @Override + public void process(Environment environment, Bindings bindings, Map properties) { + if (!isKindEnabled(environment, KIND)) { + return; + } + + bindings.filterBindings(KIND).forEach(binding -> { + MapMapper map = new MapMapper(binding.getSecret(), properties); + map.from("api-token").to("management.metrics.export.wavefront.api-token"); + map.from("uri").to("management.metrics.export.wavefront.uri"); + }); + } + +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index bd63fbb..336c563 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -15,4 +15,5 @@ org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.boot.PostgreSqlBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.RabbitMqBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.RedisBindingsPropertiesProcessor, \ - org.springframework.cloud.bindings.boot.SqlServerBindingsPropertiesProcessor + org.springframework.cloud.bindings.boot.SqlServerBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.boot.WavefrontBindingsPropertiesProcessor diff --git a/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java index 7fa74d0..f1df2eb 100644 --- a/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java +++ b/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java @@ -98,7 +98,7 @@ final class BindingSpecificEnvironmentPostProcessorTest { @Test @DisplayName("included implementations are registered") void includedImplementations() { - assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(13); + assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(14); } } diff --git a/src/test/java/org/springframework/cloud/bindings/boot/WavefrontPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/WavefrontPropertiesProcessorTest.java new file mode 100644 index 0000000..090bfe1 --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/WavefrontPropertiesProcessorTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.bindings.boot; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.cloud.bindings.Binding; +import org.springframework.cloud.bindings.Bindings; +import org.springframework.cloud.bindings.FluentMap; +import org.springframework.mock.env.MockEnvironment; + +import java.nio.file.Paths; +import java.util.Collections; +import java.util.HashMap; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.bindings.boot.WavefrontBindingsPropertiesProcessor.KIND; + +@DisplayName("Wavefront BindingsPropertiesProcessor") +final class WavefrontPropertiesProcessorTest { + + private final Bindings bindings = new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("api-token", "test-api-token") + .withEntry("uri", "test-uri") + ) + ); + + private final MockEnvironment environment = new MockEnvironment(); + + private final HashMap properties = new HashMap<>(); + + @Test + @DisplayName("contributes properties") + void test() { + new WavefrontBindingsPropertiesProcessor().process(environment, bindings, properties); + assertThat(properties) + .containsEntry("management.metrics.export.wavefront.api-token", "test-api-token") + .containsEntry("management.metrics.export.wavefront.uri", "test-uri"); + } + + @Test + @DisplayName("can be disabled") + void disabled() { + environment.setProperty("org.springframework.cloud.bindings.boot.wavefront.enable", "false"); + + new WavefrontBindingsPropertiesProcessor().process(environment, bindings, properties); + + assertThat(properties).isEmpty(); + } + +}