Add module_utils and filters for quoting and unquoting (#53)

* Move splitting code to own file.

* Move list to dictionary code to quoting as well.

* Add quoting functionality.

* Add quoting filters.

* Add integration tests to CI.

* Fix bugs, increase coverage.

* Make parsing more strict.

* Extract function parse_argument_value from split_routeros_command to make proper parsing of WHERE possible.

* Adjust expected error message in integration tests.

* Simplify code and improve coverage.

* Add changelog fragment for WHERE strictness in api module.

* Add documenation.

* Fix typo.

* Add documentation references.

* Add example to api module which uses quote_argument_value.

* Fix bug, and add tests which prevent this in the future.

* Add more escape sequence tests.

* Make sure all control characters are quoted.
This commit is contained in:
Felix Fontein 2021-10-11 23:44:40 +02:00 committed by GitHub
parent f9d246cd7a
commit d73eb1c144
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 803 additions and 145 deletions

View file

@ -0,0 +1,2 @@
shippable/posix/group1
skip/python2.6

View file

@ -0,0 +1,59 @@
---
- name: "Test split filter"
assert:
that:
- "'' | community.routeros.split == []"
- "'foo bar' | community.routeros.split == ['foo', 'bar']"
- >
'foo bar="a b c"' | community.routeros.split == ['foo', 'bar=a b c']
- name: "Test split filter error handling"
set_fact:
test: >-
{{ 'a="' | community.routeros.split }}
ignore_errors: true
register: result
- name: "Verify split filter error handling"
assert:
that:
- >-
result.msg == "Unexpected end of string during escaped parameter"
- name: "Test quote_argument filter"
assert:
that:
- >
'a=' | community.routeros.quote_argument == 'a=""'
- >
'a=b' | community.routeros.quote_argument == 'a=b'
- >
'a=b c' | community.routeros.quote_argument == 'a="b c"'
- >
'a=""' | community.routeros.quote_argument == 'a="\\"\\""'
- name: "Test quote_argument_value filter"
assert:
that:
- >
'' | community.routeros.quote_argument_value == '""'
- >
'foo' | community.routeros.quote_argument_value == 'foo'
- >
'"foo bar"' | community.routeros.quote_argument_value == '"\\"foo bar\\""'
- name: "Test join filter"
assert:
that:
- >
['a=', 'b=c d'] | community.routeros.join == 'a="" b="c d"'
- name: "Test list_to_dict filter"
assert:
that:
- >
['a=', 'b=c'] | community.routeros.list_to_dict == {'a': '', 'b': 'c'}
- >
['a=', 'b=c'] | community.routeros.list_to_dict(skip_empty_values=True) == {'b': 'c'}
- >
['a', 'b=c'] | community.routeros.list_to_dict(require_assignment=False) == {'a': none, 'b': 'c'}