diff --git a/README.md b/README.md index 07eb4ae..0900246 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,19 @@ Disable Property: `org.springframework.cloud.bindings.boot.postgresql.enable` | `spring.datasource.url` | `jdbc:postgres://{host}:{port}/{database}` | `spring.datasource.username` | `{username}` +### RabbitMQ RDBMS +Kind: `RabbitMQ` +Disable Property: `org.springframework.cloud.bindings.boot.rabbitmq.enable` + +| Property | Value (`{secret}`) +| -------- | ------------------ +| `spring.rabbitmq.addresses` | `{addresses}` +| `spring.rabbitmq.host` | `{host}` +| `spring.rabbitmq.password` | `{password}` +| `spring.rabbitmq.port` | `{port}` +| `spring.rabbitmq.username` | `{username}` +| `spring.rabbitmq.virtual-host` | `{virtual-host}` + ### Redis RDBMS Kind: `Redis` Disable Property: `org.springframework.cloud.bindings.boot.redis.enable` diff --git a/src/main/java/org/springframework/cloud/bindings/boot/RabbitMqBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/RabbitMqBindingsPropertiesProcessor.java new file mode 100644 index 0000000..b49ff3e --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/RabbitMqBindingsPropertiesProcessor.java @@ -0,0 +1,55 @@ +/* + * 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 RabbitMqBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "RabbitMQ"; + + @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("addresses").to("spring.rabbitmq.addresses"); + map.from("host").to("spring.rabbitmq.host"); + map.from("password").to("spring.rabbitmq.password"); + map.from("port").to("spring.rabbitmq.port"); + map.from("username").to("spring.rabbitmq.username"); + map.from("virtual-host").to("spring.rabbitmq.virtual-host"); + }); + } + +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 21d688d..1d4b49f 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -12,5 +12,6 @@ org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.boot.Neo4JBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.OracleBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.PostgreSqlBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.boot.RabbitMqBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.RedisBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.SqlServerBindingsPropertiesProcessor 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 049aec5..dec1cc6 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(11); + assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(12); } } diff --git a/src/test/java/org/springframework/cloud/bindings/boot/RabbitMqBindingsPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/RabbitMqBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..0dc180e --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/RabbitMqBindingsPropertiesProcessorTest.java @@ -0,0 +1,76 @@ +/* + * 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.RabbitMqBindingsPropertiesProcessor.KIND; + +@DisplayName("RabbitMQ BindingsPropertiesProcessor") +final class RabbitMqBindingsPropertiesProcessorTest { + + private final Bindings bindings = new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("addresses", "test-addresses") + .withEntry("host", "test-host") + .withEntry("password", "test-password") + .withEntry("port", "test-port") + .withEntry("username", "test-username") + .withEntry("virtual-host", "test-virtual-host") + ) + ); + + private final MockEnvironment environment = new MockEnvironment(); + + private final HashMap properties = new HashMap<>(); + + @Test + @DisplayName("contributes properties") + void test() { + new RabbitMqBindingsPropertiesProcessor().process(environment, bindings, properties); + assertThat(properties) + .containsEntry("spring.rabbitmq.addresses", "test-addresses") + .containsEntry("spring.rabbitmq.host", "test-host") + .containsEntry("spring.rabbitmq.password", "test-password") + .containsEntry("spring.rabbitmq.port", "test-port") + .containsEntry("spring.rabbitmq.username", "test-username") + .containsEntry("spring.rabbitmq.virtual-host", "test-virtual-host"); + } + + @Test + @DisplayName("can be disabled") + void disabled() { + environment.setProperty("org.springframework.cloud.bindings.boot.rabbitmq.enable", "false"); + + new RabbitMqBindingsPropertiesProcessor().process(environment, bindings, properties); + + assertThat(properties).isEmpty(); + } + +}