diff --git a/docs/source/connecting/connecting.md b/docs/source/connecting/connecting.md index 5e2719865..1011d4ae1 100644 --- a/docs/source/connecting/connecting.md +++ b/docs/source/connecting/connecting.md @@ -53,7 +53,7 @@ If you need to share `Session` with different threads / Tokio tasks etc. use `Ar ## Metadata -The driver refreshes the cluster metadata periodically, which contains information about cluster topology as well as the cluster schema. By default, the driver refreshes the cluster metadata every 60 seconds. +The driver refreshes the cluster metadata periodically, which contains information about cluster topology as well as the cluster schema. By default, the driver refreshes the cluster metadata every 60 seconds. However, you can set the `cluster_metadata_refresh_interval` to a non-negative value to periodically refresh the cluster metadata. This is useful when you do not have unexpected amount of traffic or when you have an extra traffic causing topology to change frequently. ## Scylla Cloud Serverless @@ -82,6 +82,33 @@ async fn main() -> Result<(), Box> { # } ``` +It is also possible to load the secure connection bundle from anything +which implements read: + +```rust +# extern crate scylla; +# extern crate tokio; +# fn check_only_compiles() { +use std::path::Path; +use std::error::Error; +use std::fs::File; +use scylla::client::session_builder::CloudSessionBuilder; +use scylla::cloud::CloudConfig; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let mut file = File::open("config_data.yaml").unwrap(); + let config = CloudConfig::from_reader(&mut file).unwrap(); + let session = CloudSessionBuilder::from_config(config) + .build() + .await + .unwrap(); + + Ok(()) +} +# } +``` + Note that the bundle file will be provided after the serverless cluster is created. Here is an example of a configuration file for a serverless cluster: @@ -114,4 +141,4 @@ currentContext: default authentication tls -``` \ No newline at end of file +``` diff --git a/scylla/src/client/session_builder.rs b/scylla/src/client/session_builder.rs index cc543f3b1..788c37043 100644 --- a/scylla/src/client/session_builder.rs +++ b/scylla/src/client/session_builder.rs @@ -365,11 +365,10 @@ impl GenericSessionBuilder { // here, but rather in `impl GenericSessionBuilder` block. #[cfg(feature = "cloud")] impl CloudSessionBuilder { - /// Creates a new SessionBuilder with default configuration, - /// based on provided path to Scylla Cloud Config yaml. - pub fn new(cloud_config: impl AsRef) -> Result { + /// Creates a new SessionBuilder with default configuration, based + /// on the provided [`CloudConfig`]. + pub fn from_config(cloud_config: CloudConfig) -> Self { let mut config = SessionConfig::new(); - let cloud_config = CloudConfig::read_from_yaml(cloud_config)?; let mut exec_profile_builder = ExecutionProfile::builder(); if let Some(default_consistency) = cloud_config.get_default_consistency() { exec_profile_builder = exec_profile_builder.consistency(default_consistency); @@ -380,10 +379,17 @@ impl CloudSessionBuilder { } config.default_execution_profile_handle = exec_profile_builder.build().into_handle(); config.cloud_config = Some(Arc::new(cloud_config)); - Ok(CloudSessionBuilder { + CloudSessionBuilder { config, kind: PhantomData, - }) + } + } + + /// Creates a new SessionBuilder with default configuration, + /// based on provided path to Scylla Cloud Config yaml. + pub fn new(cloud_config: impl AsRef) -> Result { + let cloud_config = CloudConfig::read_from_yaml(cloud_config)?; + Ok(Self::from_config(cloud_config)) } } diff --git a/scylla/src/cloud/config.rs b/scylla/src/cloud/config.rs index e90b76fdb..047d784ee 100644 --- a/scylla/src/cloud/config.rs +++ b/scylla/src/cloud/config.rs @@ -102,7 +102,7 @@ impl AuthInfo { } } -/// Contains cloud datacenter configuration for creating TLS connections to its nodes. +/// Contains cloud datacenter configuration for creating TLS connections to its nodes. #[derive(Debug)] pub(crate) struct Datacenter { certificate_authority: X509, @@ -541,11 +541,17 @@ mod deserialize { } impl super::CloudConfig { - pub fn read_from_yaml(config_path: impl AsRef) -> Result { - let mut yaml = File::open(config_path)?; - let config = RawCloudConfig::try_from_reader(&mut yaml)?; + /// Load cloud configuration data from the provided reader. + pub fn from_reader(mut config_reader: R) -> Result { + let config = RawCloudConfig::try_from_reader(&mut config_reader)?; Self::try_from(config) } + + /// Load cloud configuration data from a file. + pub fn read_from_yaml(config_path: impl AsRef) -> Result { + let yaml = File::open(config_path)?; + Self::from_reader(yaml) + } } #[cfg(test)]