From 3f72b944c8caeca6ac1fada8c0fff703babcc02c Mon Sep 17 00:00:00 2001 From: WangLiNaruto <122504203+WangLiNaruto@users.noreply.github.com> Date: Thu, 26 Sep 2024 19:59:14 +0800 Subject: [PATCH] create API method to get current configuration of the service (#1979) --- .../controllers/ServiceConfigurationApi.java | 28 +++++++++++++++++++ .../ServiceConfigurationExceptionHandler.java | 13 +++++++++ .../ServiceConfigurationEntity.java | 2 +- .../database/utils/EntityTransUtils.java | 15 ++++++++++ .../ServiceConfigurationManager.java | 25 +++++++++++++++++ .../modules/models/response/ResultType.java | 3 +- .../ServiceConfigurationDetails.java | 22 +++++++++++++++ ...ServiceConfigurationNotFoundException.java | 15 ++++++++++ ...urationUpdateRequestNotFoundException.java | 4 +-- 9 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/ServiceConfigurationDetails.java create mode 100644 modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationNotFoundException.java diff --git a/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceConfigurationApi.java b/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceConfigurationApi.java index e3038e952..8ffc625a0 100644 --- a/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceConfigurationApi.java +++ b/modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceConfigurationApi.java @@ -20,6 +20,7 @@ import org.eclipse.xpanse.modules.database.serviceconfiguration.update.ServiceConfigurationUpdateRequest; import org.eclipse.xpanse.modules.deployment.ServiceConfigurationManager; import org.eclipse.xpanse.modules.models.service.order.ServiceOrder; +import org.eclipse.xpanse.modules.models.serviceconfiguration.ServiceConfigurationDetails; import org.eclipse.xpanse.modules.models.serviceconfiguration.ServiceConfigurationUpdate; import org.eclipse.xpanse.modules.models.serviceconfiguration.enums.ServiceConfigurationStatus; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -51,6 +52,33 @@ public class ServiceConfigurationApi { @Resource private ServiceConfigurationManager serviceConfigurationManager; + /** + * Query the service's current configuration by id of the deployed service. + * + * @param serviceId id of the deployed service. + * @return ServiceConfigurationEntity. + */ + @Tag(name = "ServiceConfiguration", + description = "APIs for managing service's configuration.") + @GetMapping(value = "/service/current/config/{serviceId}", + produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseStatus(HttpStatus.OK) + @Operation(description = "Query the service's current configuration by" + + " id of the deployed service.") + @AuditApiRequest(methodName = "getCspFromServiceId") + public ServiceConfigurationDetails getCurrentConfigurationOfService( + @Parameter(name = "serviceId", description = "The id of the deployed service") + @PathVariable("serviceId") String serviceId) { + return serviceConfigurationManager.getCurrentConfigurationOfService(serviceId); + } + + /** + * Update the service's configuration to the registered service template. + * + * @param serviceId id of the deployed service + * @param serviceConfigurationUpdate serviceConfigurationUpdate. + * @return serviceOrder. + */ @Tag(name = "ServiceConfiguration", description = "APIs for managing service's configuration.") @PutMapping(value = "/services/config/{serviceId}", diff --git a/modules/api/src/main/java/org/eclipse/xpanse/api/exceptions/handler/ServiceConfigurationExceptionHandler.java b/modules/api/src/main/java/org/eclipse/xpanse/api/exceptions/handler/ServiceConfigurationExceptionHandler.java index 3b8b12844..436d2a819 100644 --- a/modules/api/src/main/java/org/eclipse/xpanse/api/exceptions/handler/ServiceConfigurationExceptionHandler.java +++ b/modules/api/src/main/java/org/eclipse/xpanse/api/exceptions/handler/ServiceConfigurationExceptionHandler.java @@ -14,6 +14,7 @@ import org.eclipse.xpanse.modules.models.response.Response; import org.eclipse.xpanse.modules.models.response.ResultType; import org.eclipse.xpanse.modules.models.serviceconfiguration.exceptions.ServiceConfigurationInvalidException; +import org.eclipse.xpanse.modules.models.serviceconfiguration.exceptions.ServiceConfigurationNotFoundException; import org.eclipse.xpanse.modules.models.serviceconfiguration.exceptions.ServiceConfigurationUpdateRequestNotFoundException; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; @@ -54,4 +55,16 @@ public Response handleServiceConfigurationUpdateRequestNotFoundException( return getErrorResponse(ResultType.SERVICE_CONFIG_UPDATE_REQUEST_NOT_FOUND, Collections.singletonList(ex.getMessage())); } + + /** + * Exception handler for ServiceConfigurationNotFoundException. + */ + @ExceptionHandler({ServiceConfigurationNotFoundException.class}) + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ResponseBody + public Response handleServiceConfigurationNotFoundException( + ServiceConfigurationNotFoundException ex) { + return getErrorResponse(ResultType.SERVICE_CONFIGURATION_NOT_FOUND, + Collections.singletonList(ex.getMessage())); + } } diff --git a/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceconfiguration/ServiceConfigurationEntity.java b/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceconfiguration/ServiceConfigurationEntity.java index 8d5bcd15e..0b11facd4 100644 --- a/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceconfiguration/ServiceConfigurationEntity.java +++ b/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceconfiguration/ServiceConfigurationEntity.java @@ -40,7 +40,7 @@ public class ServiceConfigurationEntity { @MapsId @OneToOne @JoinColumn(name = "service_id") - @JsonIgnoreProperties({"serviceConfigurationEntity"}) + @JsonIgnoreProperties({"deployResourceList", "serviceConfigurationEntity"}) private DeployServiceEntity deployServiceEntity; @Column(columnDefinition = "json") diff --git a/modules/database/src/main/java/org/eclipse/xpanse/modules/database/utils/EntityTransUtils.java b/modules/database/src/main/java/org/eclipse/xpanse/modules/database/utils/EntityTransUtils.java index ed9d86d3f..9b1617ce8 100644 --- a/modules/database/src/main/java/org/eclipse/xpanse/modules/database/utils/EntityTransUtils.java +++ b/modules/database/src/main/java/org/eclipse/xpanse/modules/database/utils/EntityTransUtils.java @@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.xpanse.modules.database.resource.DeployResourceEntity; import org.eclipse.xpanse.modules.database.service.DeployServiceEntity; +import org.eclipse.xpanse.modules.database.serviceconfiguration.ServiceConfigurationEntity; import org.eclipse.xpanse.modules.database.servicemigration.ServiceMigrationEntity; import org.eclipse.xpanse.modules.database.serviceorder.ServiceOrderEntity; import org.eclipse.xpanse.modules.database.servicestatemanagement.ServiceStateManagementTaskEntity; @@ -22,6 +23,7 @@ import org.eclipse.xpanse.modules.models.service.view.DeployedService; import org.eclipse.xpanse.modules.models.service.view.DeployedServiceDetails; import org.eclipse.xpanse.modules.models.service.view.VendorHostedDeployedServiceDetails; +import org.eclipse.xpanse.modules.models.serviceconfiguration.ServiceConfigurationDetails; import org.eclipse.xpanse.modules.models.workflow.migrate.view.ServiceMigrationDetails; import org.springframework.beans.BeanUtils; import org.springframework.util.CollectionUtils; @@ -167,4 +169,17 @@ public static ServiceOrderDetails transToServiceOrderDetails( return details; } + /** + * ServiceConfigurationEntity converted to ServiceConfigurationDetails. + * + * @param entity ServiceConfigurationEntity + * @return ServiceConfigurationDetails + */ + public static ServiceConfigurationDetails + transToServiceConfigurationDetails(ServiceConfigurationEntity entity) { + ServiceConfigurationDetails details = new ServiceConfigurationDetails(); + BeanUtils.copyProperties(entity, details); + details.setServiceId(entity.getId()); + return details; + } } diff --git a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/ServiceConfigurationManager.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/ServiceConfigurationManager.java index 1c728447e..0c07421a8 100644 --- a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/ServiceConfigurationManager.java +++ b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/ServiceConfigurationManager.java @@ -22,6 +22,8 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.xpanse.modules.database.resource.DeployResourceEntity; import org.eclipse.xpanse.modules.database.service.DeployServiceEntity; +import org.eclipse.xpanse.modules.database.serviceconfiguration.ServiceConfigurationEntity; +import org.eclipse.xpanse.modules.database.serviceconfiguration.ServiceConfigurationStorage; import org.eclipse.xpanse.modules.database.serviceconfiguration.update.ServiceConfigurationUpdateRequest; import org.eclipse.xpanse.modules.database.serviceconfiguration.update.ServiceConfigurationUpdateRequestQueryModel; import org.eclipse.xpanse.modules.database.serviceconfiguration.update.ServiceConfigurationUpdateStorage; @@ -41,9 +43,11 @@ import org.eclipse.xpanse.modules.models.serviceconfiguration.AnsibleHostInfo; import org.eclipse.xpanse.modules.models.serviceconfiguration.ServiceConfigurationChangeRequest; import org.eclipse.xpanse.modules.models.serviceconfiguration.ServiceConfigurationChangeResult; +import org.eclipse.xpanse.modules.models.serviceconfiguration.ServiceConfigurationDetails; import org.eclipse.xpanse.modules.models.serviceconfiguration.ServiceConfigurationUpdate; import org.eclipse.xpanse.modules.models.serviceconfiguration.enums.ServiceConfigurationStatus; import org.eclipse.xpanse.modules.models.serviceconfiguration.exceptions.ServiceConfigurationInvalidException; +import org.eclipse.xpanse.modules.models.serviceconfiguration.exceptions.ServiceConfigurationNotFoundException; import org.eclipse.xpanse.modules.models.serviceconfiguration.exceptions.ServiceConfigurationUpdateRequestNotFoundException; import org.eclipse.xpanse.modules.models.servicetemplate.ConfigManageScript; import org.eclipse.xpanse.modules.models.servicetemplate.Ocl; @@ -74,6 +78,9 @@ public class ServiceConfigurationManager { @Resource private ServiceConfigurationUpdateStorage serviceConfigurationUpdateStorage; + @Resource + private ServiceConfigurationStorage serviceConfigurationStorage; + @Resource private ServiceTemplateStorage serviceTemplateStorage; @@ -94,6 +101,24 @@ public class ServiceConfigurationManager { private ServiceConfigurationVariablesJsonSchemaGenerator serviceConfigurationVariablesJsonSchemaGenerator; + /** + * Query the service's current configuration by id of the deployed service. + * + * @param serviceId id of the deployed service + * @return ServiceConfigurationEntity. + */ + public ServiceConfigurationDetails getCurrentConfigurationOfService(String serviceId) { + ServiceConfigurationEntity entity = serviceConfigurationStorage + .findServiceConfigurationById(UUID.fromString(serviceId)); + if (Objects.isNull(entity)) { + String errorMsg = String.format("Service Configuration with service id %s not found.", + serviceId); + log.error(errorMsg); + throw new ServiceConfigurationNotFoundException(errorMsg); + } + return EntityTransUtils.transToServiceConfigurationDetails(entity); + } + /** * update ServiceConfiguration. * diff --git a/modules/models/src/main/java/org/eclipse/xpanse/modules/models/response/ResultType.java b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/response/ResultType.java index 3b8ab770c..c7c3e0d16 100644 --- a/modules/models/src/main/java/org/eclipse/xpanse/modules/models/response/ResultType.java +++ b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/response/ResultType.java @@ -75,7 +75,8 @@ public enum ResultType { INVALID_GIT_REPO_DETAILS("Invalid Git Repo Details"), FILE_LOCKED("File Locked"), INVALID_SERVICE_CONFIGURATION("Service Configuration Invalid"), - SERVICE_CONFIG_UPDATE_REQUEST_NOT_FOUND("Service Configuration Update Request Not Found"); + SERVICE_CONFIG_UPDATE_REQUEST_NOT_FOUND("Service Configuration Update Request Not Found"), + SERVICE_CONFIGURATION_NOT_FOUND("Service Configuration Not Found"); private final String value; diff --git a/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/ServiceConfigurationDetails.java b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/ServiceConfigurationDetails.java new file mode 100644 index 000000000..a1718ad68 --- /dev/null +++ b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/ServiceConfigurationDetails.java @@ -0,0 +1,22 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * SPDX-FileCopyrightText: Huawei Inc. + */ + +package org.eclipse.xpanse.modules.models.serviceconfiguration; + +import java.time.OffsetDateTime; +import java.util.Map; +import java.util.UUID; +import lombok.Data; + +/** + * Details of the service configuration. + */ +@Data +public class ServiceConfigurationDetails { + private UUID serviceId; + private Map configuration; + private OffsetDateTime createdTime; + private OffsetDateTime updatedTime; +} diff --git a/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationNotFoundException.java b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationNotFoundException.java new file mode 100644 index 000000000..b7461db95 --- /dev/null +++ b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationNotFoundException.java @@ -0,0 +1,15 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * SPDX-FileCopyrightText: Huawei Inc. + */ + +package org.eclipse.xpanse.modules.models.serviceconfiguration.exceptions; + +/** + * Exception thrown when service configuration not found. + */ +public class ServiceConfigurationNotFoundException extends RuntimeException { + public ServiceConfigurationNotFoundException(String message) { + super(message); + } +} diff --git a/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationUpdateRequestNotFoundException.java b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationUpdateRequestNotFoundException.java index 55270f6bd..0b65a51b8 100644 --- a/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationUpdateRequestNotFoundException.java +++ b/modules/models/src/main/java/org/eclipse/xpanse/modules/models/serviceconfiguration/exceptions/ServiceConfigurationUpdateRequestNotFoundException.java @@ -8,9 +8,7 @@ /** * Exception thrown when service configuration update request not found. */ - -public class ServiceConfigurationUpdateRequestNotFoundException extends RuntimeException { - +public class ServiceConfigurationUpdateRequestNotFoundException extends RuntimeException { public ServiceConfigurationUpdateRequestNotFoundException(String message) { super(message); }