Extend client silent mode

Allow resources get operations to be silent, and also not log
the validate result warning if silent.
The reason is that get actions are sometimes used in order to verify
that the object does not exist, and so we do not want to log it.

Change-Id: Ib32637da86e72ff22a7c5684a3f179b91f09406f
This commit is contained in:
Adit Sarfaty 2017-06-08 10:38:30 +03:00
parent 667beecf9a
commit 55f5303572
2 changed files with 13 additions and 11 deletions

View File

@ -123,16 +123,17 @@ class RESTClient(object):
raise error(manager='', operation=operation, details=result_msg, raise error(manager='', operation=operation, details=result_msg,
error_code=error_code) error_code=error_code)
def _validate_result(self, result, expected, operation): def _validate_result(self, result, expected, operation, silent=False):
if result.status_code not in expected: if result.status_code not in expected:
result_msg = result.json() if result.content else '' result_msg = result.json() if result.content else ''
LOG.warning("The HTTP request returned error code " if not silent:
"%(result)s, whereas %(expected)s response " LOG.warning("The HTTP request returned error code "
"codes were expected. Response body %(body)s", "%(result)s, whereas %(expected)s response "
{'result': result.status_code, "codes were expected. Response body %(body)s",
'expected': '/'.join([str(code) {'result': result.status_code,
for code in expected]), 'expected': '/'.join([str(code)
'body': result_msg}) for code in expected]),
'body': result_msg})
error_code = None error_code = None
if isinstance(result_msg, dict) and 'error_message' in result_msg: if isinstance(result_msg, dict) and 'error_message' in result_msg:
@ -199,7 +200,8 @@ class RESTClient(object):
self._validate_result( self._validate_result(
result, RESTClient._VERB_RESP_CODES[method.lower()], result, RESTClient._VERB_RESP_CODES[method.lower()],
_("%(verb)s %(url)s") % {'verb': method, 'url': request_url}) _("%(verb)s %(url)s") % {'verb': method, 'url': request_url},
silent=silent)
return result return result

View File

@ -153,8 +153,8 @@ class NsxLibApiBase(object):
def list(self): def list(self):
return self.client.list(self.uri_segment) return self.client.list(self.uri_segment)
def get(self, uuid): def get(self, uuid, silent=False):
return self.client.get(self.get_path(uuid)) return self.client.get(self.get_path(uuid), silent=silent)
def delete(self, uuid): def delete(self, uuid):
return self.client.delete(self.get_path(uuid)) return self.client.delete(self.get_path(uuid))