diff --git a/src/main/java/com/litoralregas/openvpn/router/RouterController.java b/src/main/java/com/litoralregas/openvpn/router/RouterController.java index 638f2ac..8c751c1 100644 --- a/src/main/java/com/litoralregas/openvpn/router/RouterController.java +++ b/src/main/java/com/litoralregas/openvpn/router/RouterController.java @@ -1,6 +1,7 @@ package com.litoralregas.openvpn.router; import com.litoralregas.openvpn.deployment.DeploymentAction; +import com.litoralregas.openvpn.deployment.DeploymentResponse; import com.litoralregas.openvpn.deployment.DeploymentService; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; @@ -49,14 +50,32 @@ public class RouterController { } @PostMapping("/{id}/provision") - public String provision(@PathVariable UUID id) { + public DeploymentResponse provision(@PathVariable UUID id) { Router router = service.findById(id); var deployment = deploymentService.startDeployment(router, DeploymentAction.PROVISION); - // simulate success - deploymentService.finishSuccess(deployment, "Provision completed"); + try { + service.forceStatus(id, RouterStatus.PROVISIONING); - return "Provision simulated"; + // Simulated provisioning for now. + var finishedDeployment = deploymentService.finishSuccess( + deployment, + "Provision completed successfully" + ); + + service.forceStatus(id, RouterStatus.PROVISIONED); + + return DeploymentResponse.from(finishedDeployment); + } catch (Exception exception) { + var failedDeployment = deploymentService.finishFailure( + deployment, + exception.getMessage() + ); + + service.forceStatus(id, RouterStatus.FAILED); + + return DeploymentResponse.from(failedDeployment); + } } } \ No newline at end of file diff --git a/src/main/java/com/litoralregas/openvpn/router/RouterService.java b/src/main/java/com/litoralregas/openvpn/router/RouterService.java index d14ad7e..36d349d 100644 --- a/src/main/java/com/litoralregas/openvpn/router/RouterService.java +++ b/src/main/java/com/litoralregas/openvpn/router/RouterService.java @@ -75,4 +75,11 @@ public class RouterService { case REMOVED -> false; }; } + + public Router forceStatus(UUID id, RouterStatus status) { + Router router = findById(id); + router.setStatus(status); + router.setUpdatedAt(LocalDateTime.now()); + return repository.save(router); + } } \ No newline at end of file