forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding AddressTranslator interface and impl for use in ec2
This change introduces the AddressTranslator interface, which is intended to translate peer addresses just before creating a connection to those nodes. The primary use -- which is driving the change -- is to be able to translate public IPs to private IPs in ec2. This solution is common among other CQL driver implementations. The specific implementation here also follows the convention set by HostFilter. Signed-off-by: Justin "Gus" Knowlden <gus@gusg.us>
- Loading branch information
Showing
13 changed files
with
325 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package gocql | ||
|
||
import "net" | ||
|
||
// AddressTranslator provides a way to translate node addresses (and ports) that are | ||
// discovered or received as a node event. This can be useful in an ec2 environment, | ||
// for instance, to translate public IPs to private IPs. | ||
type AddressTranslator interface { | ||
// Translate will translate the provided address and/or port to another | ||
// address and/or port. If no translation is possible, Translate will return the | ||
// address and port provided to it. | ||
Translate(addr net.IP, port int) (net.IP, int) | ||
} | ||
|
||
type AddressTranslatorFunc func(addr net.IP, port int) (net.IP, int) | ||
|
||
func (fn AddressTranslatorFunc) Translate(addr net.IP, port int) (net.IP, int) { | ||
return fn(addr, port) | ||
} | ||
|
||
// IdentityTranslator will do nothing but return what it was provided. It is essentially a no-op. | ||
func IdentityTranslator() AddressTranslator { | ||
return AddressTranslatorFunc(func(addr net.IP, port int) (net.IP, int) { | ||
return addr, port | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gocql | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestIdentityAddressTranslator_NilAddrAndZeroPort(t *testing.T) { | ||
var tr AddressTranslator = IdentityTranslator() | ||
hostIP := net.ParseIP("") | ||
if hostIP != nil { | ||
t.Errorf("expected host ip to be (nil) but was (%+v) instead", hostIP) | ||
} | ||
|
||
addr, port := tr.Translate(hostIP, 0) | ||
if addr != nil { | ||
t.Errorf("expected translated host to be (nil) but was (%+v) instead", addr) | ||
} | ||
assertEqual(t, "translated port", 0, port) | ||
} | ||
|
||
func TestIdentityAddressTranslator_HostProvided(t *testing.T) { | ||
var tr AddressTranslator = IdentityTranslator() | ||
hostIP := net.ParseIP("10.1.2.3") | ||
if hostIP == nil { | ||
t.Error("expected host ip not to be (nil)") | ||
} | ||
|
||
addr, port := tr.Translate(hostIP, 9042) | ||
if !hostIP.Equal(addr) { | ||
t.Errorf("expected translated addr to be (%+v) but was (%+v) instead", hostIP, addr) | ||
} | ||
assertEqual(t, "translated port", 9042, port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package gocql | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
"net" | ||
) | ||
|
||
func TestNewCluster_Defaults(t *testing.T) { | ||
cfg := NewCluster() | ||
assertEqual(t, "cluster config cql version", "3.0.0", cfg.CQLVersion) | ||
assertEqual(t, "cluster config timeout", 600*time.Millisecond, cfg.Timeout) | ||
assertEqual(t, "cluster config port", 9042, cfg.Port) | ||
assertEqual(t, "cluster config num-conns", 2, cfg.NumConns) | ||
assertEqual(t, "cluster config consistency", Quorum, cfg.Consistency) | ||
assertEqual(t, "cluster config max prepared statements", defaultMaxPreparedStmts, cfg.MaxPreparedStmts) | ||
assertEqual(t, "cluster config max routing key info", 1000, cfg.MaxRoutingKeyInfo) | ||
assertEqual(t, "cluster config page-size", 5000, cfg.PageSize) | ||
assertEqual(t, "cluster config default timestamp", true, cfg.DefaultTimestamp) | ||
assertEqual(t, "cluster config max wait schema agreement", 60*time.Second, cfg.MaxWaitSchemaAgreement) | ||
assertEqual(t, "cluster config reconnect interval", 60*time.Second, cfg.ReconnectInterval) | ||
} | ||
|
||
func TestNewCluster_WithHosts(t *testing.T) { | ||
cfg := NewCluster("addr1", "addr2") | ||
assertEqual(t, "cluster config hosts length", 2, len(cfg.Hosts)) | ||
assertEqual(t, "cluster config host 0", "addr1", cfg.Hosts[0]) | ||
assertEqual(t, "cluster config host 1", "addr2", cfg.Hosts[1]) | ||
} | ||
|
||
func TestClusterConfig_translateAddressAndPort_NilTranslator(t *testing.T) { | ||
cfg := NewCluster() | ||
assertNil(t, "cluster config address translator", cfg.AddressTranslator) | ||
newAddr, newPort := cfg.translateAddressPort(net.ParseIP("10.0.0.1"), 1234) | ||
assertTrue(t, "same address as provided", net.ParseIP("10.0.0.1").Equal(newAddr)) | ||
assertEqual(t, "translated host and port", 1234, newPort) | ||
} | ||
|
||
func TestClusterConfig_translateAddressAndPort_EmptyAddr(t *testing.T) { | ||
cfg := NewCluster() | ||
cfg.AddressTranslator = staticAddressTranslator(net.ParseIP("10.10.10.10"), 5432) | ||
newAddr, newPort := cfg.translateAddressPort(net.IP([]byte{}), 0) | ||
assertTrue(t, "translated address is still empty", len(newAddr) == 0) | ||
assertEqual(t, "translated port", 0, newPort) | ||
} | ||
|
||
func TestClusterConfig_translateAddressAndPort_Success(t *testing.T) { | ||
cfg := NewCluster() | ||
cfg.AddressTranslator = staticAddressTranslator(net.ParseIP("10.10.10.10"), 5432) | ||
newAddr, newPort := cfg.translateAddressPort(net.ParseIP("10.0.0.1"), 2345) | ||
assertTrue(t, "translated address", net.ParseIP("10.10.10.10").Equal(newAddr)) | ||
assertEqual(t, "translated port", 5432, newPort) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.