Skip to content

Commit a13f64d

Browse files
amiraliwsillerud
andauthored
la til unleash til tilbakekreving (#1660)
Co-authored-by: Kevin Sillerud <[email protected]>
1 parent e1ebb0b commit a13f64d

File tree

6 files changed

+149
-6
lines changed

6 files changed

+149
-6
lines changed

Diff for: pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@
286286
<version>${springdoc.version}</version>
287287
</dependency>
288288
<dependency>
289-
<groupId>no.nav.familie.felles</groupId>
290-
<artifactId>unleash</artifactId>
291-
<version>${felles.version}</version>
289+
<groupId>io.getunleash</groupId>
290+
<artifactId>unleash-client-java</artifactId>
291+
<version>10.0.1</version>
292292
</dependency>
293293
<dependency>
294294
<groupId>org.messaginghub</groupId>

Diff for: src/main/kotlin/no/nav/familie/tilbake/config/ApplicationConfig.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import java.time.Duration
2525
import java.time.temporal.ChronoUnit
2626

2727
@SpringBootConfiguration
28-
@ComponentScan(ApplicationConfig.PAKKE_NAVN, "no.nav.familie.prosessering", "no.nav.familie.unleash")
28+
@ComponentScan(ApplicationConfig.PAKKE_NAVN, "no.nav.familie.prosessering")
2929
@EnableJwtTokenValidation(ignore = ["org.springframework", "org.springdoc"])
3030
@EnableOAuth2Client(cacheEnabled = true)
3131
@EnableScheduling

Diff for: src/main/kotlin/no/nav/familie/tilbake/config/FeatureToggleConfig.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package no.nav.familie.tilbake.config
22

3+
import io.getunleash.UnleashContext
34
import io.getunleash.strategy.Strategy
4-
import no.nav.familie.unleash.UnleashService
5+
import no.nav.familie.tilbake.unleash.UnleashService
56
import org.springframework.beans.factory.annotation.Value
67
import org.springframework.context.annotation.Bean
78
import org.springframework.context.annotation.Configuration
@@ -43,7 +44,10 @@ class FeatureToggleService(
4344
class ByClusterStrategy(
4445
private val clusterName: String,
4546
) : Strategy {
46-
override fun isEnabled(parameters: MutableMap<String, String>): Boolean {
47+
override fun isEnabled(
48+
parameters: MutableMap<String, String>,
49+
context: UnleashContext,
50+
): Boolean {
4751
if (parameters.isEmpty()) return false
4852
return parameters["cluster"]?.contains(clusterName) ?: false
4953
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package no.nav.familie.tilbake.unleash
2+
3+
import io.getunleash.DefaultUnleash
4+
import io.getunleash.UnleashContext
5+
import io.getunleash.UnleashContextProvider
6+
import io.getunleash.strategy.Strategy
7+
import io.getunleash.util.UnleashConfig
8+
9+
class DefaultUnleashService(
10+
val apiUrl: String,
11+
val apiToken: String,
12+
val appName: String,
13+
val strategies: List<Strategy>,
14+
) : UnleashService {
15+
private val defaultUnleash: DefaultUnleash
16+
17+
init {
18+
19+
defaultUnleash =
20+
DefaultUnleash(
21+
UnleashConfig
22+
.builder()
23+
.appName(appName)
24+
.unleashAPI("$apiUrl/api")
25+
.apiKey(apiToken)
26+
.unleashContextProvider(lagUnleashContextProvider())
27+
.build(),
28+
*strategies.toTypedArray(),
29+
)
30+
}
31+
32+
private fun lagUnleashContextProvider(): UnleashContextProvider =
33+
UnleashContextProvider {
34+
UnleashContext
35+
.builder()
36+
.appName(appName)
37+
.build()
38+
}
39+
40+
override fun isEnabled(
41+
toggleId: String,
42+
defaultValue: Boolean,
43+
): Boolean = defaultUnleash.isEnabled(toggleId, defaultValue)
44+
45+
override fun isEnabled(
46+
toggleId: String,
47+
properties: Map<String, String>,
48+
): Boolean {
49+
val builder = UnleashContext.builder()
50+
properties.forEach { property -> builder.addProperty(property.key, property.value) }
51+
return defaultUnleash.isEnabled(toggleId, builder.build())
52+
}
53+
54+
override fun destroy() {
55+
// Spring trigger denne ved shutdown. Gjøres for å unngå at unleash fortsetter å gjøre kall ut
56+
defaultUnleash.shutdown()
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package no.nav.familie.tilbake.unleash
2+
3+
import io.getunleash.strategy.Strategy
4+
import org.slf4j.LoggerFactory
5+
import org.springframework.beans.factory.DisposableBean
6+
import org.springframework.beans.factory.annotation.Value
7+
import org.springframework.boot.context.properties.ConfigurationProperties
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties
9+
import org.springframework.context.annotation.Bean
10+
import org.springframework.context.annotation.Configuration
11+
12+
@Configuration
13+
@EnableConfigurationProperties(UnleashProperties::class)
14+
open class UnleashConfig(
15+
private val featureToggleProperties: UnleashProperties,
16+
@Value("\${UNLEASH_SERVER_API_URL}") val apiUrl: String,
17+
@Value("\${UNLEASH_SERVER_API_TOKEN}") val apiToken: String,
18+
@Value("\${NAIS_APP_NAME}") val appName: String,
19+
private val strategies: List<Strategy>,
20+
) {
21+
@Bean
22+
open fun unleashNext(): UnleashService =
23+
if (featureToggleProperties.enabled) {
24+
DefaultUnleashService(apiUrl = apiUrl, apiToken = apiToken, appName = appName, strategies = strategies)
25+
} else {
26+
logger.warn(
27+
"Funksjonsbryter-funksjonalitet er skrudd AV. " +
28+
"isEnabled gir 'false' med mindre man har oppgitt en annen default verdi.",
29+
)
30+
lagDummyUnleashService()
31+
}
32+
33+
private fun lagDummyUnleashService(): UnleashService =
34+
object : UnleashService {
35+
override fun isEnabled(
36+
toggleId: String,
37+
properties: Map<String, String>,
38+
): Boolean = isEnabled(toggleId, false)
39+
40+
override fun isEnabled(
41+
toggleId: String,
42+
defaultValue: Boolean,
43+
): Boolean = System.getenv(toggleId).run { toBoolean() } || defaultValue
44+
45+
override fun destroy() {
46+
// Dummy featureToggleService trenger ikke destroy, då den ikke har en unleash å lukke
47+
}
48+
}
49+
50+
companion object {
51+
private val logger = LoggerFactory.getLogger(UnleashConfig::class.java)
52+
}
53+
}
54+
55+
@ConfigurationProperties("unleash")
56+
class UnleashProperties(
57+
val enabled: Boolean = true,
58+
)
59+
60+
interface UnleashService : DisposableBean {
61+
fun isEnabled(toggleId: String): Boolean = isEnabled(toggleId, false)
62+
63+
fun isEnabled(
64+
toggleId: String,
65+
properties: Map<String, String>,
66+
): Boolean
67+
68+
fun isEnabled(
69+
toggleId: String,
70+
defaultValue: Boolean,
71+
): Boolean
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package no.nav.familie.tilbake.unleash
2+
3+
object UnleashContextFields {
4+
const val FAGSAK_ID = "fagsakId"
5+
const val ENHET_ID = "enhetId"
6+
const val NAV_IDENT = "navIdent"
7+
const val EPOST = "epost"
8+
const val BEHANDLING_ID = "behandlingId"
9+
}

0 commit comments

Comments
 (0)