Skip to content

Commit 6c352f6

Browse files
committed
ec2 address translator
PYTHON-198
1 parent e345bec commit 6c352f6

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

cassandra/policies.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,3 +884,23 @@ class IdentityTranslator(AddressTranslator):
884884
"""
885885
def translate(self, addr):
886886
return addr
887+
888+
889+
class EC2MultiRegionTranslator(AddressTranslator):
890+
"""
891+
Resolves private ips of the hosts in the same datacenter as the client, and public ips of hosts in other datacenters.
892+
"""
893+
def translate(self, addr):
894+
"""
895+
Reverse DNS the public broadcast_address, then lookup that hostname to get the AWS-resolved IP, which
896+
will point to the private IP address within the same datacenter.
897+
"""
898+
# get family of this address so we translate to the same
899+
family = socket.getaddrinfo(addr, 0, socket.AF_UNSPEC, socket.SOCK_STREAM)[0][0]
900+
host = socket.getfqdn(addr)
901+
for a in socket.getaddrinfo(host, 0, family, socket.SOCK_STREAM):
902+
try:
903+
return a[4][0]
904+
except Exception:
905+
pass
906+
return addr

0 commit comments

Comments
 (0)