11import re
22import time
33
4+ from retrying import retry
5+
46from cloudshell .cp .vcenter .commands .ip_result import IpResult , IpReason
57
68from cloudshell .cp .vcenter .common .vcenter .vm_location import VMLocation
9+ from cloudshell .cp .vcenter .models .GenericDeployedAppResourceModel import GenericDeployedAppResourceModel
710
811
912class RefreshIpCommand (object ):
@@ -19,15 +22,15 @@ def _do_not_run_on_static_vm(self, app_request_json):
1922 if app_request_json == '' or app_request_json is None :
2023 raise ValueError ('This command cannot be executed on a Static VM.' )
2124
22- def refresh_ip (self , si , logger , session , vcenter_data_model , vm_uuid , resource_name , cancellation_context ,app_request_json ):
25+ def refresh_ip (self , si , logger , session , vcenter_data_model , resource_model , cancellation_context ,
26+ app_request_json ):
2327 """
2428 Refreshes IP address of virtual machine and updates Address property on the resource
2529
2630 :param vim.ServiceInstance si: py_vmomi service instance
2731 :param logger:
2832 :param vCenterShell.driver.SecureCloudShellApiSession session: cloudshell session
29- :param str vm_uuid: UUID of Virtual Machine
30- :param str resource_name: Logical resource name to update address property on
33+ :param GenericDeployedAppResourceModel resource_model: UUID of Virtual Machine
3134 :param VMwarevCenterResourceModel vcenter_data_model: the vcenter data model attributes
3235 :param cancellation_context:
3336 """
@@ -36,29 +39,34 @@ def refresh_ip(self, si, logger, session, vcenter_data_model, vm_uuid, resource_
3639 default_network = VMLocation .combine (
3740 [vcenter_data_model .default_datacenter , vcenter_data_model .holding_network ])
3841
39- resource = session .GetResourceDetails (resource_name )
40-
41- match_function = self .ip_manager .get_ip_match_function (self ._get_ip_refresh_ip_regex (resource ))
42+ match_function = self .ip_manager .get_ip_match_function (
43+ self ._get_ip_refresh_ip_regex (resource_model .vm_custom_params ))
4244
43- timeout = self ._get_ip_refresh_timeout (resource )
45+ timeout = self ._get_ip_refresh_timeout (resource_model . vm_custom_params )
4446
45- vm = self .pyvmomi_service .find_by_uuid (si , vm_uuid )
47+ vm = self .pyvmomi_service .find_by_uuid (si , resource_model . vm_uuid )
4648
4749 ip_res = self .ip_manager .get_ip (vm , default_network , match_function , cancellation_context , timeout , logger )
4850
4951 if ip_res .reason == IpReason .Timeout :
5052 raise ValueError ('IP address of VM \' {0}\' could not be obtained during {1} seconds'
51- .format (resource_name , timeout ))
53+ .format (resource_model . fullname , timeout ))
5254
5355 if ip_res .reason == IpReason .Success :
54- session .UpdateResourceAddress (resource_name , ip_res .ip_address )
56+ self ._update_resource_address_with_retry (session = session ,
57+ resource_name = resource_model .fullname ,
58+ ip_address = ip_res .ip_address )
5559
5660 return ip_res .ip_address
5761
62+ @retry (stop_max_attempt_number = 5 , wait_fixed = 1000 )
63+ def _update_resource_address_with_retry (self , session , resource_name , ip_address ):
64+ session .UpdateResourceAddress (resource_name , ip_address )
65+
5866 @staticmethod
59- def _get_ip_refresh_timeout (resource ):
67+ def _get_ip_refresh_timeout (custom_params ):
6068 timeout = RefreshIpCommand ._get_custom_param (
61- resource = resource ,
69+ custom_params = custom_params ,
6270 custom_param_name = 'refresh_ip_timeout' )
6371
6472 if not timeout :
@@ -67,21 +75,18 @@ def _get_ip_refresh_timeout(resource):
6775 return float (timeout )
6876
6977 @staticmethod
70- def _get_ip_refresh_ip_regex (resource ):
78+ def _get_ip_refresh_ip_regex (custom_params ):
7179 return RefreshIpCommand ._get_custom_param (
72- resource = resource ,
80+ custom_params = custom_params ,
7381 custom_param_name = 'ip_regex' )
7482
7583 @staticmethod
76- def _get_custom_param (resource , custom_param_name ):
77- custom_param_values = []
78- vm_details = resource .VmDetails
79- if vm_details and hasattr (vm_details , 'VmCustomParams' ) and vm_details .VmCustomParams :
80- custom_params = vm_details .VmCustomParams
81- params = custom_params if isinstance (custom_params , list ) else [custom_params ]
82- custom_param_values = [custom_param .Value for custom_param
83- in params
84- if custom_param .Name == custom_param_name ]
84+ def _get_custom_param (custom_params , custom_param_name ):
85+ if not custom_params :
86+ return None
87+
88+ custom_param_values = [custom_param .value for custom_param in custom_params
89+ if custom_param .name == custom_param_name ]
8590
8691 if custom_param_values :
8792 return custom_param_values [0 ]
0 commit comments