Skip to content

Commit 197f72e

Browse files
committed
Merge branch 'develop' into feature/borismod_cleanup2
2 parents 25b0c17 + 442afc6 commit 197f72e

6 files changed

Lines changed: 42 additions & 23 deletions

File tree

tests/test_commands/test_command_orchestrator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def setUp(self):
4040
self.command_orchestrator.vc_data_model.default_dvswitch_name = 'dv_name'
4141
self.command_orchestrator.vc_data_model.default_port_group_location = 'port path'
4242
self.command_orchestrator.vc_data_model.default_network = 'default network'
43-
self.ports = Mock()
43+
self.ports = [Mock()]
4444
self.command_orchestrator._parse_remote_model = Mock(return_value=remote_resource)
4545

4646
def test_disconnect_all(self):
@@ -93,6 +93,7 @@ def test_power_cycle(self):
9393

9494
def test_refresh_ip(self):
9595
# act
96-
self.command_orchestrator.refresh_ip(self.context, self.ports)
96+
cancellation_context = object()
97+
self.command_orchestrator.refresh_ip(self.context, cancellation_context=cancellation_context, ports=self.ports)
9798
# assert
9899
self.assertTrue(self.command_orchestrator.command_wrapper.execute_command_with_connection.called)

tests/test_vcenter_driver.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def setUp(self):
2727
self.context = Mock()
2828
self.context.resource = self.resource
2929
self.driver.command_orchestrator = MagicMock()
30+
self.cancellation_context = Mock()
3031
self.ports = Mock()
3132

3233
def test_init(self):
@@ -112,7 +113,7 @@ def test_power_cycle(self):
112113
def test_refresh_ip(self):
113114
self.setUp()
114115

115-
res = self.driver.remote_refresh_ip(self.context, self.ports)
116+
res = self.driver.remote_refresh_ip(self.context, self.cancellation_context, self.ports)
116117

117118
self.assertIsNotNone(res)
118-
self.assertTrue(self.driver.command_orchestrator.refresh_ip.called_with(self.context, self.ports))
119+
self.assertTrue(self.driver.command_orchestrator.refresh_ip.called_with(self.context, self.cancellation_context, self.ports))

vCenterShell/commands/command_orchestrator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ def destroy_vm(self, context, ports):
273273
return set_command_result(result=res, unpicklable=False)
274274

275275
# remote command
276-
def refresh_ip(self, context, ports):
276+
def refresh_ip(self, context, cancellation_context, ports):
277277
"""
278278
Refresh IP Command, will refresh the ip of the vm and will update it on the resource
279-
280279
:param models.QualiDriverModels.ResourceRemoteCommandContext context: the context the command runs on
280+
:param cancellation_context:
281281
:param list[string] ports: the ports of the connection between the remote resource and the local resource, NOT IN USE!!!
282282
"""
283283
# get connection details
@@ -294,7 +294,8 @@ def refresh_ip(self, context, ports):
294294
session,
295295
resource_details.vm_uuid,
296296
resource_details.fullname,
297-
self.vc_data_model.holding_network)
297+
self.vc_data_model.holding_network,
298+
cancellation_context)
298299
return set_command_result(result=res, unpicklable=False)
299300

300301
# remote command

vCenterShell/commands/ip_result.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from enum import Enum
2+
3+
4+
class IpReason(Enum):
5+
Success = 0,
6+
Timeout = 1,
7+
Cancelled = 2
8+
9+
10+
class IpResult(object):
11+
def __init__(self, ip_address, reason):
12+
self.ip_address = ip_address
13+
self.reason = reason

vCenterShell/commands/refresh_ip.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33

44
from common.logger import getLogger
5+
from vCenterShell.commands.ip_result import IpResult, IpReason
56

67
logger = 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):

vcentershell_driver/driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def disconnect(self, context, ports, network_name):
2323
def remote_destroy_vm(self, context, ports):
2424
return self.command_orchestrator.destroy_vm(context, ports)
2525

26-
def remote_refresh_ip(self, context, ports):
27-
return self.command_orchestrator.refresh_ip(context, ports)
26+
def remote_refresh_ip(self, context, cancellation_context, ports):
27+
return self.command_orchestrator.refresh_ip(context, cancellation_context, ports)
2828

2929
def PowerOff(self, context, ports):
3030
return self.command_orchestrator.power_off(context, ports)

0 commit comments

Comments
 (0)