From aab5eeb5a26b3f8bcfd4ed587c3b8bba45002708 Mon Sep 17 00:00:00 2001 From: zhiyaoPCCW <110662017+zhiyaoPCCW@users.noreply.github.com> Date: Tue, 12 Nov 2024 09:08:14 +0800 Subject: [PATCH] feat(sdk): push kraken version from control plane to other system (#104) * push kraken version from control plane to other system * merge conflict --------- Co-authored-by: zhiyaoPCCW Co-authored-by: kuangxiang20240501 <167289548+kuangxiang20240501@users.noreply.github.com> --- .../controller/mapper/SystemInfoMapper.java | 2 +- .../controller/service/SystemInfoService.java | 4 +- .../core/client/ClientEventTypeEnum.java | 1 + .../core}/entity/SystemInfoEntity.java | 3 +- .../operator/core/enums/MgmtEventType.java | 1 + .../core}/repo/SystemInfoRepository.java | 4 +- .../service/PushKrakenVersionService.java | 34 ++++++++++++ .../sync/service/PushMgmtEventService.java | 5 +- .../service/PushKrakenVersionServiceTest.java | 54 +++++++++++++++++++ 9 files changed, 100 insertions(+), 8 deletions(-) rename kraken-java-sdk/{kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller => kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core}/entity/SystemInfoEntity.java (91%) rename kraken-java-sdk/{kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller => kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core}/repo/SystemInfoRepository.java (73%) create mode 100644 kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java create mode 100644 kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/mapper/SystemInfoMapper.java b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/mapper/SystemInfoMapper.java index 0e54be57..b4a75b73 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/mapper/SystemInfoMapper.java +++ b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/mapper/SystemInfoMapper.java @@ -1,7 +1,7 @@ package com.consoleconnect.kraken.operator.controller.mapper; -import com.consoleconnect.kraken.operator.controller.entity.SystemInfoEntity; import com.consoleconnect.kraken.operator.controller.model.SystemInfo; +import com.consoleconnect.kraken.operator.core.entity.SystemInfoEntity; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/SystemInfoService.java b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/SystemInfoService.java index b6787a8a..1e9e3f27 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/SystemInfoService.java +++ b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/SystemInfoService.java @@ -1,21 +1,21 @@ package com.consoleconnect.kraken.operator.controller.service; import com.consoleconnect.kraken.operator.controller.entity.EnvironmentEntity; -import com.consoleconnect.kraken.operator.controller.entity.SystemInfoEntity; import com.consoleconnect.kraken.operator.controller.enums.SystemStateEnum; import com.consoleconnect.kraken.operator.controller.mapper.SystemInfoMapper; import com.consoleconnect.kraken.operator.controller.model.MgmtProperty; import com.consoleconnect.kraken.operator.controller.model.SystemInfo; import com.consoleconnect.kraken.operator.controller.repo.EnvironmentRepository; -import com.consoleconnect.kraken.operator.controller.repo.SystemInfoRepository; import com.consoleconnect.kraken.operator.core.dto.Tuple2; import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto; +import com.consoleconnect.kraken.operator.core.entity.SystemInfoEntity; import com.consoleconnect.kraken.operator.core.entity.UnifiedAssetEntity; import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum; import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum; import com.consoleconnect.kraken.operator.core.event.PlatformSettingCompletedEvent; import com.consoleconnect.kraken.operator.core.exception.KrakenException; import com.consoleconnect.kraken.operator.core.model.Metadata; +import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository; import com.consoleconnect.kraken.operator.core.repo.UnifiedAssetRepository; import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService; import com.consoleconnect.kraken.operator.core.toolkit.AssetsConstants; diff --git a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/client/ClientEventTypeEnum.java b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/client/ClientEventTypeEnum.java index 1a008b5f..67c58ce1 100644 --- a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/client/ClientEventTypeEnum.java +++ b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/client/ClientEventTypeEnum.java @@ -6,5 +6,6 @@ public enum ClientEventTypeEnum { CLIENT_DEPLOYMENT, CLIENT_SERVER_API, CLIENT_TEMPLATE_UPGRADE_RESULT, + CLIENT_SYSTEM_INFO, CLIENT_MAPPER_VERSION } diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/entity/SystemInfoEntity.java b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/entity/SystemInfoEntity.java similarity index 91% rename from kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/entity/SystemInfoEntity.java rename to kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/entity/SystemInfoEntity.java index be9d4353..9fab29be 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/entity/SystemInfoEntity.java +++ b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/entity/SystemInfoEntity.java @@ -1,6 +1,5 @@ -package com.consoleconnect.kraken.operator.controller.entity; +package com.consoleconnect.kraken.operator.core.entity; -import com.consoleconnect.kraken.operator.core.entity.AbstractEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Table; diff --git a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/enums/MgmtEventType.java b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/enums/MgmtEventType.java index 86f584ac..9e6a6f22 100644 --- a/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/enums/MgmtEventType.java +++ b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/enums/MgmtEventType.java @@ -3,5 +3,6 @@ public enum MgmtEventType { RESET, CLIENT_HEART_BEAT, + CLIENT_SYSTEM_INFO, TEMPLATE_UPGRADE_RESULT; } diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/repo/SystemInfoRepository.java b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/repo/SystemInfoRepository.java similarity index 73% rename from kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/repo/SystemInfoRepository.java rename to kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/repo/SystemInfoRepository.java index b146fee1..a4152daf 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/repo/SystemInfoRepository.java +++ b/kraken-java-sdk/kraken-java-sdk-core/src/main/java/com/consoleconnect/kraken/operator/core/repo/SystemInfoRepository.java @@ -1,6 +1,6 @@ -package com.consoleconnect.kraken.operator.controller.repo; +package com.consoleconnect.kraken.operator.core.repo; -import com.consoleconnect.kraken.operator.controller.entity.SystemInfoEntity; +import com.consoleconnect.kraken.operator.core.entity.SystemInfoEntity; import java.util.Optional; import java.util.UUID; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java new file mode 100644 index 00000000..5924e8f4 --- /dev/null +++ b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionService.java @@ -0,0 +1,34 @@ +package com.consoleconnect.kraken.operator.sync.service; + +import com.consoleconnect.kraken.operator.core.entity.MgmtEventEntity; +import com.consoleconnect.kraken.operator.core.enums.EventStatusType; +import com.consoleconnect.kraken.operator.core.enums.MgmtEventType; +import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository; +import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class PushKrakenVersionService { + private static final String KEY = "CONTROL_PLANE"; + private final SystemInfoRepository systemInfoRepository; + private final MgmtEventRepository eventRepository; + + @Scheduled(cron = "${app.cron-job.sync-system-info-from-control-plane:-}") + public void runIt() { + // produce sync kraken info event + systemInfoRepository + .findOneByKey(KEY) + .ifPresent( + systemInfoEntity -> { + MgmtEventEntity entity = new MgmtEventEntity(); + entity.setStatus(EventStatusType.WAIT_TO_SEND.name()); + entity.setPayload(systemInfoEntity); + entity.setResourceId(systemInfoEntity.getId().toString()); + entity.setEventType(MgmtEventType.CLIENT_SYSTEM_INFO.name()); + eventRepository.save(entity); + }); + } +} diff --git a/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushMgmtEventService.java b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushMgmtEventService.java index 29df2213..9caefa83 100644 --- a/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushMgmtEventService.java +++ b/kraken-java-sdk/kraken-java-sdk-sync/src/main/java/com/consoleconnect/kraken/operator/sync/service/PushMgmtEventService.java @@ -38,7 +38,10 @@ public class PushMgmtEventService extends KrakenServerConnector { } protected static final List QUERY_EVENT_TYPES = - List.of(MgmtEventType.TEMPLATE_UPGRADE_RESULT, MgmtEventType.CLIENT_HEART_BEAT); + List.of( + MgmtEventType.TEMPLATE_UPGRADE_RESULT, + MgmtEventType.CLIENT_HEART_BEAT, + MgmtEventType.CLIENT_SYSTEM_INFO); public PushMgmtEventService( SyncProperty appProperty, WebClient webClient, EventSinkService eventSinkService) { diff --git a/kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java b/kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java new file mode 100644 index 00000000..622123a5 --- /dev/null +++ b/kraken-java-sdk/kraken-java-sdk-sync/src/test/java/com/consoleconnect/kraken/operator/sync/service/PushKrakenVersionServiceTest.java @@ -0,0 +1,54 @@ +package com.consoleconnect.kraken.operator.sync.service; + +import static com.consoleconnect.kraken.operator.core.enums.MgmtEventType.CLIENT_SYSTEM_INFO; + +import com.consoleconnect.kraken.operator.core.entity.MgmtEventEntity; +import com.consoleconnect.kraken.operator.core.entity.SystemInfoEntity; +import com.consoleconnect.kraken.operator.core.enums.EventStatusType; +import com.consoleconnect.kraken.operator.core.repo.MgmtEventRepository; +import com.consoleconnect.kraken.operator.core.repo.SystemInfoRepository; +import com.consoleconnect.kraken.operator.sync.CustomConfig; +import com.consoleconnect.kraken.operator.test.AbstractIntegrationTest; +import com.consoleconnect.kraken.operator.test.MockIntegrationTest; +import java.util.List; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.test.context.ContextConfiguration; + +@MockIntegrationTest +@ContextConfiguration(classes = {CustomConfig.class}) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class PushKrakenVersionServiceTest extends AbstractIntegrationTest { + + @Autowired private PushKrakenVersionService pushKrakenVersionService; + @Autowired private SystemInfoRepository systemInfoRepository; + @Autowired private MgmtEventRepository mgmtEventRepository; + + @Test + void givenSystemInfoEvent_whenRun_thenPushSuccessfully() { + + // given exist event + SystemInfoEntity entity = new SystemInfoEntity(); + entity.setKey("CONTROL_PLANE"); + entity.setControlProductVersion("1.0.0"); + entity.setProductKey("Grace"); + entity.setStageAppVersion("1.0.0"); + entity.setProductionAppVersion("1.0.0"); + systemInfoRepository.save(entity); + // when + pushKrakenVersionService.runIt(); + // then + Page eventEntities = + mgmtEventRepository.findByEventTypeInAndStatus( + List.of(CLIENT_SYSTEM_INFO.name()), + EventStatusType.WAIT_TO_SEND.name(), + Pageable.ofSize(1)); + Assertions.assertEquals( + eventEntities.getContent().get(0).getResourceId(), entity.getId().toString()); + } +}