Skip to content

Commit

Permalink
Merge pull request #892 from QualiSystems/feature/alex_add_retry_on_u…
Browse files Browse the repository at this point in the history
…pdate_resource_address

added retry in refresh_ip for api calls to cloudshell
  • Loading branch information
alexazarh authored Dec 8, 2017
2 parents 7de8d7d + 1b0ca4a commit f292c47
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions package/cloudshell/cp/vcenter/commands/refresh_ip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
import time

from retrying import retry

from cloudshell.cp.vcenter.commands.ip_result import IpResult, IpReason

from cloudshell.cp.vcenter.common.vcenter.vm_location import VMLocation
Expand All @@ -19,7 +21,8 @@ def _do_not_run_on_static_vm(self, app_request_json):
if app_request_json == '' or app_request_json is None:
raise ValueError('This command cannot be executed on a Static VM.')

def refresh_ip(self, si, logger, session, vcenter_data_model, vm_uuid, resource_name, cancellation_context,app_request_json):
def refresh_ip(self, si, logger, session, vcenter_data_model, vm_uuid, resource_name, cancellation_context,
app_request_json):
"""
Refreshes IP address of virtual machine and updates Address property on the resource
Expand All @@ -34,9 +37,10 @@ def refresh_ip(self, si, logger, session, vcenter_data_model, vm_uuid, resource_
self._do_not_run_on_static_vm(app_request_json=app_request_json)

default_network = VMLocation.combine(
[vcenter_data_model.default_datacenter, vcenter_data_model.holding_network])
[vcenter_data_model.default_datacenter, vcenter_data_model.holding_network])

resource = session.GetResourceDetails(resource_name)
# TODO remove this call and use data from context
resource = self._get_resource_with_retry(session=session, resource_name=resource_name)

match_function = self.ip_manager.get_ip_match_function(self._get_ip_refresh_ip_regex(resource))

Expand All @@ -51,15 +55,25 @@ def refresh_ip(self, si, logger, session, vcenter_data_model, vm_uuid, resource_
.format(resource_name, timeout))

if ip_res.reason == IpReason.Success:
session.UpdateResourceAddress(resource_name, ip_res.ip_address)
self._update_resource_address_with_retry(session=session,
resource_name=resource_name,
ip_address=ip_res.ip_address)

return ip_res.ip_address

@retry(stop_max_attempt_number=5, wait_fixed=1000)
def _update_resource_address_with_retry(self, session, resource_name, ip_address):
session.UpdateResourceAddress(resource_name, ip_address)

@retry(stop_max_attempt_number=5, wait_fixed=1000)
def _get_resource_with_retry(self, session, resource_name):
return session.GetResourceDetails(resource_name)

@staticmethod
def _get_ip_refresh_timeout(resource):
timeout = RefreshIpCommand._get_custom_param(
resource=resource,
custom_param_name='refresh_ip_timeout')
resource=resource,
custom_param_name='refresh_ip_timeout')

if not timeout:
raise ValueError('Refresh IP Timeout is not set')
Expand All @@ -69,8 +83,8 @@ def _get_ip_refresh_timeout(resource):
@staticmethod
def _get_ip_refresh_ip_regex(resource):
return RefreshIpCommand._get_custom_param(
resource=resource,
custom_param_name='ip_regex')
resource=resource,
custom_param_name='ip_regex')

@staticmethod
def _get_custom_param(resource, custom_param_name):
Expand Down

0 comments on commit f292c47

Please sign in to comment.