diff --git a/README.md b/README.md index 9b3f70b..06366be 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,20 @@ Disable Property: `org.springframework.cloud.bindings.boot.cassandra.enable` | `spring.data.cassandra.ssl` | `{secret.ssl}` | `spring.data.cassandra.username` | `{secret.username}` +### Couchbase +Kind: `couchbase` +Disable Property: `org.springframework.cloud.bindings.boot.couchbase.enable` + +| Property | Value +| -------- | ----- +| `spring.couchbase.bootstrap-hosts` | `{secret.bootstrap-hosts}` +| `spring.couchbase.bucket.name` | `{secret.bucket.name}` +| `spring.couchbase.bucket.password` | `{secret.bucket.passsword}` +| `spring.couchbase.env.bootstrap.http-direct-port` | `{secret.env.bootstrap.http-direct-port}` +| `spring.couchbase.env.bootstrap.http-ssl-port` | `{secret.env.bootstrap.http-ssl-port}` +| `spring.couchbase.password` | `{secret.password}` +| `spring.couchbase.username` | `{secret.username}` + ### DB2 RDBMS Kind: `DB2` Disable Property: `org.springframework.cloud.bindings.boot.db2.enable` diff --git a/src/main/java/org/springframework/cloud/bindings/boot/CouchbaseBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/CouchbaseBindingsPropertiesProcessor.java new file mode 100644 index 0000000..7c7c03b --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/CouchbaseBindingsPropertiesProcessor.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 CouchbaseBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "Couchbase"; + + @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("bootstrap-hosts").to("spring.couchbase.bootstrap-hosts"); + map.from("bucket.name").to("spring.couchbase.bucket.name"); + map.from("bucket.password").to("spring.couchbase.bucket.password"); + map.from("env.bootstrap.http-direct-port").to("spring.couchbase.env.bootstrap.http-direct-port"); + map.from("env.bootstrap.http-ssl-port").to("spring.couchbase.env.bootstrap.http-ssl-port"); + map.from("password").to("spring.couchbase.password"); + map.from("username").to("spring.couchbase.username"); + }); + } + +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index fddb761..c36a917 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -4,6 +4,7 @@ org.springframework.boot.env.EnvironmentPostProcessor=\ # Included implementations 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.MongoDbBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.MySqlBindingsPropertiesProcessor, \ 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 b647dd1..005f4be 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(8); + assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(9); } } diff --git a/src/test/java/org/springframework/cloud/bindings/boot/CouchbaseBindingsPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/CouchbaseBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..c7ac600 --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/CouchbaseBindingsPropertiesProcessorTest.java @@ -0,0 +1,78 @@ +/* + * 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.CouchbaseBindingsPropertiesProcessor.KIND; + +@DisplayName("Couchbase BindingsPropertiesProcessor") +final class CouchbaseBindingsPropertiesProcessorTest { + + private final Bindings bindings = new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("bootstrap-hosts", "test-bootstrap-hosts") + .withEntry("bucket.name", "test-bucket-name") + .withEntry("bucket.password", "test-bucket-password") + .withEntry("env.bootstrap.http-direct-port", "test-env-bootstrap-http-direct-port") + .withEntry("env.bootstrap.http-ssl-port", "test-env-bootstrap-http-ssl-port") + .withEntry("password", "test-password") + .withEntry("username", "test-username") + ) + ); + + private final MockEnvironment environment = new MockEnvironment(); + + private final HashMap properties = new HashMap<>(); + + @Test + @DisplayName("contributes properties") + void test() { + new CouchbaseBindingsPropertiesProcessor().process(environment, bindings, properties); + assertThat(properties) + .containsEntry("spring.couchbase.bootstrap-hosts", "test-bootstrap-hosts") + .containsEntry("spring.couchbase.bucket.name", "test-bucket-name") + .containsEntry("spring.couchbase.bucket.password", "test-bucket-password") + .containsEntry("spring.couchbase.env.bootstrap.http-direct-port", "test-env-bootstrap-http-direct-port") + .containsEntry("spring.couchbase.env.bootstrap.http-ssl-port", "test-env-bootstrap-http-ssl-port") + .containsEntry("spring.couchbase.password", "test-password") + .containsEntry("spring.couchbase.username", "test-username"); + } + + @Test + @DisplayName("can be disabled") + void disabled() { + environment.setProperty("org.springframework.cloud.bindings.boot.couchbase.enable", "false"); + + new CouchbaseBindingsPropertiesProcessor().process(environment, bindings, properties); + + assertThat(properties).isEmpty(); + } + +}