@@ -1042,6 +1042,19 @@ def default_retry_policy(self, policy):
1042
1042
'use_default_tempdir': True # use the system temp dir for the zip extraction
1043
1043
}
1044
1044
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
+
1045
1058
The zip file will be temporarily extracted in the same directory to
1046
1059
load the configuration and certificates.
1047
1060
"""
@@ -1174,13 +1187,13 @@ def __init__(self,
1174
1187
uses_eventlet = EventletConnection and issubclass (self .connection_class , EventletConnection )
1175
1188
1176
1189
# 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' )) :
1178
1191
# download SCB if necessary
1179
1192
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'
1181
1194
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' ])
1184
1197
try :
1185
1198
with urllib .request .urlopen (url ) as r :
1186
1199
with open (bundle_path , 'wb' ) as f :
@@ -2208,21 +2221,43 @@ def get_control_connection_host(self):
2208
2221
return self .metadata .get_host (endpoint ) if endpoint else None
2209
2222
2210
2223
@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
+ """
2212
2240
# 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 "
2214
2242
headers = {
2215
2243
"Authorization" : f"Bearer { token } " ,
2216
2244
"Content-Type" : "application/json"
2217
2245
}
2218
2246
2219
- req = urllib .request .Request (url , method = "POST " , headers = headers , data = b"" )
2247
+ req = urllib .request .Request (url , method = "GET " , headers = headers , data = b"" )
2220
2248
try :
2221
2249
with urllib .request .urlopen (req ) as response :
2222
2250
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
+
2226
2261
# handle errors
2227
2262
if 'errors' in response_data :
2228
2263
raise Exception (response_data ['errors' ][0 ]['message' ])
0 commit comments