-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove redundant async threads in migration process
- Loading branch information
1 parent
4523c34
commit 2029878
Showing
36 changed files
with
1,182 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
modules/api/src/main/java/org/eclipse/xpanse/api/controllers/ServiceMigrationApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
* | ||
*/ | ||
|
||
package org.eclipse.xpanse.api.controllers; | ||
|
||
|
||
import static org.eclipse.xpanse.modules.security.common.RoleConstants.ROLE_ADMIN; | ||
import static org.eclipse.xpanse.modules.security.common.RoleConstants.ROLE_USER; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.annotation.Resource; | ||
import jakarta.validation.Valid; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.activiti.engine.runtime.ProcessInstance; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.xpanse.modules.database.service.DeployServiceEntity; | ||
import org.eclipse.xpanse.modules.deployment.DeployServiceEntityHandler; | ||
import org.eclipse.xpanse.modules.deployment.migration.consts.MigrateConstants; | ||
import org.eclipse.xpanse.modules.models.workflow.migrate.MigrateRequest; | ||
import org.eclipse.xpanse.modules.security.IdentityProviderManager; | ||
import org.eclipse.xpanse.modules.workflow.utils.WorkflowUtils; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.access.annotation.Secured; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
/** | ||
* REST interface methods for Service Migration. | ||
*/ | ||
@Slf4j | ||
@RestController | ||
@RequestMapping("/xpanse") | ||
@CrossOrigin | ||
@Secured({ROLE_ADMIN, ROLE_USER}) | ||
public class ServiceMigrationApi { | ||
|
||
@Resource | ||
private DeployServiceEntityHandler deployServiceEntityHandler; | ||
|
||
@Resource | ||
private IdentityProviderManager identityProviderManager; | ||
|
||
@Resource | ||
private WorkflowUtils workflowUtils; | ||
|
||
/** | ||
* Create a job to migrate the deployed service. | ||
* | ||
* @return response | ||
*/ | ||
@Tag(name = "Migration", description = "APIs to manage the service migration.") | ||
@Operation(description = "Create a job to migrate the deployed service.") | ||
@PostMapping(value = "/services/migration", produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseStatus(HttpStatus.ACCEPTED) | ||
public UUID migrate(@Valid @RequestBody MigrateRequest migrateRequest) { | ||
String userId = getUserId(migrateRequest.getId()); | ||
Map<String, Object> variable = | ||
getMigrateProcessVariable(migrateRequest, UUID.randomUUID(), userId); | ||
ProcessInstance instance = | ||
workflowUtils.startProcess(MigrateConstants.PROCESS_KEY, variable); | ||
return UUID.fromString(instance.getProcessInstanceId()); | ||
} | ||
|
||
private String getUserId(UUID migrationId) { | ||
DeployServiceEntity deployServiceEntity = | ||
this.deployServiceEntityHandler.getDeployServiceEntity(migrationId); | ||
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId(); | ||
String userId = userIdOptional.orElse(null); | ||
if (!StringUtils.equals(userId, deployServiceEntity.getUserId())) { | ||
throw new AccessDeniedException( | ||
"No permissions to migrate services belonging to other users."); | ||
} | ||
return userId; | ||
} | ||
|
||
private Map<String, Object> getMigrateProcessVariable(MigrateRequest migrateRequest, | ||
UUID newServiceId, String userId) { | ||
Map<String, Object> variable = new HashMap<>(); | ||
variable.put(MigrateConstants.ID, migrateRequest.getId()); | ||
variable.put(MigrateConstants.NEW_ID, newServiceId); | ||
variable.put(MigrateConstants.MIGRATE_REQUEST, migrateRequest); | ||
variable.put(MigrateConstants.USER_ID, userId); | ||
return variable; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...main/java/org/eclipse/xpanse/api/exceptions/handler/ServiceMigrationExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* SPDX-FileCopyrightText: Huawei Inc. | ||
*/ | ||
|
||
|
||
package org.eclipse.xpanse.api.exceptions.handler; | ||
|
||
|
||
import java.util.Collections; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.eclipse.xpanse.modules.models.response.Response; | ||
import org.eclipse.xpanse.modules.models.response.ResultType; | ||
import org.eclipse.xpanse.modules.models.workflow.migrate.exceptions.ServiceMigrationFailedException; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
/** | ||
* Exception handler related to service migration. | ||
*/ | ||
@Slf4j | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@RestControllerAdvice | ||
public class ServiceMigrationExceptionHandler { | ||
|
||
/** | ||
* Exception handler for ServiceMigrationFailedException. | ||
*/ | ||
@ExceptionHandler({ServiceMigrationFailedException.class}) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ResponseBody | ||
public Response handleServiceNotMigrationException( | ||
ServiceMigrationFailedException ex) { | ||
return Response.errorResponse(ResultType.SERVICE_MIGRATION_FAILED_EXCEPTION, | ||
Collections.singletonList(ex.getMessage())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.