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 1 commit
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
15 changes: 10 additions & 5 deletions plugins/module_utils/aci.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,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 +424,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 +500,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 @@ -1523,11 +1523,16 @@ def parsed_url_path(self, url):
else:
return parse_result.path + "?" + parse_result.query

def synthesize_url(self, httpapi_url, local_path):
parse_url = urlparse(httpapi_url)
return {"protocol": parse_url.scheme, "host": parse_url.netloc, "path": local_path}
shrsr marked this conversation as resolved.
Show resolved Hide resolved

def api_call(self, method, url, data=None, return_response=False):
resp = None
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(self.synthesize_url(info.get("url"), self.path))
self.error = info.get("error")
self.httpapi_logs.extend(self.connection.pop_messages())
else:
Expand All @@ -1539,8 +1544,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