From 91f8bf400d0f7b73e66d80b00efc556247ba170f Mon Sep 17 00:00:00 2001 From: Shreyas Date: Tue, 3 Oct 2023 14:26:58 -0400 Subject: [PATCH 1/3] [minor_change] Modified 'aci_rest' and 'aci_config_snapshot' modules to display the correct URL output string --- plugins/modules/aci_config_snapshot.py | 5 +---- plugins/modules/aci_rest.py | 5 ++++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/modules/aci_config_snapshot.py b/plugins/modules/aci_config_snapshot.py index 52170ef40..bc36a5e62 100644 --- a/plugins/modules/aci_config_snapshot.py +++ b/plugins/modules/aci_config_snapshot.py @@ -279,8 +279,7 @@ def main(): target_filter={"name": export_policy}, ), ) - # Variable set for reuse of correct url in get_existing() triggered from exit_json() after query request - reset_url = aci.url + aci.get_existing() aci.payload( @@ -305,8 +304,6 @@ def main(): path = "api/node/mo/uni/backupst/jobs-[uni/fabric/configexp-{0}].json".format(export_policy) aci.api_call("GET", url="{0}/{1}".format(aci.base_url, path)) aci.result["job_details"] = aci.existing[0].get("configJobCont", {}) - # Reset state and url to display correct in output and trigger get_existing() function with correct url - aci.url = reset_url else: # Prefix the proper url to export_policy diff --git a/plugins/modules/aci_rest.py b/plugins/modules/aci_rest.py index 374c9206f..b17f047fa 100644 --- a/plugins/modules/aci_rest.py +++ b/plugins/modules/aci_rest.py @@ -406,8 +406,11 @@ def main(): module.fail_json(msg="Failed to parse provided XML payload: {0}".format(to_text(e)), payload=payload) # Perform actual request using auth cookie (Same as aci.request(), but also supports XML) - aci.url = "{0}/{1}".format(aci.base_url, path.lstrip("/")) + # NOTE By setting aci.path we ensure that Ansible displays accurate URL info when the plugin and the aci_rest module are used. + aci.path = path.lstrip("/") + aci.url = "{0}/{1}".format(aci.base_url, aci.path) if aci.params.get("method") != "get" and not rsp_subtree_preserve: + aci.path = "{0}?rsp-subtree=modified".format(aci.path) aci.url = update_qsl(aci.url, {"rsp-subtree": "modified"}) method = aci.params.get("method").upper() From 8bc0ba507b193770d22116a71b74dace04c98f0a Mon Sep 17 00:00:00 2001 From: Shreyas Date: Tue, 3 Oct 2023 16:02:11 -0400 Subject: [PATCH 2/3] [minor_change] Modified 'aci_rest' module to display the correct URL string when check mode is used --- plugins/module_utils/aci.py | 9 ++++++++- plugins/modules/aci_rest.py | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/module_utils/aci.py b/plugins/module_utils/aci.py index 9c6e2db2d..32a0bc838 100644 --- a/plugins/module_utils/aci.py +++ b/plugins/module_utils/aci.py @@ -85,7 +85,7 @@ HAS_XMLJSON_COBRA = False try: - from ansible.module_utils.six.moves.urllib.parse import urlparse + from ansible.module_utils.six.moves.urllib.parse import urlparse, urlunparse HAS_URLPARSE = True except Exception: @@ -320,6 +320,13 @@ def integrate_url(httpapi_url, local_path): return {"protocol": parse_url.scheme, "host": parse_url.netloc, "path": local_path} +def replace_apic_host(url, actual_host): + parsed_url = urlparse(url) + modified_parsed_url = parsed_url._replace(netloc=actual_host) + actual_url = urlunparse(modified_parsed_url) + return actual_url + + class ACIModule(object): def __init__(self, module): self.module = module diff --git a/plugins/modules/aci_rest.py b/plugins/modules/aci_rest.py index b17f047fa..9e1a02e43 100644 --- a/plugins/modules/aci_rest.py +++ b/plugins/modules/aci_rest.py @@ -250,6 +250,7 @@ import json import os +import re try: from ansible.module_utils.six.moves.urllib.parse import parse_qsl, urlencode, urlparse, urlunparse @@ -283,7 +284,7 @@ HAS_YAML = False from ansible.module_utils.basic import AnsibleModule -from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec +from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, replace_apic_host from ansible.module_utils._text import to_text @@ -437,6 +438,9 @@ def main(): aci.result["totalCount"] = aci.totalCount else: + # NOTE A case when aci_rest is used with check mode and the apic host is used directly from the inventory + if aci.connection is not None and aci.params.get("host") is None: + aci.url = replace_apic_host(aci.url, re.sub(r"[[\]]", "", aci.connection.get_option("host")).split(",")[0]) aci.method = method # Set changed to true so check_mode changed result is behaving similar to non aci_rest modules aci.result["changed"] = True From 4fb81a8ba2dfc7efbf3e546360e18c27a94cfe9c Mon Sep 17 00:00:00 2001 From: Shreyas Date: Wed, 4 Oct 2023 15:15:05 -0400 Subject: [PATCH 3/3] [ignore] Removed auxillary function to replace host from 'aci.py' and implemented the logic in 'aci_rest' --- plugins/module_utils/aci.py | 9 +-------- plugins/modules/aci_rest.py | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/plugins/module_utils/aci.py b/plugins/module_utils/aci.py index 32a0bc838..9c6e2db2d 100644 --- a/plugins/module_utils/aci.py +++ b/plugins/module_utils/aci.py @@ -85,7 +85,7 @@ HAS_XMLJSON_COBRA = False try: - from ansible.module_utils.six.moves.urllib.parse import urlparse, urlunparse + from ansible.module_utils.six.moves.urllib.parse import urlparse HAS_URLPARSE = True except Exception: @@ -320,13 +320,6 @@ def integrate_url(httpapi_url, local_path): return {"protocol": parse_url.scheme, "host": parse_url.netloc, "path": local_path} -def replace_apic_host(url, actual_host): - parsed_url = urlparse(url) - modified_parsed_url = parsed_url._replace(netloc=actual_host) - actual_url = urlunparse(modified_parsed_url) - return actual_url - - class ACIModule(object): def __init__(self, module): self.module = module diff --git a/plugins/modules/aci_rest.py b/plugins/modules/aci_rest.py index 9e1a02e43..ad73cb5bd 100644 --- a/plugins/modules/aci_rest.py +++ b/plugins/modules/aci_rest.py @@ -284,7 +284,7 @@ HAS_YAML = False from ansible.module_utils.basic import AnsibleModule -from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, replace_apic_host +from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec from ansible.module_utils._text import to_text @@ -440,7 +440,7 @@ def main(): else: # NOTE A case when aci_rest is used with check mode and the apic host is used directly from the inventory if aci.connection is not None and aci.params.get("host") is None: - aci.url = replace_apic_host(aci.url, re.sub(r"[[\]]", "", aci.connection.get_option("host")).split(",")[0]) + aci.url = urlunparse(urlparse(aci.url)._replace(netloc=re.sub(r"[[\]]", "", aci.connection.get_option("host")).split(",")[0])) aci.method = method # Set changed to true so check_mode changed result is behaving similar to non aci_rest modules aci.result["changed"] = True