Skip to content

Commit

Permalink
Merge branch 'develop' into feature/borismod_cleanup2
Browse files Browse the repository at this point in the history
  • Loading branch information
borismod committed Feb 28, 2016
2 parents 25b0c17 + 442afc6 commit 197f72e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
5 changes: 3 additions & 2 deletions tests/test_commands/test_command_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUp(self):
self.command_orchestrator.vc_data_model.default_dvswitch_name = 'dv_name'
self.command_orchestrator.vc_data_model.default_port_group_location = 'port path'
self.command_orchestrator.vc_data_model.default_network = 'default network'
self.ports = Mock()
self.ports = [Mock()]
self.command_orchestrator._parse_remote_model = Mock(return_value=remote_resource)

def test_disconnect_all(self):
Expand Down Expand Up @@ -93,6 +93,7 @@ def test_power_cycle(self):

def test_refresh_ip(self):
# act
self.command_orchestrator.refresh_ip(self.context, self.ports)
cancellation_context = object()
self.command_orchestrator.refresh_ip(self.context, cancellation_context=cancellation_context, ports=self.ports)
# assert
self.assertTrue(self.command_orchestrator.command_wrapper.execute_command_with_connection.called)
5 changes: 3 additions & 2 deletions tests/test_vcenter_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def setUp(self):
self.context = Mock()
self.context.resource = self.resource
self.driver.command_orchestrator = MagicMock()
self.cancellation_context = Mock()
self.ports = Mock()

def test_init(self):
Expand Down Expand Up @@ -112,7 +113,7 @@ def test_power_cycle(self):
def test_refresh_ip(self):
self.setUp()

res = self.driver.remote_refresh_ip(self.context, self.ports)
res = self.driver.remote_refresh_ip(self.context, self.cancellation_context, self.ports)

self.assertIsNotNone(res)
self.assertTrue(self.driver.command_orchestrator.refresh_ip.called_with(self.context, self.ports))
self.assertTrue(self.driver.command_orchestrator.refresh_ip.called_with(self.context, self.cancellation_context, self.ports))
7 changes: 4 additions & 3 deletions vCenterShell/commands/command_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ def destroy_vm(self, context, ports):
return set_command_result(result=res, unpicklable=False)

# remote command
def refresh_ip(self, context, ports):
def refresh_ip(self, context, cancellation_context, ports):
"""
Refresh IP Command, will refresh the ip of the vm and will update it on the resource
:param models.QualiDriverModels.ResourceRemoteCommandContext context: the context the command runs on
:param cancellation_context:
:param list[string] ports: the ports of the connection between the remote resource and the local resource, NOT IN USE!!!
"""
# get connection details
Expand All @@ -294,7 +294,8 @@ def refresh_ip(self, context, ports):
session,
resource_details.vm_uuid,
resource_details.fullname,
self.vc_data_model.holding_network)
self.vc_data_model.holding_network,
cancellation_context)
return set_command_result(result=res, unpicklable=False)

# remote command
Expand Down
13 changes: 13 additions & 0 deletions vCenterShell/commands/ip_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from enum import Enum


class IpReason(Enum):
Success = 0,
Timeout = 1,
Cancelled = 2


class IpResult(object):
def __init__(self, ip_address, reason):
self.ip_address = ip_address
self.reason = reason
31 changes: 17 additions & 14 deletions vCenterShell/commands/refresh_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time

from common.logger import getLogger
from vCenterShell.commands.ip_result import IpResult, IpReason

logger = getLogger(__name__)

Expand All @@ -14,7 +15,7 @@ def __init__(self, pyvmomi_service, resource_model_parser):
self.pyvmomi_service = pyvmomi_service
self.resource_model_parser = resource_model_parser

def refresh_ip(self, si, session, vm_uuid, resource_name, default_network):
def refresh_ip(self, si, session, vm_uuid, resource_name, default_network, cancellation_context):
"""
Refreshes IP address of virtual machine and updates Address property on the resource
Expand All @@ -23,26 +24,28 @@ def refresh_ip(self, si, session, vm_uuid, resource_name, default_network):
:param str vm_uuid: UUID of Virtual Machine
:param str resource_name: Logical resource name to update address property on
:param vim.Network default_network: the default network
:param cancellation_context:
"""
match_function = self._get_ip_match_function(session, resource_name)

vm = self.pyvmomi_service.find_by_uuid(si, vm_uuid)

if vm.guest.toolsStatus == 'toolsNotInstalled':
raise ValueError('VMWare Tools status on VM {0} are not installed'.format(resource_name))
raise ValueError('VMWare Tools status on virtual machine \'{0}\' are not installed'.format(resource_name))

ip = self._obtain_ip(vm, default_network, match_function)
ip_result = self._obtain_ip(vm, default_network, match_function, cancellation_context)

if ip is None:
raise ValueError('IP address of VM {0} could not be obtained during {1} seconds'
if ip_result.reason == IpReason.Timeout:
raise ValueError('IP address of VM \'{0}\' could not be obtained during {1} seconds'
.format(resource_name, self.TIMEOUT))

session.UpdateResourceAddress(resource_name, ip)
if ip_result.reason == IpReason.Success:
session.UpdateResourceAddress(resource_name, ip_result.ip_address)

@staticmethod
def _get_ip_match_function(session, resource_name):

logger.debug('Trying to obtain IP address for {0}'.format(resource_name))
logger.debug('Trying to obtain IP address for \'{0}\''.format(resource_name))

resource = session.GetResourceDetails(resource_name)
ip_regexes = []
Expand All @@ -58,31 +61,31 @@ def _get_ip_match_function(session, resource_name):

if ip_regexes:
ip_regex = ip_regexes[0]
logger.debug('Custom IP Regex to filter IP addresses {0}'.format(ip_regex))
logger.debug('Custom IP Regex to filter IP addresses \'{0}\''.format(ip_regex))

return re.compile(ip_regex).match

def _obtain_ip(self, vm, default_network, match_function):
def _obtain_ip(self, vm, default_network, match_function, cancellation_context):
time_elapsed = 0
ip = None
interval = self.TIMEOUT / 100
while time_elapsed < self.TIMEOUT and ip is None:
if cancellation_context.is_cancelled:
return IpResult(ip, IpReason.Cancelled)
ips = RefreshIpCommand._get_ip_addresses(vm, default_network)
if ips:
print 'filtering ip by v4'
logger.debug('Filtering IP adresses to limit to IP V4 {0}'.format(','.join(ips)))
logger.debug('Filtering IP adresses to limit to IP V4 \'{0}\''.format(','.join(ips)))
ips = RefreshIpCommand._select_ip_by_match(ips, RefreshIpCommand.IP_V4_PATTERN.match)
if ips:
print 'filtering ip by custom filter'
logger.debug('Filtering IP adresses by custom IP Regex'.format(','.join(ips)))
ips = RefreshIpCommand._select_ip_by_match(ips, match_function)
if ips:
ip = ips[0]
if ip:
return ip
return IpResult(ip, IpReason.Success)
time_elapsed += interval
time.sleep(interval)
return ip
return IpResult(ip, IpReason.Timeout)

@staticmethod
def _get_ip_addresses(vm, default_network):
Expand Down
4 changes: 2 additions & 2 deletions vcentershell_driver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def disconnect(self, context, ports, network_name):
def remote_destroy_vm(self, context, ports):
return self.command_orchestrator.destroy_vm(context, ports)

def remote_refresh_ip(self, context, ports):
return self.command_orchestrator.refresh_ip(context, ports)
def remote_refresh_ip(self, context, cancellation_context, ports):
return self.command_orchestrator.refresh_ip(context, cancellation_context, ports)

def PowerOff(self, context, ports):
return self.command_orchestrator.power_off(context, ports)
Expand Down

0 comments on commit 197f72e

Please sign in to comment.