mirror of
https://github.com/ansible-collections/community.routeros.git
synced 2025-08-02 01:05:07 +02:00
Support absent values. Support absent value 'all' for 'server' in /ip dhcp-server lease. (#107)
This commit is contained in:
parent
a2ace3fb79
commit
f797b4a231
7 changed files with 232 additions and 10 deletions
|
@ -46,18 +46,21 @@ class APIData(object):
|
|||
|
||||
|
||||
class KeyInfo(object):
|
||||
def __init__(self, _dummy=None, can_disable=False, remove_value=None, default=None, required=False, automatically_computed_from=None):
|
||||
def __init__(self, _dummy=None, can_disable=False, remove_value=None, absent_value=None, default=None, required=False, automatically_computed_from=None):
|
||||
if _dummy is not None:
|
||||
raise ValueError('KeyInfo() does not have positional arguments')
|
||||
if sum([required, default is not None, automatically_computed_from is not None, can_disable]) > 1:
|
||||
raise ValueError('required, default, automatically_computed_from, and can_disable are mutually exclusive')
|
||||
if not can_disable and remove_value is not None:
|
||||
raise ValueError('remove_value can only be specified if can_disable=True')
|
||||
if absent_value is not None and any([default is not None, automatically_computed_from is not None, can_disable]):
|
||||
raise ValueError('absent_value can not be combined with default, automatically_computed_from, can_disable=True, or absent_value')
|
||||
self.can_disable = can_disable
|
||||
self.remove_value = remove_value
|
||||
self.automatically_computed_from = automatically_computed_from
|
||||
self.default = default
|
||||
self.required = required
|
||||
self.absent_value = absent_value
|
||||
|
||||
|
||||
def split_path(path):
|
||||
|
@ -755,7 +758,7 @@ PATHS = {
|
|||
'disabled': KeyInfo(default=False),
|
||||
'insert-queue-before': KeyInfo(can_disable=True),
|
||||
'mac-address': KeyInfo(can_disable=True, remove_value=''),
|
||||
'server': KeyInfo(),
|
||||
'server': KeyInfo(absent_value='all'),
|
||||
},
|
||||
),
|
||||
('ip', 'dhcp-server', 'network'): APIData(
|
||||
|
|
|
@ -275,10 +275,12 @@ def main():
|
|||
if handle_disabled == 'exclamation':
|
||||
k = '!%s' % k
|
||||
entry[k] = None
|
||||
if hide_defaults:
|
||||
for k, field_info in path_info.fields.items():
|
||||
for k, field_info in path_info.fields.items():
|
||||
if hide_defaults:
|
||||
if field_info.default is not None and entry.get(k) == field_info.default:
|
||||
entry.pop(k)
|
||||
if field_info.absent_value and k not in entry:
|
||||
entry[k] = field_info.absent_value
|
||||
result.append(entry)
|
||||
|
||||
module.exit_json(result=result)
|
||||
|
|
|
@ -446,6 +446,15 @@ def remove_dynamic(entries):
|
|||
return result
|
||||
|
||||
|
||||
def get_api_data(api_path, path_info):
|
||||
entries = list(api_path)
|
||||
for entry in entries:
|
||||
for k, field_info in path_info.fields.items():
|
||||
if field_info.absent_value is not None and k not in entry:
|
||||
entry[k] = field_info.absent_value
|
||||
return entries
|
||||
|
||||
|
||||
def sync_list(module, api, path, path_info):
|
||||
handle_absent_entries = module.params['handle_absent_entries']
|
||||
handle_entries_content = module.params['handle_entries_content']
|
||||
|
@ -476,7 +485,7 @@ def sync_list(module, api, path, path_info):
|
|||
|
||||
api_path = compose_api_path(api, path)
|
||||
|
||||
old_data = list(api_path)
|
||||
old_data = get_api_data(api_path, path_info)
|
||||
old_data = remove_dynamic(old_data)
|
||||
stratified_old_data = defaultdict(list)
|
||||
for index, entry in enumerate(old_data):
|
||||
|
@ -588,7 +597,7 @@ def sync_list(module, api, path, path_info):
|
|||
|
||||
# For sake of completeness, retrieve the full new data:
|
||||
if modify_list or create_list or reorder_list:
|
||||
new_data = remove_dynamic(list(api_path))
|
||||
new_data = remove_dynamic(get_api_data(api_path, path_info))
|
||||
|
||||
# Remove 'irrelevant' data
|
||||
for entry in old_data:
|
||||
|
@ -656,7 +665,7 @@ def sync_with_primary_keys(module, api, path, path_info):
|
|||
|
||||
api_path = compose_api_path(api, path)
|
||||
|
||||
old_data = list(api_path)
|
||||
old_data = get_api_data(api_path, path_info)
|
||||
old_data = remove_dynamic(old_data)
|
||||
old_data_by_key = OrderedDict()
|
||||
id_by_key = {}
|
||||
|
@ -782,7 +791,7 @@ def sync_with_primary_keys(module, api, path, path_info):
|
|||
|
||||
# For sake of completeness, retrieve the full new data:
|
||||
if modify_list or create_list or reorder_list:
|
||||
new_data = remove_dynamic(list(api_path))
|
||||
new_data = remove_dynamic(get_api_data(api_path, path_info))
|
||||
|
||||
# Remove 'irrelevant' data
|
||||
for entry in old_data:
|
||||
|
@ -818,7 +827,7 @@ def sync_single_value(module, api, path, path_info):
|
|||
|
||||
api_path = compose_api_path(api, path)
|
||||
|
||||
old_data = list(api_path)
|
||||
old_data = get_api_data(api_path, path_info)
|
||||
if len(old_data) != 1:
|
||||
module.fail_json(
|
||||
msg='Internal error: retrieving /{path} resulted in {count} elements. Expected exactly 1.'.format(
|
||||
|
@ -839,7 +848,7 @@ def sync_single_value(module, api, path, path_info):
|
|||
except (LibRouterosError, UnicodeEncodeError) as e:
|
||||
module.fail_json(msg='Error while modifying: {error}'.format(error=to_native(e)))
|
||||
# Retrieve latest version
|
||||
new_data = list(api_path)
|
||||
new_data = get_api_data(api_path, path_info)
|
||||
if len(new_data) == 1:
|
||||
updated_entry = new_data[0]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue