Merge pull request #11875 from shanemcd/url-prefixing-collection

Make our collection work with prefixed API endpoints
This commit is contained in:
Shane McDonald
2022-03-21 08:09:08 -04:00
committed by GitHub

View File

@@ -133,6 +133,8 @@ class ControllerModule(AnsibleModule):
# Try to parse the hostname as a url # Try to parse the hostname as a url
try: try:
self.url = urlparse(self.host) self.url = urlparse(self.host)
# Store URL prefix for later use in build_url
self.url_prefix = self.url.path
except Exception as e: except Exception as e:
self.fail_json(msg="Unable to parse controller_host as a URL ({1}): {0}".format(self.host, e)) self.fail_json(msg="Unable to parse controller_host as a URL ({1}): {0}".format(self.host, e))
@@ -147,8 +149,9 @@ class ControllerModule(AnsibleModule):
# Make sure we start with /api/vX # Make sure we start with /api/vX
if not endpoint.startswith("/"): if not endpoint.startswith("/"):
endpoint = "/{0}".format(endpoint) endpoint = "/{0}".format(endpoint)
if not endpoint.startswith("/api/"): prefix = self.url_prefix.rstrip("/")
endpoint = "/api/v2{0}".format(endpoint) if not endpoint.startswith(prefix + "/api/"):
endpoint = prefix + "/api/v2{0}".format(endpoint)
if not endpoint.endswith('/') and '?' not in endpoint: if not endpoint.endswith('/') and '?' not in endpoint:
endpoint = "{0}/".format(endpoint) endpoint = "{0}/".format(endpoint)
@@ -589,8 +592,10 @@ class ControllerAPIModule(ControllerModule):
"application": None, "application": None,
"scope": "write", "scope": "write",
} }
# Preserve URL prefix
endpoint = self.url_prefix.rstrip('/') + '/api/v2/tokens/'
# Post to the tokens endpoint with baisc auth to try and get a token # Post to the tokens endpoint with baisc auth to try and get a token
api_token_url = (self.url._replace(path='/api/v2/tokens/')).geturl() api_token_url = (self.url._replace(path=endpoint)).geturl()
try: try:
response = self.session.open( response = self.session.open(
@@ -954,9 +959,10 @@ class ControllerAPIModule(ControllerModule):
if self.authenticated and self.oauth_token_id: if self.authenticated and self.oauth_token_id:
# Attempt to delete our current token from /api/v2/tokens/ # Attempt to delete our current token from /api/v2/tokens/
# Post to the tokens endpoint with baisc auth to try and get a token # Post to the tokens endpoint with baisc auth to try and get a token
endpoint = self.url_prefix.rstrip('/') + '/api/v2/tokens/{0}/'.format(self.oauth_token_id)
api_token_url = ( api_token_url = (
self.url._replace( self.url._replace(
path='/api/v2/tokens/{0}/'.format(self.oauth_token_id), query=None # in error cases, fail_json exists before exception handling path=endpoint, query=None # in error cases, fail_json exists before exception handling
) )
).geturl() ).geturl()