Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
allows ib_spec attrs to be filtered in update (ansible#36673)
Browse files Browse the repository at this point in the history
* allows ib_spec attrs to be filtered in update

This change will allow the ib_spec entries to be be filtered on a change
object by setting the update keyword to false.  The default value for
update is true.  When the update keyword is set to false, the keyed
entry will be removed from the update object before it is sent to the
api endpoint.

fixes ansible#36563

* fix up pep8 issues
  • Loading branch information
privateip authored Feb 26, 2018
1 parent cdb2969 commit 93b795b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
21 changes: 21 additions & 0 deletions lib/ansible/module_utils/net_tools/nios/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def run(self, ib_obj_type, ib_spec):
elif 'view' in proposed_object:
self.check_if_dns_view_exists(proposed_object['view'])
if not self.module.check_mode:
proposed_object = self.on_update(proposed_object, ib_spec)
res = self.update_object(ref, proposed_object)
result['changed'] = True

Expand Down Expand Up @@ -321,3 +322,23 @@ def compare_objects(self, current_object, proposed_object):
return False

return True

def on_update(self, proposed_object, ib_spec):
''' Event called before the update is sent to the API endpoing
This method will allow the final proposed object to be changed
and/or keys filtered before it is sent to the API endpoint to
be processed.
:args proposed_object: A dict item that will be encoded and sent
the the API endpoint with the updated data structure
:returns: updated object to be sent to API endpoint
'''
keys = set()
for key, value in iteritems(proposed_object):
update = ib_spec[key].get('update', True)
if not update:
keys.add(key)

return dict([(k, v) for k, v in iteritems(proposed_object) if k not in keys])
2 changes: 1 addition & 1 deletion lib/ansible/modules/net_tools/nios/nios_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def main():
)

ib_spec = dict(
fqdn=dict(required=True, aliases=['name'], ib_req=True),
fqdn=dict(required=True, aliases=['name'], ib_req=True, update=False),
view=dict(default='default', aliases=['dns_view'], ib_req=True),

grid_primary=dict(type='list', elements='dict', options=grid_spec),
Expand Down
26 changes: 26 additions & 0 deletions test/units/module_utils/net_tools/nios/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ def test_wapi_change(self):
self.assertTrue(res['changed'])
wapi.update_object.called_once_with(test_object)

def test_wapi_change_false(self):
self.module.params = {'provider': None, 'state': 'present', 'name': 'default',
'comment': 'updated comment', 'extattrs': None, 'fqdn': 'foo'}

test_object = [
{
"comment": "test comment",
"_ref": "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true",
"name": "default",
"extattrs": {}
}
]

test_spec = {
"name": {"ib_req": True},
"fqdn": {"ib_req": True, 'update': False},
"comment": {},
"extattrs": {}
}

wapi = self._get_wapi(test_object)
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.update_object.called_once_with(test_object)

def test_wapi_extattrs_change(self):
self.module.params = {'provider': None, 'state': 'present', 'name': 'default',
'comment': 'test comment', 'extattrs': {'Site': 'update'}}
Expand Down

0 comments on commit 93b795b

Please sign in to comment.