From fc64055aa905eecfcbc581a92a292d6be6b8b75c Mon Sep 17 00:00:00 2001 From: ChrisRousey <104754971+ChrisRousey@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:58:38 +0200 Subject: [PATCH] fix(update-deployment-persistence): persist deployment when updating --- .../core/deployment/DeploymentEndpoint.java | 125 ++++++++++-------- 1 file changed, 68 insertions(+), 57 deletions(-) diff --git a/platform-api/src/main/java/io/datacater/core/deployment/DeploymentEndpoint.java b/platform-api/src/main/java/io/datacater/core/deployment/DeploymentEndpoint.java index 66f2f915..355d2fa7 100644 --- a/platform-api/src/main/java/io/datacater/core/deployment/DeploymentEndpoint.java +++ b/platform-api/src/main/java/io/datacater/core/deployment/DeploymentEndpoint.java @@ -252,77 +252,88 @@ public Uni deleteDeployment(@PathParam("uuid") UUID deploymentId) { @Path("{uuid}") public Uni updateDeployment( @PathParam("uuid") UUID deploymentUuid, DeploymentSpec spec) { - Uni deploymentUni = getDeploymentUni(deploymentUuid); - Uni> configList = - ConfigUtilities.getMappedConfigs(spec.configSelector(), dsf); - return configList - .onItem() - .transform(configs -> ConfigUtilities.applyConfigsToDeployment(spec, configs)) - .onItem() - .transformToUni( - combinedSpec -> - Uni.combine() - .all() - .unis(Uni.createFrom().item(combinedSpec), getPipeline(combinedSpec)) - .asTuple()) - .onItem() - .transformToUni( - specAndPipeline -> - Uni.combine() - .all() - .unis( - Uni.createFrom().item(specAndPipeline.getItem2()), - getStream( - specAndPipeline.getItem1(), - StaticConfig.STREAM_IN_CONFIG, - specAndPipeline.getItem2(), - StaticConfig.STREAM_IN), - getStream( - specAndPipeline.getItem1(), - StaticConfig.STREAM_OUT_CONFIG, - specAndPipeline.getItem2(), - StaticConfig.STREAM_OUT), - deploymentUni, - configList, - Uni.createFrom().item(specAndPipeline.getItem1())) - .asTuple()) - .onItem() - .transform( - tuple -> - updateDeployment( - tuple.getItem1(), - tuple.getItem3(), - tuple.getItem2(), - tuple.getItem6(), - tuple.getItem4(), - tuple.getItem5())) - .onFailure() - .transform( - ex -> - new UpdateDeploymentException(StaticConfig.LoggerMessages.DEPLOYMENT_NOT_UPDATED)); - } - private Uni getPipeline(DeploymentSpec deploymentSpec) { return dsf.withTransaction( - (session, transaction) -> + ((session, transaction) -> session - .find(PipelineEntity.class, getPipelineUUIDFromMap(deploymentSpec.deployment())) + .find(DeploymentEntity.class, deploymentUuid) .onItem() .ifNull() .failWith( - new CreateDeploymentException(StaticConfig.LoggerMessages.PIPELINE_NOT_FOUND))); + new CreateDeploymentException(StaticConfig.LoggerMessages.DEPLOYMENT_NOT_FOUND)) + .onItem() + .transformToUni( + entity -> + Uni.combine() + .all() + .unis( + ConfigUtilities.getMappedConfigs(spec.configSelector(), dsf), + Uni.createFrom().item(entity)) + .asTuple()) + .onItem() + .transformToUni( + tuple -> { + DeploymentSpec combinedSpec = + ConfigUtilities.applyConfigsToDeployment(spec, tuple.getItem1()); + return Uni.combine() + .all() + .unis( + Uni.createFrom().item(combinedSpec), + getPipeline(combinedSpec), + Uni.createFrom().item(tuple.getItem1()), + Uni.createFrom().item(tuple.getItem2())) + .asTuple(); + }) + .onItem() + .transformToUni( + tuple -> + Uni.combine() + .all() + .unis( + Uni.createFrom().item(tuple.getItem2()), + getStream( + tuple.getItem1(), + StaticConfig.STREAM_IN_CONFIG, + tuple.getItem2(), + StaticConfig.STREAM_IN), + getStream( + tuple.getItem1(), + StaticConfig.STREAM_OUT_CONFIG, + tuple.getItem2(), + StaticConfig.STREAM_OUT), + Uni.createFrom().item(tuple.getItem4()), + Uni.createFrom().item(tuple.getItem3()), + Uni.createFrom().item(tuple.getItem1())) + .asTuple()) + .onItem() + .transform( + tuple -> { + updateDeployment( + tuple.getItem1(), + tuple.getItem3(), + tuple.getItem2(), + tuple.getItem6(), + tuple.getItem4(), + tuple.getItem5()); + return session.merge(tuple.getItem4()); + }) + .flatMap(deploymentEntity -> deploymentEntity) + .onFailure() + .transform( + ex -> + new UpdateDeploymentException( + StaticConfig.LoggerMessages.DEPLOYMENT_NOT_UPDATED)))); } - private Uni getDeploymentUni(UUID deploymentUuid) { + private Uni getPipeline(DeploymentSpec deploymentSpec) { return dsf.withTransaction( (session, transaction) -> session - .find(DeploymentEntity.class, deploymentUuid) + .find(PipelineEntity.class, getPipelineUUIDFromMap(deploymentSpec.deployment())) .onItem() .ifNull() .failWith( - new CreateDeploymentException( - StaticConfig.LoggerMessages.DEPLOYMENT_NOT_FOUND))); + new CreateDeploymentException(StaticConfig.LoggerMessages.PIPELINE_NOT_FOUND))); } private Uni getStream(