non-strict parse of configs

merge-requests/1/merge
zwa 2 years ago
parent 0178a09b80
commit 3bf2015716

@ -1,11 +1,9 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import com.charleskorn.kaml.Yaml
import kotlinx.serialization.json.Json
import org.domaindrivenarchitecture.provs.framework.core.tags.Api
import org.domaindrivenarchitecture.provs.desktop.domain.DesktopConfig
import java.io.BufferedReader
import java.io.FileReader
import org.domaindrivenarchitecture.provs.framework.core.readFromFile
import org.domaindrivenarchitecture.provs.framework.core.yamlToType
import java.io.FileWriter
@ -14,32 +12,19 @@ import java.io.FileWriter
* Throws exceptions FileNotFoundException, SerializationException if file is not found resp. cannot be parsed.
*/
internal fun getConfig(filename: String = "WorkplaceConfig.yaml"): DesktopConfig {
// read file
val inputAsString = BufferedReader(FileReader(filename)).use { it.readText() }
// deserializing
val config =
if (filename.lowercase().endsWith(".yaml")) {
Yaml.default.decodeFromString(DesktopConfig.serializer(), inputAsString)
} else {
Json.decodeFromString(DesktopConfig.serializer(), inputAsString)
}
return config
return readFromFile(filename).yamlToType<DesktopConfig>()
}
@Api
@Suppress("unused")
internal fun writeConfig(config: DesktopConfig, fileName: String = "WorkplaceConfigExample.yaml") {
if (fileName.lowercase().endsWith(".yaml")) {
FileWriter(fileName).use {
it.write(
Yaml.default.encodeToString(
DesktopConfig.serializer(),
config
)
FileWriter(fileName).use {
it.write(
Yaml.default.encodeToString(
DesktopConfig.serializer(),
config
)
}
} else {
FileWriter(fileName).use { it.write(Json.encodeToString(DesktopConfig.serializer(), config)) }
)
}
}

@ -0,0 +1,20 @@
package org.domaindrivenarchitecture.provs.framework.core
import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.serializer
import java.io.BufferedReader
import java.io.FileReader
fun readFromFile(fileName: String): String {
return BufferedReader(FileReader(fileName)).use { it.readText() }
}
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> String.yamlToType() = Yaml(configuration = YamlConfiguration(strictMode = false)).decodeFromString(
T::class.serializer(),
this
)

@ -8,11 +8,9 @@ import org.domaindrivenarchitecture.provs.server.infrastructure.k3s.getK3sConfig
/**
* Installs a k3s server.
* If docker is true, then docker will be installed (may conflict if docker is already existing) and k3s will be installed with docker option.
* If tlsHost is specified, then tls (if configured) also applies to the specified host.
*/
fun Prov.provisionK3s(configFileName: ConfigFileName?) = task {
val k3sConfig: K3sConfig = getK3sConfig(configFileName!!)
val k3sConfig: K3sConfig = getK3sConfig(configFileName)
provisionNetwork(k3sConfig)
if (k3sConfig.reprovision && testConfigExists()) {

@ -1,23 +1,14 @@
package org.domaindrivenarchitecture.provs.server.infrastructure.k3s
import com.charleskorn.kaml.Yaml
import kotlinx.serialization.json.Json
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.server.domain.k3s.*
import java.io.BufferedReader
import java.io.FileReader
import org.domaindrivenarchitecture.provs.framework.core.readFromFile
import org.domaindrivenarchitecture.provs.framework.core.yamlToType
import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig
public fun getK3sConfig(configFileName: ConfigFileName): K3sConfig {
// read file
val inputAsString = BufferedReader(FileReader(configFileName.fileName)).use { it.readText() }
private const val DEFAULT_CONFIG_FILE = "ServerConfig.yaml"
fun getK3sConfig(fileName: ConfigFileName?): K3sConfig {
return readFromFile(fileName?.fileName ?: DEFAULT_CONFIG_FILE).yamlToType()
}
// deserializing
val config =
if (configFileName.fileName.lowercase().endsWith(".yaml")) {
Yaml.default.decodeFromString(K3sConfig.serializer(), inputAsString)
} else {
Json.decodeFromString(K3sConfig.serializer(), inputAsString)
}
return config
}

@ -1,12 +1,11 @@
package org.domaindrivenarchitecture.provs.framework.core.cli
package org.domaindrivenarchitecture.provs.cofiguration.domain
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
import io.mockk.*
import org.domaindrivenarchitecture.provs.configuration.domain.TargetCliCommand
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.Secret
import org.domaindrivenarchitecture.provs.framework.core.cli.createProvInstance
import org.domaindrivenarchitecture.provs.framework.core.cli.retrievePassword
import org.domaindrivenarchitecture.provs.framework.core.local
import org.domaindrivenarchitecture.provs.framework.core.processors.PrintOnlyProcessor
import org.domaindrivenarchitecture.provs.framework.core.remote
@ -20,6 +19,7 @@ internal class CliTargetCommandKtTest {
@BeforeAll
@JvmStatic
internal fun beforeAll() {
mockkObject(Prov)
mockkStatic(::local)
mockkStatic(::remote)
every { remote(any(), any(), any(), any()) } returns Prov.newInstance(PrintOnlyProcessor())
@ -31,6 +31,7 @@ internal class CliTargetCommandKtTest {
@AfterAll
@JvmStatic
internal fun afterAll() {
unmockkObject(Prov)
unmockkStatic(::local)
unmockkStatic(::remote)
unmockkStatic(::retrievePassword)

@ -1,8 +1,10 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import com.charleskorn.kaml.InvalidPropertyValueException
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.SecretSourceType
import org.domaindrivenarchitecture.provs.desktop.domain.WorkplaceType
import org.domaindrivenarchitecture.provs.server.infrastructure.k3s.getK3sConfig
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
@ -31,17 +33,17 @@ internal class ConfigRepositoryKtTest {
@Test
fun getConfig_fails_due_to_invalidProperty() {
assertThrows<InvalidPropertyValueException> {
val exception = assertThrows<InvalidPropertyValueException> {
getConfig("src/test/resources/InvalidWorkplaceConfig.yaml")
}
assertEquals("Value for 'type' is invalid: Value 'WRONGTYPE' is not a valid option, permitted choices are: IDE, MINIMAL, OFFICE", exception.message)
}
@Test
fun getConfig_fails_due_to_non_existing_file() {
assertThrows<FileNotFoundException> {
getConfig("src/test/resources/Idonotexist.yaml")
fun getConfig_fails_due_to_missing_file() {
val exception = assertThrows<FileNotFoundException> {
getK3sConfig(ConfigFileName("src/test/resources/Idonotexist.yaml"))
}
assertEquals("src/test/resources/Idonotexist.yaml (No such file or directory)", exception.message)
}
}

@ -1,9 +1,12 @@
package org.domaindrivenarchitecture.provs.server.infrastructure.k3s
import com.charleskorn.kaml.UnknownPropertyException
import kotlinx.serialization.SerializationException
import org.domaindrivenarchitecture.provs.configuration.domain.ConfigFileName
import org.domaindrivenarchitecture.provs.server.domain.k3s.*
import org.domaindrivenarchitecture.provs.server.domain.CertmanagerEndpoint
import org.domaindrivenarchitecture.provs.server.domain.k3s.Certmanager
import org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig
import org.domaindrivenarchitecture.provs.server.domain.k3s.Loopback
import org.domaindrivenarchitecture.provs.server.domain.k3s.Node
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
@ -33,18 +36,18 @@ internal class ConfigRepositoryTest {
}
@Test
fun getConfig_fails_due_to_invalidProperty() {
assertThrows<UnknownPropertyException> {
fun getConfig_fails_due_to_missing_property() {
val exception = assertThrows<SerializationException> {
getK3sConfig(ConfigFileName("src/test/resources/InvalidWorkplaceConfig.yaml"))
}
assertEquals("Fields [fqdn, node] are required for type with serial name 'org.domaindrivenarchitecture.provs.server.domain.k3s.K3sConfig', but they were missing", exception.message)
}
@Test
fun getConfig_fails_due_to_non_existing_file() {
assertThrows<FileNotFoundException> {
fun getConfig_fails_due_to_missing_file() {
val exception = assertThrows<FileNotFoundException> {
getK3sConfig(ConfigFileName("src/test/resources/Idonotexist.yaml"))
}
assertEquals("src/test/resources/Idonotexist.yaml (No such file or directory)", exception.message)
}
}

@ -1,2 +1 @@
type: WRONGTYPE # IDE, OFFICE or MINIMAL
nonexistingkey: foo
Loading…
Cancel
Save