Skip to content

Commit ece6f28

Browse files
committed
Make providing region as optional
1 parent 13aed0d commit ece6f28

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

cassandra/cluster.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import os
4141
import urllib.request
4242
import json
43+
from typing import Optional
4344

4445
import weakref
4546
from weakref import WeakValueDictionary
@@ -1187,13 +1188,17 @@ def __init__(self,
11871188
uses_eventlet = EventletConnection and issubclass(self.connection_class, EventletConnection)
11881189

11891190
# Check if we need to download the secure connect bundle
1190-
if all(akey in cloud for akey in ('db_id', 'db_region', 'token')):
1191+
if all(akey in cloud for akey in ('db_id', 'token')):
11911192
# download SCB if necessary
11921193
if 'secure_connect_bundle' not in cloud:
1193-
bundle_path = f'astradb-scb-{cloud["db_id"]}-{cloud["db_region"]}.zip'
1194+
bundle_path = f'astradb-scb-{cloud["db_id"]}'
1195+
if 'db_region' in cloud:
1196+
bundle_path += f'-{cloud["db_region"]}.zip'
1197+
else:
1198+
bundle_path += '.zip'
11941199
if not os.path.exists(bundle_path):
11951200
log.info('Downloading Secure Cloud Bundle...')
1196-
url = self._get_astra_bundle_url(cloud['db_id'], cloud["db_region"], cloud['token'])
1201+
url = self._get_astra_bundle_url(cloud['db_id'], cloud['token'], cloud["db_region"])
11971202
try:
11981203
with urllib.request.urlopen(url) as r:
11991204
with open(bundle_path, 'wb') as f:
@@ -2221,15 +2226,15 @@ def get_control_connection_host(self):
22212226
return self.metadata.get_host(endpoint) if endpoint else None
22222227

22232228
@staticmethod
2224-
def _get_astra_bundle_url(db_id, db_region, token):
2229+
def _get_astra_bundle_url(db_id, token, db_region: Optional[str] = None):
22252230
"""
22262231
Retrieves the secure connect bundle URL for an Astra DB cluster based on the provided 'db_id',
2227-
'db_region' and 'token'.
2232+
'db_region' (optional) and 'token'.
22282233
22292234
Args:
22302235
db_id (str): The Astra DB cluster UUID.
2231-
db_region (str): The Astra DB cluster region.
22322236
token (str): The Astra token.
2237+
db_region (optional str): The Astra DB cluster region.
22332238
22342239
Returns:
22352240
str: The secure connect bundle URL for the given inputs.
@@ -2248,15 +2253,23 @@ def _get_astra_bundle_url(db_id, db_region, token):
22482253
try:
22492254
with urllib.request.urlopen(req) as response:
22502255
response_data = json.loads(response.read().decode())
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+
2257+
if db_region is not None and len(db_region) > 0:
2258+
for datacenter in response_data:
2259+
if 'secureBundleUrl' in datacenter and datacenter['secureBundleUrl']:
2260+
# happy path
2261+
if db_region == datacenter['region']:
2262+
return datacenter['secureBundleUrl']
2263+
else:
2264+
log.warning("Astra DB cluster region [%s] does not match input [%s]", datacenter['region'], db_region)
22562265
else:
2257-
log.warning("Astra DB cluster region [%s] does not match input [%s]", datacenter['region'], db_region)
2266+
raise ValueError("'secureBundleUrl' is missing from the Astra DB API response")
2267+
else:
2268+
# Return just the primary region SCB URL
2269+
if 'secureBundleUrl' in response_data[0] and response_data[0]['secureBundleUrl']:
2270+
return response_data[0]['secureBundleUrl']
22582271
else:
2259-
log.warning("'secureBundleUrl' is missing from the Astra DB API response")
2272+
raise ValueError("'secureBundleUrl' is missing from the Astra DB API response for the primary region")
22602273

22612274
# handle errors
22622275
if 'errors' in response_data:

0 commit comments

Comments
 (0)