Skip to content

Commit 13aed0d

Browse files
committed
Improvized Astra DB connection based on the given region
1 parent 9ac1704 commit 13aed0d

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

cassandra/cluster.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,19 @@ def default_retry_policy(self, policy):
10421042
'use_default_tempdir': True # use the system temp dir for the zip extraction
10431043
}
10441044
1045+
(or)
1046+
1047+
{
1048+
# Astra DB cluster UUID. Only if secure_connect_bundle is not provided
1049+
'db_id': 'db_id',
1050+
1051+
# required with db_id. Astra DB region
1052+
'db_region': 'us-east1',
1053+
1054+
# required with db_id. Astra DB token
1055+
'token': 'AstraCS:change_me:change_me'
1056+
}
1057+
10451058
The zip file will be temporarily extracted in the same directory to
10461059
load the configuration and certificates.
10471060
"""
@@ -1174,13 +1187,13 @@ def __init__(self,
11741187
uses_eventlet = EventletConnection and issubclass(self.connection_class, EventletConnection)
11751188

11761189
# Check if we need to download the secure connect bundle
1177-
if 'db_id' in cloud and 'token' in cloud:
1190+
if all(akey in cloud for akey in ('db_id', 'db_region', 'token')):
11781191
# download SCB if necessary
11791192
if 'secure_connect_bundle' not in cloud:
1180-
bundle_path = f'astra-secure-connect-{cloud["db_id"]}.zip'
1193+
bundle_path = f'astradb-scb-{cloud["db_id"]}-{cloud["db_region"]}.zip'
11811194
if not os.path.exists(bundle_path):
1182-
print('Downloading Secure Cloud Bundle')
1183-
url = self._get_astra_bundle_url(cloud['db_id'], cloud['token'])
1195+
log.info('Downloading Secure Cloud Bundle...')
1196+
url = self._get_astra_bundle_url(cloud['db_id'], cloud["db_region"], cloud['token'])
11841197
try:
11851198
with urllib.request.urlopen(url) as r:
11861199
with open(bundle_path, 'wb') as f:
@@ -2208,21 +2221,43 @@ def get_control_connection_host(self):
22082221
return self.metadata.get_host(endpoint) if endpoint else None
22092222

22102223
@staticmethod
2211-
def _get_astra_bundle_url(db_id, token):
2224+
def _get_astra_bundle_url(db_id, db_region, token):
2225+
"""
2226+
Retrieves the secure connect bundle URL for an Astra DB cluster based on the provided 'db_id',
2227+
'db_region' and 'token'.
2228+
2229+
Args:
2230+
db_id (str): The Astra DB cluster UUID.
2231+
db_region (str): The Astra DB cluster region.
2232+
token (str): The Astra token.
2233+
2234+
Returns:
2235+
str: The secure connect bundle URL for the given inputs.
2236+
2237+
Raises:
2238+
URLError: If the request to the Astra DB API fails.
2239+
"""
22122240
# set up the request
2213-
url = f"https://api.astra.datastax.com/v2/databases/{db_id}/secureBundleURL"
2241+
url = f"https://api.astra.datastax.com/v2/databases/{db_id}/datacenters"
22142242
headers = {
22152243
"Authorization": f"Bearer {token}",
22162244
"Content-Type": "application/json"
22172245
}
22182246

2219-
req = urllib.request.Request(url, method="POST", headers=headers, data=b"")
2247+
req = urllib.request.Request(url, method="GET", headers=headers, data=b"")
22202248
try:
22212249
with urllib.request.urlopen(req) as response:
22222250
response_data = json.loads(response.read().decode())
2223-
# happy path
2224-
if 'downloadURL' in response_data:
2225-
return response_data['downloadURL']
2251+
for datacenter in response_data:
2252+
if 'secureBundleUrl' in datacenter:
2253+
# happy path
2254+
if datacenter['region'] == db_region:
2255+
return datacenter['secureBundleUrl']
2256+
else:
2257+
log.warning("Astra DB cluster region [%s] does not match input [%s]", datacenter['region'], db_region)
2258+
else:
2259+
log.warning("'secureBundleUrl' is missing from the Astra DB API response")
2260+
22262261
# handle errors
22272262
if 'errors' in response_data:
22282263
raise Exception(response_data['errors'][0]['message'])

0 commit comments

Comments
 (0)