From f90ec1746535747c14785691ed785caa3c545947 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Fri, 4 Jan 2019 00:38:57 +0100 Subject: [PATCH] ACI: Implement encoded query_string (#50358) This requires urldecode support in Ansible --- lib/ansible/module_utils/network/aci/aci.py | 5 +++-- lib/ansible/module_utils/network/aci/msc.py | 6 +++--- .../integration/targets/aci_ap/tasks/main.yml | 5 ++++- .../integration/targets/aci_bd/tasks/main.yml | 2 +- .../targets/aci_bd_subnet/tasks/main.yml | 10 +++++----- .../targets/aci_contract/tasks/main.yml | 2 +- .../aci_contract_subject/tasks/main.yml | 10 +++++----- .../aci_encap_pool_range/tasks/vlan.yml | 20 +++++++++---------- .../targets/aci_filter_entry/tasks/main.yml | 10 +++++----- .../aci_vlan_pool_encap_block/tasks/main.yml | 20 +++++++++---------- .../targets/aci_vrf/tasks/main.yml | 2 +- 11 files changed, 48 insertions(+), 44 deletions(-) diff --git a/lib/ansible/module_utils/network/aci/aci.py b/lib/ansible/module_utils/network/aci/aci.py index 4c82ccf2811..f697c8a02e3 100644 --- a/lib/ansible/module_utils/network/aci/aci.py +++ b/lib/ansible/module_utils/network/aci/aci.py @@ -37,6 +37,7 @@ import os from copy import deepcopy from ansible.module_utils.parsing.convert_bool import boolean +from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils.urls import fetch_url from ansible.module_utils._text import to_bytes @@ -387,13 +388,13 @@ class ACIModule(object): # TODO: This could be designed to update existing keys def update_qs(self, params): ''' Append key-value pairs to self.filter_string ''' - accepted_params = dict((k, v) for (k, v) in params.items() if v) + accepted_params = dict((k, v) for (k, v) in params.items() if v is not None) if accepted_params: if self.filter_string: self.filter_string += '&' else: self.filter_string = '?' - self.filter_string += '&'.join(['%s=%s' % (k, v) for (k, v) in accepted_params.items()]) + self.filter_string += urlencode(accepted_params) # TODO: This could be designed to accept multiple obj_classes and keys def build_filter(self, obj_class, params): diff --git a/lib/ansible/module_utils/network/aci/msc.py b/lib/ansible/module_utils/network/aci/msc.py index f40eb466859..0ec9f9f7bbc 100644 --- a/lib/ansible/module_utils/network/aci/msc.py +++ b/lib/ansible/module_utils/network/aci/msc.py @@ -31,7 +31,7 @@ from copy import deepcopy from ansible.module_utils.basic import AnsibleModule, json -from ansible.module_utils.six.moves.urllib.parse import urljoin +from ansible.module_utils.six.moves.urllib.parse import urlencode, urljoin from ansible.module_utils.urls import fetch_url from ansible.module_utils._text import to_native, to_bytes @@ -79,8 +79,8 @@ def issubset(subset, superset): def update_qs(params): ''' Append key-value pairs to self.filter_string ''' - accepted_params = dict((k, v) for (k, v) in params.items() if v) - return '?' + '&'.join(['%s=%s' % (k, v) for (k, v) in accepted_params.items()]) + accepted_params = dict((k, v) for (k, v) in params.items() if v is not None) + return '?' + urlencode(accepted_params) def msc_argument_spec(): diff --git a/test/integration/targets/aci_ap/tasks/main.yml b/test/integration/targets/aci_ap/tasks/main.yml index a31151c372e..6c9d6684b99 100644 --- a/test/integration/targets/aci_ap/tasks/main.yml +++ b/test/integration/targets/aci_ap/tasks/main.yml @@ -103,6 +103,9 @@ ap: "{{ fakevar | default(omit) }}" register: query_all +- debug: + msg: '{{ query_ap_ap.filter_string|urldecode }}' + - name: query assertions assert: that: @@ -118,7 +121,7 @@ - query_ap_ap is not changed - query_ap_ap.current != [] - query_ap_ap.current.0.fvAp is defined - - '"query-target-filter=eq(fvAp.name, \"anstest\")" in query_ap_ap.filter_string' + - '"query-target-filter=eq(fvAp.name, \"anstest\")" in query_ap_ap.filter_string|urldecode' - '"class/fvAp.json" in query_ap_ap.url' - query_all is not changed - query_all.current | length > 1 diff --git a/test/integration/targets/aci_bd/tasks/main.yml b/test/integration/targets/aci_bd/tasks/main.yml index 3273fc1034a..6a794a5ea5f 100644 --- a/test/integration/targets/aci_bd/tasks/main.yml +++ b/test/integration/targets/aci_bd/tasks/main.yml @@ -142,7 +142,7 @@ - '"tn-anstest.json" in query_tenant.url' - query_bd_bd is not changed - query_bd_bd.current != [] - - '"query-target-filter=eq(fvBD.name, \"anstest\")" in query_bd_bd.filter_string' + - '"query-target-filter=eq(fvBD.name, \"anstest\")" in query_bd_bd.filter_string|urldecode' - '"rsp-subtree-class=fvRsBdToEpRet,fvRsCtx,fvRsIgmpsn,fvRsBDToNdP" in query_bd_bd.filter_string' - '"class/fvBD.json" in query_bd_bd.url' - query_bd is not changed diff --git a/test/integration/targets/aci_bd_subnet/tasks/main.yml b/test/integration/targets/aci_bd_subnet/tasks/main.yml index d6ac5f53367..fc7e8c0d644 100644 --- a/test/integration/targets/aci_bd_subnet/tasks/main.yml +++ b/test/integration/targets/aci_bd_subnet/tasks/main.yml @@ -174,23 +174,23 @@ - get_all_tenant is not changed - '"tn-anstest.json" in get_all_tenant.url' - get_all_bd is not changed - - '"query-target-filter=eq(fvBD.name, \"anstest\")" in get_all_bd.filter_string' + - '"query-target-filter=eq(fvBD.name, \"anstest\")" in get_all_bd.filter_string|urldecode' - '"class/fvBD.json" in get_all_bd.url' - get_all_tenant_bd is not changed - '"tn-anstest/BD-anstest.json" in get_all_tenant_bd.url' - get_all_tenant_bd.current.0.fvBD.children | length > 1 - get_subnet_tenant is not changed - - '"rsp-subtree-filter=eq(fvSubnet.ip, \"10.100.100.1/24\")" in get_subnet_tenant.filter_string' + - '"rsp-subtree-filter=eq(fvSubnet.ip, \"10.100.100.1/24\")" in get_subnet_tenant.filter_string|urldecode' - '"tn-anstest.json" in get_subnet_tenant.url' - get_subnet_bd is not changed - - '"query-target-filter=eq(fvBD.name, \"anstest\")"' - - '"rsp-subtree-filter=eq(fvSubnet.ip, \"10.100.100.1/24\")" in get_subnet_bd.filter_string' + - '"query-target-filter=eq(fvBD.name, \"anstest\")"|urldecode' + - '"rsp-subtree-filter=eq(fvSubnet.ip, \"10.100.100.1/24\")" in get_subnet_bd.filter_string|urldecode' - '"class/fvBD.json" in get_subnet_bd.url' - get_subnet is not changed - get_subnet.current | length == 1 - '"tn-anstest/BD-anstest/subnet-[10.100.100.1/24].json" in get_subnet.url' - get_subnets_gateway is not changed - - '"query-target-filter=eq(fvSubnet.ip, \"10.100.100.1/24\")" in get_subnets_gateway.filter_string' + - '"query-target-filter=eq(fvSubnet.ip, \"10.100.100.1/24\")" in get_subnets_gateway.filter_string|urldecode' - '"class/fvSubnet.json" in get_subnets_gateway.url' - name: delete subnet - check mode works diff --git a/test/integration/targets/aci_contract/tasks/main.yml b/test/integration/targets/aci_contract/tasks/main.yml index 24a054562a1..fa9ae5fb3b8 100644 --- a/test/integration/targets/aci_contract/tasks/main.yml +++ b/test/integration/targets/aci_contract/tasks/main.yml @@ -109,7 +109,7 @@ - '"tn-anstest.json" in query_tenant.url' - query_name is not changed - query_name.current != [] - - '"query-target-filter=eq(vzBrCP.name, \"anstest\")" in query_name.filter_string' + - '"query-target-filter=eq(vzBrCP.name, \"anstest\")" in query_name.filter_string|urldecode' - '"class/vzBrCP.json" in query_name.url' - query_all is not changed - query_all.current | length > 1 diff --git a/test/integration/targets/aci_contract_subject/tasks/main.yml b/test/integration/targets/aci_contract_subject/tasks/main.yml index e10b2838cf7..a53621ac28a 100644 --- a/test/integration/targets/aci_contract_subject/tasks/main.yml +++ b/test/integration/targets/aci_contract_subject/tasks/main.yml @@ -156,15 +156,15 @@ - query_tenant_subject.current.0.fvTenant.attributes.name == "anstest" - query_tenant_subject.current.0.fvTenant.children.0.vzBrCP.children | length == 1 - query_tenant_subject.current.0.fvTenant.children.0.vzBrCP.children.0.vzSubj.attributes.name == "anstest" - - '"rsp-subtree-filter=eq(vzSubj.name, \"anstest\")" in query_tenant_subject.filter_string' + - '"rsp-subtree-filter=eq(vzSubj.name, \"anstest\")" in query_tenant_subject.filter_string|urldecode' - '"rsp-subtree-class=vzSubj" in query_tenant_subject.filter_string' - '"tn-anstest.json" in query_tenant_subject.url' - query_contract_subject is not changed - query_contract_subject.current.0.vzBrCP.attributes.name == "anstest" - query_contract_subject.current.0.vzBrCP.children | length == 1 - query_contract_subject.current.0.vzBrCP.children.0.vzSubj.attributes.name == "anstest" - - '"query-target-filter=eq(vzBrCP.name, \"anstest\")" in query_contract_subject.filter_string' - - '"rsp-subtree-filter=eq(vzSubj.name, \"anstest\")" in query_contract_subject.filter_string' + - '"query-target-filter=eq(vzBrCP.name, \"anstest\")" in query_contract_subject.filter_string|urldecode' + - '"rsp-subtree-filter=eq(vzSubj.name, \"anstest\")" in query_contract_subject.filter_string|urldecode' - '"rsp-subtree-class=vzSubj" in query_contract_subject.filter_string' - '"class/vzBrCP.json" in query_contract_subject.url' - query_tenant is not changed @@ -174,12 +174,12 @@ - '"tn-anstest.json" in query_tenant.url' - query_contract is not changed - query_contract.current.0.vzBrCP.attributes.name == "anstest" - - '"query-target-filter=eq(vzBrCP.name, \"anstest\")" in query_contract.filter_string' + - '"query-target-filter=eq(vzBrCP.name, \"anstest\")" in query_contract.filter_string|urldecode' - '"rsp-subtree-class=vzSubj" in query_contract.filter_string' - '"class/vzBrCP.json" in query_contract.url' - query_subject is not changed - query_subject.current.0.vzSubj.attributes.name == "anstest" - - '"query-target-filter=eq(vzSubj.name, \"anstest\")" in query_subject.filter_string' + - '"query-target-filter=eq(vzSubj.name, \"anstest\")" in query_subject.filter_string|urldecode' - '"class/vzSubj.json" in query_subject.url' - query_all is not changed - query_all.current > 1 diff --git a/test/integration/targets/aci_encap_pool_range/tasks/vlan.yml b/test/integration/targets/aci_encap_pool_range/tasks/vlan.yml index 6c8fb9c6323..77bad2b6985 100644 --- a/test/integration/targets/aci_encap_pool_range/tasks/vlan.yml +++ b/test/integration/targets/aci_encap_pool_range/tasks/vlan.yml @@ -228,9 +228,9 @@ that: - range_query_from_to_name is not changed - range_query_from_to_name.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in range_query_from_to_name.filter_string' - - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_from_to_name.filter_string' - - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in range_query_from_to_name.filter_string' + - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in range_query_from_to_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_from_to_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in range_query_from_to_name.filter_string|urldecode' - range_query_from_to_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - range_query_from_to_name.current.0.fvnsEncapBlk.attributes.from == "vlan-20" - range_query_from_to_name.current.0.fvnsEncapBlk.attributes.to == "vlan-40" @@ -246,8 +246,8 @@ that: - range_query_from_name is not changed - range_query_from_name.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in range_query_from_name.filter_string' - - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_from_name.filter_string' + - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in range_query_from_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_from_name.filter_string|urldecode' - range_query_from_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - range_query_from_name.current.0.fvnsEncapBlk.attributes.from == "vlan-20" @@ -262,8 +262,8 @@ that: - range_query_to_name is not changed - range_query_to_name.url.endswith('class/fvnsEncapBlk.json') - - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_to_name.filter_string' - - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in range_query_to_name.filter_string' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_to_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in range_query_to_name.filter_string|urldecode' - range_query_to_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - range_query_to_name.current.0.fvnsEncapBlk.attributes.to == "vlan-40" @@ -279,7 +279,7 @@ that: - range_query_name is not changed - range_query_name.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_name.filter_string' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in range_query_name.filter_string|urldecode' - range_query_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - name: query vlan pool range - from and to are filtered @@ -293,8 +293,8 @@ that: - range_query_from_to is not changed - range_query_from_to.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in range_query_from_to.filter_string' - - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in range_query_from_to.filter_string' + - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in range_query_from_to.filter_string|urldecode' + - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in range_query_from_to.filter_string#urldecode' - range_query_from_to.current.0.fvnsEncapBlk.attributes.from == "vlan-20" - range_query_from_to.current.0.fvnsEncapBlk.attributes.to == "vlan-40" diff --git a/test/integration/targets/aci_filter_entry/tasks/main.yml b/test/integration/targets/aci_filter_entry/tasks/main.yml index ca1bfa61bd5..d7760ba15a1 100644 --- a/test/integration/targets/aci_filter_entry/tasks/main.yml +++ b/test/integration/targets/aci_filter_entry/tasks/main.yml @@ -195,13 +195,13 @@ - query_filter_entry is not changed - query_filter_entry.current.0.vzFilter.attributes.name == "anstest" - query_filter_entry.current.0.vzFilter.children | length == 1 - - '"query-target-filter=eq(vzFilter.name, \"anstest\")" in query_filter_entry.filter_string' - - '"rsp-subtree-filter=eq(vzEntry.name, \"anstest\")" in query_filter_entry.filter_string' + - '"query-target-filter=eq(vzFilter.name, \"anstest\")" in query_filter_entry.filter_string|urldecode' + - '"rsp-subtree-filter=eq(vzEntry.name, \"anstest\")" in query_filter_entry.filter_string|urldecode' - '"class/vzFilter.json" in query_filter_entry.url' - query_tenant_entry is not changed - query_tenant_entry.current | length == 1 - query_tenant_entry.current.0.fvTenant.attributes.name == "anstest" - - '"rsp-subtree-filter=eq(vzEntry.name, \"anstest\")" in query_tenant_entry.filter_string' + - '"rsp-subtree-filter=eq(vzEntry.name, \"anstest\")" in query_tenant_entry.filter_string|urldecode' - '"rsp-subtree-class=vzEntry" in query_tenant_entry.filter_string' - '"tn-anstest.json" in query_tenant_entry.url' - query_tenant_filter is not changed @@ -212,11 +212,11 @@ - '"tn-anstest/flt-anstest.json" in query_tenant_filter.url' - query_entry is not changed - query_entry.current.0.vzEntry.attributes.name == "anstest" - - '"query-target-filter=eq(vzEntry.name, \"anstest\")" in query_entry.filter_string' + - '"query-target-filter=eq(vzEntry.name, \"anstest\")" in query_entry.filter_string|urldecode' - '"class/vzEntry.json" in query_entry.url' - query_filter is not changed - query_filter.current.0.vzFilter.attributes.name == "anstest" - - '"query-target-filter=eq(vzFilter.name, \"anstest\")" in query_filter.filter_string' + - '"query-target-filter=eq(vzFilter.name, \"anstest\")" in query_filter.filter_string|urldecode' - '"rsp-subtree-class=vzEntry" in query_filter.filter_string' - '"class/vzFilter.json" in query_filter.url' - query_tenant is not changed diff --git a/test/integration/targets/aci_vlan_pool_encap_block/tasks/main.yml b/test/integration/targets/aci_vlan_pool_encap_block/tasks/main.yml index 95259fb9360..2ad855f3530 100644 --- a/test/integration/targets/aci_vlan_pool_encap_block/tasks/main.yml +++ b/test/integration/targets/aci_vlan_pool_encap_block/tasks/main.yml @@ -224,9 +224,9 @@ that: - encap_block_query_from_to_name is not changed - encap_block_query_from_to_name.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in encap_block_query_from_to_name.filter_string' - - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_from_to_name.filter_string' - - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in encap_block_query_from_to_name.filter_string' + - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in encap_block_query_from_to_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_from_to_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in encap_block_query_from_to_name.filter_string|urldecode' - encap_block_query_from_to_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - encap_block_query_from_to_name.current.0.fvnsEncapBlk.attributes.from == "vlan-20" - encap_block_query_from_to_name.current.0.fvnsEncapBlk.attributes.to == "vlan-40" @@ -242,8 +242,8 @@ that: - encap_block_query_from_name is not changed - encap_block_query_from_name.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in encap_block_query_from_name.filter_string' - - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_from_name.filter_string' + - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in encap_block_query_from_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_from_name.filter_string|urldecode' - encap_block_query_from_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - encap_block_query_from_name.current.0.fvnsEncapBlk.attributes.from == "vlan-20" @@ -258,8 +258,8 @@ that: - encap_block_query_to_name is not changed - encap_block_query_to_name.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_to_name.filter_string' - - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in encap_block_query_to_name.filter_string' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_to_name.filter_string|urldecode' + - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in encap_block_query_to_name.filter_string|urldecode' - encap_block_query_to_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - encap_block_query_to_name.current.0.fvnsEncapBlk.attributes.to == "vlan-40" @@ -275,7 +275,7 @@ that: - encap_block_query_name is not changed - encap_block_query_name.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_name.filter_string' + - '"eq(fvnsEncapBlk.name, \"anstest\")" in encap_block_query_name.filter_string|urldecode' - encap_block_query_name.current.0.fvnsEncapBlk.attributes.name == "anstest" - name: Query vlan pool range - from and to are filtered @@ -289,8 +289,8 @@ that: - encap_block_query_from_to is not changed - encap_block_query_from_to.url.endswith("class/fvnsEncapBlk.json") - - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in encap_block_query_from_to.filter_string' - - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in encap_block_query_from_to.filter_string' + - '"eq(fvnsEncapBlk.from, \"vlan-20\")" in encap_block_query_from_to.filter_string|urldecode' + - '"eq(fvnsEncapBlk.to, \"vlan-40\")" in encap_block_query_from_to.filter_string|urldecode' - encap_block_query_from_to.current.0.fvnsEncapBlk.attributes.from == "vlan-20" - encap_block_query_from_to.current.0.fvnsEncapBlk.attributes.to == "vlan-40" diff --git a/test/integration/targets/aci_vrf/tasks/main.yml b/test/integration/targets/aci_vrf/tasks/main.yml index ca8b89d6113..40606eaf739 100644 --- a/test/integration/targets/aci_vrf/tasks/main.yml +++ b/test/integration/targets/aci_vrf/tasks/main.yml @@ -119,7 +119,7 @@ - query_vrf_vrf is not changed - query_vrf_vrf.current != [] - query_vrf_vrf.current.0.fvCtx.attributes.name == "anstest" - - '"query-target-filter=eq(fvCtx.name, \"anstest\")" in query_vrf_vrf.filter_string' + - '"query-target-filter=eq(fvCtx.name, \"anstest\")" in query_vrf_vrf.filter_string|urldecode' - '"class/fvCtx.json" in query_vrf_vrf.url' - query_vrf is not changed - query_vrf.current | length == 1