diff --git a/hazelcast/discovery.py b/hazelcast/discovery.py index d40755a07c..33afb6e450 100644 --- a/hazelcast/discovery.py +++ b/hazelcast/discovery.py @@ -137,11 +137,21 @@ def _parse_response(self, https_response): private_address = value[self._PRIVATE_ADDRESS_PROPERTY] public_address = value[self._PUBLIC_ADDRESS_PROPERTY] - public_addr = _parse_address(public_address)[0] - private_to_public_addresses[Address(private_address, public_addr.port)] = public_addr + public_addr = self._parse_address(public_address) + private_addr = self._parse_address(private_address) + if private_addr.port == -1: + # If not explicitly given, set the port of the private address to port of the public address + private_addr.port = public_addr.port + private_to_public_addresses[private_addr] = public_addr return private_to_public_addresses + def _parse_address(self, address): + if ':' in address: + host, port = address.split(':') + return Address(host, int(port)) + return Address(address, -1) + @staticmethod def get_host_and_url(properties, cloud_token): """ diff --git a/tests/discovery/hazelcast_cloud_discovery_test.py b/tests/discovery/hazelcast_cloud_discovery_test.py index 7293eab55c..2eac854db6 100644 --- a/tests/discovery/hazelcast_cloud_discovery_test.py +++ b/tests/discovery/hazelcast_cloud_discovery_test.py @@ -13,6 +13,7 @@ from tests.util import get_abs_path TOKEN = "123abc456" +PRIVATE_LINK_TOKEN = "abc123def" CLOUD_URL = HazelcastCloudDiscovery._CLOUD_URL_PATH @@ -22,12 +23,20 @@ {"private-address":"10.47.0.10","public-address":"54.186.232.37:32298"} ]""" +PRIVATE_LINK_RESPONSE = """[ + {"private-address":"100.96.5.1:5701","public-address":"10.113.44.139:31115"}, + {"private-address":"100.96.4.2:5701","public-address":"10.113.44.130:31115"} +]""" + HOST = "localhost" ADDRESSES = {Address("10.47.0.8", 32298): Address("54.213.63.142", 32298), Address("10.47.0.9", 32298): Address("54.245.77.185", 32298), Address("10.47.0.10", 32298): Address("54.186.232.37", 32298)} +PRIVATE_LINK_ADDRESSES = {Address("100.96.5.1", 5701): Address("10.113.44.139", 31115), + Address("100.96.4.2", 5701): Address("10.113.44.130", 31115)} + class CloudHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): @@ -35,17 +44,17 @@ def do_GET(self): if idx > 0: if self.path[:idx + 1] == CLOUD_URL: # Found a cluster with the given token - if self.path[idx + 1:] == TOKEN: + token = self.path[idx + 1:] + if token == TOKEN: self._set_response(200, RESPONSE) - return + elif token == PRIVATE_LINK_TOKEN: + self._set_response(200, PRIVATE_LINK_RESPONSE) # Can not find a cluster with the given token else: self._set_response(404, '{"message":"Cluster with token: ' + self.path[idx + 1:] + ' not found."}') - return else: # Wrong URL self._set_response(404, "default backend - 404") - return def _set_response(self, status, message): self.send_response(status) @@ -98,6 +107,13 @@ def test_found_response(self): six.assertCountEqual(self, ADDRESSES, addresses) + def test_private_link_response(self): + discovery = HazelcastCloudDiscovery(*get_params(HOST, self.server.port, CLOUD_URL, PRIVATE_LINK_TOKEN)) + discovery._ctx = self.ctx + addresses = discovery.discover_nodes() + + six.assertCountEqual(self, PRIVATE_LINK_ADDRESSES, addresses) + def test_not_found_response(self): discovery = HazelcastCloudDiscovery(*get_params(HOST, self.server.port, CLOUD_URL, "INVALID_TOKEN")) discovery._ctx = self.ctx