Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an update to the cache each time their is an interaction with one of the environments. #1951

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions core/src/main/java/io/aiven/klaw/helpers/UtilMethods.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,4 +70,37 @@ public static TopicConfigurationRequest createTopicConfigurationRequestFromJson(
}
return new TopicConfigurationRequest();
}

public static void updateEnvStatus(
ResponseEntity<ApiResponse> 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<Env> 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);
}
}
}
18 changes: 4 additions & 14 deletions core/src/main/java/io/aiven/klaw/service/AclControllerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -764,6 +752,8 @@ private ResponseEntity<ApiResponse> invokeClusterApiAclRequest(int tenantId, Acl
}
}

updateEnvStatus(response, manageDatabase, tenantId, aclReq.getEnvironment());

return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -1173,7 +1175,7 @@ public ApiResponse resetConsumerOffsets(
HttpMethod.POST,
request,
new ParameterizedTypeReference<>() {});

updateEnvStatus(response, manageDatabase, tenantId, environmentId);
return response.getBody();

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -245,6 +240,7 @@ public ApiResponse execSchemaRequests(String avroSchemaId) throws KlawException
ResponseEntity<ApiResponse> response =
clusterApiService.postSchema(
schemaRequest, schemaRequest.getEnvironment(), schemaRequest.getTopicname(), tenantId);
updateEnvStatus(response, manageDatabase, tenantId, schemaRequest.getEnvironment());
HandleDbRequests dbHandle = manageDatabase.getHandleDbRequests();
ApiResponse apiResponse = response.getBody();
Map<String, Object> registerSchemaCustomResponse = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -819,6 +809,7 @@ private String invokeClusterApiForTopicRequest(
TOPIC_REQUEST_APPROVED,
commonUtilsService.getLoginUrl());
}
updateEnvStatus(response, manageDatabase, tenantId, topicRequest.getEnvironment());
return updateTopicReqStatus;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ public void approveConnectorRequestsSuccess() throws KlawException, KlawRestExce
"1",
"",
"1",
"2",
101);
assertThat(Objects.requireNonNull(response1)).isEqualTo(ApiResultStatus.SUCCESS.value);
}
Expand Down Expand Up @@ -514,6 +515,7 @@ public void approveConnectorRequests_ISE() throws KlawException, KlawRestExcepti
"1",
"",
"1",
"2",
101);
assertThat(Objects.requireNonNull(response1)).isEqualTo(FAILED_TO_EXECUTE_SUCCESSFULLY);
}
Expand Down