From d9d1194c8004091d783fac379e57859931a03230 Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Wed, 18 Jan 2017 16:00:23 +0100 Subject: [PATCH] cloud: ovirt: Ignore 404 error when getting entity (#20386) --- lib/ansible/module_utils/ovirt.py | 16 +++++++++++++++- .../modules/cloud/ovirt/ovirt_host_networks.py | 3 ++- .../modules/cloud/ovirt/ovirt_storage_domains.py | 7 ++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/ovirt.py b/lib/ansible/module_utils/ovirt.py index e044b878f21..c74aafb8ab9 100644 --- a/lib/ansible/module_utils/ovirt.py +++ b/lib/ansible/module_utils/ovirt.py @@ -247,6 +247,20 @@ def search_by_name(service, name, **kwargs): return res[0] +def get_entity(service): + """ + Ignore SDK Error in case of getting an entity from service. + """ + entity = None + try: + entity = service.get() + except sdk.Error: + # We can get here 404, we should ignore it, in case + # of removing entity for example. + pass + return entity + + def wait( service, condition, @@ -270,7 +284,7 @@ def wait( start = time.time() while time.time() < start + timeout: # Exit if the condition of entity is valid: - entity = service.get() + entity = get_entity(service) if condition(entity): return elif fail_condition(entity): diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py b/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py index bb0714a6bb7..5bdb7d6938a 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_host_networks.py @@ -33,6 +33,7 @@ from ansible.module_utils.ovirt import ( create_connection, equal, get_dict_of_struct, + get_entity, get_link_name, ovirt_full_argument_spec, search_by_name, @@ -192,7 +193,7 @@ class HostNetworksModule(BaseModule): update = False bond = self._module.params['bond'] networks = self._module.params['networks'] - nic = nic_service.get() + nic = get_entity(nic_service) if nic is None: return update diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_storage_domains.py b/lib/ansible/modules/cloud/ovirt/ovirt_storage_domains.py index bf4a3bb2fd1..bb0fe20a5ff 100644 --- a/lib/ansible/modules/cloud/ovirt/ovirt_storage_domains.py +++ b/lib/ansible/modules/cloud/ovirt/ovirt_storage_domains.py @@ -34,6 +34,7 @@ from ansible.module_utils.ovirt import ( check_sdk, create_connection, equal, + get_entity, ovirt_full_argument_spec, search_by_name, wait, @@ -285,7 +286,7 @@ class StorageDomainModule(BaseModule): return attached_sd_service = attached_sds_service.storage_domain_service(storage_domain.id) - attached_sd = attached_sd_service.get() + attached_sd = get_entity(attached_sd_service) if attached_sd and attached_sd.status != sdstate.MAINTENANCE: if not self._module.check_mode: @@ -305,7 +306,7 @@ class StorageDomainModule(BaseModule): return attached_sd_service = attached_sds_service.storage_domain_service(storage_domain.id) - attached_sd = attached_sd_service.get() + attached_sd = get_entity(attached_sd_service) if attached_sd and attached_sd.status == sdstate.MAINTENANCE: if not self._module.check_mode: @@ -333,7 +334,7 @@ class StorageDomainModule(BaseModule): # If storage domain isn't attached, attach it: attached_sd_service = self._service.service(storage_domain.id) - if attached_sd_service.get() is None: + if get_entity(attached_sd_service) is None: self._service.add( otypes.StorageDomain( id=storage_domain.id,