Adds more processors

* redis, mongo, cassandra
* still missing amqp

Signed-off-by: Emily Casey <ecasey@pivotal.io>
This commit is contained in:
Emily Casey
2020-04-28 17:47:30 -04:00
parent 8f19604a92
commit 78d1c4b749
10 changed files with 338 additions and 1 deletions

View File

@@ -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<String, Object> 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;
}
}

View File

@@ -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;

View File

@@ -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();

View File

@@ -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);

View File

@@ -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<String, Object> 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();
}
}

View File

@@ -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<String, Object> 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();
}
}

View File

@@ -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<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "cassandra");
CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isTrue();
}
@Test
public void rejectIfNotCassandraKind() {
CassandraCnbBindingProcessor bindingProcessor = new CassandraCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mysql");
CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isFalse();
}
@Test
public void processDataSourcePropertiesTest() {
CassandraCnbBindingProcessor bindingProcessor = new CassandraCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "cassandra");
Map<String,String> bindingSecret = new HashMap<String,String>();
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<String,Object> properties = new HashMap<String,Object>();
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");
}
}

View File

@@ -55,6 +55,8 @@ public class CnbBindingsPostProcessorTest {
.isEqualTo("mysql_password");
}
// TODO: test multiple services
@Test
public void testNoBindingsPresent(){
CnbBindingsPostProcessor environmentPostProcessor = new CnbBindingsPostProcessor();

View File

@@ -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<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mongodb");
CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isTrue();
}
@Test
public void rejectIfNotMongoKind() {
MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mysql");
CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isFalse();
}
@Test
public void processDataSourcePropertiesTest() {
MongoCnbBindingProcessor bindingProcessor = new MongoCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mongodb");
Map<String,String> bindingSecret = new HashMap<String,String>();
bindingSecret.put("uri", "some-uri");
CnbBinding binding = new CnbBinding(bindingMetadata, bindingSecret);
Map<String,Object> properties = new HashMap<String,Object>();
bindingProcessor.process(binding, properties);
assertThat(properties.get("spring.redis.mongodb.uri")).isEqualTo("some-uri");
}
}

View File

@@ -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<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "redis");
CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isTrue();
}
@Test
public void rejectIfNotRedisKind() {
RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "mysql");
CnbBinding binding = new CnbBinding(bindingMetadata, new HashMap<String, String>());
assertThat(bindingProcessor.accept(binding)).isFalse();
}
@Test
public void processDataSourcePropertiesTest() {
RedisCnbBindingProcessor bindingProcessor = new RedisCnbBindingProcessor();
Map<String, String> bindingMetadata = new HashMap<String, String>();
bindingMetadata.put("kind", "redis");
Map<String,String> bindingSecret = new HashMap<String,String>();
bindingSecret.put("hostname", "10.0.4.35");
bindingSecret.put("port", "6379");
bindingSecret.put("password", "some-password");
CnbBinding binding = new CnbBinding(bindingMetadata, bindingSecret);
Map<String,Object> properties = new HashMap<String,Object>();
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
}