mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[PR #8004/2a8da769 backport][stable-8] Redfish: Enhanced GetUpdateStatus to allow for empty responses to accomodate possible usage of 204 No Content (#8011)
Redfish: Enhanced GetUpdateStatus to allow for empty responses to accomodate possible usage of 204 No Content (#8004)
* Added handling for 204 No Content in some circumstances
Signed-off-by: Mike Raineri <michael.raineri@dell.com>
* Correcting gzip usage; open_url does the decompression automatically
Signed-off-by: Mike Raineri <michael.raineri@dell.com>
* Changelog fragment
Signed-off-by: Mike Raineri <michael.raineri@dell.com>
* Removed imports no longer used
Signed-off-by: Mike Raineri <michael.raineri@dell.com>
* Updated data unpacking to dynamically check ansible-core version and response headers to see if gzip decompression is needed
Signed-off-by: Mike Raineri <michael.raineri@dell.com>
---------
Signed-off-by: Mike Raineri <michael.raineri@dell.com>
(cherry picked from commit 2a8da76907
)
Co-authored-by: Mike Raineri <michael.raineri@dell.com>
This commit is contained in:
parent
bc7f952a29
commit
c089260c88
2 changed files with 19 additions and 9 deletions
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- redfish_info - allow for a GET operation invoked by ``GetUpdateStatus`` to allow for an empty response body for cases where a service returns 204 No Content (https://github.com/ansible-collections/community.general/issues/8003).
|
|
@ -20,6 +20,8 @@ from ansible.module_utils.six import text_type
|
|||
from ansible.module_utils.six.moves import http_client
|
||||
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||
from ansible.module_utils.ansible_release import __version__ as ansible_version
|
||||
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
||||
|
||||
GET_HEADERS = {'accept': 'application/json', 'OData-Version': '4.0'}
|
||||
POST_HEADERS = {'content-type': 'application/json', 'accept': 'application/json',
|
||||
|
@ -130,7 +132,7 @@ class RedfishUtils(object):
|
|||
return resp
|
||||
|
||||
# The following functions are to send GET/POST/PATCH/DELETE requests
|
||||
def get_request(self, uri, override_headers=None):
|
||||
def get_request(self, uri, override_headers=None, allow_no_resp=False):
|
||||
req_headers = dict(GET_HEADERS)
|
||||
if override_headers:
|
||||
req_headers.update(override_headers)
|
||||
|
@ -145,13 +147,19 @@ class RedfishUtils(object):
|
|||
force_basic_auth=basic_auth, validate_certs=False,
|
||||
follow_redirects='all',
|
||||
use_proxy=True, timeout=self.timeout)
|
||||
if override_headers:
|
||||
resp = gzip.open(BytesIO(resp.read()), 'rt', encoding='utf-8')
|
||||
data = json.loads(to_native(resp.read()))
|
||||
headers = req_headers
|
||||
else:
|
||||
data = json.loads(to_native(resp.read()))
|
||||
headers = dict((k.lower(), v) for (k, v) in resp.info().items())
|
||||
headers = dict((k.lower(), v) for (k, v) in resp.info().items())
|
||||
try:
|
||||
if headers.get('content-encoding') == 'gzip' and LooseVersion(ansible_version) < LooseVersion('2.14'):
|
||||
# Older versions of Ansible do not automatically decompress the data
|
||||
# Starting in 2.14, open_url will decompress the response data by default
|
||||
data = json.loads(to_native(gzip.open(BytesIO(resp.read()), 'rt', encoding='utf-8').read()))
|
||||
else:
|
||||
data = json.loads(to_native(resp.read()))
|
||||
except Exception as e:
|
||||
# No response data; this is okay in certain cases
|
||||
data = None
|
||||
if not allow_no_resp:
|
||||
raise
|
||||
except HTTPError as e:
|
||||
msg = self._get_extended_message(e)
|
||||
return {'ret': False,
|
||||
|
@ -1813,7 +1821,7 @@ class RedfishUtils(object):
|
|||
return {'ret': False, 'msg': 'Must provide a handle tracking the update.'}
|
||||
|
||||
# Get the task or job tracking the update
|
||||
response = self.get_request(self.root_uri + update_handle)
|
||||
response = self.get_request(self.root_uri + update_handle, allow_no_resp=True)
|
||||
if response['ret'] is False:
|
||||
return response
|
||||
|
||||
|
|
Loading…
Reference in a new issue