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

Fixed issue with default values for ssl, proxy, timeout in 'aci.py' and the display of host in the url when the plugin 'httpapi' is used #475

Merged
merged 3 commits into from
Sep 28, 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: 5 additions & 4 deletions plugins/httpapi/aci.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def logout(self):
msg = "Error on attempt to logout from APIC. {0}".format(exc_logout)
raise ConnectionError(self._return_info("", method, path, msg))
self.connection._auth = None
self._verify_response(response, method, response_data)
self._verify_response(response, method, path, response_data)

def set_parameters(self):
connection_parameters = {}
Expand Down Expand Up @@ -220,7 +220,7 @@ def send_request(self, method, path, data):
# recurse through function for retrying the request
return self.send_request(method, path, data)
# return statement executed upon each successful response from the request function
return self._verify_response(response, method, response_data)
return self._verify_response(response, method, path, response_data)

# Built-in-function
def handle_httperror(self, exc):
Expand All @@ -240,16 +240,17 @@ def validate_url(self, url):
else:
return validated_url

def _verify_response(self, response, method, response_data):
def _verify_response(self, response, method, path, response_data):
"""Process the return code and response object from APIC"""
response_value = self._get_response_value(response_data)
response_code = response.getcode()
path = self.validate_url(response.url)
# Response check to remain consistent with fetch_url's response
if str(response) == "HTTP Error 400: Bad Request":
msg = "{0}".format(response)
else:
msg = "{0} ({1} bytes)".format(response.msg, len(response_value))
return self._return_info(response_code, method, "", msg, respond_data=response_value)
return self._return_info(response_code, method, path, msg, respond_data=response_value)

def _get_response_value(self, response_data):
"""Extract string data from response_data returned from APIC"""
Expand Down
16 changes: 11 additions & 5 deletions plugins/module_utils/aci.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ def ospf_spec():
)


def integrate_url(httpapi_url, local_path):
parse_url = urlparse(httpapi_url)
return {"protocol": parse_url.scheme, "host": parse_url.netloc, "path": local_path}


class ACIModule(object):
def __init__(self, module):
self.module = module
Expand Down Expand Up @@ -410,7 +415,7 @@ def define_protocol(self):
"""Set protocol based on use_ssl parameter"""

# Set protocol for further use
self.params["protocol"] = "https" if self.params.get("use_ssl", True) else "http"
self.params["protocol"] = "https" if self.params.get("use_ssl") or self.params.get("use_ssl") is None else "http"

def set_connection(self):
if self.connection is None and self.module._socket_path:
Expand All @@ -424,7 +429,7 @@ def login(self):
payload = {
"aaaUser": {
"attributes": {
"name": self.params.get("username", "admin"),
"name": "admin" if self.params.get("username") is None else self.params.get("username"),
"pwd": self.params.get("password"),
}
}
Expand Down Expand Up @@ -500,7 +505,7 @@ def cert_auth(self, path=None, payload="", method=None):
)

if self.params.get("certificate_name") is None:
self.params["certificate_name"] = self.params.get("username", "admin")
self.params["certificate_name"] = "admin" if self.params.get("username") is None else self.params.get("username")
# NOTE: ACI documentation incorrectly adds a space between method and path
sig_request = method + path + payload
if HAS_CRYPTOGRAPHY:
Expand Down Expand Up @@ -1528,6 +1533,7 @@ def api_call(self, method, url, data=None, return_response=False):
if self.connection is not None:
self.connection.set_params(self.params)
info = self.connection.send_request(method, self.parsed_url_path(url), data)
self.url = "{protocol}://{host}/{path}".format_map(integrate_url(info.get("url"), self.path))
self.error = info.get("error")
self.httpapi_logs.extend(self.connection.pop_messages())
else:
Expand All @@ -1539,8 +1545,8 @@ def api_call(self, method, url, data=None, return_response=False):
data=data,
headers=self.headers,
method=method,
timeout=self.params.get("timeout", 30),
use_proxy=self.params.get("use_proxy", True),
timeout=30 if self.params.get("timeout") is None else self.params.get("timeout"),
use_proxy=True if self.params.get("use_proxy") is None else self.params.get("use_proxy"),
)

self.response = info.get("msg")
Expand Down
14 changes: 8 additions & 6 deletions plugins/modules/aci_config_rollback.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ def main():
aci.post_config()

elif state == "preview":
aci.url = "{protocol}://{host}/mqapi2/snapshots.diff.xml".format_map(module.params)
aci.path = "mqapi2/snapshots.diff.xml"
preview_params = {k: v for d in [{"path": aci.path}, module.params] for k, v in d.items()}
if aci.params.get("port") is not None:
aci.url = "{protocol}://{host}:{port}/{path}".format_map(preview_params)
else:
aci.url = "{protocol}://{host}/{path}".format_map(preview_params)
aci.filter_string = (
"?s1dn=uni/backupst/snapshots-[uni/fabric/configexp-{export_policy}]/snapshot-{snapshot}&"
"s2dn=uni/backupst/snapshots-[uni/fabric/configexp-{compare_export_policy}]/snapshot-{compare_snapshot}"
Expand All @@ -310,11 +315,8 @@ def get_preview(aci):
except AttributeError:
xml_to_json(aci, info.get("body"))
else:
try:
aci.result["raw"] = resp.read()
except AttributeError:
aci.result["raw"] = info.get("body")
aci.fail_json(msg="Request failed: {code} {text} (see 'raw' output)".format_map(aci.error))
aci.result["raw"] = info["body"]
aci.fail_json(msg="Request failed: see 'raw' output")


def xml_to_json(aci, response_data):
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/aci_rest/tasks/error_handling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
name: pyyaml
state: absent
ignore_errors: true # ignore errors to because of multiple executions for hosts at the same time
when: ansible_connection == "local"

- name: Create tenant with invalid src attribute
cisco.aci.aci_rest:
Expand Down Expand Up @@ -278,6 +279,7 @@
<<: *pyyaml
state: present
ignore_errors: true # ignore errors to because of multiple executions for hosts at the same time
when: ansible_connection == "local"

# Test case for invalid src

Expand Down