Repository: AlexPikalov/cdrs (language: rust)

Issue:
Cannot use hostname instead of IP-address
Hi there!

Maybe I have been reading the documentation poorly, in that case I apologize. Found similar issues, but they seem to be resolved.

If I run `let node = NodeTcpConfigBuilder::new("192.168.145.139:9042", NoneAuthenticator {}).build();` this will build and work fine. However, if I run:     
`let node = NodeTcpConfigBuilder::new("database:9042", NoneAuthenticator {}).build();` I get this message from the compiler:
`General("invalid IP address syntax")'`. 

Why can't I use hostname?

My toml
```toml
[dependencies]
cdrs = "2.3.0"
```

Cargo --version
`cargo 1.42.0 (86334295e 2020-01-31)`



Discussion / hints:
Hi @Cronnay,
You are correct host resolving functionality is missed, unfortunatelly.
I'm going to check if simple `SocketAddr::from` before creating a `TcpStream` would help

Fix patch:
diff --git a/examples/crud_operations.rs b/examples/crud_operations.rs
index e49a1b6..2152d9d 100644
--- a/examples/crud_operations.rs
+++ b/examples/crud_operations.rs
@@ -23,7 +23,7 @@ fn main() {
     let user = "user";
     let password = "password";
     let auth = StaticPasswordAuthenticator::new(&user, &password);
-    let node = NodeTcpConfigBuilder::new("127.0.0.1:9042", auth).build();
+    let node = NodeTcpConfigBuilder::new("localhost:9042", auth).build();
     let cluster_config = ClusterTcpConfig(vec![node]);
     let no_compression: CurrentSession =
         new_session(&cluster_config, RoundRobin::new()).expect("session should be created");
diff --git a/src/cluster/ssl_connection_pool.rs b/src/cluster/ssl_connection_pool.rs
index e8ab133..62fd7f1 100644
--- a/src/cluster/ssl_connection_pool.rs
+++ b/src/cluster/ssl_connection_pool.rs
@@ -3,7 +3,7 @@ use r2d2::{Builder, ManageConnection, Pool};
 use std::cell::RefCell;
 use std::error::Error;
 use std::io::Write;
-use std::net::SocketAddr;
+use std::net::{SocketAddr, ToSocketAddrs};
 
 use crate::authenticators::Authenticator;
 use crate::cluster::ConnectionPool;
@@ -39,13 +39,13 @@ pub fn new_ssl_pool<'a, A: Authenticator + Send + Sync + 'static>(
         .build(manager)
         .map_err(|err| error::Error::from(err.description()))?;
 
-    Ok(SslConnectionPool::new(
-        pool,
-        node_config
-            .addr
-            .parse::<SocketAddr>()
-            .map_err(|err| error::Error::from(err.description()))?,
-    ))
+    let addr = node_config
+        .addr
+        .to_socket_addrs()?
+        .next()
+        .ok_or_else(|| error::Error::from("Cannot parse address"))?;
+
+    Ok(SslConnectionPool::new(pool, addr))
 }
 
 /// `r2d2` connection manager.
diff --git a/src/cluster/tcp_connection_pool.rs b/src/cluster/tcp_connection_pool.rs
index 1c13aff..6ef519a 100644
--- a/src/cluster/tcp_connection_pool.rs
+++ b/src/cluster/tcp_connection_pool.rs
@@ -2,7 +2,7 @@ use r2d2::{Builder, ManageConnection};
 use std::cell::RefCell;
 use std::io;
 use std::io::Write;
-use std::net::SocketAddr;
+use std::net::ToSocketAddrs;
 
 use crate::authenticators::Authenticator;
 use crate::cluster::ConnectionPool;
@@ -36,8 +36,9 @@ pub fn new_tcp_pool<'a, A: Authenticator + Send + Sync + 'static>(
 
     let addr = node_config
         .addr
-        .parse::<SocketAddr>()
-        .map_err(|err| error::Error::from(err.to_string()))?;
+        .to_socket_addrs()?
+        .next()
+        .ok_or_else(|| error::Error::from("Cannot parse address"))?;
 
     Ok(TcpConnectionPool::new(pool, addr))
 }


Test patch:
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index a763b70..75b0ea7 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -14,7 +14,7 @@ use cdrs::query::QueryExecutor;
 use regex::Regex;
 
 #[cfg(feature = "e2e-tests")]
-const ADDR: &'static str = "127.0.0.1:9042";
+const ADDR: &'static str = "localhost:9042";
 
 #[cfg(feature = "e2e-tests")]
 type CurrentSession = Session<RoundRobin<TcpConnectionPool<NoneAuthenticator>>>;
@@ -32,8 +32,7 @@ pub fn setup_multiple(create_cqls: &[&'static str]) -> Result<CurrentSession> {
     let session = new_session(&cluster_config, lb).expect("session should be created");
     let re_table_name = Regex::new(r"CREATE TABLE IF NOT EXISTS (\w+\.\w+)").unwrap();
 
-    let create_keyspace_query =
-        "CREATE KEYSPACE IF NOT EXISTS cdrs_test WITH \
+    let create_keyspace_query = "CREATE KEYSPACE IF NOT EXISTS cdrs_test WITH \
          replication = {'class': 'SimpleStrategy', 'replication_factor': 1} \
          AND durable_writes = false";
     session.query(create_keyspace_query)?;