Skip to content

Commit

Permalink
Use KafkaClustersType as enum rather than as String (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin authored Oct 17, 2022
1 parent 6bdca45 commit 24344d6
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 66 deletions.
20 changes: 10 additions & 10 deletions src/main/java/io/aiven/klaw/config/ManageDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,13 @@ public Map<Integer, String> getTenantMap() {
return tenantMap;
}

public Map<Integer, KwClusters> getClusters(String clusterType, int tenantId) {
public Map<Integer, KwClusters> getClusters(KafkaClustersType clusterType, int tenantId) {
switch (clusterType) {
case "schemaregistry":
case SCHEMA_REGISTRY:
return kwSchemaRegClustersPertenant.get(tenantId);
case "kafkaconnect":
case KAFKA_CONNECT:
return kwKafkaConnectClustersPertenant.get(tenantId);
case "kafka":
case KAFKA:
return kwKafkaClustersPertenant.get(tenantId);
default:
return kwAllClustersPertenant.get(tenantId);
Expand Down Expand Up @@ -506,11 +506,11 @@ private void loadClustersForAllTenants() {
kwAllClustersPertenant = new HashMap<>();

for (Integer tenantId : tenantMap.keySet()) {
kafkaClusters = handleDbRequests.getAllClusters(KafkaClustersType.KAFKA.value, tenantId);
kafkaClusters = handleDbRequests.getAllClusters(KafkaClustersType.KAFKA, tenantId);
schemaRegistryClusters =
handleDbRequests.getAllClusters(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId);
handleDbRequests.getAllClusters(KafkaClustersType.SCHEMA_REGISTRY, tenantId);
kafkaConnectClusters =
handleDbRequests.getAllClusters(KafkaClustersType.KAFKA_CONNECT.value, tenantId);
handleDbRequests.getAllClusters(KafkaClustersType.KAFKA_CONNECT, tenantId);

loadClustersForOneTenant(
kafkaClusters, schemaRegistryClusters, kafkaConnectClusters, tenantId);
Expand All @@ -523,15 +523,15 @@ public void loadClustersForOneTenant(
List<KwClusters> kafkaConnectClusters,
Integer tenantId) {
if (kafkaClusters == null) {
kafkaClusters = handleDbRequests.getAllClusters(KafkaClustersType.KAFKA.value, tenantId);
kafkaClusters = handleDbRequests.getAllClusters(KafkaClustersType.KAFKA, tenantId);
}
if (schemaRegistryClusters == null) {
schemaRegistryClusters =
handleDbRequests.getAllClusters(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId);
handleDbRequests.getAllClusters(KafkaClustersType.SCHEMA_REGISTRY, tenantId);
}
if (kafkaConnectClusters == null) {
kafkaConnectClusters =
handleDbRequests.getAllClusters(KafkaClustersType.KAFKA_CONNECT.value, tenantId);
handleDbRequests.getAllClusters(KafkaClustersType.KAFKA_CONNECT, tenantId);
}

Map<Integer, KwClusters> kwKafkaClusters = new HashMap<>();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/aiven/klaw/helpers/HandleDbRequests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.aiven.klaw.dao.Topic;
import io.aiven.klaw.dao.TopicRequest;
import io.aiven.klaw.dao.UserInfo;
import io.aiven.klaw.model.KafkaClustersType;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -197,7 +198,7 @@ List<AclRequests> getCreatedAclRequestsByStatus(

List<KwRolesPermissions> getRolesPermissionsPerTenant(int tenantId);

List<KwClusters> getAllClusters(String typeOfCluster, int tenantId);
List<KwClusters> getAllClusters(KafkaClustersType typeOfCluster, int tenantId);

KwClusters getClusterDetails(int id, int tenantId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,22 +350,22 @@ public List<Acl> getAllConsumerGroups(int tenantId) {

@Override
public List<Env> selectAllEnvs(int tenantId) {
return jdbcSelectHelper.selectAllEnvs("all", tenantId);
return jdbcSelectHelper.selectAllEnvs(KafkaClustersType.ALL, tenantId);
}

@Override
public List<Env> selectAllKafkaEnvs(int tenantId) {
return jdbcSelectHelper.selectAllEnvs(KafkaClustersType.KAFKA.value, tenantId);
return jdbcSelectHelper.selectAllEnvs(KafkaClustersType.KAFKA, tenantId);
}

@Override
public List<Env> selectAllSchemaRegEnvs(int tenantId) {
return jdbcSelectHelper.selectAllEnvs(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId);
return jdbcSelectHelper.selectAllEnvs(KafkaClustersType.SCHEMA_REGISTRY, tenantId);
}

@Override
public List<Env> selectAllKafkaConnectEnvs(int tenantId) {
return jdbcSelectHelper.selectAllEnvs(KafkaClustersType.KAFKA_CONNECT.value, tenantId);
return jdbcSelectHelper.selectAllEnvs(KafkaClustersType.KAFKA_CONNECT, tenantId);
}

@Override
Expand Down Expand Up @@ -434,7 +434,7 @@ public List<KwRolesPermissions> getRolesPermissionsPerTenant(int tenantId) {
}

@Override
public List<KwClusters> getAllClusters(String typeOfCluster, int tenantId) {
public List<KwClusters> getAllClusters(KafkaClustersType typeOfCluster, int tenantId) {
return jdbcSelectHelper.getAllClusters(typeOfCluster, tenantId);
}

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/io/aiven/klaw/helpers/db/rdbms/SelectDataJdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.aiven.klaw.dao.*;
import io.aiven.klaw.model.AclPatternType;
import io.aiven.klaw.model.AclType;
import io.aiven.klaw.model.KafkaClustersType;
import io.aiven.klaw.model.RequestOperationType;
import io.aiven.klaw.model.TopicRequestTypes;
import io.aiven.klaw.repository.*;
Expand Down Expand Up @@ -499,11 +500,11 @@ public List<UserInfo> selectAllUsersInfoForTeam(Integer teamId, int tenantId) {
return userInfoRepo.findAllByTeamIdAndTenantId(teamId, tenantId);
}

public List<Env> selectAllEnvs(String type, int tenantId) {
if ("all".equals(type)) {
return Lists.newArrayList(envRepo.findAllByTenantId(tenantId));
public List<Env> selectAllEnvs(KafkaClustersType type, int tenantId) {
if (KafkaClustersType.ALL == type) {
return envRepo.findAllByTenantId(tenantId);
} else {
return Lists.newArrayList(envRepo.findAllByTypeAndTenantId(type, tenantId));
return envRepo.findAllByTypeAndTenantId(type.value, tenantId);
}
}

Expand Down Expand Up @@ -949,12 +950,11 @@ public List<KwRolesPermissions> getRolesPermissionsPerTenant(int tenantId) {
return kwRolesPermsRepo.findAllByTenantId(tenantId);
}

public List<KwClusters> getAllClusters(String typeOfCluster, int tenantId) {
if ("all".equals(typeOfCluster)) {
return Lists.newArrayList(kwClusterRepo.findAllByTenantId(tenantId));
public List<KwClusters> getAllClusters(KafkaClustersType typeOfCluster, int tenantId) {
if (KafkaClustersType.ALL == typeOfCluster) {
return kwClusterRepo.findAllByTenantId(tenantId);
} else {
return Lists.newArrayList(
kwClusterRepo.findAllByClusterTypeAndTenantId(typeOfCluster, tenantId));
return kwClusterRepo.findAllByClusterTypeAndTenantId(typeOfCluster.value, tenantId);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/aiven/klaw/model/EnvModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public class EnvModel implements Serializable {

private String totalNoPages;
private List<String> allPageNos;

public void setClusterType(KafkaClustersType type) {
this.type = type.value;
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/aiven/klaw/model/KafkaClustersType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.aiven.klaw.model;

public enum KafkaClustersType {
ALL("all"),
KAFKA("kafka"),
SCHEMA_REGISTRY("schemaregistry"),
KAFKA_CONNECT("kafkaconnect");
Expand All @@ -10,4 +11,13 @@ public enum KafkaClustersType {
KafkaClustersType(String value) {
this.value = value;
}

public static KafkaClustersType of(String name) {
for (KafkaClustersType val : values()) {
if (val.value.equals(name)) {
return val;
}
}
throw new IllegalArgumentException("Unknown KafkaClustersType name " + name);
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/aiven/klaw/service/AclControllerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ private void updateAvroSchema(
try {
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId)
.getClusters(KafkaClustersType.SCHEMA_REGISTRY, tenantId)
.get(schemaEnv.getClusterId());
SortedMap<Integer, Map<String, Object>> schemaObjects =
clusterApiService.getAvroSchema(
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public List<Map<String, String>> getConsumerOffsets(
try {
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.KAFKA.value, tenantId)
.getClusters(KafkaClustersType.KAFKA, tenantId)
.get(getEnvDetails(envId, tenantId).getClusterId());
consumerOffsetInfoList =
clusterApiService.getConsumerOffsets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public List<AclInfo> getSyncAcls(
Env envSelected = getEnvDetails(env, tenantId);
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.KAFKA.value, tenantId)
.getClusters(KafkaClustersType.KAFKA, tenantId)
.get(envSelected.getClusterId());
aclList =
getAclListFromCluster(
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/aiven/klaw/service/ClusterApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public List<Map<String, String>> getAcls(
String uriGetAcls = "/topics/getAcls/";
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.KAFKA.value, tenantId)
.getClusters(KafkaClustersType.KAFKA, tenantId)
.get(envSelected.getClusterId());

String uri;
Expand Down Expand Up @@ -417,7 +417,7 @@ public ResponseEntity<ApiResponse> approveTopicRequests(
Env envSelected = manageDatabase.getHandleDbRequests().selectEnvDetails(topicEnvId, tenantId);
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.KAFKA.value, tenantId)
.getClusters(KafkaClustersType.KAFKA, tenantId)
.get(envSelected.getClusterId());
clusterTopicRequest =
ClusterTopicRequest.builder()
Expand Down Expand Up @@ -473,7 +473,7 @@ public ResponseEntity<ApiResponse> approveAclRequests(AclRequests aclReq, int te
Env envSelected = manageDatabase.getHandleDbRequests().selectEnvDetails(env, tenantId);
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.KAFKA.value, tenantId)
.getClusters(KafkaClustersType.KAFKA, tenantId)
.get(envSelected.getClusterId());

// aiven config
Expand Down Expand Up @@ -562,7 +562,7 @@ ResponseEntity<ApiResponse> postSchema(
Env envSelected = manageDatabase.getHandleDbRequests().selectEnvDetails(env, tenantId);
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId)
.getClusters(KafkaClustersType.SCHEMA_REGISTRY, tenantId)
.get(envSelected.getClusterId());
ClusterSchemaRequest clusterSchemaRequest =
ClusterSchemaRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.aiven.klaw.config.ManageDatabase;
import io.aiven.klaw.dao.Env;
import io.aiven.klaw.dao.KwClusters;
import io.aiven.klaw.model.KafkaClustersType;
import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -81,7 +82,9 @@ public void updateEnvsStatus() {
private void updateEnvStatusPerEnv(Integer tenantId, Env env) {
String status;
KwClusters kwClusters =
manageDatabase.getClusters(env.getType(), tenantId).get(env.getClusterId());
manageDatabase
.getClusters(KafkaClustersType.of(env.getType()), tenantId)
.get(env.getClusterId());
status =
clusterApiService.getKafkaClusterStatus(
kwClusters.getBootstrapServers(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public synchronized EnvModel getEnvDetails(String envSelected, String clusterTyp
copyProperties(env, envModel);
envModel.setClusterName(
manageDatabase
.getClusters(clusterType, tenantId)
.getClusters(KafkaClustersType.of(clusterType), tenantId)
.get(envModel.getClusterId())
.getClusterName());
envModel.setTenantName(manageDatabase.getTenantMap().get(envModel.getTenantId()));
Expand Down Expand Up @@ -156,7 +156,8 @@ public UserInfoModel getUserDetails(String userId) {
public List<KwClustersModel> getClusters(String typeOfCluster) {
int tenantId = commonUtilsService.getTenantId(getUserName());
List<KwClusters> clusters =
new ArrayList<>(manageDatabase.getClusters(typeOfCluster, tenantId).values());
new ArrayList<>(
manageDatabase.getClusters(KafkaClustersType.of(typeOfCluster), tenantId).values());
List<KwClustersModel> clustersModels = new ArrayList<>();
List<Env> allEnvList = manageDatabase.getAllEnvList(tenantId);
KwClustersModel tmpClusterModel;
Expand Down Expand Up @@ -296,7 +297,7 @@ public List<EnvModel> getEnvsForRequestTopicsCluster() {

String[] reqTopicsEnvs = requestTopicsEnvs.split(",");
List<Env> listEnvs = manageDatabase.getKafkaEnvList(tenantId);
List<EnvModel> envModelList = getEnvModels(listEnvs, KafkaClustersType.KAFKA.value, tenantId);
List<EnvModel> envModelList = getEnvModels(listEnvs, KafkaClustersType.KAFKA, tenantId);

envModelList =
envModelList.stream()
Expand Down Expand Up @@ -325,7 +326,7 @@ public List<EnvModel> getKafkaEnvs() {
int tenantId = getUserDetails(getUserName()).getTenantId();
String orderOfEnvs = mailService.getEnvProperty(tenantId, "ORDER_OF_ENVS");
List<Env> listEnvs = manageDatabase.getKafkaEnvList(tenantId);
List<EnvModel> envModelList = getEnvModels(listEnvs, KafkaClustersType.KAFKA.value, tenantId);
List<EnvModel> envModelList = getEnvModels(listEnvs, KafkaClustersType.KAFKA, tenantId);
envModelList.forEach(
envModel ->
envModel.setTenantName(manageDatabase.getTenantMap().get(envModel.getTenantId())));
Expand All @@ -351,8 +352,7 @@ public List<EnvModel> getConnectorEnvs() {
int tenantId = getUserDetails(getUserName()).getTenantId();
String orderOfEnvs = mailService.getEnvProperty(tenantId, "ORDER_OF_ENVS");
List<Env> listEnvs = manageDatabase.getKafkaConnectEnvList(tenantId);
List<EnvModel> envModelList =
getEnvModels(listEnvs, KafkaClustersType.KAFKA_CONNECT.value, tenantId);
List<EnvModel> envModelList = getEnvModels(listEnvs, KafkaClustersType.KAFKA_CONNECT, tenantId);

envModelList.forEach(
envModel ->
Expand Down Expand Up @@ -462,7 +462,8 @@ private List<EnvModel> getEnvModelsPaginated(String pageNo, List<EnvModel> envLi
return envListMapUpdated;
}

private List<EnvModel> getEnvModels(List<Env> listEnvs, String clusterType, int tenantId) {
private List<EnvModel> getEnvModels(
List<Env> listEnvs, KafkaClustersType clusterType, int tenantId) {
List<EnvModel> envModelList = new ArrayList<>();
EnvModel envModel;
KwClusters kwCluster;
Expand Down Expand Up @@ -495,7 +496,7 @@ public List<EnvModel> getSchemaRegEnvs() {
List<Env> listEnvs = manageDatabase.getSchemaRegEnvList(tenantId);

List<EnvModel> envModelList =
getEnvModels(listEnvs, KafkaClustersType.SCHEMA_REGISTRY.value, tenantId);
getEnvModels(listEnvs, KafkaClustersType.SCHEMA_REGISTRY, tenantId);

envModelList.forEach(
envModel ->
Expand Down Expand Up @@ -530,8 +531,7 @@ public List<EnvModel> getKafkaConnectEnvs() {
.collect(Collectors.toList());
}

List<EnvModel> envModelList =
getEnvModels(listEnvs, KafkaClustersType.KAFKA_CONNECT.value, tenantId);
List<EnvModel> envModelList = getEnvModels(listEnvs, KafkaClustersType.KAFKA_CONNECT, tenantId);

envModelList.forEach(
envModel ->
Expand Down Expand Up @@ -570,14 +570,14 @@ public List<EnvModel> getSchemaRegEnvsStatus() {
String status;

if (manageDatabase
.getClusters(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId)
.getClusters(KafkaClustersType.SCHEMA_REGISTRY, tenantId)
.get(oneEnv.getClusterId())
.getProtocol()
.equalsIgnoreCase("plaintext"))
status =
clusterApiService.getSchemaClusterStatus(
manageDatabase
.getClusters(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId)
.getClusters(KafkaClustersType.SCHEMA_REGISTRY, tenantId)
.get(oneEnv.getClusterId())
.getBootstrapServers(),
tenantId);
Expand All @@ -588,7 +588,7 @@ public List<EnvModel> getSchemaRegEnvsStatus() {
newListEnvs.add(oneEnv);
}

return getEnvModels(newListEnvs, KafkaClustersType.SCHEMA_REGISTRY.value, tenantId);
return getEnvModels(newListEnvs, KafkaClustersType.SCHEMA_REGISTRY, tenantId);
}

public ApiResponse addNewEnv(EnvModel newEnv) throws KlawException {
Expand Down Expand Up @@ -701,7 +701,7 @@ public ApiResponse addNewCluster(KwClustersModel kwClustersModel) {
AtomicBoolean clusterNameAlreadyExists = new AtomicBoolean(false);
if (kwClustersModel.getClusterId() == null) {
manageDatabase
.getClusters("all", tenantId)
.getClusters(KafkaClustersType.ALL, tenantId)
.forEach(
(k, v) -> {
if (Objects.equals(v.getClusterName(), kwClustersModel.getClusterName())
Expand Down Expand Up @@ -1187,7 +1187,9 @@ public Map<String, String> getUpdateEnvStatus(String envId) {
String status;
try {
KwClusters kwClusters =
manageDatabase.getClusters(env.getType(), tenantId).get(env.getClusterId());
manageDatabase
.getClusters(KafkaClustersType.of(env.getType()), tenantId)
.get(env.getClusterId());
status =
clusterApiService.getKafkaClusterStatus(
kwClusters.getBootstrapServers(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public ApiResponse approveConnectorRequests(String connectorId) throws KlawExcep
.selectEnvDetails(connectorRequest.getEnvironment(), tenantId);
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.KAFKA_CONNECT.value, tenantId)
.getClusters(KafkaClustersType.KAFKA_CONNECT, tenantId)
.get(envSelected.getClusterId());
String protocol = kwClusters.getProtocol();
String kafkaConnectHost = kwClusters.getBootstrapServers();
Expand Down
Loading

0 comments on commit 24344d6

Please sign in to comment.