22import time
33
44from common .logger import getLogger
5+ from vCenterShell .commands .ip_result import IpResult , IpReason
56
67logger = getLogger (__name__ )
78
@@ -14,7 +15,7 @@ def __init__(self, pyvmomi_service, resource_model_parser):
1415 self .pyvmomi_service = pyvmomi_service
1516 self .resource_model_parser = resource_model_parser
1617
17- def refresh_ip (self , si , session , vm_uuid , resource_name , default_network ):
18+ def refresh_ip (self , si , session , vm_uuid , resource_name , default_network , cancellation_context ):
1819 """
1920 Refreshes IP address of virtual machine and updates Address property on the resource
2021
@@ -23,26 +24,28 @@ def refresh_ip(self, si, session, vm_uuid, resource_name, default_network):
2324 :param str vm_uuid: UUID of Virtual Machine
2425 :param str resource_name: Logical resource name to update address property on
2526 :param vim.Network default_network: the default network
27+ :param cancellation_context:
2628 """
2729 match_function = self ._get_ip_match_function (session , resource_name )
2830
2931 vm = self .pyvmomi_service .find_by_uuid (si , vm_uuid )
3032
3133 if vm .guest .toolsStatus == 'toolsNotInstalled' :
32- raise ValueError ('VMWare Tools status on VM {0} are not installed' .format (resource_name ))
34+ raise ValueError ('VMWare Tools status on virtual machine \' {0}\' are not installed' .format (resource_name ))
3335
34- ip = self ._obtain_ip (vm , default_network , match_function )
36+ ip_result = self ._obtain_ip (vm , default_network , match_function , cancellation_context )
3537
36- if ip is None :
37- raise ValueError ('IP address of VM {0} could not be obtained during {1} seconds'
38+ if ip_result . reason == IpReason . Timeout :
39+ raise ValueError ('IP address of VM \' {0}\' could not be obtained during {1} seconds'
3840 .format (resource_name , self .TIMEOUT ))
3941
40- session .UpdateResourceAddress (resource_name , ip )
42+ if ip_result .reason == IpReason .Success :
43+ session .UpdateResourceAddress (resource_name , ip_result .ip_address )
4144
4245 @staticmethod
4346 def _get_ip_match_function (session , resource_name ):
4447
45- logger .debug ('Trying to obtain IP address for {0}' .format (resource_name ))
48+ logger .debug ('Trying to obtain IP address for \' {0}\' ' .format (resource_name ))
4649
4750 resource = session .GetResourceDetails (resource_name )
4851 ip_regexes = []
@@ -58,31 +61,31 @@ def _get_ip_match_function(session, resource_name):
5861
5962 if ip_regexes :
6063 ip_regex = ip_regexes [0 ]
61- logger .debug ('Custom IP Regex to filter IP addresses {0}' .format (ip_regex ))
64+ logger .debug ('Custom IP Regex to filter IP addresses \' {0}\' ' .format (ip_regex ))
6265
6366 return re .compile (ip_regex ).match
6467
65- def _obtain_ip (self , vm , default_network , match_function ):
68+ def _obtain_ip (self , vm , default_network , match_function , cancellation_context ):
6669 time_elapsed = 0
6770 ip = None
6871 interval = self .TIMEOUT / 100
6972 while time_elapsed < self .TIMEOUT and ip is None :
73+ if cancellation_context .is_cancelled :
74+ return IpResult (ip , IpReason .Cancelled )
7075 ips = RefreshIpCommand ._get_ip_addresses (vm , default_network )
7176 if ips :
72- print 'filtering ip by v4'
73- logger .debug ('Filtering IP adresses to limit to IP V4 {0}' .format (',' .join (ips )))
77+ logger .debug ('Filtering IP adresses to limit to IP V4 \' {0}\' ' .format (',' .join (ips )))
7478 ips = RefreshIpCommand ._select_ip_by_match (ips , RefreshIpCommand .IP_V4_PATTERN .match )
7579 if ips :
76- print 'filtering ip by custom filter'
7780 logger .debug ('Filtering IP adresses by custom IP Regex' .format (',' .join (ips )))
7881 ips = RefreshIpCommand ._select_ip_by_match (ips , match_function )
7982 if ips :
8083 ip = ips [0 ]
8184 if ip :
82- return ip
85+ return IpResult ( ip , IpReason . Success )
8386 time_elapsed += interval
8487 time .sleep (interval )
85- return ip
88+ return IpResult ( ip , IpReason . Timeout )
8689
8790 @staticmethod
8891 def _get_ip_addresses (vm , default_network ):
0 commit comments