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

Modified 'aci_rest' and 'aci_config_snapshot' modules to display the correct URL output string #487

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
9 changes: 8 additions & 1 deletion plugins/module_utils/aci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this function in aci.py or should we place in the aci_rest which is the only place where it is used?


HAS_URLPARSE = True
except Exception:
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions plugins/modules/aci_config_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions plugins/modules/aci_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -406,8 +407,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()
Expand All @@ -434,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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is the only location where this is used because it is an exception we could also decide to only set here by leveraging the url parse/unparse function here

Suggested change
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
Expand Down