@@ -223,17 +223,56 @@ def set_serverless_acct_id(self: "RedshiftProperty") -> None:
223223
224224 def set_region_from_host (self : "RedshiftProperty" ) -> None :
225225 """
226- Sets the AWS region as parsed from the Redshift serverless endpoint.
226+ Sets the AWS region as parsed from the Redshift instance endpoint.
227227 """
228228 import re
229229
230- for serverless_pattern in (SERVERLESS_WITH_WORKGROUP_HOST_PATTERN , SERVERLESS_HOST_PATTERN ):
231- m2 = re .fullmatch (pattern = serverless_pattern , string = self .host )
230+ if self .is_serverless_host :
231+ patterns : typing .Tuple [str , ...] = (SERVERLESS_WITH_WORKGROUP_HOST_PATTERN , SERVERLESS_HOST_PATTERN )
232+ else :
233+ patterns = (PROVISIONED_HOST_PATTERN ,)
234+
235+ for host_pattern in patterns :
236+ m2 = re .fullmatch (pattern = host_pattern , string = self .host )
232237
233238 if m2 :
234239 self .put (key = "region" , value = m2 .group (typing .cast (int , m2 .lastindex )))
240+ _logger .debug ("region set to %s" , self .region )
235241 break
236242
243+ def set_region_from_endpoint_lookup (self : "RedshiftProperty" ) -> None :
244+ """
245+ Sets the AWS region as determined from a DNS lookup of the Redshift instance endpoint.
246+ """
247+ import socket
248+
249+ _logger .debug ("set_region_from_endpoint_lookup" )
250+
251+ if not all ((self .host , self .port )):
252+ _logger .debug ("host and port were unspecified, exiting set_region_from_endpoint_lookup" )
253+ return
254+ try :
255+ addr_response : typing .List [
256+ typing .Tuple [
257+ socket .AddressFamily ,
258+ socket .SocketKind ,
259+ int ,
260+ str ,
261+ typing .Union [typing .Tuple [str , int ], typing .Tuple [str , int , int , int ]],
262+ ]
263+ ] = socket .getaddrinfo (host = self .host , port = self .port , family = socket .AF_INET )
264+ _logger .debug ("%s" , addr_response )
265+ host_response : typing .Tuple [str , typing .List , typing .List ] = socket .gethostbyaddr (addr_response [0 ][4 ][0 ])
266+ ec2_instance_host : str = host_response [0 ]
267+ _logger .debug ("underlying ec2 instance host %s" , ec2_instance_host )
268+ ec2_region : str = ec2_instance_host .split ("." )[1 ]
269+ self .put (key = "region" , value = ec2_region )
270+ except :
271+ msg : str = "Unable to automatically determine AWS region from host {} port {}. Please check host and port connection parameters are correct." .format (
272+ self .host , self .port
273+ )
274+ _logger .debug (msg )
275+
237276 def set_serverless_work_group_from_host (self : "RedshiftProperty" ) -> None :
238277 """
239278 Sets the work_group as parsed from the Redshift serverless endpoint.
0 commit comments