From 57902ef9ecbbdd659956809ce5338d3546af1db0 Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Fri, 15 Jul 2022 11:34:59 -0400 Subject: [PATCH 1/8] RUST-1253 Add AWS Lambda Examples --- .evergreen/run-aws-tests.sh | 1 + Cargo.toml | 1 + tests/lambda_examples.rs | 71 +++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 tests/lambda_examples.rs diff --git a/.evergreen/run-aws-tests.sh b/.evergreen/run-aws-tests.sh index 0426edcc5..97c8e6d1f 100755 --- a/.evergreen/run-aws-tests.sh +++ b/.evergreen/run-aws-tests.sh @@ -45,3 +45,4 @@ set -o errexit source ./.evergreen/configure-rust.sh RUST_BACKTRACE=1 cargo test --features aws-auth auth_aws::auth_aws +RUST_BACKTRACE=1 cargo test --features aws-auth lambda_example::test_handler diff --git a/Cargo.toml b/Cargo.toml index de5c41052..611bec9c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -162,6 +162,7 @@ derive_more = "0.99.13" function_name = "0.2.1" futures = "0.3" home = "0.5" +lambda_runtime = "0.6.0" pretty_assertions = "1.1.0" serde_json = "1.0.64" semver = "1.0.0" diff --git a/tests/lambda_examples.rs b/tests/lambda_examples.rs new file mode 100644 index 000000000..2095d2c5f --- /dev/null +++ b/tests/lambda_examples.rs @@ -0,0 +1,71 @@ +use async_once::AsyncOnce; +use lambda_runtime::{service_fn, LambdaEvent}; +use lazy_static::lazy_static; +#[cfg(feature = "aws-auth")] +use mongodb::options::{AuthMechanism, ClientOptions, Credential}; +use mongodb::{bson::doc, Client}; +use serde_json::Value; + +#[cfg(not(feature = "aws-auth"))] +// START EXAMPLE 1 +// Initialize a global static MongoDB Client. +// +// The client can be accessed as follows: +// let client = MONGODB_CLIENT.get().await; +lazy_static! { + static ref MONGODB_CLIENT: AsyncOnce = AsyncOnce::new(async { + let uri = std::env::var("MONGODB_URI") + .expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); + Client::with_uri_str(uri) + .await + .expect("Failed to create MongoDB Client") + }); +} + +// Runs a ping operation on the "db" database and returns the response. +async fn handler(_: LambdaEvent) -> Result { + let client = MONGODB_CLIENT.get().await; + let response = client + .database("db") + .run_command(doc! { "ping": 1 }, None) + .await?; + let json = serde_json::to_value(response)?; + Ok(json) +} + +#[tokio::main] +async fn main() -> Result<(), lambda_runtime::Error> { + let service = service_fn(handler); + lambda_runtime::run(service).await?; + Ok(()) +} +// END EXAMPLE 1 + +#[cfg(feature = "aws-auth")] +// START EXAMPLE 2 +// Initialize a global static MongoDB Client with AWS authentication. +// +// The client can be accessed as follows: +// let client = MONGODB_CLIENT.get().await; +lazy_static! { + static ref MONGODB_CLIENT: AsyncOnce = AsyncOnce::new(async { + let uri = std::env::var("MONGODB_URI") + .expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); + let mut options = ClientOptions::parse(uri) + .await + .expect("Failed to parse options from URI"); + let credential = Credential::builder() + .mechanism(AuthMechanism::MongoDbAws) + .build(); + options.credential = Some(credential); + Client::with_options(options).expect("Failed to create MongoDB Client") + }); +} +// END EXAMPLE 2 + +#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] +#[cfg_attr(feature = "async-std-runtime", async_std::test)] +async fn test_handler() { + let event = LambdaEvent::new(Value::Null, Default::default()); + handler(event).await.unwrap(); +} From 169a1c1121d0d461c5c0312498a9f07e77cb4bb7 Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Thu, 4 Aug 2022 13:37:54 -0400 Subject: [PATCH 2/8] refactor --- .evergreen/check-clippy.sh | 2 +- src/test/mod.rs | 2 ++ tests/lambda_examples.rs | 71 -------------------------------------- 3 files changed, 3 insertions(+), 72 deletions(-) delete mode 100644 tests/lambda_examples.rs diff --git a/.evergreen/check-clippy.sh b/.evergreen/check-clippy.sh index 51f2ad8ec..34a0ca213 100755 --- a/.evergreen/check-clippy.sh +++ b/.evergreen/check-clippy.sh @@ -2,7 +2,7 @@ set -o errexit -source ./.evergreen/configure-rust.sh +# source ./.evergreen/configure-rust.sh source ./.evergreen/feature-combinations.sh # Pin clippy to the latest version. This should be updated when new versions of Rust are released. diff --git a/src/test/mod.rs b/src/test/mod.rs index 1a5c370d7..3f02756f9 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -10,6 +10,8 @@ mod db; #[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))] mod documentation_examples; mod index_management; +#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))] +mod lambda_examples; pub mod spec; mod util; diff --git a/tests/lambda_examples.rs b/tests/lambda_examples.rs deleted file mode 100644 index 2095d2c5f..000000000 --- a/tests/lambda_examples.rs +++ /dev/null @@ -1,71 +0,0 @@ -use async_once::AsyncOnce; -use lambda_runtime::{service_fn, LambdaEvent}; -use lazy_static::lazy_static; -#[cfg(feature = "aws-auth")] -use mongodb::options::{AuthMechanism, ClientOptions, Credential}; -use mongodb::{bson::doc, Client}; -use serde_json::Value; - -#[cfg(not(feature = "aws-auth"))] -// START EXAMPLE 1 -// Initialize a global static MongoDB Client. -// -// The client can be accessed as follows: -// let client = MONGODB_CLIENT.get().await; -lazy_static! { - static ref MONGODB_CLIENT: AsyncOnce = AsyncOnce::new(async { - let uri = std::env::var("MONGODB_URI") - .expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); - Client::with_uri_str(uri) - .await - .expect("Failed to create MongoDB Client") - }); -} - -// Runs a ping operation on the "db" database and returns the response. -async fn handler(_: LambdaEvent) -> Result { - let client = MONGODB_CLIENT.get().await; - let response = client - .database("db") - .run_command(doc! { "ping": 1 }, None) - .await?; - let json = serde_json::to_value(response)?; - Ok(json) -} - -#[tokio::main] -async fn main() -> Result<(), lambda_runtime::Error> { - let service = service_fn(handler); - lambda_runtime::run(service).await?; - Ok(()) -} -// END EXAMPLE 1 - -#[cfg(feature = "aws-auth")] -// START EXAMPLE 2 -// Initialize a global static MongoDB Client with AWS authentication. -// -// The client can be accessed as follows: -// let client = MONGODB_CLIENT.get().await; -lazy_static! { - static ref MONGODB_CLIENT: AsyncOnce = AsyncOnce::new(async { - let uri = std::env::var("MONGODB_URI") - .expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); - let mut options = ClientOptions::parse(uri) - .await - .expect("Failed to parse options from URI"); - let credential = Credential::builder() - .mechanism(AuthMechanism::MongoDbAws) - .build(); - options.credential = Some(credential); - Client::with_options(options).expect("Failed to create MongoDB Client") - }); -} -// END EXAMPLE 2 - -#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] -#[cfg_attr(feature = "async-std-runtime", async_std::test)] -async fn test_handler() { - let event = LambdaEvent::new(Value::Null, Default::default()); - handler(event).await.unwrap(); -} From 85198536be6d535df312d16fec7fe62d5005152e Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Thu, 4 Aug 2022 13:39:19 -0400 Subject: [PATCH 3/8] add new files --- src/test/lambda_examples.rs | 5 +++ src/test/lambda_examples/auth.rs | 57 +++++++++++++++++++++++++++++ src/test/lambda_examples/no_auth.rs | 48 ++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 src/test/lambda_examples.rs create mode 100644 src/test/lambda_examples/auth.rs create mode 100644 src/test/lambda_examples/no_auth.rs diff --git a/src/test/lambda_examples.rs b/src/test/lambda_examples.rs new file mode 100644 index 000000000..5255b1283 --- /dev/null +++ b/src/test/lambda_examples.rs @@ -0,0 +1,5 @@ +#![allow(dead_code)] + +#[cfg(feature = "aws-auth")] +mod auth; +mod no_auth; diff --git a/src/test/lambda_examples/auth.rs b/src/test/lambda_examples/auth.rs new file mode 100644 index 000000000..9ad6045f4 --- /dev/null +++ b/src/test/lambda_examples/auth.rs @@ -0,0 +1,57 @@ +use crate as mongodb; + +// begin lambda connection example 2 +use async_once::AsyncOnce; +use lambda_runtime::{service_fn, LambdaEvent}; +use lazy_static::lazy_static; +use mongodb::{ + bson::doc, + options::{AuthMechanism, ClientOptions, Credential}, + Client, +}; +use serde_json::Value; + +// Initialize a global static MongoDB Client with AWS authentication. +// +// The client can be accessed as follows: +// let client = MONGODB_CLIENT.get().await; +lazy_static! { + static ref MONGODB_CLIENT: AsyncOnce = AsyncOnce::new(async { + let uri = std::env::var("MONGODB_URI") + .expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); + let mut options = ClientOptions::parse(uri) + .await + .expect("Failed to parse options from URI"); + let credential = Credential::builder() + .mechanism(AuthMechanism::MongoDbAws) + .build(); + options.credential = Some(credential); + Client::with_options(options).expect("Failed to create MongoDB Client") + }); +} + +// Runs a ping operation on the "db" database and returns the response. +async fn handler(_: LambdaEvent) -> Result { + let client = MONGODB_CLIENT.get().await; + let response = client + .database("db") + .run_command(doc! { "ping": 1 }, None) + .await?; + let json = serde_json::to_value(response)?; + Ok(json) +} + +#[tokio::main] +async fn main() -> Result<(), lambda_runtime::Error> { + let service = service_fn(handler); + lambda_runtime::run(service).await?; + Ok(()) +} + +#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] +#[cfg_attr(feature = "async-std-runtime", async_std::test)] +async fn test_handler() { + let event = LambdaEvent::new(Value::Null, Default::default()); + handler(event).await.unwrap(); +} +// end lambda connection example 2 diff --git a/src/test/lambda_examples/no_auth.rs b/src/test/lambda_examples/no_auth.rs new file mode 100644 index 000000000..c524876eb --- /dev/null +++ b/src/test/lambda_examples/no_auth.rs @@ -0,0 +1,48 @@ +use crate as mongodb; + +// begin lambda connection example 1 +use async_once::AsyncOnce; +use lambda_runtime::{service_fn, LambdaEvent}; +use lazy_static::lazy_static; +use mongodb::{bson::doc, Client}; +use serde_json::Value; + +// Initialize a global static MongoDB Client. +// +// The client can be accessed as follows: +// let client = MONGODB_CLIENT.get().await; +lazy_static! { + static ref MONGODB_CLIENT: AsyncOnce = AsyncOnce::new(async { + let uri = std::env::var("MONGODB_URI") + .expect("MONGODB_URI must be set to the URI of the MongoDB deployment"); + Client::with_uri_str(uri) + .await + .expect("Failed to create MongoDB Client") + }); +} + +// Runs a ping operation on the "db" database and returns the response. +async fn handler(_: LambdaEvent) -> Result { + let client = MONGODB_CLIENT.get().await; + let response = client + .database("db") + .run_command(doc! { "ping": 1 }, None) + .await?; + let json = serde_json::to_value(response)?; + Ok(json) +} + +#[tokio::main] +async fn main() -> Result<(), lambda_runtime::Error> { + let service = service_fn(handler); + lambda_runtime::run(service).await?; + Ok(()) +} +// end lambda connection example 1 + +#[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] +#[cfg_attr(feature = "async-std-runtime", async_std::test)] +async fn test_handler() { + let event = LambdaEvent::new(Value::Null, Default::default()); + handler(event).await.unwrap(); +} From cb67544f090475b0b2cf1339c22901755fc16873 Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Thu, 4 Aug 2022 13:41:55 -0400 Subject: [PATCH 4/8] revert script --- .evergreen/check-clippy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.evergreen/check-clippy.sh b/.evergreen/check-clippy.sh index 34a0ca213..51f2ad8ec 100755 --- a/.evergreen/check-clippy.sh +++ b/.evergreen/check-clippy.sh @@ -2,7 +2,7 @@ set -o errexit -# source ./.evergreen/configure-rust.sh +source ./.evergreen/configure-rust.sh source ./.evergreen/feature-combinations.sh # Pin clippy to the latest version. This should be updated when new versions of Rust are released. From 30dbad529d47437b1a974bf97e8456333889739f Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Thu, 4 Aug 2022 13:45:10 -0400 Subject: [PATCH 5/8] fix test invocation --- .evergreen/run-aws-tests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.evergreen/run-aws-tests.sh b/.evergreen/run-aws-tests.sh index 97c8e6d1f..22a24546f 100755 --- a/.evergreen/run-aws-tests.sh +++ b/.evergreen/run-aws-tests.sh @@ -45,4 +45,4 @@ set -o errexit source ./.evergreen/configure-rust.sh RUST_BACKTRACE=1 cargo test --features aws-auth auth_aws::auth_aws -RUST_BACKTRACE=1 cargo test --features aws-auth lambda_example::test_handler +RUST_BACKTRACE=1 cargo test --features aws-auth lambda_examples::auth::test_handler From 9f93794000138cd5fdb979a84f009a040c00ded7 Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Mon, 8 Aug 2022 13:59:52 -0400 Subject: [PATCH 6/8] create client manually for test --- src/test/lambda_examples/auth.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/test/lambda_examples/auth.rs b/src/test/lambda_examples/auth.rs index 9ad6045f4..5de94f2e9 100644 --- a/src/test/lambda_examples/auth.rs +++ b/src/test/lambda_examples/auth.rs @@ -11,7 +11,9 @@ use mongodb::{ }; use serde_json::Value; -// Initialize a global static MongoDB Client with AWS authentication. +// Initialize a global static MongoDB Client with AWS authentication. The following environment +// variables should also be set: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and, optionally, +// AWS_SESSION_TOKEN. // // The client can be accessed as follows: // let client = MONGODB_CLIENT.get().await; @@ -47,11 +49,23 @@ async fn main() -> Result<(), lambda_runtime::Error> { lambda_runtime::run(service).await?; Ok(()) } +// end lambda connection example 2 + +// Runs a ping operation on the "db" database and returns the response. +async fn handler_create_client(_: LambdaEvent) -> Result { + let uri = std::env::var("MONGODB_URI").unwrap(); + let client = Client::with_uri_str(uri).await.unwrap(); + let response = client + .database("db") + .run_command(doc! { "ping": 1 }, None) + .await?; + let json = serde_json::to_value(response)?; + Ok(json) +} #[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] #[cfg_attr(feature = "async-std-runtime", async_std::test)] async fn test_handler() { let event = LambdaEvent::new(Value::Null, Default::default()); - handler(event).await.unwrap(); + handler_create_client(event).await.unwrap(); } -// end lambda connection example 2 From 533469e7f612c2c01a0027cfd858bf981586c9fb Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Mon, 8 Aug 2022 14:06:38 -0400 Subject: [PATCH 7/8] skip on versioned api --- .evergreen/check-clippy.sh | 2 +- src/test/lambda_examples/no_auth.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.evergreen/check-clippy.sh b/.evergreen/check-clippy.sh index 51f2ad8ec..34a0ca213 100755 --- a/.evergreen/check-clippy.sh +++ b/.evergreen/check-clippy.sh @@ -2,7 +2,7 @@ set -o errexit -source ./.evergreen/configure-rust.sh +# source ./.evergreen/configure-rust.sh source ./.evergreen/feature-combinations.sh # Pin clippy to the latest version. This should be updated when new versions of Rust are released. diff --git a/src/test/lambda_examples/no_auth.rs b/src/test/lambda_examples/no_auth.rs index c524876eb..458449f88 100644 --- a/src/test/lambda_examples/no_auth.rs +++ b/src/test/lambda_examples/no_auth.rs @@ -43,6 +43,9 @@ async fn main() -> Result<(), lambda_runtime::Error> { #[cfg_attr(feature = "tokio-runtime", tokio::test(flavor = "multi_thread"))] #[cfg_attr(feature = "async-std-runtime", async_std::test)] async fn test_handler() { + if std::env::var("MONGODB_API_VERSION").is_ok() { + return; + } let event = LambdaEvent::new(Value::Null, Default::default()); handler(event).await.unwrap(); } From e8cae75014fba69719c80ec406c40108d00006dd Mon Sep 17 00:00:00 2001 From: Isabel Atkinson Date: Mon, 8 Aug 2022 14:07:05 -0400 Subject: [PATCH 8/8] revert script --- .evergreen/check-clippy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.evergreen/check-clippy.sh b/.evergreen/check-clippy.sh index 34a0ca213..51f2ad8ec 100755 --- a/.evergreen/check-clippy.sh +++ b/.evergreen/check-clippy.sh @@ -2,7 +2,7 @@ set -o errexit -# source ./.evergreen/configure-rust.sh +source ./.evergreen/configure-rust.sh source ./.evergreen/feature-combinations.sh # Pin clippy to the latest version. This should be updated when new versions of Rust are released.