Skip to content

Commit

Permalink
make api_modify to ignore builtin items (ansible-collections#130)
Browse files Browse the repository at this point in the history
* make api_modify to ignore builtin items

Signed-off-by: Tomas Herfert <herfik>

* include_builtin parametr for api_info module

Signed-off-by: Tomas Herfert <herfik>

* api_info ignore_builtin changelog

Signed-off-by: Tomas Herfert <herfik>

* typo

Signed-off-by: Tomas Herfert <herfik>

* Apply suggestions from code review

Co-authored-by: Felix Fontein <[email protected]>

Signed-off-by: Tomas Herfert <herfik>
Co-authored-by: Tomas Herfert <herfik>
Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
therfert and felixfontein authored Nov 16, 2022
1 parent 4194ae9 commit b539ed6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions changelogs/fragments/130-api-modify-builtin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
minor_changes:
- api_info - new parameter ``include_builtin`` which allows to include "builtin" entries that are automatically generated by ROS and cannot be modified by the user
(https://github.com/ansible-collections/community.routeros/pull/130).
trivial:
- api_modify - ignore ``builtin`` entries
(https://github.com/ansible-collections/community.routeros/pull/130).
21 changes: 18 additions & 3 deletions plugins/modules/api_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
description:
- Allows to retrieve information for a path using the API.
- This can be used to backup a path to restore it with the M(community.routeros.api_modify) module.
- Entries are normalized, and dynamic entries are not returned. Use the I(handle_disabled) and
I(hide_defaults) options to control normalization, the I(include_dynamic) option to also return
dynamic entries, and use I(unfiltered) to return all fields including counters.
- Entries are normalized, dynamic and builtin entries are not returned. Use the I(handle_disabled) and
I(hide_defaults) options to control normalization, the I(include_dynamic) and I(include_builtin) options to also return
dynamic resp. builtin entries, and use I(unfiltered) to return all fields including counters.
- B(Note) that this module is still heavily in development, and only supports B(some) paths.
If you want to support new paths, or think you found problems with existing paths, please first
L(create an issue in the community.routeros Issue Tracker,https://github.com/ansible-collections/community.routeros/issues/).
Expand Down Expand Up @@ -196,6 +196,14 @@
- If set to C(true), they are returned as well, and the C(dynamic) keys are returned as well.
type: bool
default: false
include_builtin:
description:
- Whether to include builtin values.
- By default, they are not returned, and the C(builtin) keys are omitted.
- If set to C(true), they are returned as well, and the C(builtin) keys are returned as well.
type: bool
default: false
version_added: 2.4.0
seealso:
- module: community.routeros.api
- module: community.routeros.api_facts
Expand Down Expand Up @@ -273,6 +281,7 @@ def main():
handle_disabled=dict(type='str', choices=['exclamation', 'null-value', 'omit'], default='exclamation'),
hide_defaults=dict(type='bool', default=True),
include_dynamic=dict(type='bool', default=False),
include_builtin=dict(type='bool', default=False),
)
module_args.update(api_argument_spec())

Expand All @@ -292,6 +301,7 @@ def main():
handle_disabled = module.params['handle_disabled']
hide_defaults = module.params['hide_defaults']
include_dynamic = module.params['include_dynamic']
include_builtin = module.params['include_builtin']
try:
api_path = compose_api_path(api, path)

Expand All @@ -301,12 +311,17 @@ def main():
if not include_dynamic:
if entry.get('dynamic', False):
continue
if not include_builtin:
if entry.get('builtin', False):
continue
if not unfiltered:
for k in list(entry):
if k == '.id':
continue
if k == 'dynamic' and include_dynamic:
continue
if k == 'builtin' and include_builtin:
continue
if k not in path_info.fields:
entry.pop(k)
if handle_disabled != 'omit':
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/api_modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- Use the M(community.routeros.api_find_and_modify) module to modify one or multiple entries in a controlled way
depending on some search conditions.
- To make a backup of a path that can be restored with this module, use the M(community.routeros.api_info) module.
- The module ignores dynamic entries.
- The module ignores dynamic and builtin entries.
- B(Note) that this module is still heavily in development, and only supports B(some) paths.
If you want to support new paths, or think you found problems with existing paths, please first
L(create an issue in the community.routeros Issue Tracker,https://github.com/ansible-collections/community.routeros/issues/).
Expand Down Expand Up @@ -483,7 +483,7 @@ def match_entries(new_entries, old_entries, path_info, module):
def remove_dynamic(entries):
result = []
for entry in entries:
if entry.get('dynamic', False):
if entry.get('dynamic', False) or entry.get('builtin', False):
continue
result.append(entry)
return result
Expand Down

0 comments on commit b539ed6

Please sign in to comment.