Skip to content

Commit

Permalink
Fix inability to create service clients when `default-features = fals…
Browse files Browse the repository at this point in the history
…e` (#2055)

* add: tests for config/client construction when default features are disabled
fix: enable users to create service clients when default features are disabled
update: missing HTTP connector panic messages

* Apply suggestions from code review
Co-authored-by: John DiSanti <[email protected]>
  • Loading branch information
Velfi authored Dec 2, 2022
1 parent 5dd5f3f commit b6ab4ea
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion aws/rust-runtime/aws-config/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::Arc;
// unused when all crate features are disabled
/// Unwrap an [`Option<DynConnector>`](aws_smithy_client::erase::DynConnector), and panic with a helpful error message if it's `None`
pub(crate) fn expect_connector(connector: Option<DynConnector>) -> DynConnector {
connector.expect("A connector was not available. Either set a custom connector or enable the `rustls` and `native-tls` crate features.")
connector.expect("No HTTP connector was available. Enable the `rustls` or `native-tls` crate feature or set a connector to fix this.")
}

#[cfg(any(feature = "rustls", feature = "native-tls"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,25 @@ private class AwsFluentClientExtensions(types: Types) {
rustTemplate(
"""
/// Creates a new client from an [SDK Config](#{aws_types}::sdk_config::SdkConfig).
##[cfg(any(feature = "rustls", feature = "native-tls"))]
///
/// ## Panics
///
/// - This method will panic if the `sdk_config` is missing an async sleep implementation. If you experience this panic, set
/// the `sleep_impl` on the Config passed into this function to fix it.
/// - This method will panic if the `sdk_config` is missing an HTTP connector. If you experience this panic, set the
/// `http_connector` on the Config passed into this function to fix it.
pub fn new(sdk_config: &#{aws_types}::sdk_config::SdkConfig) -> Self {
Self::from_conf(sdk_config.into())
}
/// Creates a new client from the service [`Config`](crate::Config).
##[cfg(any(feature = "rustls", feature = "native-tls"))]
///
/// ## Panics
///
/// - This method will panic if the `conf` is missing an async sleep implementation. If you experience this panic, set
/// the `sleep_impl` on the Config passed into this function to fix it.
/// - This method will panic if the `conf` is missing an HTTP connector. If you experience this panic, set the
/// `http_connector` on the Config passed into this function to fix it.
pub fn from_conf(conf: crate::Config) -> Self {
let retry_config = conf.retry_config().cloned().unwrap_or_else(#{RetryConfig}::disabled);
let timeout_config = conf.timeout_config().cloned().unwrap_or_else(#{TimeoutConfig}::disabled);
Expand All @@ -186,11 +198,21 @@ private class AwsFluentClientExtensions(types: Types) {
});
let builder = #{aws_smithy_client}::Builder::new();
let builder = match connector {
// Use provided connector
Some(c) => builder.connector(c),
// Use default connector based on enabled features
None => builder.dyn_https_connector(#{ConnectorSettings}::from_timeout_config(&timeout_config)),
None =>{
##[cfg(any(feature = "rustls", feature = "native-tls"))]
{
// Use default connector based on enabled features
builder.dyn_https_connector(#{ConnectorSettings}::from_timeout_config(&timeout_config))
}
##[cfg(not(any(feature = "rustls", feature = "native-tls")))]
{
panic!("No HTTP connector was available. Enable the `rustls` or `native-tls` crate feature or set a connector to fix this.");
}
}
};
let mut builder = builder
.middleware(#{DynMiddleware}::new(#{Middleware}::new()))
Expand Down
1 change: 1 addition & 0 deletions aws/sdk/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"iam",
"kms",
"lambda",
"no-default-features",
"polly",
"qldbsession",
"s3",
Expand Down
19 changes: 19 additions & 0 deletions aws/sdk/integration-tests/no-default-features/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This Cargo.toml is unused in generated code. It exists solely to enable these tests to compile in-situ
[package]
name = "no-default-features"
version = "0.1.0"
authors = ["Zelda Hessler <[email protected]>"]
description = """
These tests ensure that things will fail (or not fail) as expected
when default features are disabled for all SDK and runtime crates.
"""
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dev-dependencies]
aws-config = { path = "../../build/aws-sdk/sdk/aws-config", default-features = false }
aws-sdk-s3 = { path = "../../build/aws-sdk/sdk/s3", default-features = false }
futures = "0.3.25"
tokio = { version = "1.8.4", features = ["full", "test-util"] }
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// This will fail due to lack of a connector when constructing the SDK Config
#[tokio::test]
#[should_panic(
expected = "No HTTP connector was available. Enable the `rustls` or `native-tls` crate feature or set a connector to fix this."
)]
async fn test_clients_from_sdk_config() {
aws_config::load_from_env().await;
}

// This will fail due to lack of a connector when constructing the service client
#[test]
#[should_panic(
expected = "No HTTP connector was available. Enable the `rustls` or `native-tls` crate feature or set a connector to fix this."
)]
fn test_clients_from_service_config() {
let config = aws_sdk_s3::Config::builder().build();
// This will panic due to the lack of an HTTP connector
aws_sdk_s3::Client::from_conf(config);
}

0 comments on commit b6ab4ea

Please sign in to comment.