-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feat: Add EnvironmentVariable resolving capability to server functi…
…onalities. toFHIR-Web can retrieve the list of related environment variables and the values through /metadata endpoint.
- Loading branch information
Showing
15 changed files
with
135 additions
and
33 deletions.
There are no files selected for viewing
15 changes: 0 additions & 15 deletions
15
tofhir-common/src/main/scala/io/tofhir/common/env/EnvironmentVariableResolver.scala
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...fhir/common/env/EnvironmentVariable.scala → ...fhir/engine/env/EnvironmentVariable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
tofhir-engine/src/main/scala/io/tofhir/engine/env/EnvironmentVariableResolver.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package io.tofhir.engine.env | ||
|
||
import io.tofhir.engine.model.{FhirMappingJob, FhirRepositorySinkSettings, FhirSinkSettings, FileSystemSourceSettings, MappingJobSourceSettings} | ||
|
||
/** | ||
* Provide functions to find and resolve the environment variables used within FhirMappingJob definitions. | ||
* An ENV_VAR is given as ${ENV_VAR} within the MappingJob definition. | ||
*/ | ||
object EnvironmentVariableResolver { | ||
|
||
/** | ||
* Reads the environment variables defined in the EnvironmentVariable object. | ||
* | ||
* @return A Map[String, String] containing the environment variable names and their values. | ||
*/ | ||
def getEnvironmentVariables: Map[String, String] = { | ||
EnvironmentVariable.values | ||
.map(_.toString) // Convert Enumeration values to String (names of the variables) | ||
.flatMap { envVar => | ||
sys.env.get(envVar).map(envVar -> _) // Get the environment variable value if it exists | ||
} | ||
.toMap | ||
} | ||
|
||
/** | ||
* Resolves the environment variables from a given String which is a file content of a FhirMappingJob definition. | ||
* | ||
* @param fileContent The file content potentially containing placeholders for environment variables. | ||
* @return The file content with all recognized environment variables resolved. | ||
*/ | ||
def resolveFileContent(fileContent: String): String = { | ||
EnvironmentVariable.values.foldLeft(fileContent) { (updatedContent, envVar) => | ||
val placeholder = "\\$\\{" + envVar.toString + "\\}" | ||
sys.env.get(envVar.toString) match { | ||
case Some(envValue) => | ||
updatedContent.replaceAll(placeholder, envValue) | ||
case None => | ||
updatedContent // Leave the content unchanged if the environment variable is not set | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Resolves all environment variable placeholders in a FhirMappingJob instance. | ||
* | ||
* @param job FhirMappingJob instance. | ||
* @return A new FhirMappingJob with resolved values. | ||
*/ | ||
def resolveFhirMappingJob(job: FhirMappingJob): FhirMappingJob = { | ||
job.copy( | ||
sourceSettings = resolveSourceSettings(job.sourceSettings), | ||
sinkSettings = resolveSinkSettings(job.sinkSettings) | ||
) | ||
} | ||
|
||
/** | ||
* Resolves environment variables in MappingJobSourceSettings. | ||
* | ||
* @param sourceSettings A map of source settings. | ||
* @return The map with resolved settings. | ||
*/ | ||
private def resolveSourceSettings(sourceSettings: Map[String, MappingJobSourceSettings]): Map[String, MappingJobSourceSettings] = { | ||
sourceSettings.map { case (key, value) => | ||
key -> (value match { | ||
case fs: FileSystemSourceSettings => | ||
fs.copy(dataFolderPath = resolveEnvironmentVariables(fs.dataFolderPath)) | ||
case other => other // No changes for other types | ||
}) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Resolves environment variables in FhirRepositorySinkSettings. | ||
* | ||
* @param sinkSettings FhirSinkSettings instance. | ||
* @return FhirSinkSettings with resolved fhirRepoUrl, if applicable. | ||
*/ | ||
private def resolveSinkSettings(sinkSettings: FhirSinkSettings): FhirSinkSettings = { | ||
sinkSettings match { | ||
case fr: FhirRepositorySinkSettings => | ||
fr.copy(fhirRepoUrl = resolveEnvironmentVariables(fr.fhirRepoUrl)) | ||
case other => other // No changes for other types | ||
} | ||
} | ||
|
||
/** | ||
* Resolves placeholders in the format ${ENV_VAR} with their corresponding values | ||
* from the system environment variables, but only if the variables are part | ||
* of the EnvironmentVariable enumeration. | ||
* | ||
* @param value The string potentially containing placeholders. | ||
* @return The resolved string with placeholders replaced. | ||
*/ | ||
private def resolveEnvironmentVariables(value: String): String = { | ||
val pattern = """\$\{([A-Z_]+)\}""".r | ||
|
||
pattern.replaceAllIn(value, m => { | ||
val envVar = m.group(1) | ||
if (EnvironmentVariable.values.exists(_.toString == envVar)) { | ||
sys.env.getOrElse(envVar, { | ||
throw new RuntimeException(s"Environment variable $envVar is not set.") | ||
}) | ||
} else { | ||
throw new RuntimeException(s"Environment variable $envVar is not recognized in EnvironmentVariable enumeration.") | ||
} | ||
}) | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...fhir/common/model/ICachedRepository.scala → ...engine/repository/ICachedRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package io.tofhir.common.model | ||
package io.tofhir.engine.repository | ||
|
||
/** | ||
* A cached repository. | ||
|
2 changes: 1 addition & 1 deletion
2
...ir-engine/src/main/scala/io/tofhir/engine/repository/mapping/IFhirMappingRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tofhir-server/src/main/scala/io/tofhir/server/repository/job/IJobRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...src/main/scala/io/tofhir/server/repository/terminology/ITerminologySystemRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ain/scala/io/tofhir/server/repository/terminology/TerminologySystemFolderRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters