Skip to content

Commit

Permalink
Merge pull request #3349 from GDLMadushanka/metadata
Browse files Browse the repository at this point in the history
Enable dynamic configuration of API metadata file
  • Loading branch information
GDLMadushanka authored May 29, 2024
2 parents 5272914 + 46dd4c0 commit 75a93e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public class Constants {
public static final String MI_HOST = "MI_HOST";
public static final String MI_PORT = "MI_PORT";
public static final String MI_URL = "MI_URL";
public static final String MI_GROUP_ID = "MI_GROUP_ID";
public static final String HOST = "{MI_HOST}";
public static final String PORT = "{MI_PORT}";
public static final String URL = "{MI_URL}";
public static final String GROUP_ID = "{MI_GROUP_ID}";
public static final String GROUP_ID_REGEX = "\\{MI_GROUP_ID\\}";
public static final String SERVICE_URL = "serviceUrl";
public static final String SERVICE_KEY = "serviceKey";
public static final String METADATA_KEY = "key";
public static final String METADATA_NAME = "name";
public static final String METADATA_DISPLAY_NAME = "displayName";
public static final String METADATA_DESCRIPTION = "description";
public static final String MD5 = "md5";
public static final String VERIFIER = "verifier";
public static final String LIST_STRING = "list";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class ServiceCatalogUtils {
private static List<ServiceMetaDataHolder> md5List = new ArrayList<>();
private static Boolean alreadyUploaded = false;
private static String resolvedHostName;
private static String resolvedGroupId;
private static String httpListenerPort;
private static String httpsListenerPort;
private static String resolvedUrl;
Expand Down Expand Up @@ -161,6 +162,39 @@ private static String updateServiceUrl(String currentUrl) throws ResolverExcepti
return currentUrl;
}

/**
* Dynamically resolve the {MI_GROUP_ID} placeholder in the metadata file using env variables.
* @param yaml metadata file.
*/
private static void replaceGroupIdPlaceholder(Map<String, Object> yaml) {
SystemResolver resolver = new SystemResolver();
if (resolvedGroupId == null) {
try {
resolver.setVariable(MI_GROUP_ID);
resolvedGroupId = resolver.resolve();
} catch (ResolverException e) {
// if the env variable is not set, use the existing value
return;
}
}
String key = (String) yaml.get(METADATA_KEY);
if (key.contains(GROUP_ID)) {
yaml.put(METADATA_KEY, key.replace(GROUP_ID, resolvedGroupId));
}
String name = (String) yaml.get(METADATA_NAME);
if (name.contains(GROUP_ID)) {
yaml.put(METADATA_NAME, name.replace(GROUP_ID, resolvedGroupId));
}
String displayName = (String) yaml.get(METADATA_DISPLAY_NAME);
if (displayName.contains(GROUP_ID)) {
yaml.put(METADATA_DISPLAY_NAME, displayName.replace(GROUP_ID, resolvedGroupId));
}
String description = (String) yaml.get(METADATA_DESCRIPTION);
if (description.contains(GROUP_ID)) {
yaml.put(METADATA_DESCRIPTION, description.replaceAll(GROUP_ID_REGEX, resolvedGroupId));
}
}

/**
* Update the serviceUrl of the given metadata file (if required) and return its key value.
*
Expand All @@ -175,6 +209,7 @@ public static String updateMetadataWithServiceUrl(File yamlFile) throws IOExcept
Map<String, Object> obj = (Map<String, Object>) yaml.load(yamlStream);
String currentServiceUrl = (String) obj.get(SERVICE_URL);
obj.put(SERVICE_URL, updateServiceUrl(currentServiceUrl));
replaceGroupIdPlaceholder(obj);

// Additional configurations
DumperOptions options = new DumperOptions();
Expand Down

0 comments on commit 75a93e4

Please sign in to comment.