Skip to content

Commit

Permalink
implement new migrate service using terraform-local
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Oct 13, 2023
1 parent dbc7875 commit 3a7d862
Show file tree
Hide file tree
Showing 38 changed files with 638 additions and 45 deletions.
8 changes: 7 additions & 1 deletion modules/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down Expand Up @@ -87,5 +87,11 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>workflow</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -33,6 +35,7 @@
import org.eclipse.xpanse.modules.models.service.view.ServiceVo;
import org.eclipse.xpanse.modules.orchestrator.deployment.DeployTask;
import org.eclipse.xpanse.modules.orchestrator.deployment.Deployment;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowProcessUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
Expand Down Expand Up @@ -61,6 +64,8 @@ public class ServiceDeployerApi {
@Resource
private DeployService deployService;

@Resource
private WorkflowProcessUtils workflowProcessUtils;

/**
* Get details of the managed service by id.
Expand Down Expand Up @@ -179,6 +184,27 @@ public Response purge(@PathVariable("id") String id) {
return Response.successResponse(Collections.singletonList(successMsg));
}

/**
* Start a task to migrate the deployed service using id.
*
* @param id ID of deployed service.
* @return response
*/
@Tag(name = "Service", description = "APIs to manage the service instances")
@Operation(description = "Start a task to migrate the deployed service using id.")
@PostMapping(value = "/services/migrate/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.ACCEPTED)
public UUID migrate(@PathVariable("id") String id,
@Valid @RequestBody CreateRequest deployRequest) {
UUID newId = UUID.randomUUID();
Map<String, Object> variable = new HashMap<>();
variable.put("id", id);
variable.put("newId", newId);
variable.put("createRequest", deployRequest);
workflowProcessUtils.asyncStartProcess("migrate", variable);
return newId;
}

private ServiceQueryModel getServiceQueryModel(Category category, Csp csp,
String serviceName,
String serviceVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.VariableInvalidException;
import org.eclipse.xpanse.modules.models.service.query.ServiceQueryModel;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.eclipse.xpanse.modules.workflow.utils.WorkflowProcessUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -40,13 +41,16 @@

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ServiceDeployerApi.class, DeployService.class,
DeploymentExceptionHandler.class, IdentityProviderManager.class})
WorkflowProcessUtils.class, DeploymentExceptionHandler.class, IdentityProviderManager.class})
@WebMvcTest
class DeploymentExceptionHandlerTest {

@MockBean
private DeployService deployService;

@MockBean
private WorkflowProcessUtils workflowProcessUtils;

@Autowired
private WebApplicationContext context;

Expand Down
2 changes: 1 addition & 1 deletion modules/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down
2 changes: 1 addition & 1 deletion modules/credential/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down
2 changes: 1 addition & 1 deletion modules/database/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down
2 changes: 1 addition & 1 deletion modules/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.xpanse.modules.deployment.deployers.terraform.terraformboot.model.TerraformResult;
import org.eclipse.xpanse.modules.models.security.model.CurrentUserInfo;
import org.eclipse.xpanse.modules.models.service.common.enums.Csp;
import org.eclipse.xpanse.modules.models.service.deploy.CreateRequest;
import org.eclipse.xpanse.modules.models.service.deploy.DeployResource;
import org.eclipse.xpanse.modules.models.service.deploy.DeployResult;
import org.eclipse.xpanse.modules.models.service.deploy.enums.ServiceDeploymentState;
Expand Down Expand Up @@ -191,13 +192,18 @@ private void encodeDeployVariable(ServiceTemplateEntity serviceTemplate,
*/
@Async("taskExecutor")
public void asyncDeployService(Deployment deployment, DeployTask deployTask) {
deploy(deployment, deployTask);
}

private boolean deploy(Deployment deployment, DeployTask deployTask) {
MDC.put(TASK_ID, deployTask.getId().toString());
DeployServiceEntity deployServiceEntity = getNewDeployServiceTask(deployTask);
DeployResult deployResult = new DeployResult();
try {
deployServiceEntity.setServiceDeploymentState(ServiceDeploymentState.DEPLOYING);
deployServiceStorage.storeAndFlush(deployServiceEntity);
deployment.deploy(deployTask);
return true;
} catch (RuntimeException e) {
log.error("asyncDeployService failed.", e);
deployServiceEntity.setServiceDeploymentState(ServiceDeploymentState.DEPLOY_FAILED);
Expand All @@ -209,6 +215,7 @@ public void asyncDeployService(Deployment deployment, DeployTask deployTask) {
maskSensitiveFields(deployServiceEntity);
deployServiceStorage.storeAndFlush(deployServiceEntity);
rollbackOnDeploymentFailure(deployServiceEntity, deployResult, deployment, deployTask);
return false;
}
}

Expand Down Expand Up @@ -300,26 +307,32 @@ public Deployment getDestroyHandler(DeployTask deployTask) {
*/
@Async("taskExecutor")
public void asyncDestroyService(Deployment deployment, DeployTask deployTask) {
destroy(deployment, deployTask);
}

private boolean destroy(Deployment deployment, DeployTask deployTask) {
MDC.put(TASK_ID, deployTask.getId().toString());
DeployServiceEntity deployServiceEntity =
deployServiceStorage.findDeployServiceById(deployTask.getId());
if (Objects.isNull(deployServiceEntity)) {
String errorMsg = String.format("Service with id %s not found.", deployTask.getId());
log.error(errorMsg);
throw new ServiceNotDeployedException(errorMsg);
throw new ServiceNotDeployedException("Service with id %s not found.");
}
try {
deployServiceEntity.setServiceDeploymentState(ServiceDeploymentState.DESTROYING);
deployServiceStorage.storeAndFlush(deployServiceEntity);
String stateFile = deployServiceEntity.getPrivateProperties().get(STATE_FILE_NAME);
deployment.destroy(deployTask, stateFile);
return true;
} catch (Exception e) {
log.error("asyncDestroyService failed", e);
deployServiceEntity.setResultMessage(e.getMessage());
deployServiceEntity.setServiceDeploymentState(ServiceDeploymentState.DESTROY_FAILED);
if (deployServiceStorage.storeAndFlush(deployServiceEntity)) {
deployment.deleteTaskWorkspace(deployTask.getId().toString());
}
return false;
}

}
Expand Down Expand Up @@ -568,4 +581,26 @@ private void maskSensitiveFields(DeployServiceEntity deployServiceEntity) {
}
}
}

/**
* Deployment service.
*/
public boolean deployService(UUID newId, CreateRequest createRequest) {
DeployTask deployTask = new DeployTask();
createRequest.setId(newId);
deployTask.setId(newId);
deployTask.setCreateRequest(createRequest);
Deployment deployment = getDeployHandler(deployTask);
return deploy(deployment, deployTask);
}

/**
* Destroy service by deployed service id.
*/
public boolean destroyService(String id) {
DeployTask deployTask = new DeployTask();
deployTask.setId(UUID.fromString(id));
Deployment deployment = getDestroyHandler(deployTask);
return destroy(deployment, deployTask);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ private void flushDeployServiceEntity(DeployResult deployResult, UUID taskId) {
deployServiceEntity.setServiceDeploymentState(ServiceDeploymentState.DEPLOY_SUCCESS);
deployServiceEntity.setProperties(deployResult.getProperties());
deployServiceEntity.setPrivateProperties(deployResult.getPrivateProperties());
deployServiceEntity.setDeployResourceList(
getDeployResourceEntityList(deployResult.getResources(), deployServiceEntity));
deployServiceEntity.getDeployResourceList().clear();
deployServiceEntity.getDeployResourceList()
.addAll(getDeployResourceEntityList(deployResult.getResources(),
deployServiceEntity));
maskSensitiveFields(deployServiceEntity);
deployServiceStorage.storeAndFlush(deployServiceEntity);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
<artifactId>logging</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion modules/models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Map;
import java.util.UUID;
import lombok.Data;
Expand All @@ -21,7 +22,7 @@
* Request body for service creation.
*/
@Data
public class CreateRequest {
public class CreateRequest implements Serializable {

@Hidden
private UUID id;
Expand Down
2 changes: 1 addition & 1 deletion modules/monitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down
2 changes: 1 addition & 1 deletion modules/orchestrator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion modules/policy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
Expand Down
3 changes: 2 additions & 1 deletion modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>xpanse-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -32,6 +32,7 @@
<module>security</module>
<module>common</module>
<module>policy</module>
<module>workflow</module>
</modules>

</project>
2 changes: 1 addition & 1 deletion modules/security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down
2 changes: 1 addition & 1 deletion modules/servicetemplate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.eclipse.xpanse</groupId>
<artifactId>modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.eclipse.xpanse.modules</groupId>
Expand Down

0 comments on commit 3a7d862

Please sign in to comment.