diff --git a/README.md b/README.md index 3da8895..00d1298 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,17 @@ Disable Property: `org.springframework.cloud.bindings.boot.wavefront.enable` | `management.metrics.export.wavefront.api-token` | `{secret/api-token}` | `management.metrics.export.wavefront.uri` | `{secret/uri}` +### Eureka +Kind: `Eureka` +Disable Property: `org.springframework.cloud.bindings.boot.eureka.enable` + +| Property | Value +| -------- | ------------------ +| `eureka.client.oauth2.client-id` | `{secret/client-id}` +| `eureka.client.oauth2.access-token-uri` | `{secret/access-token-uri}` +| `eureka.client.region` | `default` +| `eureka.client.serviceUrl.defaultZone` | `{secret/uri}/eureka/` + ## License This buildpack is released under version 2.0 of the [Apache License][a]. diff --git a/src/main/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessor.java new file mode 100644 index 0000000..be4c9a2 --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessor.java @@ -0,0 +1,56 @@ +/* + * 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}. + */ +final class EurekaBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "Eureka"; + + @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("client-id").to("eureka.client.oauth2.client-id"); + map.from("access-token-uri").to("eureka.client.oauth2.access-token-uri"); + map.from("access-token-uri").to("eureka.client.oauth2.access-token-uri"); + map.from("uri").to("eureka.client.serviceUrl.defaultZone", + (uri) -> String.format("%s/eureka/", uri) + ); + + properties.put("eureka.client.region", "default"); + }); + + } +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 2d0eaa8..e8e543d 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -19,4 +19,5 @@ org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.boot.RabbitMqBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.RedisBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.SqlServerBindingsPropertiesProcessor, \ - org.springframework.cloud.bindings.boot.WavefrontBindingsPropertiesProcessor + org.springframework.cloud.bindings.boot.WavefrontBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.boot.EurekaBindingsPropertiesProcessor diff --git a/src/test/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..dc784f1 --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessorTest.java @@ -0,0 +1,72 @@ +/* + * 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.EurekaBindingsPropertiesProcessor.KIND; + +@DisplayName("Eureka BindingsPropertiesProcessor") +final class EurekaBindingsPropertiesProcessorTest { + + private final Bindings bindings = new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("uri", "test-uri") + .withEntry("client-id", "test-client-id") + .withEntry("client-secret", "test-client-secret") + .withEntry("access-token-uri", "test-access-token-uri") + ) + ); + + private final MockEnvironment environment = new MockEnvironment(); + + private final HashMap properties = new HashMap<>(); + + @Test + @DisplayName("contributes properties") + void test() { + new EurekaBindingsPropertiesProcessor().process(environment, bindings, properties); + assertThat(properties) + .containsEntry("eureka.client.region", "default") + .containsEntry("eureka.client.oauth2.client-id", "test-client-id") + .containsEntry("eureka.client.oauth2.access-token-uri", "test-access-token-uri") + .containsEntry("eureka.client.serviceUrl.defaultZone", "test-uri/eureka/"); + } + + @Test + @DisplayName("can be disabled") + void disabled() { + environment.setProperty("org.springframework.cloud.bindings.boot.eureka.enable", "false"); + + new KafkaBindingsPropertiesProcessor().process(environment, bindings, properties); + + assertThat(properties).isEmpty(); + } + +}