summaryrefslogtreecommitdiff
path: root/lib/ansible/galaxy/api.py
diff options
context:
space:
mode:
authorAdrian Likins <alikins@redhat.com>2019-08-28 16:59:34 -0400
committerGitHub <noreply@github.com>2019-08-28 16:59:34 -0400
commitaf01cb114cc4225ad323c99df9fd188b1b519069 (patch)
treefbf03638bc00654c81d1cfab7cfff2c5c4dfceaf /lib/ansible/galaxy/api.py
parentf9b0ab774f71b09c3f1c734f4bd19d57966dd431 (diff)
downloadansible-af01cb114cc4225ad323c99df9fd188b1b519069.tar.gz
Support galaxy v3/autohub API in ansible-galaxy (#60982)
* Add galaxy collections API v3 support Issue: ansible/galaxy-dev#60 - Determine if server supports v3 Use 'available_versions' from `GET /api` to determine if 'v3' api is available on the server. - Support v3 pagination style ie, 'limit/offset style', with the paginated responses based on https://jsonapi.org/format/#fetching-pagination v2 galaxy uses pagination that is more or less 'django rest framework style' or 'page/page_size style', based on the default drf pagination described at https://www.django-rest-framework.org/api-guide/pagination/#pagenumberpagination - Support galaxy v3 style error response The error objects returned by the galaxy v3 api are based on the JSONAPI response/errors format (https://jsonapi.org/format/#errors). This handles that style response. At least for publish_collection for now. Needs extracting/generalizing. Handle HTTPError in CollectionRequirement.from_name() with _handle_http_error(). It will raise AnsibleError based on the json in an error response. - Update unit tests update test/unit/galaxy/test_collection* to paramaterize calls to test against mocked v2 and v3 servers apis. Update artifacts_versions_json() to tale an api version paramater. Add error_json() for generating v3/v3 style error responses. So now, the urls generated and the pagination schema of the response will use the v3 version if the passed in GalaxyAPI 'galaxy_api' instance has 'v3' in it's available_api_versions * Move checking of server avail versions to collections.py collections.py needs to know the server api versions supported before it makes collection related calls, so the 'lazy' server version check in api.GalaxyAPI is never called and isn't set, so 'v3' servers weren't found. Update unit tests to mock the return value of the request instead of GalaxyAPI itself.
Diffstat (limited to 'lib/ansible/galaxy/api.py')
-rw-r--r--lib/ansible/galaxy/api.py19
1 files changed, 13 insertions, 6 deletions
diff --git a/lib/ansible/galaxy/api.py b/lib/ansible/galaxy/api.py
index 7df9d3b1af..45de4cbb89 100644
--- a/lib/ansible/galaxy/api.py
+++ b/lib/ansible/galaxy/api.py
@@ -43,11 +43,14 @@ def g_connect(method):
if not self.initialized:
display.vvvv("Initial connection to galaxy_server: %s" % self.api_server)
server_version = self._get_server_api_version()
+
if server_version not in self.SUPPORTED_VERSIONS:
raise AnsibleError("Unsupported Galaxy server API version: %s" % server_version)
self.baseurl = _urljoin(self.api_server, "api", server_version)
+
self.version = server_version # for future use
+
display.vvvv("Base API: %s" % self.baseurl)
self.initialized = True
return method(self, *args, **kwargs)
@@ -63,25 +66,32 @@ class GalaxyAPI(object):
SUPPORTED_VERSIONS = ['v1']
- def __init__(self, galaxy, name, url, username=None, password=None, token=None):
+ def __init__(self, galaxy, name, url, username=None, password=None, token=None, token_type=None):
self.galaxy = galaxy
self.name = name
self.username = username
self.password = password
self.token = token
+ self.token_type = token_type or 'Token'
self.api_server = url
self.validate_certs = not context.CLIARGS['ignore_certs']
self.baseurl = None
self.version = None
self.initialized = False
+ self.available_api_versions = {}
display.debug('Validate TLS certificates for %s: %s' % (self.api_server, self.validate_certs))
- def _auth_header(self, required=True):
+ def _auth_header(self, required=True, token_type=None):
+ '''Generate the Authorization header.
+
+ Valid token_type values are 'Token' (galaxy v2) and 'Bearer' (galaxy v3)'''
token = self.token.get() if self.token else None
+ # 'Token' for v2 api, 'Bearer' for v3
+ token_type = token_type or self.token_type
if token:
- return {'Authorization': "Token %s" % token}
+ return {'Authorization': "%s %s" % (token_type, token)}
elif self.username:
token = "%s:%s" % (to_text(self.username, errors='surrogate_or_strict'),
to_text(self.password, errors='surrogate_or_strict', nonstring='passthru') or '')
@@ -123,9 +133,6 @@ class GalaxyAPI(object):
except Exception as e:
raise AnsibleError("Could not process data from the API server (%s): %s " % (url, to_native(e)))
- if 'current_version' not in data:
- raise AnsibleError("missing required 'current_version' from server response (%s)" % url)
-
return data['current_version']
@g_connect