Skip to content

Commit

Permalink
fix error state when rollback success
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Jan 31, 2024
1 parent caec276 commit 455a7e1
Show file tree
Hide file tree
Showing 37 changed files with 1,453 additions and 966 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.modules.deployment.deployers.terraform.TerraformDeploymentResultCallbackManager;
import org.eclipse.xpanse.modules.deployment.deployers.terraform.terraformboot.generated.model.TerraformResult;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void deployCallback(
@PathVariable("task_id") String taskId,
@Valid @RequestBody TerraformResult result) {

terraformDeploymentResultCallbackManager.deployCallback(taskId, result);
terraformDeploymentResultCallbackManager.deployCallback(UUID.fromString(taskId), result);
}

/**
Expand All @@ -70,7 +71,7 @@ public void destroyCallback(
@PathVariable("task_id") String taskId,
@Valid @RequestBody TerraformResult result) {

terraformDeploymentResultCallbackManager.destroyCallback(taskId, result);
terraformDeploymentResultCallbackManager.destroyCallback(UUID.fromString(taskId), result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ public List<DeployServiceEntity> listServices(
predicateList.add(criteriaBuilder.equal(root.get("userId"),
serviceQuery.getUserId()));

return query.where(criteriaBuilder.and(predicateList.toArray(new Predicate[0])))
query.where(criteriaBuilder.and(predicateList.toArray(new Predicate[0])))
.getRestriction();

query.orderBy(criteriaBuilder.desc(root.get("createTime")));

return query.getRestriction();
};

return deployServiceRepository.findAll(specification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.database.resource.DeployResourceEntity;
import org.eclipse.xpanse.modules.database.service.DeployServiceEntity;
import org.eclipse.xpanse.modules.models.service.deploy.DeployResource;
Expand Down Expand Up @@ -48,27 +49,24 @@ public DeployServiceEntity updateDeployServiceEntityWithDeployResult(
throws RuntimeException {
if (Objects.nonNull(deployResult.getState()) && Objects.nonNull(storedEntity)) {
log.info("Deploy task update deploy service entity with id:{}", deployResult.getId());
DeployServiceEntity deployServiceEntityToFlush = new DeployServiceEntity();
BeanUtils.copyProperties(storedEntity, deployServiceEntityToFlush);
if (DeployerTaskStatus.DEPLOY_SUCCESS == deployResult.getState()) {
deployServiceEntityToFlush.setServiceDeploymentState(
ServiceDeploymentState.DEPLOY_SUCCESS);
deployServiceEntityToFlush.setServiceState(ServiceState.RUNNING);
} else {
deployServiceEntityToFlush.setServiceDeploymentState(
ServiceDeploymentState.DEPLOY_FAILED);
deployServiceEntityToFlush.setServiceState(ServiceState.NOT_RUNNING);
deployServiceEntityToFlush.setResultMessage(deployResult.getMessage());
}
updateDeployResourceEntity(deployResult, deployServiceEntityToFlush);
return deployServiceEntityHandler.storeAndFlush(deployServiceEntityToFlush);
DeployServiceEntity deployServiceEntityToStore = new DeployServiceEntity();
BeanUtils.copyProperties(storedEntity, deployServiceEntityToStore);
updateEntityWithDeployResult(deployResult, deployServiceEntityToStore);
return deployServiceEntityHandler.storeAndFlush(deployServiceEntityToStore);
} else {
return storedEntity;
}
}

private void updateDeployResourceEntity(DeployResult deployResult,
DeployServiceEntity deployServiceEntity) {
private void updateEntityWithDeployResult(DeployResult deployResult,
DeployServiceEntity deployServiceEntity) {

if (StringUtils.isNotBlank(deployResult.getMessage())) {
deployServiceEntity.setResultMessage(deployResult.getMessage());
}
deployServiceEntity.setServiceState(getServiceState(deployResult.getState()));
deployServiceEntity.setServiceDeploymentState(
getServiceDeploymentState(deployResult.getState()));

if (!CollectionUtils.isEmpty(deployServiceEntity.getProperties())) {
deployServiceEntity.getProperties().clear();
Expand All @@ -94,6 +92,26 @@ private void updateDeployResourceEntity(DeployResult deployResult,
sensitiveDataHandler.maskSensitiveFields(deployServiceEntity);
}

private ServiceState getServiceState(DeployerTaskStatus state) {
if (state == DeployerTaskStatus.DEPLOY_SUCCESS) {
return ServiceState.RUNNING;
}
return ServiceState.NOT_RUNNING;
}

private ServiceDeploymentState getServiceDeploymentState(
DeployerTaskStatus deployerTaskStatus) {
return switch (deployerTaskStatus) {
case DEPLOY_SUCCESS -> ServiceDeploymentState.DEPLOY_SUCCESS;
case DEPLOY_FAILED, ROLLBACK_SUCCESS -> ServiceDeploymentState.DEPLOY_FAILED;
case DESTROY_SUCCESS, PURGE_SUCCESS -> ServiceDeploymentState.DESTROY_SUCCESS;
case DESTROY_FAILED -> ServiceDeploymentState.DESTROY_FAILED;
case ROLLBACK_FAILED -> ServiceDeploymentState.ROLLBACK_FAILED;
case INIT, PURGE_FAILED -> ServiceDeploymentState.MANUAL_CLEANUP_REQUIRED;
};
}


/**
* Convert deploy resources to deploy resource entities.
*/
Expand All @@ -111,52 +129,4 @@ private List<DeployResourceEntity> getDeployResourceEntityList(
}
return deployResourceEntities;
}

/**
* Method to update destroy result to DeployServiceEntity and store to database.
*
* @param destroyResult Destroy Result.
* @param deployServiceEntity DB entity to be updated
* @param isCalledWhenRollback is service destroyed as part of rollback.
* @return updated entity.
* @throws RuntimeException exception thrown in case of errors.
*/
public DeployServiceEntity updateDeployServiceEntityWithDestroyResult(
DeployResult destroyResult,
DeployServiceEntity deployServiceEntity,
boolean isCalledWhenRollback)
throws RuntimeException {
if (Objects.nonNull(destroyResult) && Objects.nonNull(destroyResult.getState())) {
log.info("Update stored deploy service entity by result of destroy task with id:{}",
destroyResult.getId());
DeployServiceEntity deployServiceEntityToFlush = new DeployServiceEntity();
BeanUtils.copyProperties(deployServiceEntity, deployServiceEntityToFlush);
if (isCalledWhenRollback) {
if (destroyResult.getState() == DeployerTaskStatus.DESTROY_SUCCESS) {
deployServiceEntityToFlush.setServiceDeploymentState(
ServiceDeploymentState.DEPLOY_FAILED);
deployServiceEntityToFlush.setServiceState(ServiceState.NOT_RUNNING);
} else {
deployServiceEntityToFlush.setServiceDeploymentState(
ServiceDeploymentState.ROLLBACK_FAILED);
deployServiceEntityToFlush.setServiceState(ServiceState.RUNNING);
}
} else {
if (destroyResult.getState() == DeployerTaskStatus.DESTROY_SUCCESS) {
deployServiceEntityToFlush.setServiceDeploymentState(
ServiceDeploymentState.DESTROY_SUCCESS);
deployServiceEntityToFlush.setServiceState(ServiceState.NOT_RUNNING);
} else {
deployServiceEntityToFlush.setServiceDeploymentState(
ServiceDeploymentState.DESTROY_FAILED);
deployServiceEntityToFlush.setServiceState(ServiceState.RUNNING);
deployServiceEntityToFlush.setResultMessage(destroyResult.getMessage());
}
}
updateDeployResourceEntity(destroyResult, deployServiceEntityToFlush);
return deployServiceEntityHandler.storeAndFlush(deployServiceEntityToFlush);
} else {
return deployServiceEntity;
}
}
}

0 comments on commit 455a7e1

Please sign in to comment.