Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_xml and from_xml filter plugin #56

Merged
merged 33 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e68671e
Add filter plugin for xml_to_json
ashwini-mhatre Mar 24, 2021
64826c1
Add xml_to_json and json_to_xml filter plugin
ashwini-mhatre Mar 25, 2021
f7d3a81
add valiadte input function
ashwini-mhatre Mar 26, 2021
7863d4e
Add changelog fragments
ashwini-mhatre Mar 26, 2021
a702805
Remove unwanted code
ashwini-mhatre Mar 26, 2021
43b87ef
Fix tox linters
ashwini-mhatre Mar 26, 2021
5e0d600
Add toxlinters
ashwini-mhatre Mar 26, 2021
f4825b7
Address review comments
ashwini-mhatre Mar 26, 2021
1ca534d
fix linter
ashwini-mhatre Mar 26, 2021
e722126
Address review comments
ashwini-mhatre Mar 26, 2021
604c113
Address review comments
ashwini-mhatre Mar 26, 2021
8baebc6
fix syntax errors
ashwini-mhatre Mar 29, 2021
4e00e83
Add uts for xml_tojson
ashwini-mhatre Mar 30, 2021
055a162
Add Uts for xml_to_json and json_to_xml
ashwini-mhatre Mar 30, 2021
5e3d79f
Add integration testcases for xml_to_json and json_to_xml plugin
ashwini-mhatre Mar 30, 2021
0ef8bfb
Merge branch 'main' of https://github.com/ansible-collections/ansible…
ashwini-mhatre Mar 30, 2021
a68ea89
fix linters
ashwini-mhatre Mar 31, 2021
5a8d78d
Fix linters
ashwini-mhatre Mar 31, 2021
9376985
Address review comments
ashwini-mhatre Mar 31, 2021
ba0263b
fix linters
ashwini-mhatre Mar 31, 2021
9248953
address review comments
ashwini-mhatre Mar 31, 2021
440760e
address review comments
ashwini-mhatre Mar 31, 2021
daa4f30
revert unwanted changes
ashwini-mhatre Mar 31, 2021
d845d76
revert unwanted changes
ashwini-mhatre Mar 31, 2021
fc28e99
Address review comments
ashwini-mhatre Mar 31, 2021
655f327
Address review comments
ashwini-mhatre Apr 1, 2021
43a49ec
Address review comments
ashwini-mhatre Apr 1, 2021
12b04eb
fix linters
ashwini-mhatre Apr 1, 2021
df52ffd
fix sanity
ashwini-mhatre Apr 1, 2021
ddfa8e3
Add docs
ashwini-mhatre Apr 1, 2021
d3694f7
Address review comments
ashwini-mhatre Apr 8, 2021
f395e39
Address review comments
ashwini-mhatre Apr 8, 2021
32141a9
Merge branch 'main' of https://github.com/ansible-collections/ansible…
ashwini-mhatre Apr 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ PEP440 is the schema used to describe the versions of Ansible.
### Filter plugins
Name | Description
--- | ---
[ansible.utils.from_xml](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.from_xml_filter.rst)|Convert given XML string to native python dictionary.
[ansible.utils.get_path](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.get_path_filter.rst)|Retrieve the value in a variable using a path
[ansible.utils.index_of](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.index_of_filter.rst)|Find the indices of items in a list matching some criteria
[ansible.utils.to_paths](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.to_paths_filter.rst)|Flatten a complex object into a dictionary of paths and values
[ansible.utils.to_xml](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.to_xml_filter.rst)|Convert given JSON string to XML
[ansible.utils.validate](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.validate_filter.rst)|Validate data with provided criteria

### Lookup plugins
Expand Down
3 changes: 3 additions & 0 deletions changelogs/fragments/add-xmltojson-and-jsontoxml-filter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Add from_xml and to_xml fiter plugin (https://github.com/ansible-collections/ansible.utils/pull/56).
173 changes: 173 additions & 0 deletions docs/ansible.utils.from_xml_filter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
.. _ansible.utils.from_xml_filter:


**********************
ansible.utils.from_xml
**********************

**Convert given XML string to native python dictionary.**


Version added: 2.0.2

.. contents::
:local:
:depth: 1


Synopsis
--------
- This plugin converts the XML string to a native python dictionary.
- Using the parameters below- ``data|ansible.utils.from_xml``




Parameters
----------

.. raw:: html

<table border=0 cellpadding=0 class="documentation-table">
<tr>
<th colspan="1">Parameter</th>
<th>Choices/<font color="blue">Defaults</font></th>
<th>Configuration</th>
<th width="100%">Comments</th>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>data</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
/ <span style="color: red">required</span>
</div>
</td>
<td>
</td>
<td>
</td>
<td>
<div>The input XML string.</div>
<div>This option represents the XML value that is passed to the filter plugin in pipe format.</div>
<div>For example <code>config_data|ansible.utils.from_xml</code>, in this case <code>config_data</code> represents this option.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>engine</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
<b>Default:</b><br/><div style="color: blue">"xmltodict"</div>
</td>
<td>
</td>
<td>
<div>Conversion library to use within the filter plugin.</div>
</td>
</tr>
</table>
<br/>




Examples
--------

.. code-block:: yaml

#### Simple examples with out any engine. plugin will use default value as xmltodict

tasks:
- name: convert given XML to native python dictionary
ansible.builtin.set_fact:
data: "
<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"><schemas><schema/></schemas></netconf-state>
"

- debug:
msg: "{{ data|ansible.utils.from_xml }}"

##TASK######
# TASK [convert given XML to native python dictionary] *****************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:5
# ok: [localhost] => {
# "ansible_facts": {
# "data": " <netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"><schemas><schema/></schemas></netconf-state> "
# },
# "changed": false
# }
#
# TASK [debug] *************************************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
# "msg": {
# "netconf-state": {
# "@xmlns": "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring",
# "schemas": {
# "schema": null
# }
# }
# }
# }

#### example2 with engine=xmltodict

tasks:
- name: convert given XML to native python dictionary
ansible.builtin.set_fact:
data: "
<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"><schemas><schema/></schemas></netconf-state>
"

- debug:
msg: "{{ data|ansible.utils.from_xml('xmltodict') }}"

##TASK######
# TASK [convert given XML to native python dictionary] *****************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:5
# ok: [localhost] => {
# "ansible_facts": {
# "data": " <netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\"><schemas><schema/></schemas></netconf-state> "
# },
# "changed": false
# }
#
# TASK [debug] *************************************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
# "msg": {
# "netconf-state": {
# "@xmlns": "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring",
# "schemas": {
# "schema": null
# }
# }
# }
# }




Status
------


Authors
~~~~~~~

- Ashwini Mhatre (@amhatre)


.. hint::
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.
166 changes: 166 additions & 0 deletions docs/ansible.utils.to_xml_filter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
.. _ansible.utils.to_xml_filter:


********************
ansible.utils.to_xml
********************

**Convert given JSON string to XML**


Version added: 2.0.2

.. contents::
:local:
:depth: 1


Synopsis
--------
- This plugin converts the JSON string to XML.
- Using the parameters below- ``data|ansible.utils.to_xml``




Parameters
----------

.. raw:: html

<table border=0 cellpadding=0 class="documentation-table">
<tr>
<th colspan="1">Parameter</th>
<th>Choices/<font color="blue">Defaults</font></th>
<th>Configuration</th>
<th width="100%">Comments</th>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>data</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">dictionary</span>
/ <span style="color: red">required</span>
</div>
</td>
<td>
</td>
<td>
</td>
<td>
<div>The input JSON string .</div>
<div>This option represents the JSON value that is passed to the filter plugin in pipe format.</div>
<div>For example <code>config_data|ansible.utils.to_xml</code>, in this case <code>config_data</code> represents this option.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>engine</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">string</span>
</div>
</td>
<td>
<b>Default:</b><br/><div style="color: blue">"xmltodict"</div>
</td>
<td>
</td>
<td>
<div>Conversion library to use within the filter plugin.</div>
</td>
</tr>
</table>
<br/>




Examples
--------

.. code-block:: yaml

#### Simple examples with out any engine. plugin will use default value as xmltodict

- name: Define JSON data
ansible.builtin.set_fact:
data:
"interface-configurations":
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"
"interface-configuration":
- debug:
msg: "{{ data|ansible.utils.to_xml }}"

# TASK [Define JSON data ] *************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:5
# ok: [localhost] => {
# "ansible_facts": {
# "data": {
# "interface-configurations": {
# "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
# "interface-configuration": null
# }
# }
# },
# "changed": false
# }
#
# TASK [debug] ***********************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
# "msg": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<interface-configurations xmlns=\"http://cisco.com/ns/yang/
# Cisco-IOS-XR-ifmgr-cfg\">\n\t<interface-configuration></interface-configuration>\n</interface-configurations>"
# }

#### example2 with engine=xmltodict

- name: Define JSON data
ansible.builtin.set_fact:
data:
"interface-configurations":
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"
"interface-configuration":
- debug:
msg: "{{ data|ansible.utils.to_xml('xmltodict') }}"

# TASK [Define JSON data ] *************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:5
# ok: [localhost] => {
# "ansible_facts": {
# "data": {
# "interface-configurations": {
# "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
# "interface-configuration": null
# }
# }
# },
# "changed": false
# }
# TASK [debug] ***********************************************************************************************************
# task path: /Users/amhatre/ansible-collections/playbooks/test_utils_json_to_xml.yaml:13
# Loading collection ansible.utils from /Users/amhatre/ansible-collections/collections/ansible_collections/ansible/utils
# ok: [localhost] => {
# "msg": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<interface-configurations xmlns=\"http://cisco.com/ns/yang/
# Cisco-IOS-XR-ifmgr-cfg\">\n\t<interface-configuration></interface-configuration>\n</interface-configurations>"
# }




Status
------


Authors
~~~~~~~

- Ashwini Mhatre (@amhatre)


.. hint::
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.
Loading