Skip to content

Commit 1a08d20

Browse files
authored
feat(cluster): add keyspace, auth, and ssl fields to ClusterConfig struct to (#25)
improve configuration flexibility and support for authentication and SSL connections Signed-off-by: matozinho <matozinhof.academico@gmail.com>
1 parent 1dbd2c8 commit 1a08d20

File tree

3 files changed

+97
-23
lines changed

3 files changed

+97
-23
lines changed

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export interface ClusterConfig {
1212
nodes: Array<string>
1313
compression?: Compression
1414
defaultExecutionProfile?: ExecutionProfile
15+
keyspace?: string
16+
auth?: Auth
17+
ssl?: Ssl
1518
}
1619
export const enum Consistency {
1720
Any = 0,

src/cluster/cluster_config/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::cluster::{
2-
cluster_config::compression::Compression, execution_profile::ExecutionProfile,
2+
cluster_config::compression::Compression,
3+
execution_profile::ExecutionProfile,
4+
scylla_cluster::{Auth, Ssl},
35
};
46

57
pub mod compression;
@@ -9,4 +11,9 @@ pub struct ClusterConfig {
911
pub nodes: Vec<String>,
1012
pub compression: Option<Compression>,
1113
pub default_execution_profile: Option<ExecutionProfile>,
14+
15+
// connection fields
16+
pub keyspace: Option<String>,
17+
pub auth: Option<Auth>,
18+
pub ssl: Option<Ssl>,
1219
}

src/cluster/scylla_cluster.rs

Lines changed: 86 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ struct ScyllaCluster {
1414
uri: String,
1515
compression: Option<Compression>,
1616
default_execution_profile: Option<ExecutionProfile>,
17+
18+
// connection fields
19+
connection: Option<ConnectionOptions>,
1720
}
1821

1922
#[napi(object)]
@@ -24,15 +27,15 @@ struct ConnectionOptions {
2427
}
2528

2629
#[napi(object)]
27-
#[derive(Clone)]
28-
struct Auth {
30+
#[derive(Clone, Debug)]
31+
pub struct Auth {
2932
pub username: String,
3033
pub password: String,
3134
}
3235

3336
#[napi(object)]
3437
#[derive(Clone)]
35-
struct Ssl {
38+
pub struct Ssl {
3639
pub ca_filepath: String,
3740
pub verify_mode: Option<VerifyMode>,
3841
}
@@ -55,6 +58,9 @@ impl ScyllaCluster {
5558
nodes,
5659
compression,
5760
default_execution_profile,
61+
keyspace,
62+
auth,
63+
ssl,
5864
} = cluster_config;
5965

6066
let uri = nodes.first().expect("at least one node is required");
@@ -63,6 +69,11 @@ impl ScyllaCluster {
6369
uri: uri.to_string(),
6470
compression,
6571
default_execution_profile,
72+
connection: Some(ConnectionOptions {
73+
keyspace,
74+
auth,
75+
ssl,
76+
}),
6677
}
6778
}
6879

@@ -76,39 +87,92 @@ impl ScyllaCluster {
7687
let mut builder = scylla::SessionBuilder::new().known_node(self.uri.as_str());
7788

7889
// TODO: We need to think of a better way to deal with keyspace possibly being options
79-
let keyspace = match (&keyspace_or_options, &options) {
90+
let keyspace: Result<Option<String>, napi::Error> = match (&keyspace_or_options, &options) {
8091
(Some(Either::A(keyspace)), _) => Ok(Some(keyspace.clone())),
81-
(Some(Either::B(_)), Some(_)) => Err(napi::Error::new(
82-
napi::Status::InvalidArg,
83-
"Options cannot be provided twice",
84-
)),
85-
(Some(Either::B(options)), _) => Ok(options.keyspace.clone()),
86-
(None, Some(options)) => Ok(options.keyspace.clone()),
87-
(None, None) => Ok(None),
92+
(Some(Either::B(options)), _) => {
93+
if options.keyspace.is_none() {
94+
Ok(
95+
self
96+
.connection
97+
.as_ref()
98+
.and_then(|conn| conn.keyspace.clone()),
99+
)
100+
} else {
101+
Ok(options.keyspace.clone())
102+
}
103+
}
104+
(None, Some(options)) => {
105+
if options.keyspace.is_none() {
106+
Ok(
107+
self
108+
.connection
109+
.as_ref()
110+
.and_then(|conn| conn.keyspace.clone()),
111+
)
112+
} else {
113+
Ok(options.keyspace.clone())
114+
}
115+
}
116+
(None, None) => Ok(
117+
self
118+
.connection
119+
.as_ref()
120+
.and_then(|conn| conn.keyspace.clone()),
121+
),
88122
};
89123

90124
let auth = match (&keyspace_or_options, &options) {
91-
(Some(Either::A(_)), Some(options)) => Ok(options.auth.clone()),
125+
(Some(Either::A(_)), Some(options)) => Ok(options.auth.clone()), // when keyspace is provided as a string
126+
(Some(Either::A(_)), None) => Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone())), // when keyspace is provided as a string and options is not provided
127+
(Some(Either::B(options)), None) => {
128+
if options.auth.is_none() {
129+
Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone()))
130+
} else {
131+
Ok(options.auth.clone())
132+
}
133+
} // when keyspace is provided as an object
92134
(Some(Either::B(_)), Some(_)) => Err(napi::Error::new(
93135
napi::Status::InvalidArg,
94136
"Options cannot be provided twice",
95-
)),
96-
(Some(Either::B(options)), None) => Ok(options.auth.clone()),
97-
(None, Some(options)) => Ok(options.auth.clone()),
98-
(None, None) => Ok(None),
99-
(Some(Either::A(_)), None) => Ok(None),
137+
)), // when keyspace is provided as an object and options is already provided
138+
(None, Some(options)) => {
139+
if options.auth.is_none() {
140+
Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone()))
141+
} else {
142+
Ok(options.auth.clone())
143+
}
144+
} // when keyspace is not provided and options is provided (shouldn't happen)
145+
(None, None) => Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone())), // when keyspace is not provided and options is not provided
100146
};
101147

102148
let ssl = match (&keyspace_or_options, &options) {
103-
(Some(Either::A(_)), Some(options)) => Ok(options.ssl.clone()),
149+
(Some(Either::A(_)), Some(options)) => {
150+
if options.ssl.is_none() {
151+
Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone()))
152+
} else {
153+
Ok(options.ssl.clone())
154+
}
155+
},
104156
(Some(Either::B(_)), Some(_)) => Err(napi::Error::new(
105157
napi::Status::InvalidArg,
106158
"Options cannot be provided twice",
107159
)),
108-
(Some(Either::B(options)), None) => Ok(options.ssl.clone()),
109-
(None, Some(options)) => Ok(options.ssl.clone()),
110-
(None, None) => Ok(None),
111-
(Some(Either::A(_)), None) => Ok(None),
160+
(Some(Either::B(options)), None) => {
161+
if options.ssl.is_none() {
162+
Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone()))
163+
} else {
164+
Ok(options.ssl.clone())
165+
}
166+
},
167+
(None, Some(options)) => {
168+
if options.ssl.is_none() {
169+
Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone()))
170+
} else {
171+
Ok(options.ssl.clone())
172+
}
173+
},
174+
(None, None) => Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone())),
175+
(Some(Either::A(_)), None) => Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone())),
112176
};
113177

114178
if let Some(keyspace) = keyspace.clone()? {

0 commit comments

Comments
 (0)