Skip to content

Commit

Permalink
Refactoring Db handling...
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Hiemer committed Jun 1, 2018
1 parent 214b8d2 commit 03cec57
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 61 deletions.
14 changes: 8 additions & 6 deletions manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
instances: 1
timeout: 180
memory: 1024M
domain:
applications:
- name: mysql
host: mysql
path: target/ssb-mysql.jar
- name: osb-mysql
host: osb-mysql
path: osb-service/target/osb-mysql-1.2.0.jar
buildpack: https://github.com/cloudfoundry/java-buildpack.git
env:
CF_TARGET:
SPRING_PROFILES_ACTIVE: cloud
SPRING_CLOUD_CONFIG_URI: http://config-server.cf.dev.eu-de-central.msh.host
SPRING_APPLICATION_NAME: osb-mysql
SPRING_CLOUD_CONFIG_USERNAME: admin
SPRING_CLOUD_CONFIG_PASSWORD: cloudfoundry
SPRING_PROFILES_ACTIVE: dev.eu-de-central
JAVA_OPTS: -Djava.security.egd=file:/dev/urandom
2 changes: 1 addition & 1 deletion osb-core
2 changes: 1 addition & 1 deletion osb-deployment
2 changes: 1 addition & 1 deletion osb-persistence
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.evoila.cf.broker.backup;

import de.evoila.cf.broker.bean.BackupTypeConfiguration;
import de.evoila.cf.broker.custom.mysql.MySQLUtils;
import de.evoila.cf.broker.exception.ServiceInstanceDoesNotExistException;
import de.evoila.cf.broker.model.ServiceInstance;
import de.evoila.cf.broker.repository.ServiceInstanceRepository;
import de.evoila.cf.broker.service.BackupCustomService;
import de.evoila.cf.model.DatabaseCredential;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnBean(BackupTypeConfiguration.class)
public class BackupCustomServiceImpl implements BackupCustomService {

BackupTypeConfiguration backupTypeConfiguration;

ServiceInstanceRepository serviceInstanceRepository;

public BackupCustomServiceImpl(BackupTypeConfiguration backupTypeConfiguration,
ServiceInstanceRepository serviceInstanceRepository) {
this.backupTypeConfiguration = backupTypeConfiguration;
this.serviceInstanceRepository = serviceInstanceRepository;
}

public DatabaseCredential getCredentialsForInstanceId(String serviceInstanceId) throws ServiceInstanceDoesNotExistException {
ServiceInstance instance = serviceInstanceRepository.getServiceInstance(serviceInstanceId);

if(instance == null || instance.getHosts().size() <= 0) {
throw new ServiceInstanceDoesNotExistException(serviceInstanceId);
}

DatabaseCredential credential = new DatabaseCredential();
credential.setContext(MySQLUtils.dbName(instance.getId()));
credential.setUsername(instance.getUsername());
credential.setPassword(instance.getPassword());
credential.setHostname(instance.getHosts().get(0).getIp());
credential.setPort(instance.getHosts().get(0).getPort());
credential.setType(backupTypeConfiguration.getType());

return credential;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected Map<String, Object> createCredentials(String bindingId, ServiceInstanc

String username = usernameRandomString.nextString();
String password = passwordRandomString.nextString();
String database = serviceInstance.getId();
String database = MySQLUtils.dbName(serviceInstance.getId());

try {
mysqlCustomImplementation.bindRoleToDatabase(jdbcService, username, password, database);
Expand Down Expand Up @@ -89,11 +89,7 @@ protected Map<String, Object> createCredentials(String bindingId, ServiceInstanc
@Override
protected void deleteBinding(ServiceInstanceBinding binding, ServiceInstance serviceInstance, Plan plan) throws ServiceBrokerException {
MySQLDbService jdbcService;
try {
jdbcService = mysqlCustomImplementation.connection(serviceInstance, plan);
} catch (SQLException e1) {
throw new ServiceBrokerException("Could not connect to database");
}
jdbcService = this.connection(serviceInstance, plan);

if (jdbcService == null)
throw new ServiceBrokerException("Could not connect to database");
Expand All @@ -118,7 +114,7 @@ private MySQLDbService connection(ServiceInstance serviceInstance, Plan plan) {
plan.getMetadata().getIngressInstanceGroup());

jdbcService.createConnection(serviceInstance.getUsername(), serviceInstance.getPassword(),
"admin", serverAddresses);
MySQLUtils.dbName(serviceInstance.getId()), serverAddresses);
} else if (plan.getPlatform() == Platform.EXISTING_SERVICE)
jdbcService.createConnection(existingEndpointBean.getUsername(), existingEndpointBean.getPassword(),
existingEndpointBean.getDatabase(), serviceInstance.getHosts());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
package de.evoila.cf.broker.custom.mysql;

import de.evoila.cf.broker.bean.ExistingEndpointBean;
import de.evoila.cf.broker.model.Plan;
import de.evoila.cf.broker.model.Platform;
import de.evoila.cf.broker.model.ServiceInstance;
import de.evoila.cf.broker.repository.ServiceDefinitionRepository;
import de.evoila.cf.cpi.existing.MySQLExistingServiceFactory;
import org.slf4j.Logger;
Expand Down Expand Up @@ -45,20 +42,4 @@ public void unbindRoleFromDatabase(MySQLDbService jdbcService, String username)
jdbcService.executeUpdate("DROP USER \"" + username + "\"");
}

public MySQLDbService connection(ServiceInstance serviceInstance, Plan plan) throws SQLException {
MySQLDbService jdbcService = new MySQLDbService();
if (jdbcService.isConnected())
return jdbcService;
else {

if(plan.getPlatform() == Platform.BOSH)
jdbcService.createConnection(serviceInstance.getUsername(), serviceInstance.getPassword(),
"admin", serviceInstance.getHosts());
else if (plan.getPlatform() == Platform.EXISTING_SERVICE)
jdbcService.createConnection(existingEndpointBean.getUsername(), existingEndpointBean.getPassword(),
existingEndpointBean.getDatabase(), existingEndpointBean.getHosts());
return jdbcService;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public boolean createConnection(String username, String password, String databas

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://" + connectionUrl;
String url = "jdbc:mysql://" + connectionUrl + "/" + database;
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException | InstantiationException | IllegalAccessException e) {
log.info("Could not establish connection", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.evoila.cf.broker.custom.mysql;

public class MySQLUtils {

public static String dbName(String uuid) {
if (uuid != null && uuid.length() > 15)
return "d" + uuid.replace("-", "").substring(0, 15);
else
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* @author Rene Schollmeyer
*
*/

@Service
@ConditionalOnBean(HAProxyConfiguration.class)
public class HAProxyServiceImpl extends HAProxyService {
Expand All @@ -29,6 +28,6 @@ public Mode getMode(ServerAddress serverAddress) {

@Override
public List<String> getOptions(ServerAddress serverAddress) {
return new ArrayList<String>();
return new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.evoila.cf.cpi.bosh;

import de.evoila.cf.broker.bean.BoshProperties;
import de.evoila.cf.broker.custom.mysql.MySQLUtils;
import de.evoila.cf.broker.model.Plan;
import de.evoila.cf.broker.model.ServiceInstance;
import de.evoila.cf.broker.util.RandomString;
Expand All @@ -17,6 +18,7 @@
*/
public class MySQLDeploymentManager extends DeploymentManager {

private RandomString randomStringUsername = new RandomString(10);
private RandomString randomStringPassword = new RandomString(15);

public static final String INSTANCE_GROUP = "mariadb";
Expand Down Expand Up @@ -61,17 +63,17 @@ protected void replaceParameters(ServiceInstance serviceInstance, Manifest manif
}

String password = randomStringPassword.nextString();
String username = randomStringUsername.nextString();

List<Map<String, String>> databases = new ArrayList<>();
Map<String, String> database = new HashMap<>();
database.put("name", serviceInstance.getId());
database.put("username", (String) mysql.get("admin_username"));
database.put("name", MySQLUtils.dbName(serviceInstance.getId()));
database.put("username", username);
database.put("password", password);
databases.add(database);
mysql.put("seeded_databases", databases);

//mysql.put("seeded_databases", databases);

serviceInstance.setUsername((String) mysql.get("admin_username"));
serviceInstance.setUsername(username);
serviceInstance.setPassword(password);

mysqldExporter.put(MYSQLD_EXPORTER_PASSWORD, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.evoila.cf.broker.bean.ExistingEndpointBean;
import de.evoila.cf.broker.custom.mysql.MySQLCustomImplementation;
import de.evoila.cf.broker.custom.mysql.MySQLDbService;
import de.evoila.cf.broker.custom.mysql.MySQLUtils;
import de.evoila.cf.broker.exception.PlatformException;
import de.evoila.cf.broker.model.Plan;
import de.evoila.cf.broker.model.Platform;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void deleteDatabase(MySQLDbService connection, String database) throws Pl
public void deleteInstance(ServiceInstance serviceInstance, Plan plan) throws PlatformException {
MySQLDbService mySQLDbService = this.connection(serviceInstance, plan);

deleteDatabase(mySQLDbService, serviceInstance.getId());
deleteDatabase(mySQLDbService, MySQLUtils.dbName(serviceInstance.getId()));
}

@Override
Expand All @@ -70,7 +71,7 @@ public ServiceInstance createInstance(ServiceInstance serviceInstance, Plan plan

MySQLDbService mySQLDbService = this.connection(serviceInstance, plan);

createDatabase(mySQLDbService, serviceInstance.getId());
createDatabase(mySQLDbService, MySQLUtils.dbName(serviceInstance.getId()));

return serviceInstance;
}
Expand Down

0 comments on commit 03cec57

Please sign in to comment.