Skip to content

Commit

Permalink
Add ImageUtils and method for loading Yaml from File object
Browse files Browse the repository at this point in the history
  • Loading branch information
im-konge committed May 14, 2024
1 parent 50f81d7 commit d4b982f
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.testframe.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Class containing methods for handling images
*/
public class ImageUtils {
private static final Logger LOGGER = LogManager.getLogger(ImageUtils.class);

private static final Pattern IMAGE_PATTERN_FULL_PATH =
Pattern.compile("^(?<registry>[^/]*)/(?<org>[^/]*)/(?<image>[^:]*):(?<tag>.*)$");
private static final Pattern IMAGE_PATTERN = Pattern.compile("^(?<org>[^/]*)/(?<image>[^:]*):(?<tag>.*)$");

/**
* Method that, for specified {@param image}, replaces the registry, based on the parameters.
*
* @param image image that should be replaced with new values
* @param newRegistry desired registry
*
* @return updated image based on the parameters
*/
public static String changeRegistry(String image, String newRegistry) {
return changeRegistryOrgAndTag(image, newRegistry, null, null);
}

/**
* Method that, for specified {@param image}, replaces the registry and organization, based on the parameters.
*
* @param image image that should be replaced with new values
* @param newRegistry desired registry
* @param newOrg desired organization
*
* @return updated image based on the parameters
*/
public static String changeRegistryAndOrg(String image, String newRegistry, String newOrg) {
return changeRegistryOrgAndTag(image, newRegistry, newOrg, null);
}

/**
* Method that, for specified {@param image}, replaces the registry and tag, based on the parameters.
*
* @param image image that should be replaced with new values
* @param newRegistry desired registry
* @param newTag desired tag
*
* @return updated image based on the parameters
*/
public static String changeRegistryAndTag(String image, String newRegistry, String newTag) {
return changeRegistryOrgAndTag(image, newRegistry, null, newTag);
}

/**
* Method that, for specified {@param image}, replaces the organization, based on the parameters.
*
* @param image image that should be replaced with new values
* @param newOrg desired organization
*
* @return updated image based on the parameters
*/
public static String changeOrg(String image, String newOrg) {
return changeRegistryOrgAndTag(image, null, newOrg, null);
}

/**
* Method that, for specified {@param image}, replaces the organization and tag, based on the parameters.
*
* @param image image that should be replaced with new values
* @param newOrg desired organization
* @param newTag desired tag
*
* @return updated image based on the parameters
*/
public static String changeOrgAndTag(String image, String newOrg, String newTag) {
return changeRegistryOrgAndTag(image, null, newOrg, newTag);
}

/**
* Method that, for specified {@param image}, replaces the tag, based on the parameters.
*
* @param image image that should be replaced with new values
* @param newTag desired tag
*
* @return updated image based on the parameters
*/
public static String changeTag(String image, String newTag) {
return changeRegistryOrgAndTag(image, null, null, newTag);
}

/**
* Method that, for specified {@param image}, replaces the registry, organization, and tag, based on the parameters.
*
* @param image image that should be replaced with new values
* @param newRegistry desired registry
* @param newOrg desired organization
* @param newTag desired tag
*
* @return updated image based on the parameters
*/
public static String changeRegistryOrgAndTag(String image, String newRegistry, String newOrg, String newTag) {
Matcher m = IMAGE_PATTERN_FULL_PATH.matcher(image);

if (m.find()) {
String registry = setImagePropertiesIfNeeded(m.group("registry"), newRegistry);
String org = setImagePropertiesIfNeeded(m.group("org"), newOrg);
String tag = setImagePropertiesIfNeeded(m.group("tag"), newTag);

String newImage = registry + "/" + org + "/" + m.group("image") + ":" + tag;

LOGGER.info("Updating container image to {}", newImage);

return newImage;
}

m = IMAGE_PATTERN.matcher(image);

if (m.find()) {
String registry = newRegistry != null ? newRegistry + "/" : "";
String org = setImagePropertiesIfNeeded(m.group("org"), newOrg);
String tag = setImagePropertiesIfNeeded(m.group("tag"), newTag);

String newImage = registry + org + "/" + m.group("image") + ":" + tag;

LOGGER.info("Updating container image to {}", newImage);

return newImage;
}

return image;
}

private static String setImagePropertiesIfNeeded(String currentValue, String newValue) {
if (newValue != null
&& !newValue.isEmpty()
&& !currentValue.equals(newValue)
) {
return newValue;
}
return currentValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package io.skodjob.testframe.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -77,6 +78,29 @@ public static <T> T configFromYaml(String yamlFile, Class<T> c) {
}
}

/**
* Parses YAML configuration into an object.
*
* @param yamlFile The YAML file.
* @param c The class of the object to parse into.
* @param <T> The type of the object.
*
* @return The parsed object.
*
* @throws IllegalArgumentException if the YAML is invalid.
* @throws RuntimeException if an I/O error occurs.
*/
public static <T> T configFromYaml(File yamlFile, Class<T> c) {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
return mapper.readValue(yamlFile, c);
} catch (InvalidFormatException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Runs a callable function until it passes or the maximum number of retries is reached.
*
Expand Down

0 comments on commit d4b982f

Please sign in to comment.