-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Legger til integrasjonstest baseklasse med med postgresSql te…
- Loading branch information
Showing
66 changed files
with
2,578 additions
and
1,988 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/no/nav/k9punsj/configuration/DbConfiguration.kt
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,36 @@ | ||
package no.nav.k9punsj.configuration | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class DbConfiguration( | ||
@Value("\${no.nav.db.url}") private val url: String, | ||
@Value("\${no.nav.db.username}") private val username: String, | ||
@Value("\${no.nav.db.password}") private val password: String?, | ||
@Value("\${no.nav.db.vault_mountpath}") private val vaultMountpath: String | ||
) { | ||
|
||
@Bean | ||
fun hikariConfig() = createHikariConfig(url, username, password) | ||
|
||
@Bean | ||
fun isVaultEnabled(): Boolean { | ||
return vaultMountpath.isNotBlank() | ||
} | ||
|
||
@Bean | ||
fun getVaultDbPath(): String { | ||
return vaultMountpath | ||
} | ||
|
||
@Bean | ||
fun databaseName(): String { | ||
return getDbNameFromUrl(url) | ||
} | ||
} | ||
|
||
fun getDbNameFromUrl(urlWithDbName: String): String { | ||
return Regex("(?<=/)[a-zA-Z][-_a-zA-Z\\d]*\$").find(urlWithDbName)?.value ?: "" | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/no/nav/k9punsj/configuration/HikariConfig.kt
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,26 @@ | ||
package no.nav.k9punsj.configuration | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import com.zaxxer.hikari.HikariDataSource | ||
|
||
fun createHikariConfig( | ||
jdbcUrl: String, | ||
username: String? = null, | ||
password: String? = null | ||
) = HikariConfig().apply { | ||
this.jdbcUrl = jdbcUrl | ||
maximumPoolSize = 10 | ||
minimumIdle = 1 | ||
idleTimeout = 10001 | ||
connectionTimeout = 1000 | ||
maxLifetime = 30001 | ||
driverClassName = "org.postgresql.Driver" | ||
username?.let { this.username = it } | ||
password?.let { this.password = it } | ||
isAutoCommit = true | ||
} | ||
|
||
fun hikariConfig(hikariConfig: DbConfiguration): HikariDataSource { | ||
migrate(hikariConfig) | ||
return getDataSource(hikariConfig) | ||
} |
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,49 @@ | ||
package no.nav.k9punsj.configuration | ||
|
||
import com.zaxxer.hikari.HikariDataSource | ||
import no.nav.vault.jdbc.hikaricp.HikariCPVaultUtil | ||
import org.flywaydb.core.Flyway | ||
import org.flywaydb.core.api.output.MigrateResult | ||
import java.util.Locale | ||
import javax.sql.DataSource | ||
|
||
enum class Role { | ||
Admin; | ||
|
||
override fun toString() = name.lowercase(Locale.getDefault()) | ||
} | ||
|
||
fun getDataSource(configuration: DbConfiguration): HikariDataSource = | ||
if (configuration.isVaultEnabled()) { | ||
dataSourceFromVault(configuration, Role.Admin) | ||
} else { | ||
HikariDataSource(configuration.hikariConfig()) | ||
} | ||
|
||
fun dataSourceFromVault(hikariConfig: DbConfiguration, role: Role): HikariDataSource = | ||
HikariCPVaultUtil.createHikariDataSourceWithVaultIntegration( | ||
hikariConfig.hikariConfig(), | ||
hikariConfig.getVaultDbPath(), | ||
"${hikariConfig.databaseName()}-$role" | ||
) | ||
|
||
fun migrate(configuration: DbConfiguration) = | ||
if (configuration.isVaultEnabled()) { | ||
runMigration( | ||
dataSourceFromVault(configuration, Role.Admin), | ||
"SET ROLE \'${configuration.databaseName()}-${Role.Admin}\'" | ||
) | ||
} else { | ||
runMigration(HikariDataSource(configuration.hikariConfig())) | ||
} | ||
|
||
fun loadFlyway(dataSource: DataSource, initSql: String? = null) = | ||
Flyway.configure() | ||
.locations("migreringer/") | ||
.dataSource(dataSource) | ||
.initSql(initSql) | ||
.load()!! | ||
|
||
fun runMigration(dataSource: DataSource, initSql: String? = null): MigrateResult? { | ||
return loadFlyway(dataSource, initSql).migrate() | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/kotlin/no/nav/k9punsj/kafka/KafkaHendelseProducer.kt
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.