Skip to content

Commit 2e472d0

Browse files
ysaito1001Zelda Hessler
andauthored
Make service config just contain a FrozenLayer in the orchestrator mode (#2762)
## Motivation and Context Service config structs now only contain a `aws_smithy_types::config_bag::FrozenLayer` in the orchestrator mode (no changes for the middleware mode). ## Description This PR reduces the individual fields of service configs to contain just `FrozenLayer`. This makes service configs work more seamlessly with runtime plugins in the orchestrator mode. Note that service config _builder_ s still contain individual fields. We're planning to make them just contain `aws_smithy_types::config_bag::Layer`. To do that, though, we need to make `Layer` cloneable and that will be handled in a separate PR. For builders, the only change you will in the PR is that their `build` method will put fields into a `Layer`, freeze it, and pass it to service configs. This PR is marked as a breaking change because it's based on [another PR](#2728) that's also breaking change. ## 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: Yuki Saito <awsaito@amazon.com> Co-authored-by: Zelda Hessler <zhessler@amazon.com>
1 parent b2bdcba commit 2e472d0

File tree

53 files changed

+1203
-453
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1203
-453
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
allowed_external_types = [
22
"aws_smithy_async::rt::sleep::SharedAsyncSleep",
3+
"aws_smithy_types::config_bag::storable::Storable",
4+
"aws_smithy_types::config_bag::storable::StoreReplace",
5+
"aws_smithy_types::config_bag::storable::Storer",
36
]

aws/rust-runtime/aws-credential-types/src/cache.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub use lazy_caching::Builder as LazyBuilder;
1414
use no_caching::NoCredentialsCache;
1515

1616
use crate::provider::{future, SharedCredentialsProvider};
17+
use aws_smithy_types::config_bag::{Storable, StoreReplace};
1718
use std::sync::Arc;
1819

1920
/// Asynchronous Cached Credentials Provider
@@ -62,6 +63,10 @@ impl ProvideCachedCredentials for SharedCredentialsCache {
6263
}
6364
}
6465

66+
impl Storable for SharedCredentialsCache {
67+
type Storer = StoreReplace<SharedCredentialsCache>;
68+
}
69+
6570
#[derive(Clone, Debug)]
6671
pub(crate) enum Inner {
6772
Lazy(lazy_caching::Builder),
@@ -122,3 +127,7 @@ impl CredentialsCache {
122127
}
123128
}
124129
}
130+
131+
impl Storable for CredentialsCache {
132+
type Storer = StoreReplace<CredentialsCache>;
133+
}

aws/rust-runtime/aws-credential-types/src/provider.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ construct credentials from hardcoded values.
7272
//! ```
7373
7474
use crate::Credentials;
75+
use aws_smithy_types::config_bag::{Storable, StoreReplace};
7576
use std::sync::Arc;
7677

7778
/// Credentials provider errors
@@ -350,3 +351,7 @@ impl ProvideCredentials for SharedCredentialsProvider {
350351
self.0.provide_credentials()
351352
}
352353
}
354+
355+
impl Storable for SharedCredentialsProvider {
356+
type Storer = StoreReplace<SharedCredentialsProvider>;
357+
}

aws/rust-runtime/aws-types/external-types.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ allowed_external_types = [
22
"aws_credential_types::cache::CredentialsCache",
33
"aws_credential_types::provider::SharedCredentialsProvider",
44
"aws_smithy_async::rt::sleep::SharedAsyncSleep",
5-
"aws_smithy_async::time::TimeSource",
65
"aws_smithy_async::time::SharedTimeSource",
6+
"aws_smithy_async::time::TimeSource",
77
"aws_smithy_client::http_connector",
88
"aws_smithy_client::http_connector::HttpConnector",
99
"aws_smithy_http::endpoint::Endpoint",
1010
"aws_smithy_http::endpoint::EndpointPrefix",
1111
"aws_smithy_http::endpoint::error::InvalidEndpointError",
12+
"aws_smithy_types::config_bag::storable::Storable",
13+
"aws_smithy_types::config_bag::storable::StoreReplace",
14+
"aws_smithy_types::config_bag::storable::Storer",
1215
"aws_smithy_types::retry::RetryConfig",
1316
"aws_smithy_types::timeout::TimeoutConfig",
1417
"http::uri::Uri",

aws/rust-runtime/aws-types/src/app_name.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
//! New-type for a configurable app name.
77
8+
use aws_smithy_types::config_bag::{Storable, StoreReplace};
89
use std::borrow::Cow;
910
use std::error::Error;
1011
use std::fmt;
@@ -38,6 +39,10 @@ impl fmt::Display for AppName {
3839
}
3940
}
4041

42+
impl Storable for AppName {
43+
type Storer = StoreReplace<AppName>;
44+
}
45+
4146
impl AppName {
4247
/// Creates a new app name.
4348
///
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
//! Newtypes for endpoint-related parameters
7+
//!
8+
//! Parameters require newtypes so they have distinct types when stored in layers in config bag.
9+
10+
use aws_smithy_types::config_bag::{Storable, StoreReplace};
11+
12+
/// Newtype for `use_fips`
13+
#[derive(Clone, Debug)]
14+
pub struct UseFips(pub bool);
15+
impl Storable for UseFips {
16+
type Storer = StoreReplace<UseFips>;
17+
}
18+
19+
/// Newtype for `use_dual_stack`
20+
#[derive(Clone, Debug)]
21+
pub struct UseDualStack(pub bool);
22+
impl Storable for UseDualStack {
23+
type Storer = StoreReplace<UseDualStack>;
24+
}
25+
26+
/// Newtype for `endpoint_url`
27+
#[derive(Clone, Debug)]
28+
pub struct EndpointUrl(pub String);
29+
impl Storable for EndpointUrl {
30+
type Storer = StoreReplace<EndpointUrl>;
31+
}

aws/rust-runtime/aws-types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
pub mod app_name;
1818
pub mod build_metadata;
19+
pub mod endpoint_config;
1920
#[doc(hidden)]
2021
pub mod os_shim_internal;
2122
pub mod region;

aws/rust-runtime/aws-types/src/region.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
//! Region type for determining the endpoint to send requests to.
77
8+
use aws_smithy_types::config_bag::{Storable, StoreReplace};
89
use std::borrow::Cow;
910
use std::fmt::{Display, Formatter};
1011

@@ -35,6 +36,10 @@ impl Display for Region {
3536
}
3637
}
3738

39+
impl Storable for Region {
40+
type Storer = StoreReplace<Region>;
41+
}
42+
3843
impl Region {
3944
/// Creates a new `Region` from the given string.
4045
pub fn new(region: impl Into<Cow<'static, str>>) -> Self {

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialCaches.kt

Lines changed: 91 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CredentialsCacheDecorator : ClientCodegenDecorator {
2727
codegenContext: ClientCodegenContext,
2828
baseCustomizations: List<ConfigCustomization>,
2929
): List<ConfigCustomization> {
30-
return baseCustomizations + CredentialCacheConfig(codegenContext.runtimeConfig)
30+
return baseCustomizations + CredentialCacheConfig(codegenContext)
3131
}
3232

3333
override fun operationCustomizations(
@@ -49,44 +49,65 @@ class CredentialsCacheDecorator : ClientCodegenDecorator {
4949
/**
5050
* Add a `.credentials_cache` field and builder to the `Config` for a given service
5151
*/
52-
class CredentialCacheConfig(runtimeConfig: RuntimeConfig) : ConfigCustomization() {
52+
class CredentialCacheConfig(codegenContext: ClientCodegenContext) : ConfigCustomization() {
53+
private val runtimeConfig = codegenContext.runtimeConfig
54+
private val runtimeMode = codegenContext.smithyRuntimeMode
5355
private val codegenScope = arrayOf(
54-
"cache" to AwsRuntimeType.awsCredentialTypes(runtimeConfig).resolve("cache"),
55-
"provider" to AwsRuntimeType.awsCredentialTypes(runtimeConfig).resolve("provider"),
56+
"CredentialsCache" to AwsRuntimeType.awsCredentialTypes(runtimeConfig).resolve("cache::CredentialsCache"),
5657
"DefaultProvider" to defaultProvider(),
58+
"SharedCredentialsCache" to AwsRuntimeType.awsCredentialTypes(runtimeConfig).resolve("cache::SharedCredentialsCache"),
59+
"SharedCredentialsProvider" to AwsRuntimeType.awsCredentialTypes(runtimeConfig).resolve("provider::SharedCredentialsProvider"),
5760
)
5861

5962
override fun section(section: ServiceConfig) = writable {
6063
when (section) {
61-
ServiceConfig.ConfigStruct -> rustTemplate(
62-
"""pub(crate) credentials_cache: #{cache}::SharedCredentialsCache,""",
63-
*codegenScope,
64-
)
65-
66-
ServiceConfig.ConfigImpl -> rustTemplate(
67-
"""
68-
/// Returns the credentials cache.
69-
pub fn credentials_cache(&self) -> #{cache}::SharedCredentialsCache {
70-
self.credentials_cache.clone()
64+
ServiceConfig.ConfigStruct -> {
65+
if (runtimeMode.defaultToMiddleware) {
66+
rustTemplate(
67+
"""pub(crate) credentials_cache: #{SharedCredentialsCache},""",
68+
*codegenScope,
69+
)
7170
}
72-
""",
73-
*codegenScope,
74-
)
71+
}
72+
73+
ServiceConfig.ConfigImpl -> {
74+
if (runtimeMode.defaultToOrchestrator) {
75+
rustTemplate(
76+
"""
77+
/// Returns the credentials cache.
78+
pub fn credentials_cache(&self) -> #{SharedCredentialsCache} {
79+
self.inner.load::<#{SharedCredentialsCache}>().expect("credentials cache should be set").clone()
80+
}
81+
""",
82+
*codegenScope,
83+
)
84+
} else {
85+
rustTemplate(
86+
"""
87+
/// Returns the credentials cache.
88+
pub fn credentials_cache(&self) -> #{SharedCredentialsCache} {
89+
self.credentials_cache.clone()
90+
}
91+
""",
92+
*codegenScope,
93+
)
94+
}
95+
}
7596

7697
ServiceConfig.BuilderStruct ->
77-
rustTemplate("credentials_cache: Option<#{cache}::CredentialsCache>,", *codegenScope)
98+
rustTemplate("credentials_cache: Option<#{CredentialsCache}>,", *codegenScope)
7899

79100
ServiceConfig.BuilderImpl -> {
80101
rustTemplate(
81102
"""
82103
/// Sets the credentials cache for this service
83-
pub fn credentials_cache(mut self, credentials_cache: #{cache}::CredentialsCache) -> Self {
104+
pub fn credentials_cache(mut self, credentials_cache: #{CredentialsCache}) -> Self {
84105
self.set_credentials_cache(Some(credentials_cache));
85106
self
86107
}
87108
88109
/// Sets the credentials cache for this service
89-
pub fn set_credentials_cache(&mut self, credentials_cache: Option<#{cache}::CredentialsCache>) -> &mut Self {
110+
pub fn set_credentials_cache(&mut self, credentials_cache: Option<#{CredentialsCache}>) -> &mut Self {
90111
self.credentials_cache = credentials_cache;
91112
self
92113
}
@@ -95,29 +116,56 @@ class CredentialCacheConfig(runtimeConfig: RuntimeConfig) : ConfigCustomization(
95116
)
96117
}
97118

98-
ServiceConfig.BuilderBuild -> rustTemplate(
99-
"""
100-
credentials_cache: self
101-
.credentials_cache
102-
.unwrap_or_else({
103-
let sleep = self.sleep_impl.clone();
104-
|| match sleep {
105-
Some(sleep) => {
106-
#{cache}::CredentialsCache::lazy_builder()
107-
.sleep(sleep)
108-
.into_credentials_cache()
109-
}
110-
None => #{cache}::CredentialsCache::lazy(),
111-
}
112-
})
113-
.create_cache(
114-
self.credentials_provider.unwrap_or_else(|| {
115-
#{provider}::SharedCredentialsProvider::new(#{DefaultProvider})
116-
})
117-
),
118-
""",
119-
*codegenScope,
120-
)
119+
ServiceConfig.BuilderBuild -> {
120+
if (runtimeMode.defaultToOrchestrator) {
121+
rustTemplate(
122+
"""
123+
layer.store_put(
124+
self.credentials_cache
125+
.unwrap_or_else({
126+
let sleep = self.sleep_impl.clone();
127+
|| match sleep {
128+
Some(sleep) => {
129+
#{CredentialsCache}::lazy_builder()
130+
.sleep(sleep)
131+
.into_credentials_cache()
132+
}
133+
None => #{CredentialsCache}::lazy(),
134+
}
135+
})
136+
.create_cache(self.credentials_provider.unwrap_or_else(|| {
137+
#{SharedCredentialsProvider}::new(#{DefaultProvider})
138+
})),
139+
);
140+
""",
141+
*codegenScope,
142+
)
143+
} else {
144+
rustTemplate(
145+
"""
146+
credentials_cache: self
147+
.credentials_cache
148+
.unwrap_or_else({
149+
let sleep = self.sleep_impl.clone();
150+
|| match sleep {
151+
Some(sleep) => {
152+
#{CredentialsCache}::lazy_builder()
153+
.sleep(sleep)
154+
.into_credentials_cache()
155+
}
156+
None => #{CredentialsCache}::lazy(),
157+
}
158+
})
159+
.create_cache(
160+
self.credentials_provider.unwrap_or_else(|| {
161+
#{SharedCredentialsProvider}::new(#{DefaultProvider})
162+
})
163+
),
164+
""",
165+
*codegenScope,
166+
)
167+
}
168+
}
121169

122170
else -> emptySection
123171
}

0 commit comments

Comments
 (0)