Skip to content

Commit

Permalink
issue 3824 dynamically calculate param values on server side if confi…
Browse files Browse the repository at this point in the history
…gured (#3826)
  • Loading branch information
SilinPavel authored Dec 12, 2024
1 parent df30807 commit d456438
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Supplier<String>> PLACEHOLDER_TO_FUNCTION =
new HashMap<String, Supplier<String>>() {{
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -454,6 +455,19 @@ public PipelineRun launchPipeline(final PipelineConfiguration configuration, fin
return run;
}

private static void calculateDynamicParameterValues(final PipelineConfiguration configuration) {
final Map<String, PipeConfValueVO> 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)) {
Expand Down

0 comments on commit d456438

Please sign in to comment.