1111import logging
1212import os
1313import re
14+ from typing import Any , Dict
1415
1516from cloudinit import subp
1617from cloudinit import util
@@ -971,18 +972,33 @@ def get_ib_hwaddrs_by_interface():
971972 return ret
972973
973974
974- def has_url_connectivity (url ) :
975- """Return true when the instance has access to the provided URL
975+ def has_url_connectivity (url_data : Dict [ str , Any ]) -> bool :
976+ """Return true when the instance has access to the provided URL.
976977
977978 Logs a warning if url is not the expected format.
979+
980+ url_data is a dictionary of kwargs to send to readurl. E.g.:
981+
982+ has_url_connectivity({
983+ "url": "http://example.invalid",
984+ "headers": {"some": "header"},
985+ "timeout": 10
986+ })
978987 """
988+ if 'url' not in url_data :
989+ LOG .warning (
990+ "Ignoring connectivity check. No 'url' to check in %s" , url_data )
991+ return False
992+ url = url_data ['url' ]
979993 if not any ([url .startswith ('http://' ), url .startswith ('https://' )]):
980994 LOG .warning (
981995 "Ignoring connectivity check. Expected URL beginning with http*://"
982996 " received '%s'" , url )
983997 return False
998+ if 'timeout' not in url_data :
999+ url_data ['timeout' ] = 5
9841000 try :
985- readurl (url , timeout = 5 )
1001+ readurl (** url_data )
9861002 except UrlError :
9871003 return False
9881004 return True
@@ -1025,14 +1041,15 @@ class EphemeralIPv4Network(object):
10251041
10261042 No operations are performed if the provided interface already has the
10271043 specified configuration.
1028- This can be verified with the connectivity_url .
1044+ This can be verified with the connectivity_url_data .
10291045 If unconnected, bring up the interface with valid ip, prefix and broadcast.
10301046 If router is provided setup a default route for that interface. Upon
10311047 context exit, clean up the interface leaving no configuration behind.
10321048 """
10331049
10341050 def __init__ (self , interface , ip , prefix_or_mask , broadcast , router = None ,
1035- connectivity_url = None , static_routes = None ):
1051+ connectivity_url_data : Dict [str , Any ] = None ,
1052+ static_routes = None ):
10361053 """Setup context manager and validate call signature.
10371054
10381055 @param interface: Name of the network interface to bring up.
@@ -1041,7 +1058,7 @@ def __init__(self, interface, ip, prefix_or_mask, broadcast, router=None,
10411058 prefix.
10421059 @param broadcast: Broadcast address for the IPv4 network.
10431060 @param router: Optionally the default gateway IP.
1044- @param connectivity_url : Optionally, a URL to verify if a usable
1061+ @param connectivity_url_data : Optionally, a URL to verify if a usable
10451062 connection already exists.
10461063 @param static_routes: Optionally a list of static routes from DHCP
10471064 """
@@ -1056,7 +1073,7 @@ def __init__(self, interface, ip, prefix_or_mask, broadcast, router=None,
10561073 'Cannot setup network: {0}' .format (e )
10571074 ) from e
10581075
1059- self .connectivity_url = connectivity_url
1076+ self .connectivity_url_data = connectivity_url_data
10601077 self .interface = interface
10611078 self .ip = ip
10621079 self .broadcast = broadcast
@@ -1066,11 +1083,11 @@ def __init__(self, interface, ip, prefix_or_mask, broadcast, router=None,
10661083
10671084 def __enter__ (self ):
10681085 """Perform ephemeral network setup if interface is not connected."""
1069- if self .connectivity_url :
1070- if has_url_connectivity (self .connectivity_url ):
1086+ if self .connectivity_url_data :
1087+ if has_url_connectivity (self .connectivity_url_data ):
10711088 LOG .debug (
10721089 'Skip ephemeral network setup, instance has connectivity'
1073- ' to %s' , self .connectivity_url )
1090+ ' to %s' , self .connectivity_url_data [ 'url' ] )
10741091 return
10751092
10761093 self ._bringup_device ()
0 commit comments