From 36618d280a84709ef2bc2eb06808208ddeb3bf14 Mon Sep 17 00:00:00 2001 From: sanopsmx Date: Sun, 1 Nov 2020 20:16:30 +0530 Subject: [PATCH] feat(pipeline executions/gate) : Added code to save multiple pipelines at once to sql db. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is part of: spinnaker/spinnaker#6147. Enhanced PipelineController.groovy to Added new rest api method bulksavePipeline(List pipelineList, Boolean staleCheck) This method accepts a list of pipelines json. This method returns a Map object having the below data: { “Successful”: , “Failed”: , “Failed_list”: [ map = new LinkedHashMap(1); + map.put("id", taskId); + Map task = map; + for (int i = 0; i < maxPolls; i++) { + try { + Thread.sleep(intervalMs); + } catch (InterruptedException ignored) { + } + + task = getTask(taskId); + if (new ArrayList<>(Arrays.asList("SUCCEEDED", "TERMINAL")) + .contains((String) task.get("status"))) { + List> bulksaveTasks = (List>) task.get("steps"); + long count = 0; + if (bulksaveTasks != null && !bulksaveTasks.isEmpty()) { + count = + bulksaveTasks.stream() + .filter( + hashmap -> + ("SUCCEEDED".equals((String) hashmap.get("status")) + || "TERMINAL".equals((String) hashmap.get("status")))) + .count(); + } + if (count == 2) { + return task; + } + } + } + return task; + } + public Map createAndWaitForCompletion(Map body, int maxPolls) { return createAndWaitForCompletion(body, maxPolls, 1000); } @@ -127,6 +174,10 @@ public Map createAndWaitForCompletion(Map body) { return createAndWaitForCompletion(body, 32, 1000); } + public Map bulkCreateAndWaitForCompletion(Map body) { + return bulkCreateAndWaitForCompletion(body, 300, 1000); + } + /** @deprecated This pipeline operation does not belong here. */ @Deprecated public Map cancelPipeline(final String id, final String reason) { diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/PipelineController.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/PipelineController.groovy index 9dd75d40d5..003a533c30 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/PipelineController.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/controllers/PipelineController.groovy @@ -96,6 +96,43 @@ class PipelineController { } } + @CompileDynamic + @ApiOperation(value = "Save an array of pipeline definition") + @PostMapping('/bulksave') + Map bulksavePipeline( + @RequestBody List pipelineList, + @RequestParam(value = "staleCheck", required = false, defaultValue = "false") + Boolean staleCheck) { + + def retData = [] + def operation = [ + description: "bulk save pipeline", + application: "bulk save application", + job : [ + [ + type : "savePipeline", + pipeline : (String) Base64.encoder.encodeToString(objectMapper.writeValueAsString(pipelineList).getBytes("UTF-8")), + user : AuthenticatedRequest.spinnakerUser.orElse("anonymous"), + staleCheck: staleCheck, + bulksave : true + ] + ] + ] + + def result = taskService.bulkCreateAndWaitForCompletion(operation) + String resultStatus = result.get("status") + + if (!"SUCCEEDED".equalsIgnoreCase(resultStatus)) { + String exception = result.variables.find { it.key == "exception" }?.value?.details?.errors?.getAt(0) + throw new PipelineException( + exception ?: "Pipeline bulk save operation did not succeed: ${result.get("id", "unknown task id")} (status: ${resultStatus})" + ) + } else { + retData = result.variables.find { it.key == "bulksave"}?.value + } + return retData + } + @ApiOperation(value = "Rename a pipeline definition") @PostMapping('move') void renamePipeline(@RequestBody Map renameCommand) {