From 9f99fbc8fbf9421040685ada7d9964e1ff89ce29 Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Mon, 11 May 2020 16:42:14 -0700 Subject: [PATCH] Elasticsearch This change adds support for Elasticsearch bindings. [resolves #18] Signed-off-by: Ben Hale --- README.md | 21 ++++- ...sticsearchBindingsPropertiesProcessor.java | 61 ++++++++++++++ src/main/resources/META-INF/spring.factories | 1 + ...gSpecificEnvironmentPostProcessorTest.java | 2 +- ...searchBindingsPropertiesProcessorTest.java | 82 +++++++++++++++++++ 5 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessor.java create mode 100644 src/test/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessorTest.java diff --git a/README.md b/README.md index 91259b4..be7de9e 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,25 @@ Disable Property: `org.springframework.cloud.bindings.boot.db2.enable` | `spring.datasource.url` | `jdbc:db2://{host}:{port}/{database}` | `spring.datasource.username` | `{username}` + +### Elasticsearch +Kind: `Elasticsearch` +Disable Property: `org.springframework.cloud.bindings.boot.elasticsearch.enable` + +| Property | Value (`{secret}`) +| -------- | ------------------ +| `spring.data.elasticsearch.client.reactive.endpoints` | `{endpoints}` +| `spring.data.elasticsearch.client.reactive.password` | `{password}` +| `spring.data.elasticsearch.client.reactive.use-ssl` | `{use-ssl}` +| `spring.data.elasticsearch.client.reactive.username` | `{username}` +| `spring.elasticsearch.jest.password` | `{password}` +| `spring.elasticsearch.jest.proxy.host` | `{proxy.host}` +| `spring.elasticsearch.jest.proxy.port` | `{proxy.port}` +| `spring.elasticsearch.jest.username` | `{username}` +| `spring.elasticsearch.rest.password` | `{password}` +| `spring.elasticsearch.rest.uris` | `{uris}` +| `spring.elasticsearch.rest.username` | `{username}` + ### MongoDB Kind: `MongoDB` Disable Property: `org.springframework.cloud.bindings.boot.mongodb.enable` @@ -138,7 +157,7 @@ Disable Property: `org.springframework.cloud.bindings.boot.redis.enable` | `spring.redis.ssl` | `{ssl}` | `spring.redis.url` | `{url}` -### Oracle RDBMS +### SQLServer RDBMS Kind: `SQLServer` Disable Property: `org.springframework.cloud.bindings.boot.sqlserver.enable` diff --git a/src/main/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessor.java new file mode 100644 index 0000000..5e402b0 --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessor.java @@ -0,0 +1,61 @@ +/* + * 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 ElasticsearchBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "Elasticsearch"; + + + @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("endpoints").to("spring.data.elasticsearch.client.reactive.endpoints"); + map.from("password").to("spring.data.elasticsearch.client.reactive.password"); + map.from("use-ssl").to("spring.data.elasticsearch.client.reactive.use-ssl"); + map.from("username").to("spring.data.elasticsearch.client.reactive.username"); + map.from("password").to("spring.elasticsearch.jest.password"); + map.from("proxy.host").to("spring.elasticsearch.jest.proxy.host"); + map.from("proxy.port").to("spring.elasticsearch.jest.proxy.port"); + map.from("username").to("spring.elasticsearch.jest.username"); + map.from("password").to("spring.elasticsearch.rest.password"); + map.from("uris").to("spring.elasticsearch.rest.uris"); + map.from("username").to("spring.elasticsearch.rest.username"); + }); + } + +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index c36a917..116d0c2 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -6,6 +6,7 @@ org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.boot.CassandraBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.CouchbaseBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.Db2BindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.boot.ElasticsearchBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.MongoDbBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.MySqlBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.OracleBindingsPropertiesProcessor, \ 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 005f4be..d87106b 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(9); + assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(10); } } diff --git a/src/test/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..57b0c2b --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/ElasticsearchBindingsPropertiesProcessorTest.java @@ -0,0 +1,82 @@ +/* + * 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.ElasticsearchBindingsPropertiesProcessor.KIND; + +@DisplayName("Elasticsearch BindingsPropertiesProcessor") +final class ElasticsearchBindingsPropertiesProcessorTest { + + private final Bindings bindings = new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("endpoints", "test-endpoints") + .withEntry("password", "test-password") + .withEntry("use-ssl", "test-use-ssl") + .withEntry("username", "test-username") + .withEntry("proxy.host", "test-proxy-host") + .withEntry("proxy.port", "test-proxy-port") + .withEntry("uris", "test-uris") + ) + ); + + private final MockEnvironment environment = new MockEnvironment(); + + private final HashMap properties = new HashMap<>(); + + @Test + @DisplayName("contributes properties") + void test() { + new ElasticsearchBindingsPropertiesProcessor().process(environment, bindings, properties); + assertThat(properties) + .containsEntry("spring.data.elasticsearch.client.reactive.endpoints", "test-endpoints") + .containsEntry("spring.data.elasticsearch.client.reactive.password", "test-password") + .containsEntry("spring.data.elasticsearch.client.reactive.use-ssl", "test-use-ssl") + .containsEntry("spring.data.elasticsearch.client.reactive.username", "test-username") + .containsEntry("spring.elasticsearch.jest.password", "test-password") + .containsEntry("spring.elasticsearch.jest.proxy.host", "test-proxy-host") + .containsEntry("spring.elasticsearch.jest.proxy.port", "test-proxy-port") + .containsEntry("spring.elasticsearch.jest.username", "test-username") + .containsEntry("spring.elasticsearch.rest.password", "test-password") + .containsEntry("spring.elasticsearch.rest.uris", "test-uris") + .containsEntry("spring.elasticsearch.rest.username", "test-username"); + } + + @Test + @DisplayName("can be disabled") + void disabled() { + environment.setProperty("org.springframework.cloud.bindings.boot.elasticsearch.enable", "false"); + + new ElasticsearchBindingsPropertiesProcessor().process(environment, bindings, properties); + + assertThat(properties).isEmpty(); + } + +}