RedisBindingsPropertiesProcessor
Signed-off-by: Ben Hale <bhale@vmware.com>
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* 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.Binding;
|
||||
|
||||
|
||||
public class RedisCnbBindingProcessor implements CnbBindingProcessor {
|
||||
|
||||
public static final String REDIS_KIND = "redis";
|
||||
|
||||
@Override
|
||||
public boolean accept(Binding binding) {
|
||||
return binding.getKind().equals(REDIS_KIND);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(Binding 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();
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* 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.Binding;
|
||||
|
||||
|
||||
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");
|
||||
Binding binding = new Binding(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");
|
||||
Binding binding = new Binding(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");
|
||||
Binding binding = new Binding(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
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}.
|
||||
*/
|
||||
public final class RedisBindingsPropertiesProcessor implements BindingsPropertiesProcessor {
|
||||
|
||||
/**
|
||||
* The {@link Binding} kind that this processor is interested in: {@value}.
|
||||
**/
|
||||
public static final String KIND = "redis";
|
||||
|
||||
@Override
|
||||
public void process(@NonNull Bindings bindings, @NotNull Map<String, Object> properties) {
|
||||
bindings.filterBindings(KIND).forEach(binding -> {
|
||||
Map<String, String> secret = binding.getSecret();
|
||||
|
||||
properties.put("spring.redis.host", secret.get("hostname"));
|
||||
properties.put("spring.redis.password", secret.get("password"));
|
||||
properties.put("spring.redis.port", secret.get("port"));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,4 +3,5 @@ org.springframework.boot.env.EnvironmentPostProcessor=\
|
||||
# Included implementations
|
||||
org.springframework.cloud.bindings.BindingsPropertiesProcessor=\
|
||||
org.springframework.cloud.bindings.CassandraBindingsPropertiesProcessor, \
|
||||
org.springframework.cloud.bindings.MongoDbBindingsPropertiesProcessor
|
||||
org.springframework.cloud.bindings.MongoDbBindingsPropertiesProcessor, \
|
||||
org.springframework.cloud.bindings.RedisBindingsPropertiesProcessor
|
||||
|
||||
@@ -117,7 +117,7 @@ final class BindingsEnvironmentPostProcessorTest {
|
||||
@Test
|
||||
@DisplayName("included implementations are registered")
|
||||
void includedImplementations() {
|
||||
assertThat(new BindingsEnvironmentPostProcessor().processors).hasSize(2);
|
||||
assertThat(new BindingsEnvironmentPostProcessor().processors).hasSize(3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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.RedisBindingsPropertiesProcessor.KIND;
|
||||
|
||||
@DisplayName("Redis BindingsPropertiesProcessor")
|
||||
final class RedisBindingsPropertiesProcessorTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("contributes properties")
|
||||
void test() {
|
||||
HashMap<String, Object> properties = new HashMap<>();
|
||||
|
||||
new RedisBindingsPropertiesProcessor().process(new Bindings(
|
||||
new Binding("test-name", Paths.get("test-path"),
|
||||
Collections.singletonMap("kind", KIND),
|
||||
new FluentMap()
|
||||
.withEntry("hostname", "test-hostname")
|
||||
.withEntry("password", "test-password")
|
||||
.withEntry("port", "test-port")
|
||||
)
|
||||
), properties);
|
||||
|
||||
assertThat(properties)
|
||||
.containsEntry("spring.redis.host", "test-hostname")
|
||||
.containsEntry("spring.redis.password", "test-password")
|
||||
.containsEntry("spring.redis.port", "test-port");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user