From 3502f3b48690500c8d60942ca85bb3527f856e4e Mon Sep 17 00:00:00 2001 From: Kyle Williams <36274986+kyle-williams-1@users.noreply.github.com> Date: Wed, 1 Sep 2021 14:59:27 -0600 Subject: [PATCH] redfish: clean etag of quotes before patch (#3296) * Some vendors surround header etag with quotes, which need to be cleaned before sending a patch * Minor change fragment * Add etag strip quote option * Rebase * Cleanup fragment * Apply suggestions from code review Co-authored-by: Felix Fontein * Update plugins/modules/remote_management/redfish/redfish_command.py Co-authored-by: Felix Fontein * Description update * Update plugins/modules/remote_management/redfish/redfish_config.py Co-authored-by: Felix Fontein Co-authored-by: Kyle Williams Co-authored-by: Felix Fontein --- changelogs/fragments/3296-clean-etag.yaml | 2 ++ plugins/module_utils/redfish_utils.py | 5 ++++- .../redfish/redfish_command.py | 17 +++++++++++++++-- .../remote_management/redfish/redfish_config.py | 17 +++++++++++++++-- 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/3296-clean-etag.yaml diff --git a/changelogs/fragments/3296-clean-etag.yaml b/changelogs/fragments/3296-clean-etag.yaml new file mode 100644 index 0000000000..317772cb15 --- /dev/null +++ b/changelogs/fragments/3296-clean-etag.yaml @@ -0,0 +1,2 @@ +minor_changes: + - "redfish_command and redfish_config and redfish_utils module utils - add parameter to strip etag of quotes before patch, since some vendors do not properly ``If-Match`` etag with quotes (https://github.com/ansible-collections/community.general/pull/3296)." diff --git a/plugins/module_utils/redfish_utils.py b/plugins/module_utils/redfish_utils.py index 0f8e6630ba..b4d0dba015 100644 --- a/plugins/module_utils/redfish_utils.py +++ b/plugins/module_utils/redfish_utils.py @@ -29,7 +29,7 @@ FAIL_MSG = 'Issuing a data modification command without specifying the '\ class RedfishUtils(object): def __init__(self, creds, root_uri, timeout, module, resource_id=None, - data_modification=False): + data_modification=False, strip_etag_quotes=False): self.root_uri = root_uri self.creds = creds self.timeout = timeout @@ -37,6 +37,7 @@ class RedfishUtils(object): self.service_root = '/redfish/v1/' self.resource_id = resource_id self.data_modification = data_modification + self.strip_etag_quotes = strip_etag_quotes self._init_session() def _auth_params(self, headers): @@ -121,6 +122,8 @@ class RedfishUtils(object): if not etag: etag = r['data'].get('@odata.etag') if etag: + if self.strip_etag_quotes: + etag = etag.strip('"') req_headers['If-Match'] = etag username, password, basic_auth = self._auth_params(req_headers) try: diff --git a/plugins/modules/remote_management/redfish/redfish_command.py b/plugins/modules/remote_management/redfish/redfish_command.py index 72392ec9f3..e79308f2d7 100644 --- a/plugins/modules/remote_management/redfish/redfish_command.py +++ b/plugins/modules/remote_management/redfish/redfish_command.py @@ -207,6 +207,15 @@ options: description: - The transfer method to use with the image type: str + strip_etag_quotes: + description: + - Removes surrounding quotes of etag used in C(If-Match) header + of C(PATCH) requests. + - Only use this option to resolve bad vendor implementation where + C(If-Match) only matches the unquoted etag string. + type: bool + default: false + version_added: 3.7.0 author: "Jose Delarosa (@jose-delarosa)" ''' @@ -631,7 +640,8 @@ def main(): transfer_protocol_type=dict(), transfer_method=dict(), ) - ) + ), + strip_etag_quotes=dict(type='bool', default=False), ), required_together=[ ('username', 'password'), @@ -686,10 +696,13 @@ def main(): # VirtualMedia options virtual_media = module.params['virtual_media'] + # Etag options + strip_etag_quotes = module.params['strip_etag_quotes'] + # Build root URI root_uri = "https://" + module.params['baseuri'] rf_utils = RedfishUtils(creds, root_uri, timeout, module, - resource_id=resource_id, data_modification=True) + resource_id=resource_id, data_modification=True, strip_etag_quotes=strip_etag_quotes) # Check that Category is valid if category not in CATEGORY_COMMANDS_ALL: diff --git a/plugins/modules/remote_management/redfish/redfish_config.py b/plugins/modules/remote_management/redfish/redfish_config.py index 9b15a3e63e..ff4b15487e 100644 --- a/plugins/modules/remote_management/redfish/redfish_config.py +++ b/plugins/modules/remote_management/redfish/redfish_config.py @@ -91,6 +91,15 @@ options: - setting dict of EthernetInterface on OOB controller type: dict version_added: '0.2.0' + strip_etag_quotes: + description: + - Removes surrounding quotes of etag used in C(If-Match) header + of C(PATCH) requests. + - Only use this option to resolve bad vendor implementation where + C(If-Match) only matches the unquoted etag string. + type: bool + default: false + version_added: 3.7.0 author: "Jose Delarosa (@jose-delarosa)" ''' @@ -237,7 +246,8 @@ def main(): nic_config=dict( type='dict', default={} - ) + ), + strip_etag_quotes=dict(type='bool', default=False), ), required_together=[ ('username', 'password'), @@ -275,10 +285,13 @@ def main(): nic_addr = module.params['nic_addr'] nic_config = module.params['nic_config'] + # Etag options + strip_etag_quotes = module.params['strip_etag_quotes'] + # Build root URI root_uri = "https://" + module.params['baseuri'] rf_utils = RedfishUtils(creds, root_uri, timeout, module, - resource_id=resource_id, data_modification=True) + resource_id=resource_id, data_modification=True, strip_etag_quotes=strip_etag_quotes) # Check that Category is valid if category not in CATEGORY_COMMANDS_ALL: