Skip to content

Commit 69aebf8

Browse files
engechasChase Engelbrecht
andauthored
Add TenantContext extension (#952)
* Add TenantContext extension Signed-off-by: Chase Engelbrecht <engechas@dev-dsk-engechas-2a-28f138b0.us-west-2.amazon.com> * Generic comment Signed-off-by: Chase Engelbrecht <engechas@dev-dsk-engechas-2a-28f138b0.us-west-2.amazon.com> * Fix tests Signed-off-by: Chase Engelbrecht <engechas@dev-dsk-engechas-2a-28f138b0.us-west-2.amazon.com> --------- Signed-off-by: Chase Engelbrecht <engechas@dev-dsk-engechas-2a-28f138b0.us-west-2.amazon.com> Co-authored-by: Chase Engelbrecht <engechas@dev-dsk-engechas-2a-28f138b0.us-west-2.amazon.com>
1 parent dfc785b commit 69aebf8

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.commons.utils
7+
8+
import kotlin.coroutines.AbstractCoroutineContextElement
9+
import kotlin.coroutines.CoroutineContext
10+
import kotlin.coroutines.coroutineContext
11+
12+
/**
13+
* Coroutine context element that carries the tenant ID for the current request.
14+
* Set when launching a coroutine and read when building SDK requests.
15+
*/
16+
data class TenantContext(val tenantId: String?) : AbstractCoroutineContextElement(TenantContext) {
17+
companion object Key : CoroutineContext.Key<TenantContext>
18+
}
19+
20+
/**
21+
* Retrieves the tenant ID from the current coroutine context.
22+
* Returns null if no [TenantContext] is present or if the tenant ID is not set.
23+
*/
24+
suspend fun currentTenantId(): String? = coroutineContext[TenantContext]?.tenantId
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.commons.utils
7+
8+
import org.junit.jupiter.api.Assertions.assertEquals
9+
import org.junit.jupiter.api.Assertions.assertNull
10+
import org.junit.jupiter.api.Test
11+
import kotlin.coroutines.EmptyCoroutineContext
12+
import kotlin.coroutines.startCoroutine
13+
14+
internal class TenantContextTests {
15+
16+
private fun runSuspend(context: kotlin.coroutines.CoroutineContext = EmptyCoroutineContext, block: suspend () -> Unit) {
17+
var result: Result<Unit>? = null
18+
block.startCoroutine(object : kotlin.coroutines.Continuation<Unit> {
19+
override val context = context
20+
override fun resumeWith(r: Result<Unit>) { result = r }
21+
})
22+
result!!.getOrThrow()
23+
}
24+
25+
@Test
26+
fun `test currentTenantId returns tenant id when set`() {
27+
runSuspend(TenantContext("test-tenant")) {
28+
assertEquals("test-tenant", currentTenantId())
29+
}
30+
}
31+
32+
@Test
33+
fun `test currentTenantId returns null for single tenant deployment`() {
34+
runSuspend {
35+
assertNull(currentTenantId())
36+
}
37+
}
38+
39+
@Test
40+
fun `test currentTenantId returns null when tenant id header is absent`() {
41+
runSuspend(TenantContext(null)) {
42+
assertNull(currentTenantId())
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)