40
40
import os
41
41
import urllib .request
42
42
import json
43
+ from typing import Optional
43
44
44
45
import weakref
45
46
from weakref import WeakValueDictionary
@@ -1187,13 +1188,17 @@ def __init__(self,
1187
1188
uses_eventlet = EventletConnection and issubclass (self .connection_class , EventletConnection )
1188
1189
1189
1190
# 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' )):
1191
1192
# download SCB if necessary
1192
1193
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'
1194
1199
if not os .path .exists (bundle_path ):
1195
1200
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" ])
1197
1202
try :
1198
1203
with urllib .request .urlopen (url ) as r :
1199
1204
with open (bundle_path , 'wb' ) as f :
@@ -2221,15 +2226,15 @@ def get_control_connection_host(self):
2221
2226
return self .metadata .get_host (endpoint ) if endpoint else None
2222
2227
2223
2228
@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 ):
2225
2230
"""
2226
2231
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'.
2228
2233
2229
2234
Args:
2230
2235
db_id (str): The Astra DB cluster UUID.
2231
- db_region (str): The Astra DB cluster region.
2232
2236
token (str): The Astra token.
2237
+ db_region (optional str): The Astra DB cluster region.
2233
2238
2234
2239
Returns:
2235
2240
str: The secure connect bundle URL for the given inputs.
@@ -2248,15 +2253,23 @@ def _get_astra_bundle_url(db_id, db_region, token):
2248
2253
try :
2249
2254
with urllib .request .urlopen (req ) as response :
2250
2255
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 )
2256
2265
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' ]
2258
2271
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 " )
2260
2273
2261
2274
# handle errors
2262
2275
if 'errors' in response_data :
0 commit comments