Skip to content

Commit 0d31702

Browse files
committed
feat: provide client config property for region provider
1 parent c40ca4f commit 0d31702

File tree

13 files changed

+126
-30
lines changed

13 files changed

+126
-30
lines changed

aws-runtime/aws-config/api/aws-config.api

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,27 @@ public abstract class aws/sdk/kotlin/runtime/config/AbstractAwsSdkClientFactory
222222
public static synthetic fun fromEnvironment$default (Laws/sdk/kotlin/runtime/config/AbstractAwsSdkClientFactory;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
223223
}
224224

225+
public abstract interface class aws/sdk/kotlin/runtime/config/AwsSdkClientConfig : aws/smithy/kotlin/runtime/client/SdkClientConfig {
226+
public abstract fun getApplicationId ()Ljava/lang/String;
227+
public abstract fun getRegion ()Ljava/lang/String;
228+
public abstract fun getRegionProvider ()Laws/sdk/kotlin/runtime/region/RegionProvider;
229+
public abstract fun getUseDualStack ()Z
230+
public abstract fun getUseFips ()Z
231+
}
232+
233+
public abstract interface class aws/sdk/kotlin/runtime/config/AwsSdkClientConfig$Builder {
234+
public abstract fun getApplicationId ()Ljava/lang/String;
235+
public abstract fun getRegion ()Ljava/lang/String;
236+
public abstract fun getRegionProvider ()Laws/sdk/kotlin/runtime/region/RegionProvider;
237+
public abstract fun getUseDualStack ()Ljava/lang/Boolean;
238+
public abstract fun getUseFips ()Ljava/lang/Boolean;
239+
public abstract fun setApplicationId (Ljava/lang/String;)V
240+
public abstract fun setRegion (Ljava/lang/String;)V
241+
public abstract fun setRegionProvider (Laws/sdk/kotlin/runtime/region/RegionProvider;)V
242+
public abstract fun setUseDualStack (Ljava/lang/Boolean;)V
243+
public abstract fun setUseFips (Ljava/lang/Boolean;)V
244+
}
245+
225246
public final class aws/sdk/kotlin/runtime/config/AwsSdkSetting {
226247
public static final field INSTANCE Laws/sdk/kotlin/runtime/config/AwsSdkSetting;
227248
public final fun getAwsAccessKeyId ()Laws/smithy/kotlin/runtime/config/EnvironmentSetting;

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/AbstractAwsSdkClientFactory.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package aws.sdk.kotlin.runtime.config
77

8-
import aws.sdk.kotlin.runtime.client.AwsSdkClientConfig
98
import aws.sdk.kotlin.runtime.config.compression.resolveDisableRequestCompression
109
import aws.sdk.kotlin.runtime.config.compression.resolveRequestMinCompressionSizeBytes
1110
import aws.sdk.kotlin.runtime.config.endpoints.resolveUseDualStack
@@ -74,7 +73,7 @@ public abstract class AbstractAwsSdkClientFactory<
7473
block?.let(config::apply)
7574

7675
config.logMode = config.logMode ?: ClientSettings.LogMode.resolve(platform = platform)
77-
config.region = config.region ?: resolveRegion(profile = profile)
76+
config.region = config.region ?: config.regionProvider?.getRegion() ?: resolveRegion(profile = profile)
7877
config.useFips = config.useFips ?: resolveUseFips(profile = profile)
7978
config.useDualStack = config.useDualStack ?: resolveUseDualStack(profile = profile)
8079
config.applicationId = config.applicationId ?: resolveUserAgentAppId(platform, profile)

aws-runtime/aws-core/common/src/aws/sdk/kotlin/runtime/client/AwsSdkClientConfig.kt renamed to aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/config/AwsSdkClientConfig.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package aws.sdk.kotlin.runtime.client
6+
package aws.sdk.kotlin.runtime.config
77

88
import aws.smithy.kotlin.runtime.client.SdkClientConfig
99

10+
import aws.sdk.kotlin.runtime.region.RegionProvider
11+
1012
/**
1113
* Base interface all generated AWS SDK Kotlin clients implement
1214
*/
@@ -18,6 +20,16 @@ public interface AwsSdkClientConfig : SdkClientConfig {
1820
* information
1921
*/
2022
public val region: String?
23+
/**
24+
* An optional region provider that determines the AWS region for client operations. When specified, this provider
25+
* takes precedence over the default region provider chain, unless a static region is explicitly configured.
26+
*
27+
* Region Resolution Priority:
28+
* 1. Static region (if specified)
29+
* 2. Custom region provider (if configured)
30+
* 3. Default region provider chain
31+
*/
32+
public val regionProvider: RegionProvider?
2133

2234
/**
2335
* Flag to toggle whether to use [FIPS](https://aws.amazon.com/compliance/fips/) endpoints when making requests.
@@ -54,6 +66,17 @@ public interface AwsSdkClientConfig : SdkClientConfig {
5466
*/
5567
public var region: String?
5668

69+
/**
70+
* An optional region provider that determines the AWS region for client operations. When specified, this provider
71+
* takes precedence over the default region provider chain, unless a static region is explicitly configured.
72+
*
73+
* Region Resolution Priority:
74+
* 1. Static region (if specified)
75+
* 2. Custom region provider (if configured)
76+
* 3. Default region provider chain
77+
*/
78+
public var regionProvider: RegionProvider?
79+
5780
/**
5881
* Flag to toggle whether to use [FIPS](https://aws.amazon.com/compliance/fips/) endpoints when making requests.
5982
* Disabled by default.

aws-runtime/aws-config/jvm/test/aws/sdk/kotlin/runtime/config/AbstractAwsSdkClientFactoryTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
package aws.sdk.kotlin.runtime.config
77

8-
import aws.sdk.kotlin.runtime.client.AwsSdkClientConfig
98
import aws.sdk.kotlin.runtime.config.profile.loadAwsSharedConfig
109
import aws.sdk.kotlin.runtime.config.useragent.resolveUserAgentAppId
1110
import aws.sdk.kotlin.runtime.config.utils.mockPlatform
11+
import aws.sdk.kotlin.runtime.region.RegionProvider
1212
import aws.smithy.kotlin.runtime.client.*
1313
import aws.smithy.kotlin.runtime.retries.StandardRetryStrategy
1414
import aws.smithy.kotlin.runtime.util.PlatformProvider
@@ -129,6 +129,7 @@ private interface TestClient : SdkClient {
129129
override val clientName: String = builder.clientName
130130
override val logMode: LogMode = builder.logMode ?: LogMode.Default
131131
override val region: String? = builder.region
132+
override var regionProvider: RegionProvider? = builder.regionProvider
132133
override var useFips: Boolean = builder.useFips ?: false
133134
override var useDualStack: Boolean = builder.useDualStack ?: false
134135
override val applicationId: String? = builder.applicationId
@@ -141,6 +142,7 @@ private interface TestClient : SdkClient {
141142
override var clientName: String = "Test"
142143
override var logMode: LogMode? = LogMode.Default
143144
override var region: String? = null
145+
override var regionProvider: RegionProvider? = null
144146
override var useFips: Boolean? = null
145147
override var useDualStack: Boolean? = null
146148
override var applicationId: String? = null

aws-runtime/aws-core/api/aws-core.api

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,3 @@ public final class aws/sdk/kotlin/runtime/client/AwsClientOption {
3535
public final fun getRegion ()Laws/smithy/kotlin/runtime/collections/AttributeKey;
3636
}
3737

38-
public abstract interface class aws/sdk/kotlin/runtime/client/AwsSdkClientConfig : aws/smithy/kotlin/runtime/client/SdkClientConfig {
39-
public abstract fun getApplicationId ()Ljava/lang/String;
40-
public abstract fun getRegion ()Ljava/lang/String;
41-
public abstract fun getUseDualStack ()Z
42-
public abstract fun getUseFips ()Z
43-
}
44-
45-
public abstract interface class aws/sdk/kotlin/runtime/client/AwsSdkClientConfig$Builder {
46-
public abstract fun getApplicationId ()Ljava/lang/String;
47-
public abstract fun getRegion ()Ljava/lang/String;
48-
public abstract fun getUseDualStack ()Ljava/lang/Boolean;
49-
public abstract fun getUseFips ()Ljava/lang/Boolean;
50-
public abstract fun setApplicationId (Ljava/lang/String;)V
51-
public abstract fun setRegion (Ljava/lang/String;)V
52-
public abstract fun setUseDualStack (Ljava/lang/Boolean;)V
53-
public abstract fun setUseFips (Ljava/lang/Boolean;)V
54-
}
55-

codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/AwsKotlinDependency.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import software.amazon.smithy.kotlin.codegen.core.isValidVersion
1212
// root namespace for the AWS client-runtime
1313
const val AWS_CLIENT_RT_ROOT_NS = "aws.sdk.kotlin.runtime"
1414

15+
// groupId and version for kotlin coroutines
16+
const val KOTLINX_COROUTINES_GROUP: String = "org.jetbrains.kotlinx"
17+
const val KOTLINX_COROUTINES_VERSION: String = "1.7.3"
18+
1519
private fun getDefaultRuntimeVersion(): String {
1620
// generated as part of the build, see smithy-aws-kotlin-codegen/build.gradle.kts
1721
try {
@@ -38,6 +42,7 @@ object AwsKotlinDependency {
3842
val AWS_CONFIG = KotlinDependency(GradleConfiguration.Api, AWS_CLIENT_RT_ROOT_NS, AWS_CLIENT_RT_GROUP, "aws-config", AWS_CLIENT_RT_VERSION)
3943
val AWS_ENDPOINT = KotlinDependency(GradleConfiguration.Api, "$AWS_CLIENT_RT_ROOT_NS.endpoint", AWS_CLIENT_RT_GROUP, "aws-endpoint", AWS_CLIENT_RT_VERSION)
4044
val AWS_HTTP = KotlinDependency(GradleConfiguration.Implementation, "$AWS_CLIENT_RT_ROOT_NS.http", AWS_CLIENT_RT_GROUP, "aws-http", AWS_CLIENT_RT_VERSION)
45+
val KOTLINX_COROUTINES = KotlinDependency(GradleConfiguration.Implementation, "kotlinx.coroutines", KOTLINX_COROUTINES_GROUP, "kotlinx-coroutines-core", KOTLINX_COROUTINES_VERSION)
4146
}
4247

4348
// remap aws-sdk-kotlin dependencies to project notation

codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/AwsRuntimeTypes.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ object AwsRuntimeTypes {
1616
val AwsErrorMetadata = symbol("AwsErrorMetadata")
1717
val AwsServiceException = symbol("AwsServiceException")
1818
val ClientException = symbol("ClientException")
19+
}
1920

20-
object Client : RuntimeTypePackage(AwsKotlinDependency.AWS_CORE, "client") {
21-
val AwsSdkClientConfig = symbol("AwsSdkClientConfig")
22-
}
21+
object Coroutines : RuntimeTypePackage(AwsKotlinDependency.KOTLINX_COROUTINES) {
22+
val runBlocking = symbol("runBlocking")
2323
}
2424

2525
object Endpoint : RuntimeTypePackage(AwsKotlinDependency.AWS_ENDPOINT) {
@@ -36,6 +36,8 @@ object AwsRuntimeTypes {
3636
object Config : RuntimeTypePackage(AwsKotlinDependency.AWS_CONFIG) {
3737
val AbstractAwsSdkClientFactory = symbol("AbstractAwsSdkClientFactory", "config")
3838

39+
val AwsSdkClientConfig = symbol("AwsSdkClientConfig", "config")
40+
3941
object Endpoints : RuntimeTypePackage(AwsKotlinDependency.AWS_CONFIG, "config.endpoints") {
4042
val AccountIdEndpointMode = symbol("AccountIdEndpointMode")
4143
val resolveEndpointUrl = symbol("resolveEndpointUrl")
@@ -54,6 +56,11 @@ object AwsRuntimeTypes {
5456
val StaticCredentialsProvider = symbol("StaticCredentialsProvider")
5557
val manage = symbol("manage", "auth.credentials.internal", isExtension = true)
5658
}
59+
60+
object Region : RuntimeTypePackage(AwsKotlinDependency.AWS_CONFIG, "region") {
61+
val RegionProvider = symbol("RegionProvider")
62+
val resolveRegion = symbol("resolveRegion")
63+
}
5764
}
5865

5966
object Http : RuntimeTypePackage(AwsKotlinDependency.AWS_HTTP) {

codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/AwsServiceConfigIntegration.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,32 @@ class AwsServiceConfigIntegration : KotlinIntegration {
2424
val RegionProp: ConfigProperty = ConfigProperty {
2525
name = "region"
2626
symbol = KotlinTypes.String.toBuilder().nullable().build()
27-
baseClass = AwsRuntimeTypes.Core.Client.AwsSdkClientConfig
27+
baseClass = AwsRuntimeTypes.Config.AwsSdkClientConfig
2828
useNestedBuilderBaseClass()
2929
documentation = """
3030
The AWS region (e.g. `us-west-2`) to make requests to. See about AWS
3131
[global infrastructure](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/) for more
3232
information
3333
""".trimIndent()
34+
35+
propertyType = ConfigPropertyType.Custom(
36+
render = { prop, writer ->
37+
writer.write("override val #1L: #2T? = builder.#1L ?: #3T{ builder.regionProvider?.getRegion() ?: #4T() }",
38+
prop.propertyName,
39+
prop.symbol,
40+
AwsRuntimeTypes.Coroutines.runBlocking,
41+
AwsRuntimeTypes.Config.Region.resolveRegion
42+
)
43+
}
44+
)
45+
3446
order = -100
3547
}
3648

3749
val UserAgentAppId: ConfigProperty = ConfigProperty {
3850
name = "applicationId"
3951
symbol = KotlinTypes.String.asNullable()
40-
baseClass = AwsRuntimeTypes.Core.Client.AwsSdkClientConfig
52+
baseClass = AwsRuntimeTypes.Config.AwsSdkClientConfig
4153
useNestedBuilderBaseClass()
4254
documentation = """
4355
An optional application specific identifier.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package aws.sdk.kotlin.codegen
2+
3+
import software.amazon.smithy.kotlin.codegen.core.CodegenContext
4+
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
5+
import software.amazon.smithy.kotlin.codegen.model.asNullable
6+
import software.amazon.smithy.kotlin.codegen.rendering.util.ConfigProperty
7+
8+
/**
9+
* Adds region provider integration to the client config
10+
*/
11+
class RegionProviderIntegration : KotlinIntegration {
12+
companion object {
13+
val RegionProviderProp: ConfigProperty = ConfigProperty {
14+
name = "regionProvider"
15+
symbol = AwsRuntimeTypes.Config.Region.RegionProvider.asNullable()
16+
baseClass = AwsRuntimeTypes.Config.AwsSdkClientConfig
17+
useNestedBuilderBaseClass()
18+
documentation = """
19+
An optional region provider that determines the AWS region for client operations. When specified, this provider
20+
takes precedence over the default region provider chain, unless a static region is explicitly configured.
21+
22+
The region resolution order is:
23+
1. Static region (if specified)
24+
2. Custom region provider (if configured)
25+
3. Default region provider chain
26+
""".trimIndent()
27+
}
28+
}
29+
30+
override fun additionalServiceConfigProps(ctx: CodegenContext): List<ConfigProperty> = buildList {
31+
add(RegionProviderProp)
32+
}
33+
}

codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/ServiceClientCompanionObjectWriter.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ServiceClientCompanionObjectWriter : AppendingSectionWriter {
3535
declareSection(FinalizeEnvironmentalConfig) {
3636
write("super.#L(builder, sharedConfig, activeProfile)", funName)
3737
writeResolveEndpointUrl()
38+
// writeRegionWithRegionProvider()
3839
}
3940
}
4041
}
@@ -54,6 +55,16 @@ class ServiceClientCompanionObjectWriter : AppendingSectionWriter {
5455
write("#S,", names.sharedConfigKey)
5556
}
5657
}
58+
59+
// private fun KotlinWriter.writeRegionWithRegionProvider() {
60+
// withBlock(
61+
// "builder.config.region = builder.config.region ?: builder.config.regionProvider?.getRegion() ?: #T(",
62+
// ")",
63+
// AwsRuntimeTypes.Config.Region.resolveRegion,
64+
// ) {
65+
// write("profile = activeProfile")
66+
// }
67+
// }
5768
}
5869

5970
internal data class EndpointUrlConfigNames(

0 commit comments

Comments
 (0)