From 1a6b95a8f11be9fce8e80e2fc349c0ef70eb6e62 Mon Sep 17 00:00:00 2001 From: Bill Dodd Date: Sun, 12 May 2019 03:24:07 -0500 Subject: [PATCH] redfish: Add If-Match ETag header to PATCH requests (#56150) * add If-Match ETag header to PATCH requests * add changelog fragment * ensure header keys are lowercase * fix dict comprehension for python 2.6 and earlier --- ...50-add-if-match-header-to-patch-requests.yaml | 3 +++ lib/ansible/module_utils/redfish_utils.py | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/56150-add-if-match-header-to-patch-requests.yaml diff --git a/changelogs/fragments/56150-add-if-match-header-to-patch-requests.yaml b/changelogs/fragments/56150-add-if-match-header-to-patch-requests.yaml new file mode 100644 index 0000000000..0d37c80754 --- /dev/null +++ b/changelogs/fragments/56150-add-if-match-header-to-patch-requests.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: +- redfish_command - add If-Match etag header to Redfish PATCH requests (https://github.com/ansible/ansible/issues/56050) diff --git a/lib/ansible/module_utils/redfish_utils.py b/lib/ansible/module_utils/redfish_utils.py index 85a87f1613..7502ea50b9 100644 --- a/lib/ansible/module_utils/redfish_utils.py +++ b/lib/ansible/module_utils/redfish_utils.py @@ -37,6 +37,7 @@ class RedfishUtils(object): follow_redirects='all', use_proxy=False, timeout=self.timeout) data = json.loads(resp.read()) + headers = dict((k.lower(), v) for (k, v) in resp.info().items()) except HTTPError as e: msg = self._get_extended_message(e) return {'ret': False, @@ -49,7 +50,7 @@ class RedfishUtils(object): except Exception as e: return {'ret': False, 'msg': "Failed GET request to '%s': '%s'" % (uri, to_text(e))} - return {'ret': True, 'data': data} + return {'ret': True, 'data': data, 'headers': headers} def post_request(self, uri, pyld): try: @@ -75,9 +76,20 @@ class RedfishUtils(object): return {'ret': True, 'resp': resp} def patch_request(self, uri, pyld): + headers = PATCH_HEADERS + r = self.get_request(uri) + if r['ret']: + # Get etag from etag header or @odata.etag property + etag = r['headers'].get('etag') + if not etag: + etag = r['data'].get('@odata.etag') + if etag: + # Make copy of headers and add If-Match header + headers = dict(headers) + headers['If-Match'] = etag try: resp = open_url(uri, data=json.dumps(pyld), - headers=PATCH_HEADERS, method="PATCH", + headers=headers, method="PATCH", url_username=self.creds['user'], url_password=self.creds['pswd'], force_basic_auth=True, validate_certs=False,