From 04eed55e6aa6ea31b21b22af56bb1d77c923296f Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Sat, 9 May 2020 08:54:23 -0700 Subject: [PATCH] DataSource BindingsPropertiesProcessors Signed-off-by: Ben Hale --- .../cloud/cnb/jdbc/DB2JdbcKind.java | 39 ------------- .../cloud/cnb/jdbc/MysqlJdbcKind.java | 51 ---------------- .../cloud/cnb/jdbc/OracleJdbcKind.java | 39 ------------- .../cloud/cnb/jdbc/PostgresJdbcKind.java | 43 -------------- .../cloud/cnb/jdbc/SqlServerJdbcKind.java | 39 ------------- .../cloud/cnb/jdbc/JdbcBindingTest.java | 12 ---- cnb-bindings/pom.xml | 15 +++-- .../cloud/bindings/Bindings.java | 6 +- .../CassandraBindingsPropertiesProcessor.java | 2 +- .../Db2BindingsPropertiesProcessor.java | 48 +++++++++++++++ .../MongoDbBindingsPropertiesProcessor.java | 2 +- .../MySqlBindingsPropertiesProcessor.java | 58 +++++++++++++++++++ .../OracleBindingsPropertiesProcessor.java | 48 +++++++++++++++ ...PostgreSqlBindingsPropertiesProcessor.java | 48 +++++++++++++++ .../RedisBindingsPropertiesProcessor.java | 2 +- .../SqlServerBindingsPropertiesProcessor.java | 48 +++++++++++++++ .../main/resources/META-INF/spring.factories | 7 ++- .../BindingsEnvironmentPostProcessorTest.java | 2 +- .../Db2BindingsPropertiesProcessorTest.java | 57 ++++++++++++++++++ .../MySqlBindingsPropertiesProcessorTest.java | 56 ++++++++++++++++++ ...OracleBindingsPropertiesProcessorTest.java | 57 ++++++++++++++++++ ...greSqlBindingsPropertiesProcessorTest.java | 57 ++++++++++++++++++ ...ServerBindingsPropertiesProcessorTest.java | 57 ++++++++++++++++++ 23 files changed, 557 insertions(+), 236 deletions(-) delete mode 100644 cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/DB2JdbcKind.java delete mode 100644 cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/MysqlJdbcKind.java delete mode 100644 cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/OracleJdbcKind.java delete mode 100644 cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/PostgresJdbcKind.java delete mode 100644 cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/SqlServerJdbcKind.java create mode 100644 cnb-bindings/src/main/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessor.java create mode 100644 cnb-bindings/src/main/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessor.java create mode 100644 cnb-bindings/src/main/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessor.java create mode 100644 cnb-bindings/src/main/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessor.java create mode 100644 cnb-bindings/src/main/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessor.java create mode 100644 cnb-bindings/src/test/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessorTest.java create mode 100644 cnb-bindings/src/test/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessorTest.java create mode 100644 cnb-bindings/src/test/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessorTest.java create mode 100644 cnb-bindings/src/test/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessorTest.java create mode 100644 cnb-bindings/src/test/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessorTest.java diff --git a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/DB2JdbcKind.java b/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/DB2JdbcKind.java deleted file mode 100644 index 6c11445..0000000 --- a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/DB2JdbcKind.java +++ /dev/null @@ -1,39 +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.jdbc; - -import org.springframework.cloud.cnb.Binding; - -public class DB2JdbcKind implements JdbcKind{ - - public static final String DB2_KIND = "db2"; - public static final String DB2_SCHEME = "db2"; - - @Override - public boolean forBinding(Binding binding) { - return binding.getKind().equals(DB2_KIND); - } - - @Override - public String getScheme() { - return DB2_SCHEME; - } - - @Override - public String getDriverClassName() { - return "com.ibm.db2.jcc.DB2Driver"; - } -} diff --git a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/MysqlJdbcKind.java b/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/MysqlJdbcKind.java deleted file mode 100644 index ff9fb96..0000000 --- a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/MysqlJdbcKind.java +++ /dev/null @@ -1,51 +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.jdbc; - -import org.springframework.cloud.cnb.Binding; - -public class MysqlJdbcKind implements JdbcKind{ - - public static final String MYSQL_KIND = "mysql"; - public static final String MYSQL_SCHEME = "mysql"; - - @Override - public boolean forBinding(Binding binding) { - return binding.getKind().equals(MYSQL_KIND); - } - - @Override - public String getScheme() { - return MYSQL_SCHEME; - } - - @Override - public String getDriverClassName() { - String driverClassNameToUse = null; - try { - driverClassNameToUse = "org.mariadb.jdbc.Driver"; - Class.forName(driverClassNameToUse, false, getClass().getClassLoader()); - } catch (ClassNotFoundException e) { - try { - driverClassNameToUse = "com.mysql.cj.jdbc.Driver"; - Class.forName(driverClassNameToUse, false, getClass().getClassLoader()); - } catch (ClassNotFoundException e2) { - return null; - } - } - return driverClassNameToUse; - } -} diff --git a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/OracleJdbcKind.java b/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/OracleJdbcKind.java deleted file mode 100644 index 6a50e89..0000000 --- a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/OracleJdbcKind.java +++ /dev/null @@ -1,39 +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.jdbc; - -import org.springframework.cloud.cnb.Binding; - -public class OracleJdbcKind implements JdbcKind{ - - public static final String ORACLE_KIND = "oracle"; - public static final String ORACLE_SCHEME = "oracle"; - - @Override - public boolean forBinding(Binding binding) { - return binding.getKind().equals(ORACLE_KIND); - } - - @Override - public String getScheme() { - return ORACLE_SCHEME; - } - - @Override - public String getDriverClassName() { - return "oracle.jdbc.OracleDriver"; - } -} diff --git a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/PostgresJdbcKind.java b/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/PostgresJdbcKind.java deleted file mode 100644 index 873f57e..0000000 --- a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/PostgresJdbcKind.java +++ /dev/null @@ -1,43 +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.jdbc; - -import java.util.Arrays; -import java.util.List; - -import org.springframework.cloud.cnb.Binding; - - -public class PostgresJdbcKind implements JdbcKind{ - - public static final List POSTGRES_KINDS = Arrays.asList("postgres", "postgresql"); - public static final String POSTGRES_SCHEME = "postgres"; - - @Override - public boolean forBinding(Binding binding) { - return POSTGRES_KINDS.contains(binding.getKind()); - } - - @Override - public String getScheme() { - return POSTGRES_SCHEME; - } - - @Override - public String getDriverClassName() { - return "org.postgresql.Driver"; - } -} diff --git a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/SqlServerJdbcKind.java b/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/SqlServerJdbcKind.java deleted file mode 100644 index 70d95a3..0000000 --- a/cnb-bindings-jdbc/src/main/java/org/springframework/cloud/cnb/jdbc/SqlServerJdbcKind.java +++ /dev/null @@ -1,39 +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.jdbc; - -import org.springframework.cloud.cnb.Binding; - -public class SqlServerJdbcKind implements JdbcKind{ - - public static final String SQLSERVER_KIND = "sqlserver"; - public static final String SQLSERVER_SCHEME = "sqlserver"; - - @Override - public boolean forBinding(Binding binding) { - return binding.getKind().equals(SQLSERVER_KIND); - } - - @Override - public String getScheme() { - return SQLSERVER_SCHEME; - } - - @Override - public String getDriverClassName() { - return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; - } -} diff --git a/cnb-bindings-jdbc/src/test/java/org/springframework/cloud/cnb/jdbc/JdbcBindingTest.java b/cnb-bindings-jdbc/src/test/java/org/springframework/cloud/cnb/jdbc/JdbcBindingTest.java index 2044002..8374062 100644 --- a/cnb-bindings-jdbc/src/test/java/org/springframework/cloud/cnb/jdbc/JdbcBindingTest.java +++ b/cnb-bindings-jdbc/src/test/java/org/springframework/cloud/cnb/jdbc/JdbcBindingTest.java @@ -42,18 +42,6 @@ public class JdbcBindingTest { isEqualTo("jdbc:mysql://10.0.4.35:3306/some-db?user=some-username&password=some-password"); } - @Test - public void testJdbcBinding_db2() { - Binding db2Binding = bindingWithKind("db2"); - assertThat(JdbcBinding.isJDCBBinding(db2Binding)).isTrue(); - JdbcBinding jdbcBinding = new JdbcBinding(db2Binding); - - assertThat(jdbcBinding.getJdbcUrl()). - isEqualTo("jdbc:db2://10.0.4.35:3306/some-db?user=some-username&password=some-password"); - assertThat(jdbcBinding.getDriverClassName()). - isEqualTo("com.ibm.db2.jcc.DB2Driver"); - } - @Test public void testJdbcBinding_oracle() { Binding oracleBinding = bindingWithKind("oracle"); diff --git a/cnb-bindings/pom.xml b/cnb-bindings/pom.xml index e51fad8..d102d79 100644 --- a/cnb-bindings/pom.xml +++ b/cnb-bindings/pom.xml @@ -42,6 +42,16 @@ provided + + org.mariadb.jdbc + mariadb-java-client + test + + + org.mockito + mockito-core + test + org.springframework.boot spring-boot-starter-test @@ -53,11 +63,6 @@ - - org.mockito - mockito-core - test - diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/Bindings.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/Bindings.java index c0eb485..f3b0066 100644 --- a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/Bindings.java +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/Bindings.java @@ -103,7 +103,7 @@ public final class Bindings { */ public @Nullable Binding findBinding(@NotNull String name) { for (Binding binding : bindings) { - if (binding.getName().equals(name)) { + if (binding.getName().equalsIgnoreCase(name)) { return binding; } } @@ -133,8 +133,8 @@ public final class Bindings { List filtered = new ArrayList<>(); for (Binding binding : bindings) { - if ((kind == null || binding.getKind().equals(kind)) && - (provider == null) || binding.getProvider().equals(provider)) { + if ((kind == null || binding.getKind().equalsIgnoreCase(kind)) && + (provider == null) || binding.getProvider().equalsIgnoreCase(provider)) { filtered.add(binding); } diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/CassandraBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/CassandraBindingsPropertiesProcessor.java index ed19690..c0a662b 100644 --- a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/CassandraBindingsPropertiesProcessor.java +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/CassandraBindingsPropertiesProcessor.java @@ -29,7 +29,7 @@ public final class CassandraBindingsPropertiesProcessor implements BindingsPrope /** * The {@link Binding} kind that this processor is interested in: {@value}. **/ - public static final String KIND = "cassandra"; + public static final String KIND = "Cassandra"; @Override public void process(@NonNull Bindings bindings, @NotNull Map properties) { diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessor.java new file mode 100644 index 0000000..6ea9ee2 --- /dev/null +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessor.java @@ -0,0 +1,48 @@ +/* + * 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 Db2BindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "DB2"; + + @Override + public void process(@NonNull Bindings bindings, @NotNull Map properties) { + bindings.filterBindings(KIND).forEach(binding -> { + Map secret = binding.getSecret(); + + properties.put("spring.datasource.driver-class-name", "com.ibm.db2.jcc.DB2Driver"); + properties.put("spring.datasource.password", secret.get("password")); + properties.put("spring.datasource.url", String.format("jdbc:db2://%s:%s/%s?user=%s&password=%s", + secret.get("host"), secret.get("port"), secret.get("db"), secret.get("username"), + secret.get("password"))); + properties.put("spring.datasource.username", secret.get("username")); + }); + } + +} diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java index 209dbc1..f1a73e5 100644 --- a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MongoDbBindingsPropertiesProcessor.java @@ -28,7 +28,7 @@ public final class MongoDbBindingsPropertiesProcessor implements BindingsPropert /** * The {@link Binding} kind that this processor is interested in: {@value}. **/ - public static final String KIND = "mongodb"; + public static final String KIND = "MongoDB"; @Override public void process(@NonNull Bindings bindings, @NonNull Map properties) { diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessor.java new file mode 100644 index 0000000..e5c77d4 --- /dev/null +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessor.java @@ -0,0 +1,58 @@ +/* + * 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 MySqlBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "MySQL"; + + @Override + public void process(@NonNull Bindings bindings, @NotNull Map properties) { + bindings.filterBindings(KIND).forEach(binding -> { + Map secret = binding.getSecret(); + + try { + Class.forName("org.mariadb.jdbc.Driver", false, getClass().getClassLoader()); + properties.put("spring.datasource.driver-class-name", "org.mariadb.jdbc.Driver"); + } catch (ClassNotFoundException e) { + try { + Class.forName("com.mysql.cj.jdbc.Driver", false, getClass().getClassLoader()); + properties.put("spring.datasource.driver-class-name", "com.mysql.cj.jdbc.Driver"); + } catch (ClassNotFoundException ignored) { + } + } + + properties.put("spring.datasource.password", secret.get("password")); + properties.put("spring.datasource.url", String.format("jdbc:mysql://%s:%s/%s?user=%s&password=%s", + secret.get("host"), secret.get("port"), secret.get("db"), secret.get("username"), + secret.get("password"))); + properties.put("spring.datasource.username", secret.get("username")); + }); + } + +} diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessor.java new file mode 100644 index 0000000..af388c3 --- /dev/null +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessor.java @@ -0,0 +1,48 @@ +/* + * 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 OracleBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "Oracle"; + + @Override + public void process(@NonNull Bindings bindings, @NotNull Map properties) { + bindings.filterBindings(KIND).forEach(binding -> { + Map secret = binding.getSecret(); + + properties.put("spring.datasource.driver-class-name", "oracle.jdbc.OracleDriver"); + properties.put("spring.datasource.password", secret.get("password")); + properties.put("spring.datasource.url", String.format("jdbc:oracle://%s:%s/%s?user=%s&password=%s", + secret.get("host"), secret.get("port"), secret.get("db"), secret.get("username"), + secret.get("password"))); + properties.put("spring.datasource.username", secret.get("username")); + }); + } + +} diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessor.java new file mode 100644 index 0000000..bb992e9 --- /dev/null +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessor.java @@ -0,0 +1,48 @@ +/* + * 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 PostgreSqlBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "PostgreSQL"; + + @Override + public void process(@NonNull Bindings bindings, @NotNull Map properties) { + bindings.filterBindings(KIND).forEach(binding -> { + Map secret = binding.getSecret(); + + properties.put("spring.datasource.driver-class-name", "org.postgresql.Driver"); + properties.put("spring.datasource.password", secret.get("password")); + properties.put("spring.datasource.url", String.format("jdbc:postgres://%s:%s/%s?user=%s&password=%s", + secret.get("host"), secret.get("port"), secret.get("db"), secret.get("username"), + secret.get("password"))); + properties.put("spring.datasource.username", secret.get("username")); + }); + } + +} diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessor.java index b303e8f..147bb25 100644 --- a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessor.java +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/RedisBindingsPropertiesProcessor.java @@ -29,7 +29,7 @@ public final class RedisBindingsPropertiesProcessor implements BindingsPropertie /** * The {@link Binding} kind that this processor is interested in: {@value}. **/ - public static final String KIND = "redis"; + public static final String KIND = "Redis"; @Override public void process(@NonNull Bindings bindings, @NotNull Map properties) { diff --git a/cnb-bindings/src/main/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessor.java b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessor.java new file mode 100644 index 0000000..e8db6aa --- /dev/null +++ b/cnb-bindings/src/main/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessor.java @@ -0,0 +1,48 @@ +/* + * 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 SqlServerBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "SQLServer"; + + @Override + public void process(@NonNull Bindings bindings, @NotNull Map properties) { + bindings.filterBindings(KIND).forEach(binding -> { + Map secret = binding.getSecret(); + + properties.put("spring.datasource.driver-class-name", "com.microsoft.sqlserver.jdbc.SQLServerDriver"); + properties.put("spring.datasource.password", secret.get("password")); + properties.put("spring.datasource.url", String.format("jdbc:sqlserver://%s:%s/%s?user=%s&password=%s", + secret.get("host"), secret.get("port"), secret.get("db"), secret.get("username"), + secret.get("password"))); + properties.put("spring.datasource.username", secret.get("username")); + }); + } + +} diff --git a/cnb-bindings/src/main/resources/META-INF/spring.factories b/cnb-bindings/src/main/resources/META-INF/spring.factories index 31b34d1..fb08309 100644 --- a/cnb-bindings/src/main/resources/META-INF/spring.factories +++ b/cnb-bindings/src/main/resources/META-INF/spring.factories @@ -3,5 +3,10 @@ org.springframework.boot.env.EnvironmentPostProcessor=\ # Included implementations org.springframework.cloud.bindings.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.CassandraBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.Db2BindingsPropertiesProcessor, \ org.springframework.cloud.bindings.MongoDbBindingsPropertiesProcessor, \ - org.springframework.cloud.bindings.RedisBindingsPropertiesProcessor + org.springframework.cloud.bindings.MySqlBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.OracleBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.PostgreSqlBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.RedisBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.SqlServerBindingsPropertiesProcessor diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java index 542bade..bc84e16 100644 --- a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/BindingsEnvironmentPostProcessorTest.java @@ -117,7 +117,7 @@ final class BindingsEnvironmentPostProcessorTest { @Test @DisplayName("included implementations are registered") void includedImplementations() { - assertThat(new BindingsEnvironmentPostProcessor().processors).hasSize(3); + assertThat(new BindingsEnvironmentPostProcessor().processors).hasSize(8); } } diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..59ab609 --- /dev/null +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/Db2BindingsPropertiesProcessorTest.java @@ -0,0 +1,57 @@ +/* + * 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.Db2BindingsPropertiesProcessor.KIND; + +@DisplayName("DB2 BindingsPropertiesProcessor") +final class Db2BindingsPropertiesProcessorTest { + + @Test + @DisplayName("contributes properties") + void test() { + HashMap properties = new HashMap<>(); + + new Db2BindingsPropertiesProcessor().process(new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("db", "test-db") + .withEntry("host", "test-host") + .withEntry("password", "test-password") + .withEntry("port", "test-port") + .withEntry("username", "test-username") + ) + ), properties); + + assertThat(properties) + .containsEntry("spring.datasource.driver-class-name", "com.ibm.db2.jcc.DB2Driver") + .containsEntry("spring.datasource.password", "test-password") + .containsEntry("spring.datasource.url", + "jdbc:db2://test-host:test-port/test-db?user=test-username&password=test-password") + .containsEntry("spring.datasource.username", "test-username"); + } + +} diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..01594e7 --- /dev/null +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/MySqlBindingsPropertiesProcessorTest.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; + +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.MySqlBindingsPropertiesProcessor.KIND; + +@DisplayName("MySQL BindingsPropertiesProcessor") +final class MySqlBindingsPropertiesProcessorTest { + + @Test + @DisplayName("contributes properties") + void test() { + HashMap properties = new HashMap<>(); + + new MySqlBindingsPropertiesProcessor().process(new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("db", "test-db") + .withEntry("host", "test-host") + .withEntry("password", "test-password") + .withEntry("port", "test-port") + .withEntry("username", "test-username") + ) + ), properties); + + assertThat(properties) + .containsEntry("spring.datasource.driver-class-name", "org.mariadb.jdbc.Driver") + .containsEntry("spring.datasource.password", "test-password") + .containsEntry("spring.datasource.url", "jdbc:mysql://test-host:test-port/test-db?user=test-username&password=test-password") + .containsEntry("spring.datasource.username", "test-username"); + } + +} diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..0a6d723 --- /dev/null +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/OracleBindingsPropertiesProcessorTest.java @@ -0,0 +1,57 @@ +/* + * 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.OracleBindingsPropertiesProcessor.KIND; + +@DisplayName("Oracle BindingsPropertiesProcessor") +final class OracleBindingsPropertiesProcessorTest { + + @Test + @DisplayName("contributes properties") + void test() { + HashMap properties = new HashMap<>(); + + new OracleBindingsPropertiesProcessor().process(new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("db", "test-db") + .withEntry("host", "test-host") + .withEntry("password", "test-password") + .withEntry("port", "test-port") + .withEntry("username", "test-username") + ) + ), properties); + + assertThat(properties) + .containsEntry("spring.datasource.driver-class-name", "oracle.jdbc.OracleDriver") + .containsEntry("spring.datasource.password", "test-password") + .containsEntry("spring.datasource.url", + "jdbc:oracle://test-host:test-port/test-db?user=test-username&password=test-password") + .containsEntry("spring.datasource.username", "test-username"); + } + +} diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..1e7f413 --- /dev/null +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/PostgreSqlBindingsPropertiesProcessorTest.java @@ -0,0 +1,57 @@ +/* + * 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.PostgreSqlBindingsPropertiesProcessor.KIND; + +@DisplayName("PostgreSQL BindingsPropertiesProcessor") +final class PostgreSqlBindingsPropertiesProcessorTest { + + @Test + @DisplayName("contributes properties") + void test() { + HashMap properties = new HashMap<>(); + + new PostgreSqlBindingsPropertiesProcessor().process(new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("db", "test-db") + .withEntry("host", "test-host") + .withEntry("password", "test-password") + .withEntry("port", "test-port") + .withEntry("username", "test-username") + ) + ), properties); + + assertThat(properties) + .containsEntry("spring.datasource.driver-class-name", "org.postgresql.Driver") + .containsEntry("spring.datasource.password", "test-password") + .containsEntry("spring.datasource.url", + "jdbc:postgres://test-host:test-port/test-db?user=test-username&password=test-password") + .containsEntry("spring.datasource.username", "test-username"); + } + +} diff --git a/cnb-bindings/src/test/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessorTest.java b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..2d302ab --- /dev/null +++ b/cnb-bindings/src/test/java/org/springframework/cloud/bindings/SqlServerBindingsPropertiesProcessorTest.java @@ -0,0 +1,57 @@ +/* + * 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.SqlServerBindingsPropertiesProcessor.KIND; + +@DisplayName("SQLServer BindingsPropertiesProcessor") +final class SqlServerBindingsPropertiesProcessorTest { + + @Test + @DisplayName("contributes properties") + void test() { + HashMap properties = new HashMap<>(); + + new SqlServerBindingsPropertiesProcessor().process(new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("db", "test-db") + .withEntry("host", "test-host") + .withEntry("password", "test-password") + .withEntry("port", "test-port") + .withEntry("username", "test-username") + ) + ), properties); + + assertThat(properties) + .containsEntry("spring.datasource.driver-class-name", "com.microsoft.sqlserver.jdbc.SQLServerDriver") + .containsEntry("spring.datasource.password", "test-password") + .containsEntry("spring.datasource.url", + "jdbc:sqlserver://test-host:test-port/test-db?user=test-username&password=test-password") + .containsEntry("spring.datasource.username", "test-username"); + } + +}