Commit 7b207c76 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis
Browse files

Merge branch 'feature-astakosclient-docs' into develop

Refs #3439
parents f9c8157c 8894686b
......@@ -53,6 +53,7 @@ SYNNEFO_PROJECTS = {
'snf-webproject': 'dev',
'snf-common': 'dev',
'snf-image': 'dev',
'snf-astakos-client': 'dev',
'snf-cyclades-app': 'dev'
}
......
......@@ -121,6 +121,7 @@ They are also available from our apt repository: ``apt.okeanos.grnet.gr``
* `snf-pithos-webclient <http://docs.dev.grnet.gr/pithos-webclient/latest/index.html>`_
* `snf-cyclades-app <http://docs.dev.grnet.gr/snf-cyclades-app/latest/index.html>`_
* `snf-cyclades-gtools <http://docs.dev.grnet.gr/snf-cyclades-gtools/latest/index.html>`_
* `snf-astakos-client <http://docs.dev.grnet.gr/snf-astakos-client/latest/index.html>`_
* `snf-vncauthproxy <https://code.grnet.gr/projects/vncauthproxy>`_
* `snf-image <https://code.grnet.gr/projects/snf-image/wiki/>`_
* `snf-image-creator <http://docs.dev.grnet.gr/snf-image-creator/latest/index.html>`_
......
......@@ -40,13 +40,14 @@ from copy import copy
import simplejson
from astakosclient.utils import retry, scheme_to_class
from astakosclient.errors import \
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden, \
NoUserName, NoUUID
# --------------------------------------------------------------------
# Astakos Client Class
def getTokenFromCookie(request, cookie_name):
def get_token_from_cookie(request, cookie_name):
"""Extract token from the cookie name provided
Cookie should be in the same form as astakos
......@@ -107,8 +108,8 @@ class AstakosClient():
# ----------------------------------
@retry
def _callAstakos(self, token, request_path,
headers=None, body=None, method="GET"):
def _call_astakos(self, token, request_path,
headers=None, body=None, method="GET"):
"""Make the actual call to Astakos Service"""
hashed_token = hashlib.sha1()
hashed_token.update(token)
......@@ -145,7 +146,7 @@ class AstakosClient():
# Send request
try:
(data, status) = _doRequest(conn, method, request_path, **kwargs)
(data, status) = _do_request(conn, method, request_path, **kwargs)
except Exception as err:
self.logger.error("Failed to send request: %s" % repr(err))
raise AstakosClientException(str(err))
......@@ -167,8 +168,8 @@ class AstakosClient():
return simplejson.loads(unicode(data))
# ------------------------
def authenticate(self, token, usage=False):
"""Check if user is authenticated Astakos user
def get_user_info(self, token, usage=False):
"""Authenticate user and get user's info as a dictionary
Keyword arguments:
token -- user's token (string)
......@@ -182,18 +183,23 @@ class AstakosClient():
auth_path = "/im/authenticate"
if usage:
auth_path += "?usage=1"
return self._callAstakos(token, auth_path)
return self._call_astakos(token, auth_path)
# ----------------------------------
def _uuidCatalog(self, token, uuids, req_path):
def _uuid_catalog(self, token, uuids, req_path):
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({'uuids': uuids})
data = self._callAstakos(
data = self._call_astakos(
token, req_path, req_headers, req_body, "POST")
# XXX: check if exists
return data.get("uuid_catalog")
if "uuid_catalog" in data:
return data.get("uuid_catalog")
else:
m = "_uuid_catalog request returned %s. No uuid_catalog found" \
% data
self.logger.error(m)
raise AstakosClientException(m)
def getDisplayNames(self, token, uuids):
def get_usernames(self, token, uuids):
"""Return a uuid_catalog dictionary for the given uuids
Keyword arguments:
......@@ -205,43 +211,52 @@ class AstakosClient():
"""
req_path = "/user_catalogs"
return self._uuidCatalog(token, uuids, req_path)
return self._uuid_catalog(token, uuids, req_path)
def getDisplayName(self, token, uuid):
"""Return the displayName of a uuid (see getDisplayNames)"""
def get_username(self, token, uuid):
"""Return the user name of a uuid (see get_usernames)"""
if not uuid:
m = "No uuid was given"
self.logger.error(m)
raise ValueError(m)
uuid_dict = self.getDisplayNames(token, [uuid])
# XXX: check if exists
return uuid_dict.get(uuid)
uuid_dict = self.get_usernames(token, [uuid])
if uuid in uuid_dict:
return uuid_dict.get(uuid)
else:
raise NoUserName(uuid)
def getServiceDisplayNames(self, token, uuids):
def service_get_usernames(self, token, uuids):
"""Return a uuid_catalog dict using a service's token"""
req_path = "/service/api/user_catalogs"
return self._uuidCatalog(token, uuids, req_path)
return self._uuid_catalog(token, uuids, req_path)
def getServiceDisplayName(self, token, uuid):
def service_get_username(self, token, uuid):
"""Return the displayName of a uuid using a service's token"""
if not uuid:
m = "No uuid was given"
self.logger.error(m)
raise ValueError(m)
uuid_dict = self.getServiceDisplayNames(token, [uuid])
# XXX: check if exists
return uuid_dict.get(uuid)
uuid_dict = self.service_get_usernames(token, [uuid])
if uuid in uuid_dict:
return uuid_dict.get(uuid)
else:
raise NoUserName(uuid)
# ----------------------------------
def _displayNameCatalog(self, token, display_names, req_path):
def _displayname_catalog(self, token, display_names, req_path):
req_headers = {'content-type': 'application/json'}
req_body = simplejson.dumps({'displaynames': display_names})
data = self._callAstakos(
data = self._call_astakos(
token, req_path, req_headers, req_body, "POST")
# XXX: check if exists
return data.get("displayname_catalog")
if "displayname_catalog" in data:
return data.get("displayname_catalog")
else:
m = "_displayname_catalog request returned %s. " \
"No displayname_catalog found" % data
self.logger.error(m)
raise AstakosClientException(m)
def getUUIDs(self, token, display_names):
def get_uuids(self, token, display_names):
"""Return a displayname_catalog for the given names
Keyword arguments:
......@@ -253,44 +268,48 @@ class AstakosClient():
"""
req_path = "/user_catalogs"
return self._displayNameCatalog(token, display_names, req_path)
return self._displayname_catalog(token, display_names, req_path)
def getUUID(self, token, display_name):
def get_uuid(self, token, display_name):
"""Return the uuid of a name (see getUUIDs)"""
if not display_name:
m = "No display_name was given"
self.logger.error(m)
raise ValueError(m)
name_dict = self.getUUIDs(token, [display_name])
# XXX: check if exists
return name_dict.get(display_name)
name_dict = self.get_uuids(token, [display_name])
if display_name in name_dict:
return name_dict.get(display_name)
else:
raise NoUUID(display_name)
def getServiceUUIDs(self, token, display_names):
def service_get_uuids(self, token, display_names):
"""Return a display_name catalog using a service's token"""
req_path = "/service/api/user_catalogs"
return self._displayNameCatalog(token, display_names, req_path)
return self._displayname_catalog(token, display_names, req_path)
def getServiceUUID(self, token, display_name):
def service_get_uuid(self, token, display_name):
"""Return the uuid of a name using a service's token"""
if not display_name:
m = "No display_name was given"
self.logger.error(m)
raise ValueError(m)
name_dict = self.getServiceUUIDs(token, [display_name])
# XXX: check if exists
return name_dict.get(display_name)
name_dict = self.service_get_uuids(token, [display_name])
if display_name in name_dict:
return name_dict.get(display_name)
else:
raise NoUUID(display_name)
# ----------------------------------
def getServices(self):
def get_services(self):
"""Return a list of dicts with the registered services"""
return self._callAstakos("dummy token", "/im/get_services")
return self._call_astakos("dummy token", "/im/get_services")
# --------------------------------------------------------------------
# Private functions
# We want _doRequest to be a distinct function
# so that we can replace it during unit tests.
def _doRequest(conn, method, url, **kwargs):
def _do_request(conn, method, url, **kwargs):
"""The actual request. This function can easily be mocked"""
conn.request(method, url, **kwargs)
response = conn.getresponse()
......
......@@ -63,3 +63,17 @@ class NotFound(AstakosClientException):
def __init__(self, message):
"""404 Not Found"""
super(NotFound, self).__init__(message, 404)
class NoUserName(AstakosClientException):
def __init__(self, uuid):
"""No display name for the given uuid"""
message = "No display name for the given uuid: %s" % uuid
super(NoUserName, self).__init__(message)
class NoUUID(AstakosClientException):
def __init__(self, display_name):
"""No uuid for the given display name"""
message = "No uuid for the given display name: %s" % display_name
super(NoUUID, self).__init__(message)
This diff is collapsed.
# Makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build
# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
clean:
-rm -rf $(BUILDDIR)/*
html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."
json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."
htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."
qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/synnefo.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/synnefo.qhc"
devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/synnefo"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/synnefo"
@echo "# devhelp"
epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."
latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."
man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."
info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."
linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."
doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
from astakosclient.version import __version__
project = u'nnefo'
copyright = u'2012-2013, GRNET'
version = __version__
release = __version__
html_title = 'synnefo ' + version
templates_path = ['_templates']
source_suffix = '.rst'
master_doc = 'index'
exclude_patterns = ['_build']
pygments_style = 'sphinx'
html_theme = 'default'
html_theme_options = {
'collapsiblesidebar': 'true',
'footerbgcolor': '#55b577',
'footertextcolor': '#000000',
'sidebarbgcolor': '#ffffff',
'sidebarbtncolor': '#f2f2f2',
'sidebartextcolor': '#000000',
'sidebarlinkcolor': '#328e4a',
'relbarbgcolor': '#55b577',
'relbartextcolor': '#ffffff',
'relbarlinkcolor': '#ffffff',
'bgcolor': '#ffffff',
'textcolor': '#000000',
'headbgcolor': '#ffffff',
'headtextcolor': '#000000',
'headlinkcolor': '#c60f0f',
'linkcolor': '#328e4a',
'visitedlinkcolor': '#63409b',
'codebgcolor': '#eeffcc',
'codetextcolor': '#333333'
}
html_static_path = ['_static']
htmlhelp_basename = 'synnefodoc'
intersphinx_mapping = {
'pithon': ('http://docs.python.org/', None),
'django': ('https://docs.djangoproject.com/en/dev/',
'https://docs.djangoproject.com/en/dev/_objects/')
}
SYNNEFO_DOCS_BASE_URL = 'http://docs.dev.grnet.gr/'
SYNNEFO_PROJECTS = {
'synnefo': 'dev',
'pithos': 'dev',
'snf-webproject': 'dev',
'snf-common': 'dev',
'snf-image': 'dev',
'snf-cyclades-app': 'dev'
}
for name, ver in SYNNEFO_PROJECTS.iteritems():
intersphinx_mapping[name.replace("-", "")] = (SYNNEFO_DOCS_BASE_URL +
'%s/%s/' % (name, ver),
None)
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
'sphinx.ext.viewcode']
.. _snf-astakos-client:
Component snf-astakos-client
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The Synnefo component :ref:`snf-astakos-client <snf-astakos-client>` defines a
default client for the :ref:`Astakos <astakos>` service. It is designed to be
simple and minimal, hence easy to debug and test.
It uses the user's authentication token to query Astakos for:
* User's info
* Usernames for given UUIDs
* UUIDs for given usernames
It can also query Astakos with another service's (Cyclades or Pithos)
authentication token for:
* Usernames for given UUIDs
* UUIDs for given usernames
Additionally, there are options for using the `objpool
<https://github.com/grnet/objpool>`_ library to pool the http connections.
Basic example
=============
The ``astakosclient`` module provides the ``AstakosClient`` class. This section
demonstrates how to get user's info using ``astakosclient``.
.. code-block:: python
from astakosclient import AstakosClient
client = AstakosClient("https://accounts.example.com")
user_info = client.get_user_info("UQpYas7ElzWGD5yCcEXtjw==")
print user_info['username']
Another example where we ask for the username of a user with UUID:
``b3de8eb0-3958-477e-als9-789af8dd352c``
.. code-block:: python
from astakosclient import AstakosClient
client = AstakosClient("https://accounts.example.com")
username = client.get_username("UQpYas7ElzWGD5yCcEXtjw==",
"b3de8eb0-3958-477e-als9-789af8dd352c")
print username
Classes and functions
=====================
This section describes in depth the API of ``astakosclient``.
Astakos Client
--------------
*class* astakosclient.\ **AstakosClient(**\ astakos_url,
retry=0, use_pool=False, pool_size=8, logger=None\ **)**
Initialize an instance of **AstakosClient** given the *astakos_url*.
Optionally one can specify if we are going to use a pool, the pool_size
and the number of retries if the connection fails.
This class provides the following methods:
**get_user_info(**\ token, usage=False\ **)**
Given a valid authentication token it returns a dict with the
correspoinding user's info. If usage is set to True more
information about user's resources will be returned.
In case of error raise an AstakosClientException exception.
**get_usernames(**\ token, uuids\ **)**
Given a valid authentication token and a list of UUIDs
return a uuid_catalog, that is a dictionary with the given
UUIDs as keys and the corresponding user names as values.
Invalid UUIDs will not be in the dictionary.
In case of error raise an AstakosClientException exception.
**get_username(**\ token, uuid\ **)**
Given a valid authentication token and a UUID (as string)
return the corresponding user name (as string).
In case of invalid UUID raise NoUserName exception.
In case of error raise an AstakosClientException exception.
**service_get_usernames(**\ token, uuids\ **)**
Same as get_usernames but called with a service's token.
**service_get_username(**\ token, uuid\ **)**
Same as get_username but called with a service's token.
**get_uuids(**\ token, display_names\ **)**
Given a valid authentication token and a list of usernames
return a displayname_catalog, that is a dictionary with the given
usernames as keys and the corresponding UUIDs as values.
Invalid usernames will not be in the dictionary.
In case of error raise an AstakosClientException exception.
**get_uuid(**\ token, display_name\ **)**
Given a valid authentication token and a username (as string)
return the corresponding UUID (as string).
In case of invalid user name raise NoUUID exception.
In case of error raise an AstakosClientException exception.
**service_get_uuids(**\ token, uuids\ **)**
Same as get_uuids but called with a service's token.
**service_get_uuid(**\ token, uuid\ **)**
Same as get_uuid but called with a service's token.
**get_services()**
Return a list of dicts with the registered services.
Public Functions
----------------
**get_token_from_cookie(**\ request, cookie_name\ **)**
Given a Django request object and an Astakos cookie name
extract the user's token from it.
Exceptions and Errors
=====================
*exception* **AstakosClientException**
Raised in case of an error. It contains an error message and the
corresponding http status code. Other exceptions raise by astakosclient
module are derived from this one.
*exception* **BadRequest**
Raised in case of a Bad Request, with status 400.
*exception* **Unauthorized**
Raised in case of Invalid token (unauthorized access), with status 401.
*exception* **Forbidden**
The server understood the request, but is refusing to fulfill it.
Status 401.
*exception* **NotFound**
The server has not found anything matching the Request-URI. Status 404.