diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CassandraCnbBindingProcessor.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CassandraCnbBindingProcessor.java new file mode 100644 index 0000000..70d0bdb --- /dev/null +++ b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CassandraCnbBindingProcessor.java @@ -0,0 +1,43 @@ +/* + * Copyright 2019 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.cnb.boot; + +import java.util.Map; + +import org.springframework.cloud.cnb.core.CnbBinding; + + +public class CassandraCnbBindingProcessor implements CnbBindingProcessor { + public static final String CASSANDRA_KIND = "cassandra"; + + @Override + public boolean accept(CnbBinding binding) { + return binding.getKind().equals(CASSANDRA_KIND); + } + + @Override + public void process(CnbBinding binding, Map properties) { + properties.put("spring.data.cassandra.username", binding.getSecret().get("username")); + properties.put("spring.data.cassandra.password", binding.getSecret().get("password")); + properties.put("spring.data.cassandra.contact-points", binding.getSecret().get("node_ips")); + properties.put("spring.data.cassandra.port", binding.getSecret().get("port")); + } + + @Override + public CnbBindingProcessorProperties getProperties() { + return null; + } +} diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingProcessorProperties.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingProcessorProperties.java index 0a41688..b57792d 100644 --- a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingProcessorProperties.java +++ b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingProcessorProperties.java @@ -54,6 +54,8 @@ public class CnbBindingProcessorProperties { public static class Builder { private CnbBindingProcessorProperties processorProperties = new CnbBindingProcessorProperties(); + // Todo: add kind & provider, make serviceName the binding name + public Builder propertyPrefixes(String propertyPrefixes) { this.processorProperties.propertyPrefixes = propertyPrefixes; return this; diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessor.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessor.java index f8772ed..1dbe2e4 100644 --- a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessor.java +++ b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessor.java @@ -74,6 +74,8 @@ public class CnbBindingsPostProcessor implements EnvironmentPostProcessor, Order public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + // TODO: allow users to disable processing of a given binding by name, kind, or processor + increaseInvocationCount(); CnbBindings bindings = CNBBindingsSingleton.getCnbBindingsInstance(); diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/DataSourceCnbBindingProcessor.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/DataSourceCnbBindingProcessor.java index e4d52ae..cc204fe 100644 --- a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/DataSourceCnbBindingProcessor.java +++ b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/DataSourceCnbBindingProcessor.java @@ -21,7 +21,7 @@ import org.springframework.cloud.cnb.core.CnbBinding; import org.springframework.cloud.cnb.jdbc.JdbcBinding; -public class DataSourceCnbBindingProcessor implements CnbBindingProcessor{ +public class DataSourceCnbBindingProcessor implements CnbBindingProcessor { @Override public boolean accept(CnbBinding binding) { return JdbcBinding.isJDCBBinding(binding); diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessor.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessor.java new file mode 100644 index 0000000..1e74b24 --- /dev/null +++ b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessor.java @@ -0,0 +1,44 @@ +/* + * Copyright 2019 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.cnb.boot; + +import java.util.Map; + +import org.springframework.cloud.cnb.core.CnbBinding; + +public class MongoCnbBindingProcessor implements CnbBindingProcessor { + private static final String MONGO_KIND = "mongodb"; + + @Override + public boolean accept(CnbBinding binding) { + return binding.getKind().equals(MONGO_KIND); + } + + @Override + public void process(CnbBinding binding, Map properties) { + properties.put("spring.redis.mongodb.uri", binding.getSecret().get("uri")); + + // TODO: build uri from discrete fields? + } + + @Override + public CnbBindingProcessorProperties getProperties() { + return CnbBindingProcessorProperties.builder() + .propertyPrefixes("spring.data.mongodb") + .serviceName("MongoDB") + .build(); + } +} diff --git a/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessor.java b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessor.java new file mode 100644 index 0000000..cdc3263 --- /dev/null +++ b/cnb-bindings-boot/src/main/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessor.java @@ -0,0 +1,47 @@ +/* + * Copyright 2019 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.cnb.boot; + +import java.util.Map; + +import org.springframework.cloud.cnb.core.CnbBinding; + + +public class RedisCnbBindingProcessor implements CnbBindingProcessor { + + public static final String REDIS_KIND = "redis"; + + @Override + public boolean accept(CnbBinding binding) { + return binding.getKind().equals(REDIS_KIND); + } + + @Override + public void process(CnbBinding binding, Map properties) { + properties.put("spring.redis.host", binding.getSecret().get("hostname")); //TODO: also support "host" + properties.put("spring.redis.port", binding.getSecret().get("port")); //TODO: handle missing + properties.put("spring.redis.password", binding.getSecret().get("password")); //TODO: handle missing + + // TODO: spring.redis.ssl + } + + @Override + public CnbBindingProcessorProperties getProperties() { + return CnbBindingProcessorProperties.builder() + .propertyPrefixes("spring.redis") + .build(); + } +} diff --git a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/CassandraCnbBindingProcessorTests.java b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/CassandraCnbBindingProcessorTests.java new file mode 100644 index 0000000..4112006 --- /dev/null +++ b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/CassandraCnbBindingProcessorTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2019 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.cnb.boot; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import org.springframework.cloud.cnb.core.CnbBinding; + + +import static org.assertj.core.api.Assertions.assertThat; + +public class CassandraCnbBindingProcessorTests { + + @Test + public void acceptIfCassandraKind() { + CassandraCnbBindingProcessor bindingProcessor = new CassandraCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "cassandra"); + CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap()); + assertThat(bindingProcessor.accept(binding)).isTrue(); + } + + @Test + public void rejectIfNotCassandraKind() { + CassandraCnbBindingProcessor bindingProcessor = new CassandraCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "mysql"); + CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap()); + assertThat(bindingProcessor.accept(binding)).isFalse(); + } + + @Test + public void processDataSourcePropertiesTest() { + CassandraCnbBindingProcessor bindingProcessor = new CassandraCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "cassandra"); + Map bindingSecret = new HashMap(); + bindingSecret.put("port", "9042"); + bindingSecret.put("node_ips", "10.0.4.35,10.0.4.36"); + bindingSecret.put("password", "some-password"); + bindingSecret.put("username", "some-username"); + CnbBinding binding = new CnbBinding(bindingMetadata, bindingSecret); + Map properties = new HashMap(); + bindingProcessor.process(binding, properties); + assertThat(properties.get("spring.data.cassandra.username")).isEqualTo("some-username"); + assertThat(properties.get("spring.data.cassandra.password")).isEqualTo("some-password"); + assertThat(properties.get("spring.data.cassandra.contact-points")).isEqualTo("10.0.4.35,10.0.4.36"); + assertThat(properties.get("spring.data.cassandra.port")).isEqualTo("9042"); + } + +} diff --git a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessorTest.java b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessorTest.java index 727bcb2..4bc234a 100644 --- a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessorTest.java +++ b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/CnbBindingsPostProcessorTest.java @@ -55,6 +55,8 @@ public class CnbBindingsPostProcessorTest { .isEqualTo("mysql_password"); } + // TODO: test multiple services + @Test public void testNoBindingsPresent(){ CnbBindingsPostProcessor environmentPostProcessor = new CnbBindingsPostProcessor(); diff --git a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessorTests.java b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessorTests.java new file mode 100644 index 0000000..68ac116 --- /dev/null +++ b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/MongoCnbBindingProcessorTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 2019 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.cnb.boot; + + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import org.springframework.cloud.cnb.core.CnbBinding; + + +import static org.assertj.core.api.Assertions.assertThat; + +public class MongoCnbBindingProcessorTests { + + @Test + public void acceptIfMongoKind() { + MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "mongodb"); + CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap()); + assertThat(bindingProcessor.accept(binding)).isTrue(); + } + + @Test + public void rejectIfNotMongoKind() { + MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "mysql"); + CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap()); + assertThat(bindingProcessor.accept(binding)).isFalse(); + } + + @Test + public void processDataSourcePropertiesTest() { + MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "mongodb"); + Map bindingSecret = new HashMap(); + bindingSecret.put("uri", "some-uri"); + CnbBinding binding = new CnbBinding(bindingMetadata, bindingSecret); + Map properties = new HashMap(); + bindingProcessor.process(binding, properties); + assertThat(properties.get("spring.redis.mongodb.uri")).isEqualTo("some-uri"); + } + +} diff --git a/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessorTests.java b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessorTests.java new file mode 100644 index 0000000..a4869db --- /dev/null +++ b/cnb-bindings-boot/src/test/java/org/springframework/cloud/cnb/boot/RedisCnbBindingProcessorTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2019 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.cnb.boot; + + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import org.springframework.cloud.cnb.core.CnbBinding; + + +import static org.assertj.core.api.Assertions.assertThat; + +public class RedisCnbBindingProcessorTests { + + @Test + public void acceptIfRedisKind() { + RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "redis"); + CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap()); + assertThat(bindingProcessor.accept(binding)).isTrue(); + } + + @Test + public void rejectIfNotRedisKind() { + RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "mysql"); + CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap()); + assertThat(bindingProcessor.accept(binding)).isFalse(); + } + + @Test + public void processDataSourcePropertiesTest() { + RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor(); + Map bindingMetadata = new HashMap(); + bindingMetadata.put("kind", "redis"); + Map bindingSecret = new HashMap(); + bindingSecret.put("hostname", "10.0.4.35"); + bindingSecret.put("port", "6379"); + bindingSecret.put("password", "some-password"); + CnbBinding binding = new CnbBinding(bindingMetadata, bindingSecret); + Map properties = new HashMap(); + bindingProcessor.process(binding, properties); + assertThat(properties.get("spring.redis.host")).isEqualTo("10.0.4.35"); + assertThat(properties.get("spring.redis.port")).isEqualTo("6379"); + assertThat(properties.get("spring.redis.password")).isEqualTo("some-password"); + } + + //TODO: TLS for redis + +}