Skip to content

Commit 525870b

Browse files
authored
Unleashklient (#43)
* Lagt til UnleashService for å kunne toggle funksjonalitet
1 parent 9d660ad commit 525870b

File tree

9 files changed

+186
-0
lines changed

9 files changed

+186
-0
lines changed

.github/pull_request_template.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Hvorfor er denne endringen nødvendig? ✨

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ include("log")
99
include("pdl")
1010
include("sikkerhet")
1111
include("util")
12+
include("unleash")
1213
include("test-util")

unleash/build.gradle.kts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
kotlin("plugin.spring") version "1.9.22"
3+
}
4+
5+
dependencies {
6+
implementation("org.springframework.boot:spring-boot")
7+
implementation("org.springframework.boot:spring-boot-autoconfigure")
8+
api("io.getunleash:unleash-client-java:9.2.0")
9+
}
10+
11+
tasks.sourcesJar {
12+
duplicatesStrategy = DuplicatesStrategy.WARN
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package no.nav.tilleggsstonader.libs.unleash
2+
3+
import io.getunleash.DefaultUnleash
4+
import io.getunleash.UnleashContext
5+
import io.getunleash.Variant
6+
import io.getunleash.strategy.Strategy
7+
import io.getunleash.util.UnleashConfig
8+
9+
internal class DefaultUnleashService(
10+
apiUrl: String,
11+
apiToken: String,
12+
appName: String,
13+
strategies: List<Strategy>,
14+
) : UnleashService {
15+
16+
private val defaultUnleash: DefaultUnleash = DefaultUnleash(
17+
UnleashConfig.builder()
18+
.appName(appName)
19+
.unleashAPI("$apiUrl/api")
20+
.apiKey(apiToken)
21+
.unleashContextProvider {
22+
UnleashContext.builder().appName(appName).build()
23+
}.build(),
24+
*strategies.toTypedArray(),
25+
)
26+
27+
override fun isEnabled(
28+
toggle: ToggleId,
29+
defaultValue: Boolean,
30+
): Boolean {
31+
return defaultUnleash.isEnabled(toggle.toggleId, defaultValue)
32+
}
33+
34+
override fun isEnabled(
35+
toggle: ToggleId,
36+
properties: Map<String, String>,
37+
): Boolean {
38+
val builder = UnleashContext.builder()
39+
properties.forEach { property -> builder.addProperty(property.key, property.value) }
40+
return defaultUnleash.isEnabled(toggle.toggleId, builder.build())
41+
}
42+
43+
override fun getVariant(toggle: ToggleId, defaultValue: Variant): Variant {
44+
return defaultUnleash.getVariant(toggle.toggleId, defaultValue)
45+
}
46+
47+
override fun destroy() {
48+
// Spring trigger denne ved shutdown. Gjøres for å unngå at unleash fortsetter å gjøre kall ut
49+
defaultUnleash.shutdown()
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package no.nav.tilleggsstonader.libs.unleash
2+
3+
import io.getunleash.Variant
4+
5+
/**
6+
* Dummyservice når unleash er disabled
7+
*/
8+
internal class DummyUnleashService : UnleashService {
9+
override fun isEnabled(
10+
toggle: ToggleId,
11+
properties: Map<String, String>,
12+
): Boolean {
13+
return isEnabled(toggle, false)
14+
}
15+
16+
override fun isEnabled(
17+
toggle: ToggleId,
18+
defaultValue: Boolean,
19+
): Boolean {
20+
return System.getenv(toggle.toggleId).run { toBoolean() }
21+
}
22+
23+
override fun getVariant(toggle: ToggleId, defaultValue: Variant): Variant {
24+
error("Støtter ikke variant i ${this.javaClass::getSimpleName}")
25+
}
26+
27+
override fun destroy() {
28+
// Dummy featureToggleService trenger ikke destroy, då den ikke har en unleash å lukke
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package no.nav.tilleggsstonader.libs.unleash
2+
3+
interface ToggleId {
4+
val toggleId: String
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package no.nav.tilleggsstonader.libs.unleash
2+
3+
import io.getunleash.strategy.Strategy
4+
import org.slf4j.LoggerFactory
5+
import org.springframework.beans.factory.annotation.Value
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
7+
import org.springframework.context.annotation.Bean
8+
import org.springframework.context.annotation.Configuration
9+
10+
@Configuration
11+
class UnleashConfiguration(
12+
private val strategies: List<Strategy>,
13+
) {
14+
private val logger = LoggerFactory.getLogger(UnleashConfiguration::class.java)
15+
16+
@Bean
17+
@ConditionalOnProperty("unleash.enabled", havingValue = "true", matchIfMissing = true)
18+
fun unleashService(
19+
@Value("\${UNLEASH_SERVER_API_URL}") apiUrl: String,
20+
@Value("\${UNLEASH_SERVER_API_TOKEN}") apiToken: String,
21+
@Value("\${NAIS_APP_NAME}") appName: String,
22+
): UnleashService {
23+
logger.info("Oppretter FeatureToggleService med strategies:${strategies.map { it.javaClass.simpleName }}")
24+
return DefaultUnleashService(
25+
apiUrl = apiUrl,
26+
apiToken = apiToken,
27+
appName = appName,
28+
strategies = strategies,
29+
)
30+
}
31+
32+
@Bean
33+
@ConditionalOnProperty("unleash.enabled", havingValue = "false", matchIfMissing = false)
34+
fun dummyUnleashService(): UnleashService {
35+
logger.warn(
36+
"Funksjonsbryter-funksjonalitet er skrudd AV. " +
37+
"isEnabled gir 'false' med mindre man har oppgitt en annen default verdi.",
38+
)
39+
return DummyUnleashService()
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package no.nav.tilleggsstonader.libs.unleash
2+
3+
import io.getunleash.Variant
4+
import org.springframework.beans.factory.DisposableBean
5+
6+
interface UnleashService : DisposableBean {
7+
fun isEnabled(toggle: ToggleId): Boolean {
8+
return isEnabled(toggle, false)
9+
}
10+
11+
fun isEnabled(
12+
toggle: ToggleId,
13+
properties: Map<String, String>,
14+
): Boolean
15+
16+
fun isEnabled(
17+
toggle: ToggleId,
18+
defaultValue: Boolean,
19+
): Boolean
20+
21+
fun getVariant(toggle: ToggleId, defaultValue: Variant): Variant
22+
23+
fun getVariant(toggle: ToggleId): Variant {
24+
return getVariant(toggle, Variant.DISABLED_VARIANT)
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package no.nav.tilleggsstonader.libs.unleash
2+
3+
private val unleashService: UnleashService =
4+
DefaultUnleashService(
5+
apiUrl = "https://tilleggsstonader-unleash-api.nav.cloud.nais.io",
6+
apiToken = System.getenv("UNLEASH_API_TOKEN"),
7+
appName = "tilleggsstonader-sak",
8+
strategies = emptyList(),
9+
)
10+
11+
fun main() {
12+
println(unleashService.getVariant(Toggle.ROUTING_TILSYN_BARN))
13+
println(unleashService.isEnabled(Toggle.ROUTING_TILSYN_BARN))
14+
}
15+
16+
enum class Toggle(override val toggleId: String) : ToggleId {
17+
ROUTING_TILSYN_BARN("sak.soknad-routing.tilsyn-barn"),
18+
}

0 commit comments

Comments
 (0)