Skip to content

Commit

Permalink
Lagt til støtte for egendefinerte strategier og mulighet for å sende …
Browse files Browse the repository at this point in the history
…inn egendefinerte "Context"-felter (#725)

* Fikset unleash pom unleash config i parent pom

* Lagt til støtte for custom strategier og strategien byFagsakId

* Fjernet ByFagsakStrategy da vi heller kan bruke custom constraints i UnleashContext
  • Loading branch information
bragejahren authored Sep 5, 2023
1 parent 3bcaf53 commit 1bf096d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@
<artifactId>valutakurs-klient</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>unleash</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
21 changes: 0 additions & 21 deletions unleash/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>

<plugins>
<plugin>
Expand All @@ -56,26 +55,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package no.nav.familie.unleash
import io.getunleash.DefaultUnleash
import io.getunleash.UnleashContext
import io.getunleash.UnleashContextProvider
import io.getunleash.strategy.Strategy
import io.getunleash.util.UnleashConfig

class DefaultUnleashService(
val apiUrl: String,
val apiToken: String,
val appName: String
val appName: String,
val strategies: List<Strategy>,
) : UnleashService {

private val defaultUnleash: DefaultUnleash
Expand All @@ -20,7 +22,8 @@ class DefaultUnleashService(
.appName(appName)
.unleashAPI("$apiUrl/api")
.apiKey(apiToken)
.unleashContextProvider(lagUnleashContextProvider()).build()
.unleashContextProvider(lagUnleashContextProvider()).build(),
*strategies.toTypedArray(),
)
}

Expand All @@ -36,6 +39,12 @@ class DefaultUnleashService(
return defaultUnleash.isEnabled(toggleId, defaultValue)
}

override fun isEnabled(toggleId: String, properties: Map<String, String>): Boolean {
val builder = UnleashContext.builder()
properties.forEach { property -> builder.addProperty(property.key, property.value) }
return defaultUnleash.isEnabled(toggleId, builder.build())
}

override fun destroy() {
// Spring trigger denne ved shutdown. Gjøres for å unngå at unleash fortsetter å gjøre kall ut
defaultUnleash.shutdown()
Expand Down
15 changes: 11 additions & 4 deletions unleash/src/main/kotlin/no/nav/familie/unleash/UnleashConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.familie.unleash

import io.getunleash.strategy.Strategy
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.DisposableBean
import org.springframework.beans.factory.annotation.Value
Expand All @@ -14,23 +15,28 @@ open class UnleashConfig(
private val featureToggleProperties: UnleashProperties,
@Value("\${UNLEASH_SERVER_API_URL}") val apiUrl: String,
@Value("\${UNLEASH_SERVER_API_TOKEN}") val apiToken: String,
@Value("\${NAIS_APP_NAME}") val appName: String
@Value("\${NAIS_APP_NAME}") val appName: String,
private val strategies: List<Strategy>,
) {

@Bean
open fun unleashNext(): UnleashService =
if (featureToggleProperties.enabled) {
DefaultUnleashService(apiUrl = apiUrl, apiToken = apiToken, appName = appName)
DefaultUnleashService(apiUrl = apiUrl, apiToken = apiToken, appName = appName, strategies = strategies)
} else {
logger.warn(
"Funksjonsbryter-funksjonalitet er skrudd AV. " +
"isEnabled gir 'false' med mindre man har oppgitt en annen default verdi."
"isEnabled gir 'false' med mindre man har oppgitt en annen default verdi.",
)
lagDummyUnleashService()
}

private fun lagDummyUnleashService(): UnleashService {
return object : UnleashService {
override fun isEnabled(toggleId: String, properties: Map<String, String>): Boolean {
return isEnabled(toggleId, false)
}

override fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean {
return System.getenv(toggleId).run { toBoolean() } || defaultValue
}
Expand All @@ -49,14 +55,15 @@ open class UnleashConfig(

@ConfigurationProperties("unleash")
class UnleashProperties(
val enabled: Boolean = true
val enabled: Boolean = true,
)

interface UnleashService : DisposableBean {

fun isEnabled(toggleId: String): Boolean {
return isEnabled(toggleId, false)
}
fun isEnabled(toggleId: String, properties: Map<String, String>): Boolean

fun isEnabled(toggleId: String, defaultValue: Boolean): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package no.nav.familie.unleash

object UnleashContextFields {
val FAGSAK_ID = "fagsakId"
}

0 comments on commit 1bf096d

Please sign in to comment.