diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterInfo.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterConnection.java similarity index 66% rename from cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterInfo.java rename to cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterConnection.java index e04218bc..16508844 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterInfo.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/ClusterConnection.java @@ -1,5 +1,6 @@ package kr.hakdang.cadio.core.domain.cluster; +import io.micrometer.common.util.StringUtils; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -7,7 +8,7 @@ import lombok.extern.slf4j.Slf4j; /** - * ClusterInfo + * ClusterConnection * * @author akageun * @since 2024-07-02 @@ -15,7 +16,7 @@ @Slf4j @Getter @NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ClusterInfo { +public class ClusterConnection { private String contactPoints; private int port; private String localDatacenter; @@ -23,7 +24,7 @@ public class ClusterInfo { private String password; @Builder - public ClusterInfo(String contactPoints, int port, String localDatacenter, String username, String password) { + public ClusterConnection(String contactPoints, int port, String localDatacenter, String username, String password) { //TODO : Validation this.contactPoints = contactPoints; @@ -32,4 +33,8 @@ public ClusterInfo(String contactPoints, int port, String localDatacenter, Strin this.username = username; this.password = password; //TODO : 암호화 } + + public boolean isAuthCredentials() { + return StringUtils.isNotBlank(this.username) && StringUtils.isNotBlank(this.password); + } } diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/TempClusterConnector.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/TempClusterConnector.java new file mode 100644 index 00000000..f6aef1bb --- /dev/null +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/TempClusterConnector.java @@ -0,0 +1,62 @@ +package kr.hakdang.cadio.core.domain.cluster; + +import com.datastax.oss.driver.api.core.CqlSession; +import com.datastax.oss.driver.api.core.CqlSessionBuilder; +import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfo; +import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoProvider; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + +/** + * TempClusterConnector + * - 임시 목적으로 사용할 connector + * + * @author akageun + * @since 2024-07-03 + */ +@Slf4j +@Service +public class TempClusterConnector { + + private final ClusterInfoProvider clusterInfoProvider; + + public TempClusterConnector( + ClusterInfoProvider clusterInfoProvider + ) { + this.clusterInfoProvider = clusterInfoProvider; + } + + public List makeContactPoint(String contactPoints, int port) { + String[] contactPointArr = StringUtils.split(contactPoints, ","); + + List result = new ArrayList<>(); + for (String contactPoint : contactPointArr) { + result.add(new InetSocketAddress(contactPoint, port)); + } + + return result; + } + + public CqlSession makeSession(ClusterConnection clusterConnection) { + CqlSessionBuilder builder = CqlSession.builder() + .addContactPoints(makeContactPoint(clusterConnection.getContactPoints(), clusterConnection.getPort())) + .withLocalDatacenter(clusterConnection.getLocalDatacenter()); + + if (clusterConnection.isAuthCredentials()) { + builder.withAuthCredentials(clusterConnection.getUsername(), clusterConnection.getPassword()); + } + + return builder.build(); + } + + public CqlSession makeSession(String clusterId) { + ClusterInfo info = clusterInfoProvider.findClusterInfo(clusterId); + return makeSession(info.makeClusterConnector()); + } +} diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/BaseClusterInfo.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/BaseClusterInfo.java index 70c53131..9c242f04 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/BaseClusterInfo.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/BaseClusterInfo.java @@ -1,7 +1,7 @@ package kr.hakdang.cadio.core.domain.cluster.info; import com.fasterxml.jackson.core.type.TypeReference; -import kr.hakdang.cadio.core.domain.cluster.ClusterInfo; +import kr.hakdang.cadio.core.domain.cluster.ClusterConnection; import java.io.File; import java.util.List; @@ -14,7 +14,7 @@ */ public abstract class BaseClusterInfo { - protected final static TypeReference> TYPED = new TypeReference>() { + protected final static TypeReference> TYPED = new TypeReference<>() { }; protected String cadioBaseDir() { diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfo.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfo.java new file mode 100644 index 00000000..0eec5c70 --- /dev/null +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfo.java @@ -0,0 +1,54 @@ +package kr.hakdang.cadio.core.domain.cluster.info; + +import kr.hakdang.cadio.core.domain.cluster.ClusterConnection; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * ClusterInfo + * + * @author akageun + * @since 2024-07-03 + */ +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ClusterInfo { + private String clusterId; + private String clusterName; + private String contactPoints; + private int port; + private String localDatacenter; + private String username; + private String password; + + @Builder(toBuilder = true) + public ClusterInfo( + String clusterId, + String clusterName, + String contactPoints, + int port, + String localDatacenter, + String username, + String password + ) { + this.clusterId = clusterId; + this.clusterName = clusterName; + this.contactPoints = contactPoints; + this.port = port; + this.localDatacenter = localDatacenter; + this.username = username; + this.password = password; + } + + public ClusterConnection makeClusterConnector() { + return ClusterConnection.builder() + .contactPoints(contactPoints) + .port(port) + .localDatacenter(localDatacenter) + .username(username) + .password(password) + .build(); + } +} diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java index 0325c58b..dd28ab79 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoManager.java @@ -3,13 +3,14 @@ import com.fasterxml.jackson.databind.ObjectMapper; import kr.hakdang.cadio.common.Jsons; import kr.hakdang.cadio.core.domain.bootstrap.BootstrapProvider; -import kr.hakdang.cadio.core.domain.cluster.ClusterInfo; +import kr.hakdang.cadio.core.domain.cluster.ClusterConnection; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.io.File; import java.util.Collections; import java.util.List; +import java.util.UUID; /** * ClusterManager @@ -33,13 +34,7 @@ public ClusterInfoManager( this.clusterInfoProvider = clusterInfoProvider; } - public void register( - String contactPoints, - int port, - String localDatacenter, - String username, - String password - ) { + public void register(ClusterInfoRegisterArgs args) { ObjectMapper om = Jsons.OBJECT_MAPPER; try { @@ -47,8 +42,7 @@ public void register( File baseDir = new File(cadioBaseDir); if (!baseDir.exists()) { - boolean result = baseDir.mkdir(); - log.info("make base directory : {}, path : {}", result, cadioBaseDir); + log.info("make base directory : {}, path : {}", baseDir.mkdir(), cadioBaseDir); } File clusterJsonFile = getClusterJsonFile(); @@ -57,16 +51,9 @@ public void register( } List result = om.readValue(clusterJsonFile, TYPED); - result.add(ClusterInfo.builder() - .contactPoints(contactPoints) - .port(port) - .localDatacenter(localDatacenter) - .username(username) - .password(password) - .build()); + result.add(args.makeClusterInfo(UUID.randomUUID().toString())); om.writeValue(clusterJsonFile, result); - bootstrapProvider.updateMinClusterCountCheck(clusterInfoProvider.checkMinClusterCount()); } catch (Exception e) { log.error(e.getMessage(), e); diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoProvider.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoProvider.java index 10bc8ead..e1d663ef 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoProvider.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoProvider.java @@ -1,17 +1,18 @@ package kr.hakdang.cadio.core.domain.cluster.info; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import kr.hakdang.cadio.common.Jsons; -import kr.hakdang.cadio.core.domain.cluster.ClusterInfo; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * ClusterInfoProvider @@ -25,6 +26,8 @@ @Service public class ClusterInfoProvider extends BaseClusterInfo { + private static final ObjectMapper OM = Jsons.OBJECT_MAPPER; + public boolean checkMinClusterCount() { long count = registeredCount(); @@ -32,16 +35,16 @@ public boolean checkMinClusterCount() { } public long registeredCount() { - ObjectMapper om = Jsons.OBJECT_MAPPER; try { - List result = om.readValue(getClusterJsonFile(), TYPED); + List result = OM.readValue(getClusterJsonFile(), TYPED); if (CollectionUtils.isEmpty(result)) { return 0; } return result.size(); + } catch (FileNotFoundException e) { log.warn(e.getMessage(), e); //첫 실행시, TODO : 에러메시지 등 고민 필요 return 0; @@ -51,4 +54,23 @@ public long registeredCount() { } } + public List getList() { + try { + return OM.readValue(getClusterJsonFile(), TYPED); + } catch (IOException e) { + log.error(e.getMessage(), e); + return Collections.emptyList(); + } + } + + /** + * @return key : clusterId + */ + public Map getDictionary() { + return getList().stream().collect(Collectors.toMap(ClusterInfo::getClusterId, o -> o)); + } + + public ClusterInfo findClusterInfo(String clusterId) { + return getDictionary().get(clusterId); + } } diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoRegisterArgs.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoRegisterArgs.java new file mode 100644 index 00000000..f06cfb6f --- /dev/null +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoRegisterArgs.java @@ -0,0 +1,49 @@ +package kr.hakdang.cadio.core.domain.cluster.info; + +import kr.hakdang.cadio.core.domain.cluster.ClusterConnection; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * ClusterInfoRegisterArgs + * + * @author akageun + * @since 2024-07-03 + */ +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ClusterInfoRegisterArgs { + private String clusterName; + private String contactPoints; + private int port; + private String localDatacenter; + private String username; + private String password; + + //Append + + @Builder + public ClusterInfoRegisterArgs(String clusterName, String contactPoints, int port, String localDatacenter, String username, String password) { + this.clusterName = clusterName; + this.contactPoints = contactPoints; + this.port = port; + this.localDatacenter = localDatacenter; + this.username = username; + this.password = password; + } + + public ClusterInfo makeClusterInfo(String clusterId) { + return ClusterInfo.builder() + .clusterId(clusterId) + .clusterName(clusterName) + .contactPoints(contactPoints) + .port(port) + .localDatacenter(localDatacenter) + .username(username) + .password(password) + .build(); + } + +} diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoValidateArgs.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoValidateArgs.java new file mode 100644 index 00000000..e7fb4927 --- /dev/null +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/info/ClusterInfoValidateArgs.java @@ -0,0 +1,10 @@ +package kr.hakdang.cadio.core.domain.cluster.info; + +/** + * ClusterInfoValidateArgs + * + * @author akageun + * @since 2024-07-03 + */ +public class ClusterInfoValidateArgs { +} diff --git a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java index 8cff35da..0be79c0a 100644 --- a/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java +++ b/cadio-core/src/main/java/kr/hakdang/cadio/core/domain/cluster/keyspace/ClusterKeyspaceCommander.java @@ -33,7 +33,6 @@ public ClusterKeyspaceListResult keyspaceList(CqlSession session) { boolean wasApplied = resultSet.wasApplied(); List keyspaceList = new ArrayList<>(); for (Row row : resultSet.all()) { - log.info("row :{}", row.getFormattedContents()); keyspaceList.add( KeyspaceResult.builder() .keyspaceName(row.getString("keyspace_name")) diff --git a/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/ClusterInfoManagerTest.java b/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/ClusterConnectionManagerTest.java similarity index 75% rename from cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/ClusterInfoManagerTest.java rename to cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/ClusterConnectionManagerTest.java index f6b12aba..e7050e55 100644 --- a/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/ClusterInfoManagerTest.java +++ b/cadio-core/src/test/java/kr/hakdang/cadio/core/domain/cluster/ClusterConnectionManagerTest.java @@ -7,13 +7,13 @@ import org.springframework.beans.factory.annotation.Autowired; @Slf4j -class ClusterInfoManagerTest extends IntegrationTest { +class ClusterConnectionManagerTest extends IntegrationTest { @Autowired private ClusterInfoManager clusterInfoManager; @Test void registerTest() { - clusterInfoManager.register("127.0.0.1", 9042, "dc1", "", ""); + // clusterInfoManager.register("127.0.0.1", 9042, "dc1", "", ""); } } diff --git a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterApi.java b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterApi.java index 4905cab2..606da73c 100644 --- a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterApi.java +++ b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterApi.java @@ -2,8 +2,12 @@ import com.datastax.oss.driver.api.core.CqlSession; import jakarta.validation.Valid; +import kr.hakdang.cadio.core.domain.bootstrap.BootstrapProvider; +import kr.hakdang.cadio.core.domain.cluster.TempClusterConnector; +import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfo; import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoManager; import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoProvider; +import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoRegisterArgs; import kr.hakdang.cadio.web.common.dto.response.ApiResponse; import kr.hakdang.cadio.web.route.BaseSample; import lombok.AccessLevel; @@ -16,11 +20,14 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.UUID; import static java.util.Collections.emptyMap; @@ -35,19 +42,44 @@ @RequestMapping("/api/cassandra/cluster") public class ClusterApi extends BaseSample { - @Autowired - private ClusterInfoProvider clusterInfoProvider; + private final BootstrapProvider bootstrapProvider; + private final ClusterInfoProvider clusterInfoProvider; - @Autowired - private ClusterInfoManager clusterInfoManager; + private final ClusterInfoManager clusterInfoManager; + + private final TempClusterConnector tempClusterConnector; + + public ClusterApi( + BootstrapProvider bootstrapProvider, + ClusterInfoProvider clusterInfoProvider, + ClusterInfoManager clusterInfoManager, + TempClusterConnector tempClusterConnector + ) { + this.bootstrapProvider = bootstrapProvider; + this.clusterInfoProvider = clusterInfoProvider; + this.clusterInfoManager = clusterInfoManager; + this.tempClusterConnector = tempClusterConnector; + } @GetMapping("") - public Map getCassandraClusterList() { - return emptyMap(); + public ApiResponse> getCassandraClusterList( + @RequestParam(required = false, defaultValue = "false") boolean withPassword + ) { + Map result = new HashMap<>(); + List clusters = clusterInfoProvider.getList(); + if (!withPassword) { + clusters = clusters.stream() + .map(info -> info.toBuilder().password(null).build()) + .toList(); + } + + result.put("clusters", clusters); + + return ApiResponse.ok(result); } @GetMapping("/{clusterId}") - public Map getCassandraClusterDetail( + public ApiResponse> getCassandraClusterDetail( @PathVariable String clusterId ) { Map result = new HashMap<>(); @@ -57,27 +89,28 @@ public Map getCassandraClusterDetail( log.error("error : {}", e.getMessage(), e); throw e; } - return emptyMap(); + return ApiResponse.ok(emptyMap()); } @PostMapping("") public ApiResponse> clusterRegister( - @RequestBody ClusterRegisterRequest request + @Valid @RequestBody ClusterRegisterRequest request ) { - clusterInfoManager.register( - request.contactPoints, request.port, request.localDatacenter, request.username, request.password - ); + try ( + CqlSession session = tempClusterConnector.makeSession(request.makeClusterConnector()); + ) { + String clusterName = session.getMetadata().getClusterName() + .orElse(UUID.randomUUID().toString()); + + ClusterInfoRegisterArgs args = request.makeArgs(clusterName); + //실행 안되면 exception + + clusterInfoManager.register(args); + + bootstrapProvider.updateMinClusterCountCheck(clusterInfoProvider.checkMinClusterCount()); + } return ApiResponse.ok(emptyMap()); } - @Getter - @NoArgsConstructor(access = AccessLevel.PRIVATE) - public static class ClusterRegisterRequest { - private String contactPoints; - private int port; - private String localDatacenter; - private String username; - private String password; - } } diff --git a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterRegisterRequest.java b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterRegisterRequest.java new file mode 100644 index 00000000..a31ab945 --- /dev/null +++ b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterRegisterRequest.java @@ -0,0 +1,54 @@ +package kr.hakdang.cadio.web.route.cluster; + +import kr.hakdang.cadio.core.domain.cluster.ClusterConnection; +import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoRegisterArgs; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * ClusterRegisterRequest + * + * @author akageun + * @since 2024-07-03 + */ +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ClusterRegisterRequest { + private String contactPoints; + private int port; + private String localDatacenter; + private String username; + private String password; + + @Builder + public ClusterRegisterRequest(String contactPoints, int port, String localDatacenter, String username, String password) { + this.contactPoints = contactPoints; + this.port = port; + this.localDatacenter = localDatacenter; + this.username = username; + this.password = password; + } + + public ClusterInfoRegisterArgs makeArgs(String clusterName) { + return ClusterInfoRegisterArgs.builder() + .clusterName(clusterName) + .contactPoints(contactPoints) + .port(port) + .localDatacenter(localDatacenter) + .username(username) + .password(password) + .build(); + } + + public ClusterConnection makeClusterConnector() { + return ClusterConnection.builder() + .contactPoints(contactPoints) + .port(port) + .localDatacenter(localDatacenter) + .username(username) + .password(password) + .build(); + } +} diff --git a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterKeyspaceApi.java b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/keyspace/ClusterKeyspaceApi.java similarity index 54% rename from cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterKeyspaceApi.java rename to cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/keyspace/ClusterKeyspaceApi.java index 875d4a90..0bea921b 100644 --- a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterKeyspaceApi.java +++ b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/keyspace/ClusterKeyspaceApi.java @@ -1,17 +1,18 @@ -package kr.hakdang.cadio.web.route.cluster; +package kr.hakdang.cadio.web.route.cluster.keyspace; import com.datastax.oss.driver.api.core.CqlSession; +import kr.hakdang.cadio.core.domain.cluster.TempClusterConnector; import kr.hakdang.cadio.core.domain.cluster.keyspace.ClusterKeyspaceCommander; import kr.hakdang.cadio.core.domain.cluster.keyspace.ClusterKeyspaceDescribeArgs; +import kr.hakdang.cadio.core.domain.cluster.keyspace.ClusterKeyspaceListResult; +import kr.hakdang.cadio.web.common.dto.response.ApiResponse; import kr.hakdang.cadio.web.route.BaseSample; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -26,24 +27,29 @@ @RequestMapping("/api/cassandra/cluster/{clusterId}") public class ClusterKeyspaceApi extends BaseSample { - @Autowired - private ClusterKeyspaceCommander clusterKeyspaceCommander; + private final TempClusterConnector tempClusterConnector; + private final ClusterKeyspaceCommander clusterKeyspaceCommander; + public ClusterKeyspaceApi( + TempClusterConnector tempClusterConnector, + ClusterKeyspaceCommander clusterKeyspaceCommander + ) { + this.tempClusterConnector = tempClusterConnector; + this.clusterKeyspaceCommander = clusterKeyspaceCommander; + } @GetMapping("/keyspace") - public Map keyspaceList( + public ApiResponse> keyspaceList( @PathVariable String clusterId ) { Map result = new HashMap<>(); - try (CqlSession session = makeSession()) { //TODO : interface 작업할 때 facade layer 로 변경 예정 - result.put("result", clusterKeyspaceCommander.keyspaceList(session)); + try (CqlSession session = tempClusterConnector.makeSession(clusterId)) { //TODO : interface 작업할 때 facade layer 로 변경 예정 + ClusterKeyspaceListResult result1 = clusterKeyspaceCommander.keyspaceList(session); - } catch (Exception e) { - log.error("error : {}", e.getMessage(), e); - throw e; + result.put("keyspaceList", result1.getKeyspaceList()); } - return result; + return ApiResponse.ok(result); } @GetMapping("/keyspace/{keyspace}") @@ -52,16 +58,13 @@ public Map keyspaceDetail( @PathVariable String keyspace ) { Map result = new HashMap<>(); - try (CqlSession session = makeSession()) { //TODO : interface 작업할 때 facade layer 로 변경 예정 + try (CqlSession session = tempClusterConnector.makeSession(clusterId)) { //TODO : interface 작업할 때 facade layer 로 변경 예정 result.put("describe", clusterKeyspaceCommander.describe(session, ClusterKeyspaceDescribeArgs.builder() .keyspace(keyspace) .withChildren(false) .pretty(true) .build())); - } catch (Exception e) { - log.error("error : {}", e.getMessage(), e); - throw e; } return result; diff --git a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterQueryApi.java b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/query/ClusterQueryApi.java similarity index 63% rename from cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterQueryApi.java rename to cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/query/ClusterQueryApi.java index 32e45a73..2645d2d9 100644 --- a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/ClusterQueryApi.java +++ b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/query/ClusterQueryApi.java @@ -1,8 +1,9 @@ -package kr.hakdang.cadio.web.route.cluster; +package kr.hakdang.cadio.web.route.cluster.query; import com.datastax.oss.driver.api.core.CqlSession; import kr.hakdang.cadio.core.domain.cluster.ClusterQueryCommander; import kr.hakdang.cadio.core.domain.cluster.ClusterQueryCommanderResult; +import kr.hakdang.cadio.core.domain.cluster.TempClusterConnector; import kr.hakdang.cadio.web.common.dto.response.ApiResponse; import kr.hakdang.cadio.web.route.BaseSample; import lombok.AccessLevel; @@ -10,6 +11,7 @@ import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -18,8 +20,6 @@ import java.util.HashMap; import java.util.Map; -import static java.util.Collections.emptyMap; - /** * ClusterQueryApi * @@ -31,35 +31,33 @@ @RequestMapping("/api/cassandra/cluster") public class ClusterQueryApi extends BaseSample { - @Autowired - private ClusterQueryCommander clusterQueryCommander; + private final TempClusterConnector tempClusterConnector; + private final ClusterQueryCommander clusterQueryCommander; - @PostMapping("/query") + public ClusterQueryApi( + TempClusterConnector tempClusterConnector, + ClusterQueryCommander clusterQueryCommander + ) { + this.tempClusterConnector = tempClusterConnector; + this.clusterQueryCommander = clusterQueryCommander; + } + + @PostMapping("/{clusterId}/query") public ApiResponse> clusterQueryCommand( + @PathVariable String clusterId, @RequestBody ClusterQueryRequest request ) { - Map map = new HashMap<>(); - try (CqlSession session = makeSession()) { //TODO : interface 작업할 때 facade layer 로 변경 예정 - ClusterQueryCommanderResult result1 = clusterQueryCommander.execute(session, request.getQuery(), request.getNextToken()); + + try (CqlSession session = tempClusterConnector.makeSession(clusterId)) { //TODO : interface 작업할 때 facade layer 로 변경 예정 + ClusterQueryCommanderResult result1 = clusterQueryCommander.execute(session, request.getQuery(), request.getNextCursor()); map.put("wasApplied", result1.isWasApplied()); map.put("nextToken", result1.getNextToken()); map.put("rows", result1.getRows()); map.put("columnNames", result1.getColumnNames()); - } catch (Exception e) { - log.error("error : {}", e.getMessage(), e); - throw e; } - return ApiResponse.ok(map); } - - @Getter - @NoArgsConstructor(access = AccessLevel.PRIVATE) - public static class ClusterQueryRequest { - private String query; - private String nextToken; - } } diff --git a/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/query/ClusterQueryRequest.java b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/query/ClusterQueryRequest.java new file mode 100644 index 00000000..08059b9d --- /dev/null +++ b/cadio-web/src/main/java/kr/hakdang/cadio/web/route/cluster/query/ClusterQueryRequest.java @@ -0,0 +1,21 @@ +package kr.hakdang.cadio.web.route.cluster.query; + +import jakarta.validation.constraints.NotBlank; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * ClusterQueryRequest + * + * @author akageun + * @since 2024-07-03 + */ +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ClusterQueryRequest { + @NotBlank + private String query; + + private String nextCursor; +} diff --git a/cadio-web/src/main/webapp/package.json b/cadio-web/src/main/webapp/package.json index d008ef62..cbd22136 100644 --- a/cadio-web/src/main/webapp/package.json +++ b/cadio-web/src/main/webapp/package.json @@ -1,15 +1,15 @@ { - "name": "cassandra-web-console-ui", + "name": "cadio-ui", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", - "@types/jest": "^27.5.2", - "@types/node": "^16.18.29", - "@types/react": "^18.2.6", - "@types/react-dom": "^18.2.4", + "@types/jest": "^27.5.2", + "@types/node": "^16.18.29", + "@types/react": "^18.2.6", + "@types/react-dom": "^18.2.4", "axios": "^1.2.4", "dayjs": "^1.11.7", "bootstrap": "^5.3.3", @@ -48,7 +48,7 @@ ] }, "proxy": "http://localhost:20000", - "devDependencies": { - "@types/bootstrap": "^5.2.10" - } + "devDependencies": { + "@types/bootstrap": "^5.2.10" + } } diff --git a/cadio-web/src/main/webapp/src/pages/cluster/cluster-view.js b/cadio-web/src/main/webapp/src/pages/cluster/cluster-view.js index 9b7b55ad..7e6f50ae 100644 --- a/cadio-web/src/main/webapp/src/pages/cluster/cluster-view.js +++ b/cadio-web/src/main/webapp/src/pages/cluster/cluster-view.js @@ -1,6 +1,5 @@ import {Link, useParams} from "react-router-dom"; import {useEffect} from "react"; -import QueryEditor from "./components/query/query-editor"; import useCluster from "./hooks/useCluster"; import {useClusterState} from "./context/clusterContext"; @@ -16,8 +15,6 @@ const ClusterView = (props) => { useEffect(() => { //show component - console.log("routeParams ", routeParams.clusterId) - doGetKeyspaceList(); return () => { @@ -39,20 +36,23 @@ const ClusterView = (props) => {
  • - + Cluster Home
  • - + Query Editor
  • - + Metrics
  • @@ -71,7 +71,7 @@ const ClusterView = (props) => { Loading...
: - keyspaceList.map((info, infoIndex) => { + keyspaceList && keyspaceList.length > 0 && keyspaceList.map((info, infoIndex) => { return (
  • {
    • - + Settings
    • - + Sign out
    • diff --git a/cadio-web/src/main/webapp/src/pages/cluster/components/cluster-home.js b/cadio-web/src/main/webapp/src/pages/cluster/components/cluster-home.js index 4f172052..78a82dbe 100644 --- a/cadio-web/src/main/webapp/src/pages/cluster/components/cluster-home.js +++ b/cadio-web/src/main/webapp/src/pages/cluster/components/cluster-home.js @@ -1,5 +1,4 @@ import {Link, useParams} from "react-router-dom"; -import QueryEditor from "./query/query-editor"; import {useClusterState} from "../context/clusterContext"; import {useEffect} from "react"; @@ -16,8 +15,6 @@ const ClusterHome = () => { useEffect(() => { //show component - console.log("routeParams ", routeParams.clusterId) - return () => { //hide component @@ -30,7 +27,8 @@ const ClusterHome = () => {