Skip to content

Commit

Permalink
don't try to update unsupported parameters
Browse files Browse the repository at this point in the history
Fixes: #975
  • Loading branch information
evgeni committed Sep 25, 2020
1 parent 210c02f commit 2a75c35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/975-filter-update-payload.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Don't try to update an entity, if only parameters that aren't supported by the server are detected as changed. (https://github.com/theforeman/foreman-ansible-modules/issues/975)
22 changes: 21 additions & 1 deletion plugins/module_utils/foreman_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,25 @@ def ensure_entity(self, resource, desired_entity, current_entity, params=None, s

return updated_entity

def _validate_supported_payload(self, resource, action, payload):
"""Check whether the payload only contains supported keys.
Emits a warning for keys that are not part of the apidoc.
Parameters:
resource (string): Plural name of the api resource to check
action (string): Name of the action to check payload against
payload (dict): API paylod to be checked
Return value:
The payload as it can be submitted to the API
"""
filtered_payload = self._resource_prepare_params(resource, action, payload)
# On Python 2 dict.keys() is just a list, but we need a set here.
unsupported_parameters = set(payload.keys()) - set(filtered_payload.keys())
if unsupported_parameters:
warn_msg = "The following parameters are not supported by your server when performing {0} on {1}: {2}. They were ignored."
self.warn(warn_msg.format(action, resource, unsupported_parameters))
return filtered_payload

def _create_entity(self, resource, desired_entity, params, foreman_spec):
"""Create entity with given properties
Expand All @@ -692,6 +711,7 @@ def _create_entity(self, resource, desired_entity, params, foreman_spec):
The new current state if the entity
"""
payload = _flatten_entity(desired_entity, foreman_spec)
self._validate_supported_payload(resource, 'create', payload)
if not self.check_mode:
if params:
payload.update(params)
Expand Down Expand Up @@ -737,7 +757,7 @@ def _update_entity(self, resource, desired_entity, current_entity, params, forem
old_value = sorted(old_value, key=operator.itemgetter(sort_key))
if new_value != old_value:
payload[key] = value
if payload:
if self._validate_supported_payload(resource, 'update', payload):
payload['id'] = current_entity['id']
if not self.check_mode:
if params:
Expand Down

0 comments on commit 2a75c35

Please sign in to comment.