From ceec222e3cfff571ce92f949e79b6d6af2d5ed57 Mon Sep 17 00:00:00 2001 From: Pavel Silin Date: Thu, 12 Dec 2024 15:26:05 +0100 Subject: [PATCH] issue 3824 dynamically calculate param values on server side if configured --- .../DynamicPipelineRunParameterUtils.java | 47 +++++++++++++++++++ .../manager/pipeline/PipelineRunManager.java | 14 ++++++ 2 files changed, 61 insertions(+) create mode 100644 api/src/main/java/com/epam/pipeline/manager/pipeline/DynamicPipelineRunParameterUtils.java diff --git a/api/src/main/java/com/epam/pipeline/manager/pipeline/DynamicPipelineRunParameterUtils.java b/api/src/main/java/com/epam/pipeline/manager/pipeline/DynamicPipelineRunParameterUtils.java new file mode 100644 index 0000000000..4b851b4985 --- /dev/null +++ b/api/src/main/java/com/epam/pipeline/manager/pipeline/DynamicPipelineRunParameterUtils.java @@ -0,0 +1,47 @@ +/* + * Copyright 2024 EPAM Systems, Inc. (https://www.epam.com/) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.epam.pipeline.manager.pipeline; + + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public final class DynamicPipelineRunParameterUtils { + + private DynamicPipelineRunParameterUtils() { + // + } + + private static final Pattern PATTERN = Pattern.compile("\\$\\[([a-zA-Z_]+)]"); + private static final Map> PLACEHOLDER_TO_FUNCTION = + new HashMap>() {{ + put("UUID", () -> UUID.randomUUID().toString()); + }}; + + static String applyDynamicValue(final String value) { + final Matcher valueMatcher = PATTERN.matcher(value); + if (valueMatcher.find()) { + final String dynamicFunctionName = valueMatcher.group(1); + return PLACEHOLDER_TO_FUNCTION.getOrDefault(dynamicFunctionName, () -> null).get(); + } + return null; + } +} diff --git a/api/src/main/java/com/epam/pipeline/manager/pipeline/PipelineRunManager.java b/api/src/main/java/com/epam/pipeline/manager/pipeline/PipelineRunManager.java index 3b941992a5..bb73ac89d5 100644 --- a/api/src/main/java/com/epam/pipeline/manager/pipeline/PipelineRunManager.java +++ b/api/src/main/java/com/epam/pipeline/manager/pipeline/PipelineRunManager.java @@ -408,6 +408,7 @@ public PipelineRun launchPipeline(final PipelineConfiguration configuration, fin messageHelper.getMessage(MessageConstants.ERROR_INSTANCE_DISK_IS_INVALID, instanceDisk)); } + calculateDynamicParameterValues(configuration); adjustInstanceDisk(configuration); checkGPUInstance(configuration, region.getId()); @@ -454,6 +455,19 @@ public PipelineRun launchPipeline(final PipelineConfiguration configuration, fin return run; } + private static void calculateDynamicParameterValues(final PipelineConfiguration configuration) { + final Map parameters = configuration.getParameters(); + for (final PipeConfValueVO parameter : parameters.values()) { + if (!parameter.getType().equals(PipeConfValueVO.DEFAULT_TYPE)) { + continue; + } + final String resolvedValue = DynamicPipelineRunParameterUtils.applyDynamicValue(parameter.getValue()); + if (resolvedValue != null) { + parameter.setValue(resolvedValue); + } + } + } + private void checkGPUInstance(final PipelineConfiguration configuration, final Long regionId) { final String instanceType = configuration.getInstanceType(); if (StringUtils.isNotBlank(instanceType)) {