Skip to content

Commit

Permalink
Merge pull request #2 from PaaS-TA/working-serviceplan
Browse files Browse the repository at this point in the history
 [OPENPAAS-SERVICE-JAVA-BROKER-MYSQL] DB connection 변수화
  • Loading branch information
yu-jin-song committed Jul 15, 2022
2 parents 353e451 + e2ee6cb commit 7dd4a81
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.openpaas.servicebroker.mysql.config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openpaas.servicebroker.model.Catalog;
import org.openpaas.servicebroker.model.Plan;
import org.openpaas.servicebroker.model.ServiceDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Spring boot 구동시 Catalog API 에서 사용하는 Catalog Bean 를 생성하는 클래스
Expand All @@ -18,25 +21,39 @@
*
*/
@Configuration
@PropertySource("classpath:serviceplan.properties")
public class CatalogConfig {


@Autowired
private Environment env;

public static String planACon = "";
public static String planBCon = "";

@Bean
public Catalog catalog() {
public Catalog catalog() {
planACon = env.getRequiredProperty("service.plan.a.con");
planBCon = env.getRequiredProperty("service.plan.b.con");

return new Catalog( Arrays.asList(
new ServiceDefinition(
"96b9e707-0e2b-47e3-a21a-fd01a8eb0452",
"Mysql-DB",
"Mysql-DB",
"A simple mysql implementation",
true,
true,
Arrays.asList(
new Plan("411d0c3e-b086-4a24-b041-0aeef1a819d1",
"Mysql-Plan1-10con",
"This is a mysql plan1. 10 user connections",
// "Mysql-Plan1-10con",
env.getRequiredProperty("service.plan.a.name"),
// "This is a mysql plan1. 10 user connections",
"This is a mysql plan1. " + planACon + " user connections",
getPlanMetadata("A"),true),
new Plan("4a932d9d-9bc5-4a86-937f-e2c14bb9f497",
"Mysql-Plan2-100con",
"This is a mysql plan2. 100 user connections",
// "Mysql-Plan2-100con",
env.getRequiredProperty("service.plan.b.name"),
// "This is a mysql plan2. 100 user connections",
"This is a mysql plan2. " + planBCon + " user connections",
getPlanMetadata("B"),false)),
Arrays.asList("mysql", "document"),
getServiceDefinitionMetadata(),
Expand Down Expand Up @@ -92,12 +109,12 @@ private List<Map<String,Object>> getCosts(String planType) {
private List<String> getBullets(String planType) {
if(planType.equals("A")){
return Arrays.asList("Shared MysqlDB server",
"10 concurrent connections (not enforced)");
planACon + " concurrent connections (not enforced)");
}else if(planType.equals("B")){
return Arrays.asList("Shared MysqlDB server",
"100 concurrent connections (not enforced)");
planBCon + " concurrent connections (not enforced)");
}
return Arrays.asList("Shared MysqlDB server",
return Arrays.asList("Shared MysqlDB server",
"10 concurrent connections (not enforced)");
}

Expand All @@ -107,4 +124,4 @@ private List<String> getRequires() {
}


}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package org.openpaas.servicebroker.mysql.service.impl;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openpaas.servicebroker.exception.ServiceBrokerException;
import org.openpaas.servicebroker.model.CreateServiceInstanceBindingRequest;
import org.openpaas.servicebroker.model.CreateServiceInstanceRequest;
Expand All @@ -18,11 +9,21 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Mysql 데이터베이스를 조작하기위한 유틸리티 클래스.
Expand All @@ -31,6 +32,7 @@
*
*/
@PropertySource("classpath:datasource.properties")
@PropertySource("classpath:serviceplan.properties")
@Service
public class MysqlAdminService {

Expand Down Expand Up @@ -59,12 +61,12 @@ public class MysqlAdminService {
public static final String SERVICE_BINDING_DELETE_BY_BINDING_ID = "delete from broker.service_binding where binding_id = ?";

public static final String SERVICE_BINDING_FIND_USERNAME_BY_BINDING_ID = "select username from broker.service_binding where binding_id = ?";

// Plan별 MAX_USER_CONNECTIONS 정보
public static String planA = "411d0c3e-b086-4a24-b041-0aeef1a819d1";
public static int planAconnections = 10;
public static int planAconnections;
public static String planB = "4a932d9d-9bc5-4a86-937f-e2c14bb9f497";
public static int planBconnections = 100;
public static int planBconnections;

static String DATABASE_PREFIX = "op_";

Expand All @@ -84,6 +86,12 @@ public MysqlAdminService(JdbcTemplate jdbcTemplate) {

@Autowired
private Environment env;

@PostConstruct
private void initPlanConnections() {
planAconnections = Integer.parseInt(env.getRequiredProperty("service.plan.a.con"));
planBconnections = Integer.parseInt(env.getRequiredProperty("service.plan.b.con"));
}

/**
* ServiceInstance의 유무를 확인합니다
Expand Down Expand Up @@ -118,7 +126,7 @@ public boolean isExistsUser(String userId){
*/
public ServiceInstance findById(String id){
System.out.println("MysqlAdminService.findById");
ServiceInstance serviceInstance = null;;
ServiceInstance serviceInstance = null;
try {
serviceInstance = jdbcTemplate.queryForObject(SERVICE_INSTANCES_FIND_BY_INSTANCE_ID, mapper, id);
serviceInstance.withDashboardUrl(getDashboardUrl(serviceInstance.getServiceInstanceId()));
Expand All @@ -144,7 +152,7 @@ public ServiceInstance createServiceInstanceByRequest(CreateServiceInstanceReque
*/
public ServiceInstanceBinding findBindById(String id){
System.out.println("MysqlAdminService.findBindById");
ServiceInstanceBinding serviceInstanceBinding = null;;
ServiceInstanceBinding serviceInstanceBinding = null;
try {
serviceInstanceBinding = jdbcTemplate.queryForObject(SERVICE_BINDING_FIND_BY_BINDING_ID, mapper2, id);
} catch (Exception e) {
Expand Down Expand Up @@ -489,7 +497,7 @@ public void setUserConnections(String planId, String id) throws ServiceBrokerExc
}
}

// User MAX_USER_CONNECTIONS 설정 조정
// User Connections 체크
public boolean checkUserConnections(String planId, String id) throws ServiceBrokerException{

/* Plan 정보 설정 */
Expand All @@ -509,6 +517,10 @@ public boolean checkUserConnections(String planId, String id) throws ServiceBrok

return false;
}

public String getPlanId() {
return planA;
}

private static final class ServiceInstanceRowMapper implements RowMapper<ServiceInstance> {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.openpaas.servicebroker.mysql.service.impl;


import java.util.List;
import java.util.Map;

import org.openpaas.servicebroker.exception.ServiceBrokerException;
import org.openpaas.servicebroker.exception.ServiceInstanceDoesNotExistException;
import org.openpaas.servicebroker.exception.ServiceInstanceExistsException;
Expand All @@ -19,6 +16,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* 서비스 인스턴스 서비스가 제공해야하는 메소드를 정의한 인터페이스 클래스인 ServiceInstance를 상속하여
* Mysql 서비스 인스턴스 서비스 관련 메소드를 구현한 클래스.
Expand Down Expand Up @@ -146,8 +146,16 @@ public ServiceInstance updateServiceInstance(UpdateServiceInstanceRequest reques
mysqlAdminService.delete(instance.getServiceInstanceId());
*/

/* 기존 ServiceInstance의 Plan에 변견될경우 다음 처리를 수행합니다. */
/* 기존 ServiceInstance의 Plan이 변경될경우 다음 처리를 수행합니다. */
if(!instance.getPlanId().equals(updatedInstance.getPlanId())){
List<Map<String,Object>> list = mysqlAdminService.findBindByInstanceId(instance.getServiceInstanceId());
String planA = mysqlAdminService.getPlanId();

// instance가 하나라도 bind 되어 있고, plan을 downsizing 하려는 경우 에러를 발생시킵니다.
if(list.size() != 0 && !instance.getPlanId().equals(planA) && updatedInstance.getPlanId().equals(planA)) {
throw new ServiceInstanceUpdateNotSupportedException("Plan cannot be changed.");
}

// Plan 정보에 따라 해당 Database 사용자의 MAX_USER_CONNECTIONS 정보를 조정합니다.
try {
mysqlAdminService.setUserConnections(updatedInstance.getPlanId(), instance.getServiceInstanceId());
Expand All @@ -169,4 +177,4 @@ public ServiceInstance getServiceInstance(String id) {
return mysqlAdminService.findById(id);
}

}
}
5 changes: 5 additions & 0 deletions src/main/resources/serviceplan.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# mysql broker service plan Settings
service.plan.a.name = service-plan-a-name
service.plan.a.con = service-plan-a-connections
service.plan.b.name = service-plan-b-name
service.plan.b.con = service-plan-b-connections

0 comments on commit 7dd4a81

Please sign in to comment.