Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce function to deserialize CloudConfig from Read #1183

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions docs/source/connecting/connecting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,6 +82,33 @@ async fn main() -> Result<(), Box<dyn Error>> {
# }
```

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<dyn Error>> {
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:

Expand Down Expand Up @@ -114,4 +141,4 @@ currentContext: default
authentication
tls

```
```
18 changes: 12 additions & 6 deletions scylla/src/client/session_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,10 @@ impl GenericSessionBuilder<DefaultMode> {
// here, but rather in `impl<K> GenericSessionBuilder<K>` 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<Path>) -> Result<Self, CloudConfigError> {
/// 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);
Expand All @@ -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<Path>) -> Result<Self, CloudConfigError> {
let cloud_config = CloudConfig::read_from_yaml(cloud_config)?;
Ok(Self::from_config(cloud_config))
}
}

Expand Down
14 changes: 10 additions & 4 deletions scylla/src/cloud/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -541,11 +541,17 @@ mod deserialize {
}

impl super::CloudConfig {
pub fn read_from_yaml(config_path: impl AsRef<Path>) -> Result<Self, CloudConfigError> {
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<R: Read>(mut config_reader: R) -> Result<Self, CloudConfigError> {
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<Path>) -> Result<Self, CloudConfigError> {
let yaml = File::open(config_path)?;
Self::from_reader(yaml)
}
}

#[cfg(test)]
Expand Down
Loading