Skip to content

Commit

Permalink
Optimize method for reading the files generated by the deployer
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Nov 12, 2024
1 parent 3993e86 commit dc9ac5b
Show file tree
Hide file tree
Showing 123 changed files with 1,161 additions and 1,213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.xpanse.modules.deployment.ServiceDeploymentEntityHandler;
import org.eclipse.xpanse.modules.deployment.ServiceOrderManager;
import org.eclipse.xpanse.modules.deployment.migration.consts.MigrateConstants;
import org.eclipse.xpanse.modules.logging.CustomRequestIdGenerator;
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.BillingModeNotSupported;
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.EulaNotAccepted;
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.ServiceLockedException;
Expand Down Expand Up @@ -148,7 +149,7 @@ private void validateData(MigrateRequest migrateRequest) {

private DeployTask getMigrateTask(MigrateRequest migrateRequest) {
DeployTask migrateTask = new DeployTask();
migrateTask.setOrderId(UUID.randomUUID());
migrateTask.setOrderId(CustomRequestIdGenerator.generateOrderId());
migrateTask.setTaskType(ServiceOrderType.MIGRATE);
migrateTask.setServiceId(migrateRequest.getOriginalServiceId());
migrateTask.setOriginalServiceId(migrateRequest.getOriginalServiceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.xpanse.modules.deployment.ServiceDeploymentEntityHandler;
import org.eclipse.xpanse.modules.deployment.ServiceOrderManager;
import org.eclipse.xpanse.modules.deployment.recreate.consts.RecreateConstants;
import org.eclipse.xpanse.modules.logging.CustomRequestIdGenerator;
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.InvalidServiceStateException;
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.ServiceLockedException;
import org.eclipse.xpanse.modules.models.service.enums.ServiceDeploymentState;
Expand Down Expand Up @@ -135,7 +136,7 @@ public ServiceOrder recreateService(@Valid @PathVariable("serviceId") String ser

private DeployTask getRecreateTask(ServiceDeploymentEntity serviceDeploymentEntity) {
DeployTask recreateTask = new DeployTask();
recreateTask.setOrderId(UUID.randomUUID());
recreateTask.setOrderId(CustomRequestIdGenerator.generateOrderId());
recreateTask.setTaskType(ServiceOrderType.RECREATE);
recreateTask.setServiceId(serviceDeploymentEntity.getId());
recreateTask.setOriginalServiceId(serviceDeploymentEntity.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.modules.deployment.deployers.terraform.exceptions.TerraformBootRequestFailedException;
import org.eclipse.xpanse.modules.deployment.deployers.terraform.exceptions.TerraformExecutorException;
import org.eclipse.xpanse.modules.deployment.exceptions.DeploymentScriptsCreationFailedException;
import org.eclipse.xpanse.modules.models.billing.exceptions.ServicePriceCalculationFailed;
import org.eclipse.xpanse.modules.models.common.exceptions.InvalidDeployerToolException;
import org.eclipse.xpanse.modules.models.response.Response;
Expand Down Expand Up @@ -270,4 +271,16 @@ public Response handleInvalidDeployerToolException(InvalidDeployerToolException
return getErrorResponse(ResultType.INVALID_DEPLOYER_TOOL,
Collections.singletonList(ex.getMessage()));
}

/**
* Exception handler for DeploymentScriptsCreationFailedException.
*/
@ExceptionHandler({DeploymentScriptsCreationFailedException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Response handleDeploymentScriptsCreatedException(
DeploymentScriptsCreationFailedException ex) {
return getErrorResponse(ResultType.DEPLOYMENT_SCRIPTS_CREATION_FAILED,
Collections.singletonList(ex.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.eclipse.xpanse.modules.deployment;

import static org.eclipse.xpanse.modules.deployment.utils.DeploymentScriptsHelper.TF_STATE_FILE_NAME;

import jakarta.annotation.Resource;
import java.time.OffsetDateTime;
import java.util.ArrayList;
Expand All @@ -25,6 +27,7 @@
import org.eclipse.xpanse.modules.database.servicetemplate.ServiceTemplateStorage;
import org.eclipse.xpanse.modules.deployment.migration.consts.MigrateConstants;
import org.eclipse.xpanse.modules.deployment.recreate.consts.RecreateConstants;
import org.eclipse.xpanse.modules.logging.CustomRequestIdGenerator;
import org.eclipse.xpanse.modules.models.service.deploy.DeployRequest;
import org.eclipse.xpanse.modules.models.service.deploy.DeployResource;
import org.eclipse.xpanse.modules.models.service.enums.ServiceDeploymentState;
Expand Down Expand Up @@ -128,7 +131,7 @@ public void rollbackOnDeploymentFailure(DeployTask rollbackTask,
DeployResult rollbackResult;
RuntimeException exception = null;
log.info("Performing rollback of already provisioned resources.");
rollbackTask.setOrderId(UUID.randomUUID());
rollbackTask.setOrderId(CustomRequestIdGenerator.generateOrderId());
rollbackTask.setTaskType(ServiceOrderType.ROLLBACK);
ServiceOrderEntity serviceOrderEntity = serviceOrderManager
.storeNewServiceOrderEntity(rollbackTask, serviceDeploymentEntity);
Expand Down Expand Up @@ -169,14 +172,27 @@ private ServiceDeploymentEntity updateDeployServiceEntityWithDeployResult(
log.info("Update deploy service entity {} with deploy result {}",
serviceDeploymentEntity.getId(), deployResult);
if (StringUtils.isNotBlank(deployResult.getTfStateContent())) {
deployResult.getDeploymentGeneratedFiles()
.put(TF_STATE_FILE_NAME, deployResult.getTfStateContent());
ServiceTemplateEntity serviceTemplateEntity =
serviceTemplateStorage.getServiceTemplateById(
serviceDeploymentEntity.getServiceTemplateId());
DeployerKind deployerKind =
serviceTemplateEntity.getOcl().getDeployment().getDeployerTool().getKind();
resourceHandlerManager.getResourceHandler(serviceDeploymentEntity.getCsp(),
deployerKind).handler(deployResult);
} else {
if (Objects.nonNull(serviceDeploymentEntity.getDeploymentGeneratedFiles())) {
String storedTfStateContent = serviceDeploymentEntity.getDeploymentGeneratedFiles()
.get(TF_STATE_FILE_NAME);
if (StringUtils.isNotBlank(storedTfStateContent)) {
deployResult.setTfStateContent(storedTfStateContent);
deployResult.getDeploymentGeneratedFiles()
.put(TF_STATE_FILE_NAME, deployResult.getTfStateContent());
}
}
}

ServiceDeploymentEntity deployServiceToUpdate = new ServiceDeploymentEntity();
BeanUtils.copyProperties(serviceDeploymentEntity, deployServiceToUpdate);
updateServiceEntityWithDeployResult(deployResult, taskType, deployServiceToUpdate);
Expand All @@ -185,7 +201,8 @@ private ServiceDeploymentEntity updateDeployServiceEntityWithDeployResult(


private void updateServiceEntityWithDeployResult(DeployResult deployResult,
ServiceOrderType taskType, ServiceDeploymentEntity serviceDeployment) {
ServiceOrderType taskType,
ServiceDeploymentEntity serviceDeployment) {
boolean isTaskSuccessful = deployResult.getIsTaskSuccessful();
ServiceDeploymentState deploymentState =
getServiceDeploymentState(taskType, isTaskSuccessful);
Expand Down Expand Up @@ -322,15 +339,14 @@ private void updateServiceOrderEntityWithDeployResult(DeployResult deployResult,
entityToUpdate.setTaskStatus(taskStatus);
entityToUpdate.setCompletedTime(OffsetDateTime.now());
entityToUpdate.setErrorMsg(deployResult.getMessage());
// finally, update the service order entity of this current order task.
// finally, update the service order entity of this order task.
serviceOrderStorage.storeAndFlush(entityToUpdate);
}

private void completeParentServiceOrder(UUID parentOrderId) {
ServiceOrderEntity parentOrder = serviceOrderStorage.getEntityById(parentOrderId);
// When the parent order is not a migrate task, complete it.
if (parentOrder.getTaskType() != ServiceOrderType.MIGRATE
&& parentOrder.getTaskType() != ServiceOrderType.RECREATE) {
if (parentOrder.getTaskType() != ServiceOrderType.MIGRATE) {
ServiceOrderEntity entityToUpdate = new ServiceOrderEntity();
BeanUtils.copyProperties(parentOrder, entityToUpdate);
entityToUpdate.setCompletedTime(OffsetDateTime.now());
Expand Down Expand Up @@ -370,7 +386,6 @@ private void processRelatedWorkflowTask(ServiceOrderEntity serviceOrder) {
}
}
}

} catch (Exception e) {
log.error("Failed to process the related workflow task of service order: {}",
serviceOrder.getOrderId(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import org.eclipse.xpanse.modules.database.service.ServiceDeploymentEntity;
import org.eclipse.xpanse.modules.database.serviceconfiguration.ServiceConfigurationEntity;
import org.eclipse.xpanse.modules.database.servicetemplate.ServiceTemplateEntity;
import org.eclipse.xpanse.modules.database.servicetemplate.ServiceTemplateStorage;
import org.eclipse.xpanse.modules.logging.CustomRequestIdGenerator;
import org.eclipse.xpanse.modules.models.service.order.enums.ServiceOrderType;
import org.eclipse.xpanse.modules.models.servicetemplate.ServiceConfigurationManage;
import org.eclipse.xpanse.modules.models.servicetemplate.ServiceConfigurationParameter;
Expand All @@ -38,15 +38,15 @@ public class DeployServiceEntityConverter {
/**
* Method to create a DeployTask from DeployServiceEntity.
*
* @param orderType ServiceOrderType.
* @param orderType ServiceOrderType.
* @param serviceDeploymentEntity DeployServiceEntity object.
* @return DeployTask object.
*/
public DeployTask getDeployTaskByStoredService(ServiceOrderType orderType,
ServiceDeploymentEntity serviceDeploymentEntity) {
public DeployTask getDeployTaskByStoredService(
ServiceOrderType orderType, ServiceDeploymentEntity serviceDeploymentEntity) {
// Set Ocl and CreateRequest
DeployTask deployTask = new DeployTask();
deployTask.setOrderId(UUID.randomUUID());
deployTask.setOrderId(CustomRequestIdGenerator.generateOrderId());
deployTask.setTaskType(orderType);
deployTask.setServiceId(serviceDeploymentEntity.getId());
deployTask.setUserId(serviceDeploymentEntity.getUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import java.util.Objects;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.deployment.DeployResultManager;
import org.eclipse.xpanse.modules.deployment.deployers.opentofu.tofumaker.generated.model.OpenTofuResult;
import org.eclipse.xpanse.modules.deployment.deployers.terraform.utils.TfResourceTransUtils;
import org.eclipse.xpanse.modules.orchestrator.deployment.DeployResult;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -48,11 +48,12 @@ private DeployResult getDeployResult(OpenTofuResult result) {
deployResult.setIsTaskSuccessful(true);
deployResult.setMessage(null);
}
deployResult.setTfStateContent(result.getTerraformState());
deployResult.getDeploymentGeneratedFiles()
.put(TfResourceTransUtils.STATE_FILE_NAME, result.getTerraformState());
if (Objects.nonNull(result.getImportantFileContentMap())) {
deployResult.getDeploymentGeneratedFiles().putAll(result.getImportantFileContentMap());
if (StringUtils.isNotBlank(result.getTerraformState())) {
deployResult.setTfStateContent(result.getTerraformState());
}
if (Objects.nonNull(result.getGeneratedFileContentMap())) {
deployResult.getDeploymentGeneratedFiles()
.putAll(result.getGeneratedFileContentMap());
}
return deployResult;
}
Expand Down
Loading

0 comments on commit dc9ac5b

Please sign in to comment.