From 2ff098352ae936296df7fc3f1bc4aea6b4526c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aindri=C3=BA=20Lavelle?= <121855584+aindriu-aiven@users.noreply.github.com> Date: Thu, 2 Nov 2023 08:33:57 +0000 Subject: [PATCH] Add an update to the cache each time their is an interaction with one of the environments. (#1951) * Add an update to the cache each time their is an interaction with one of the environments. Signed-off-by: Aindriu Lavelle * Update to use parseInt instead of valueOf Signed-off-by: Aindriu Lavelle --------- Signed-off-by: Aindriu Lavelle --- .../io/aiven/klaw/helpers/UtilMethods.java | 43 +++++++++++++++++++ .../klaw/service/AclControllerService.java | 18 ++------ .../aiven/klaw/service/ClusterApiService.java | 6 ++- .../KafkaConnectControllerService.java | 1 + .../SchemaRegistryControllerService.java | 10 ++--- .../klaw/service/TopicControllerService.java | 15 ++----- .../klaw/service/ClusterApiServiceTest.java | 2 + 7 files changed, 60 insertions(+), 35 deletions(-) diff --git a/core/src/main/java/io/aiven/klaw/helpers/UtilMethods.java b/core/src/main/java/io/aiven/klaw/helpers/UtilMethods.java index 80873eb59c..0e15bb8fc3 100644 --- a/core/src/main/java/io/aiven/klaw/helpers/UtilMethods.java +++ b/core/src/main/java/io/aiven/klaw/helpers/UtilMethods.java @@ -1,10 +1,20 @@ package io.aiven.klaw.helpers; +import static io.aiven.klaw.helpers.KwConstants.DATE_TIME_DDMMMYYYY_HHMMSS_FORMATTER; + import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import io.aiven.klaw.config.ManageDatabase; +import io.aiven.klaw.dao.Env; +import io.aiven.klaw.model.ApiResponse; import io.aiven.klaw.model.TopicConfigurationRequest; +import io.aiven.klaw.model.enums.ClusterStatus; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.Map; +import java.util.Optional; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.oauth2.core.user.DefaultOAuth2User; @@ -60,4 +70,37 @@ public static TopicConfigurationRequest createTopicConfigurationRequestFromJson( } return new TopicConfigurationRequest(); } + + public static void updateEnvStatus( + ResponseEntity response, + ManageDatabase manageDatabase, + int tenantId, + String environmentId) { + + if (response.getStatusCode().is2xxSuccessful() + && (response.getBody() != null && response.getBody().isSuccess())) { + UtilMethods.updateLatestStatus( + ClusterStatus.ONLINE, manageDatabase, tenantId, Integer.parseInt(environmentId)); + } else { + if (response.getStatusCode().is5xxServerError()) { + UtilMethods.updateLatestStatus( + ClusterStatus.NOT_KNOWN, manageDatabase, tenantId, Integer.parseInt(environmentId)); + } + } + } + + public static void updateLatestStatus( + ClusterStatus clusterStatus, ManageDatabase manageDatabase, int tenantId, int envId) { + + LocalDateTime statusTime = LocalDateTime.now(ZoneOffset.UTC); + Optional opt = manageDatabase.getEnv(tenantId, envId); + + if (opt.isPresent()) { + Env e = opt.get(); + e.setEnvStatus(clusterStatus); + e.setEnvStatusTime(statusTime); + e.setEnvStatusTimeString(DATE_TIME_DDMMMYYYY_HHMMSS_FORMATTER.format(statusTime)); + manageDatabase.addEnvToCache(tenantId, e, false); + } + } } diff --git a/core/src/main/java/io/aiven/klaw/service/AclControllerService.java b/core/src/main/java/io/aiven/klaw/service/AclControllerService.java index 4d818f702b..08dfa58e64 100644 --- a/core/src/main/java/io/aiven/klaw/service/AclControllerService.java +++ b/core/src/main/java/io/aiven/klaw/service/AclControllerService.java @@ -9,6 +9,7 @@ import static io.aiven.klaw.error.KlawErrorMessages.ACL_ERR_107; import static io.aiven.klaw.error.KlawErrorMessages.REQ_ERR_101; import static io.aiven.klaw.helpers.KwConstants.REQUESTOR_SUBSCRIPTIONS; +import static io.aiven.klaw.helpers.UtilMethods.updateEnvStatus; import static io.aiven.klaw.model.enums.MailType.ACL_DELETE_REQUESTED; import static io.aiven.klaw.model.enums.MailType.ACL_REQUESTED; import static io.aiven.klaw.model.enums.MailType.ACL_REQUEST_APPROVED; @@ -29,20 +30,7 @@ import io.aiven.klaw.helpers.HandleDbRequests; import io.aiven.klaw.helpers.Pager; import io.aiven.klaw.model.ApiResponse; -import io.aiven.klaw.model.enums.AclIPPrincipleType; -import io.aiven.klaw.model.enums.AclPatternType; -import io.aiven.klaw.model.enums.AclType; -import io.aiven.klaw.model.enums.ApiResultStatus; -import io.aiven.klaw.model.enums.EntityType; -import io.aiven.klaw.model.enums.KafkaClustersType; -import io.aiven.klaw.model.enums.KafkaFlavors; -import io.aiven.klaw.model.enums.MailType; -import io.aiven.klaw.model.enums.MetadataOperationType; -import io.aiven.klaw.model.enums.Order; -import io.aiven.klaw.model.enums.PermissionType; -import io.aiven.klaw.model.enums.RequestEntityType; -import io.aiven.klaw.model.enums.RequestOperationType; -import io.aiven.klaw.model.enums.RequestStatus; +import io.aiven.klaw.model.enums.*; import io.aiven.klaw.model.requests.AclRequestsModel; import io.aiven.klaw.model.response.AclRequestsResponseModel; import io.aiven.klaw.model.response.OffsetDetails; @@ -764,6 +752,8 @@ private ResponseEntity invokeClusterApiAclRequest(int tenantId, Acl } } + updateEnvStatus(response, manageDatabase, tenantId, aclReq.getEnvironment()); + return response; } diff --git a/core/src/main/java/io/aiven/klaw/service/ClusterApiService.java b/core/src/main/java/io/aiven/klaw/service/ClusterApiService.java index 61d000c91e..4ccd2f6888 100644 --- a/core/src/main/java/io/aiven/klaw/service/ClusterApiService.java +++ b/core/src/main/java/io/aiven/klaw/service/ClusterApiService.java @@ -2,6 +2,7 @@ import static io.aiven.klaw.error.KlawErrorMessages.*; import static io.aiven.klaw.helpers.KwConstants.*; +import static io.aiven.klaw.helpers.UtilMethods.updateEnvStatus; import io.aiven.klaw.config.ManageDatabase; import io.aiven.klaw.dao.AclRequests; @@ -410,6 +411,7 @@ public String approveConnectorRequests( String connectorConfig, String kafkaConnectHost, String clusterIdentification, + String environemt, int tenantId) throws KlawException, KlawRestException { log.info("approveConnectorRequests {} {}", connectorConfig, kafkaConnectHost); @@ -443,7 +445,7 @@ public String approveConnectorRequests( response = getRestTemplate(null) .exchange(uri, HttpMethod.POST, request, new ParameterizedTypeReference<>() {}); - + updateEnvStatus(response, manageDatabase, tenantId, environemt); ApiResponse apiResponse = response.getBody(); if (apiResponse != null) { if (apiResponse.isSuccess()) { @@ -1173,7 +1175,7 @@ public ApiResponse resetConsumerOffsets( HttpMethod.POST, request, new ParameterizedTypeReference<>() {}); - + updateEnvStatus(response, manageDatabase, tenantId, environmentId); return response.getBody(); } catch (Exception e) { diff --git a/core/src/main/java/io/aiven/klaw/service/KafkaConnectControllerService.java b/core/src/main/java/io/aiven/klaw/service/KafkaConnectControllerService.java index 10e71ed41f..6445325b02 100644 --- a/core/src/main/java/io/aiven/klaw/service/KafkaConnectControllerService.java +++ b/core/src/main/java/io/aiven/klaw/service/KafkaConnectControllerService.java @@ -707,6 +707,7 @@ public ApiResponse approveConnectorRequests(String connectorId) jsonConnectorConfig, kafkaConnectHost, kwClusters.getClusterName() + kwClusters.getClusterId(), + connectorRequest.getEnvironment(), tenantId); if (Objects.equals(updateConnectorReqStatus, ApiResultStatus.SUCCESS.value)) { diff --git a/core/src/main/java/io/aiven/klaw/service/SchemaRegistryControllerService.java b/core/src/main/java/io/aiven/klaw/service/SchemaRegistryControllerService.java index 468f6d834a..b9ef3d06fd 100644 --- a/core/src/main/java/io/aiven/klaw/service/SchemaRegistryControllerService.java +++ b/core/src/main/java/io/aiven/klaw/service/SchemaRegistryControllerService.java @@ -9,6 +9,7 @@ import static io.aiven.klaw.error.KlawErrorMessages.SCHEMA_ERR_107; import static io.aiven.klaw.error.KlawErrorMessages.SCHEMA_ERR_108; import static io.aiven.klaw.error.KlawErrorMessages.SCHEMA_ERR_109; +import static io.aiven.klaw.helpers.UtilMethods.updateEnvStatus; import static io.aiven.klaw.model.enums.MailType.*; import static org.springframework.beans.BeanUtils.copyProperties; @@ -24,13 +25,7 @@ import io.aiven.klaw.helpers.HandleDbRequests; import io.aiven.klaw.helpers.Pager; import io.aiven.klaw.model.ApiResponse; -import io.aiven.klaw.model.enums.ApiResultStatus; -import io.aiven.klaw.model.enums.KafkaClustersType; -import io.aiven.klaw.model.enums.Order; -import io.aiven.klaw.model.enums.PermissionType; -import io.aiven.klaw.model.enums.RequestEntityType; -import io.aiven.klaw.model.enums.RequestOperationType; -import io.aiven.klaw.model.enums.RequestStatus; +import io.aiven.klaw.model.enums.*; import io.aiven.klaw.model.requests.SchemaPromotion; import io.aiven.klaw.model.requests.SchemaRequestModel; import io.aiven.klaw.model.response.BaseRequestsResponseModel; @@ -245,6 +240,7 @@ public ApiResponse execSchemaRequests(String avroSchemaId) throws KlawException ResponseEntity response = clusterApiService.postSchema( schemaRequest, schemaRequest.getEnvironment(), schemaRequest.getTopicname(), tenantId); + updateEnvStatus(response, manageDatabase, tenantId, schemaRequest.getEnvironment()); HandleDbRequests dbHandle = manageDatabase.getHandleDbRequests(); ApiResponse apiResponse = response.getBody(); Map registerSchemaCustomResponse = null; diff --git a/core/src/main/java/io/aiven/klaw/service/TopicControllerService.java b/core/src/main/java/io/aiven/klaw/service/TopicControllerService.java index 9c18cb45dd..c7a8724d7b 100644 --- a/core/src/main/java/io/aiven/klaw/service/TopicControllerService.java +++ b/core/src/main/java/io/aiven/klaw/service/TopicControllerService.java @@ -17,6 +17,7 @@ import static io.aiven.klaw.error.KlawErrorMessages.TOPICS_ERR_114; import static io.aiven.klaw.error.KlawErrorMessages.TOPICS_VLD_ERR_121; import static io.aiven.klaw.helpers.KwConstants.ORDER_OF_TOPIC_ENVS; +import static io.aiven.klaw.helpers.UtilMethods.updateEnvStatus; import static io.aiven.klaw.model.enums.MailType.*; import static org.springframework.beans.BeanUtils.copyProperties; @@ -44,18 +45,7 @@ import io.aiven.klaw.model.TopicConfiguration; import io.aiven.klaw.model.TopicConfigurationRequest; import io.aiven.klaw.model.TopicInfo; -import io.aiven.klaw.model.enums.AclPatternType; -import io.aiven.klaw.model.enums.AclType; -import io.aiven.klaw.model.enums.ApiResultStatus; -import io.aiven.klaw.model.enums.EntityType; -import io.aiven.klaw.model.enums.KafkaClustersType; -import io.aiven.klaw.model.enums.MailType; -import io.aiven.klaw.model.enums.MetadataOperationType; -import io.aiven.klaw.model.enums.Order; -import io.aiven.klaw.model.enums.PermissionType; -import io.aiven.klaw.model.enums.RequestEntityType; -import io.aiven.klaw.model.enums.RequestOperationType; -import io.aiven.klaw.model.enums.RequestStatus; +import io.aiven.klaw.model.enums.*; import io.aiven.klaw.model.requests.TopicRequestModel; import io.aiven.klaw.model.response.TopicConfig; import io.aiven.klaw.model.response.TopicDetailsPerEnv; @@ -819,6 +809,7 @@ private String invokeClusterApiForTopicRequest( TOPIC_REQUEST_APPROVED, commonUtilsService.getLoginUrl()); } + updateEnvStatus(response, manageDatabase, tenantId, topicRequest.getEnvironment()); return updateTopicReqStatus; } diff --git a/core/src/test/java/io/aiven/klaw/service/ClusterApiServiceTest.java b/core/src/test/java/io/aiven/klaw/service/ClusterApiServiceTest.java index a234e65495..0cd3e9bf0d 100644 --- a/core/src/test/java/io/aiven/klaw/service/ClusterApiServiceTest.java +++ b/core/src/test/java/io/aiven/klaw/service/ClusterApiServiceTest.java @@ -481,6 +481,7 @@ public void approveConnectorRequestsSuccess() throws KlawException, KlawRestExce "1", "", "1", + "2", 101); assertThat(Objects.requireNonNull(response1)).isEqualTo(ApiResultStatus.SUCCESS.value); } @@ -514,6 +515,7 @@ public void approveConnectorRequests_ISE() throws KlawException, KlawRestExcepti "1", "", "1", + "2", 101); assertThat(Objects.requireNonNull(response1)).isEqualTo(FAILED_TO_EXECUTE_SUCCESSFULLY); }