Unverified Commit 8dc89631 authored by ysaito1001's avatar ysaito1001 Committed by GitHub
Browse files

Configure orchestrator's key components in builder's build method (#2802)



## Motivation and Context
Moves setting orchestrator components out of `ServiceRuntimePlugin` and
puts it in service config builders' build method.

## Description
This PR is the forth in a series of config refactoring. Here, we move
pieces of code out of `ServiceRuntimePlugin::config` method so that key
orchestrator components meant for the service-level config should only
be constructed once when a service config is created, e.g. during
builder's `build` method. Previously, those components were newly
created every time an operation is invoked.

Wherever `self.handle.conf...` is used, the PR has moved it from
`ServiceRuntimePlugin::config` to the builders' `build` method.

Note that there will be a separate PR to better handle auth resolver &
identity resolver in the context of the ongoing config refactoring.

## Testing
- [x] Passed tests in CI

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: default avatarYuki Saito <awsaito@amazon.com>
parent e6293b22
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -145,11 +145,11 @@ class CredentialCacheConfig(codegenContext: ClientCodegenContext) : ConfigCustom
                if (runtimeMode.defaultToOrchestrator) {
                if (runtimeMode.defaultToOrchestrator) {
                    rustTemplate(
                    rustTemplate(
                        """
                        """
                        self.inner.store_put(
                        layer.store_put(
                            self.inner.load::<#{CredentialsCache}>()
                            layer.load::<#{CredentialsCache}>()
                                .cloned()
                                .cloned()
                                .unwrap_or_else({
                                .unwrap_or_else({
                                    let sleep = self.inner.load::<#{SharedAsyncSleep}>().cloned();
                                    let sleep = layer.load::<#{SharedAsyncSleep}>().cloned();
                                    || match sleep {
                                    || match sleep {
                                        Some(sleep) => {
                                        Some(sleep) => {
                                            #{CredentialsCache}::lazy_builder()
                                            #{CredentialsCache}::lazy_builder()
@@ -159,7 +159,7 @@ class CredentialCacheConfig(codegenContext: ClientCodegenContext) : ConfigCustom
                                        None => #{CredentialsCache}::lazy(),
                                        None => #{CredentialsCache}::lazy(),
                                    }
                                    }
                                })
                                })
                                .create_cache(self.inner.load::<#{SharedCredentialsProvider}>().cloned().unwrap_or_else(|| {
                                .create_cache(layer.load::<#{SharedCredentialsProvider}>().cloned().unwrap_or_else(|| {
                                    #{SharedCredentialsProvider}::new(#{DefaultProvider})
                                    #{SharedCredentialsProvider}::new(#{DefaultProvider})
                                }))
                                }))
                        );
                        );
+0 −16
Original line number Original line Diff line number Diff line
@@ -78,22 +78,6 @@ private class AuthServiceRuntimePluginCustomization(private val codegenContext:
                    // enable the aws-runtime `sign-eventstream` feature
                    // enable the aws-runtime `sign-eventstream` feature
                    addDependency(AwsCargoDependency.awsRuntime(runtimeConfig).withFeature("event-stream").toType().toSymbol())
                    addDependency(AwsCargoDependency.awsRuntime(runtimeConfig).withFeature("event-stream").toType().toSymbol())
                }
                }
                section.putConfigValue(this) {
                    rustTemplate("#{SigningService}::from_static(self.handle.conf.signing_service())", *codegenScope)
                }
                rustTemplate(
                    """
                    if let Some(region) = self.handle.conf.region() {
                        #{put_signing_region}
                    }
                    """,
                    *codegenScope,
                    "put_signing_region" to writable {
                        section.putConfigValue(this) {
                            rustTemplate("#{SigningRegion}::from(region.clone())", *codegenScope)
                        }
                    },
                )
            }
            }


            else -> {}
            else -> {}
+38 −14
Original line number Original line Diff line number Diff line
@@ -14,6 +14,7 @@ import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.OptionalAuthTrait
import software.amazon.smithy.model.traits.OptionalAuthTrait
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.SmithyRuntimeMode
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationSection
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationSection
@@ -54,6 +55,7 @@ class SigV4SigningDecorator : ClientCodegenDecorator {
        return baseCustomizations.extendIf(applies(codegenContext)) {
        return baseCustomizations.extendIf(applies(codegenContext)) {
            SigV4SigningConfig(
            SigV4SigningConfig(
                codegenContext.runtimeConfig,
                codegenContext.runtimeConfig,
                codegenContext.smithyRuntimeMode,
                codegenContext.serviceShape.hasEventStreamOperations(codegenContext.model),
                codegenContext.serviceShape.hasEventStreamOperations(codegenContext.model),
                codegenContext.serviceShape.expectTrait(),
                codegenContext.serviceShape.expectTrait(),
            )
            )
@@ -78,11 +80,19 @@ class SigV4SigningDecorator : ClientCodegenDecorator {


class SigV4SigningConfig(
class SigV4SigningConfig(
    private val runtimeConfig: RuntimeConfig,
    private val runtimeConfig: RuntimeConfig,
    private val runtimeMode: SmithyRuntimeMode,
    private val serviceHasEventStream: Boolean,
    private val serviceHasEventStream: Boolean,
    private val sigV4Trait: SigV4Trait,
    private val sigV4Trait: SigV4Trait,
) : ConfigCustomization() {
) : ConfigCustomization() {
    private val codegenScope = arrayOf(
        "Region" to AwsRuntimeType.awsTypes(runtimeConfig).resolve("region::Region"),
        "SigningService" to AwsRuntimeType.awsTypes(runtimeConfig).resolve("SigningService"),
        "SigningRegion" to AwsRuntimeType.awsTypes(runtimeConfig).resolve("region::SigningRegion"),
    )

    override fun section(section: ServiceConfig): Writable = writable {
    override fun section(section: ServiceConfig): Writable = writable {
        if (section is ServiceConfig.ConfigImpl) {
        when (section) {
            ServiceConfig.ConfigImpl -> {
                if (serviceHasEventStream) {
                if (serviceHasEventStream) {
                    // enable the aws-sig-auth `sign-eventstream` feature
                    // enable the aws-sig-auth `sign-eventstream` feature
                    addDependency(AwsRuntimeType.awsSigAuthEventStream(runtimeConfig).toSymbol())
                    addDependency(AwsRuntimeType.awsSigAuthEventStream(runtimeConfig).toSymbol())
@@ -99,6 +109,20 @@ class SigV4SigningConfig(
                    """,
                    """,
                )
                )
            }
            }
            ServiceConfig.BuilderBuild -> {
                if (runtimeMode.defaultToOrchestrator) {
                    rustTemplate(
                        """
                        layer.put(#{SigningService}::from_static(${sigV4Trait.name.dq()}));
                        layer.load::<#{Region}>().cloned().map(|r| layer.put(#{SigningRegion}::from(r)));
                        """,
                        *codegenScope,
                    )
                }
            }

            else -> emptySection
        }
    }
    }
}
}


+3 −7
Original line number Original line Diff line number Diff line
@@ -104,12 +104,6 @@ class UserAgentDecorator : ClientCodegenDecorator {


        override fun section(section: ServiceRuntimePluginSection): Writable = writable {
        override fun section(section: ServiceRuntimePluginSection): Writable = writable {
            when (section) {
            when (section) {
                is ServiceRuntimePluginSection.AdditionalConfig -> {
                    section.putConfigValue(this) {
                        rust("#T.clone()", ClientRustModule.Meta.toType().resolve("API_METADATA"))
                    }
                }

                is ServiceRuntimePluginSection.RegisterInterceptor -> {
                is ServiceRuntimePluginSection.RegisterInterceptor -> {
                    section.registerInterceptor(runtimeConfig, this) {
                    section.registerInterceptor(runtimeConfig, this) {
                        rust("#T::new()", awsRuntime.resolve("user_agent::UserAgentInterceptor"))
                        rust("#T::new()", awsRuntime.resolve("user_agent::UserAgentInterceptor"))
@@ -212,7 +206,9 @@ class UserAgentDecorator : ClientCodegenDecorator {
                }
                }


                is ServiceConfig.BuilderBuild -> writable {
                is ServiceConfig.BuilderBuild -> writable {
                    if (runtimeMode.defaultToMiddleware) {
                    if (runtimeMode.defaultToOrchestrator) {
                        rust("layer.put(#T.clone());", ClientRustModule.Meta.toType().resolve("API_METADATA"))
                    } else {
                        rust("app_name: self.app_name,")
                        rust("app_name: self.app_name,")
                    }
                    }
                }
                }
+1 −0
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@ internal class SigV4SigningDecoratorTest {
            codegenContext,
            codegenContext,
            SigV4SigningConfig(
            SigV4SigningConfig(
                codegenContext.runtimeConfig,
                codegenContext.runtimeConfig,
                codegenContext.smithyRuntimeMode,
                true,
                true,
                SigV4Trait.builder().name("test-service").build(),
                SigV4Trait.builder().name("test-service").build(),
            ),
            ),
Loading