From 8676000486dcbf0ee6b1e3bdf50b2fec317278ad Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Wed, 14 Mar 2018 14:23:19 +0200 Subject: [PATCH] Retry search calls on error Search queries sometimes return error 400 with: error_code: 60508 error_message: 'Indexing is in progress, please retry after sometime. This patche will retry the request on this error. Change-Id: Id47886ba0e72ea946dcf07ae0bdbc81fef1dd4f0 --- vmware_nsxlib/v3/__init__.py | 9 ++++++++- vmware_nsxlib/v3/client.py | 2 ++ vmware_nsxlib/v3/exceptions.py | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/vmware_nsxlib/v3/__init__.py b/vmware_nsxlib/v3/__init__.py index a73331a6..99fc5134 100644 --- a/vmware_nsxlib/v3/__init__.py +++ b/vmware_nsxlib/v3/__init__.py @@ -134,7 +134,14 @@ class NsxLibBase(object): url += "&cursor=%d" % cursor if page_size: url += "&page_size=%d" % page_size - return self.client.url_get(url) + + # Retry the search on case of error + @utils.retry_upon_exception(exceptions.NsxIndexingInProgress, + max_attempts=self.client.max_attempts) + def do_search(url): + return self.client.url_get(url) + + return do_search(url) def search_all_by_tags(self, tags, resource_type=None): """Return all the results searched based on tags.""" diff --git a/vmware_nsxlib/v3/client.py b/vmware_nsxlib/v3/client.py index 27016b27..8f4234e4 100644 --- a/vmware_nsxlib/v3/client.py +++ b/vmware_nsxlib/v3/client.py @@ -34,6 +34,8 @@ def http_error_to_exception(status_code, error_code): requests.codes.NOT_FOUND: {'202': exceptions.BackendResourceNotFound, 'default': exceptions.ResourceNotFound}, + requests.codes.BAD_REQUEST: + {'60508': exceptions.NsxIndexingInProgress}, requests.codes.CONFLICT: exceptions.StaleRevision, requests.codes.PRECONDITION_FAILED: exceptions.StaleRevision, requests.codes.INTERNAL_SERVER_ERROR: diff --git a/vmware_nsxlib/v3/exceptions.py b/vmware_nsxlib/v3/exceptions.py index 3aec9edf..dad5a150 100644 --- a/vmware_nsxlib/v3/exceptions.py +++ b/vmware_nsxlib/v3/exceptions.py @@ -140,3 +140,8 @@ class SecurityGroupMaximumCapacityReached(ManagerError): class NsxSearchInvalidQuery(NsxLibException): message = _("Invalid input for NSX search query. Reason: %(reason)s") + + +class NsxIndexingInProgress(NsxLibException): + message = _("Bad Request due to indexing is in progress, please retry " + "after sometime")