| 1 |
======================= |
| 2 |
Using the V3 Client API |
| 3 |
======================= |
| 4 |
|
| 5 |
Introduction |
| 6 |
============ |
| 7 |
|
| 8 |
The main concepts in the Identity v3 API are: |
| 9 |
|
| 10 |
* :py:mod:`~keystoneclient.v3.credentials` |
| 11 |
* :py:mod:`~keystoneclient.v3.domain_configs` |
| 12 |
* :py:mod:`~keystoneclient.v3.domains` |
| 13 |
* :py:mod:`~keystoneclient.v3.endpoints` |
| 14 |
* :py:mod:`~keystoneclient.v3.groups` |
| 15 |
* :py:mod:`~keystoneclient.v3.policies` |
| 16 |
* :py:mod:`~keystoneclient.v3.projects` |
| 17 |
* :py:mod:`~keystoneclient.v3.regions` |
| 18 |
* :py:mod:`~keystoneclient.v3.role_assignments` |
| 19 |
* :py:mod:`~keystoneclient.v3.roles` |
| 20 |
* :py:mod:`~keystoneclient.v3.services` |
| 21 |
* :py:mod:`~keystoneclient.v3.tokens` |
| 22 |
* :py:mod:`~keystoneclient.v3.users` |
| 23 |
|
| 24 |
The :py:mod:`keystoneclient.v3.client` API lets you query and make changes |
| 25 |
through ``managers``. For example, to manipulate a project (formerly |
| 26 |
called tenant), you interact with a |
| 27 |
:py:class:`keystoneclient.v3.projects.ProjectManager` object. |
| 28 |
|
| 29 |
You obtain access to managers through attributes of a |
| 30 |
:py:class:`keystoneclient.v3.client.Client` object. For example, the |
| 31 |
``projects`` attribute of a ``Client`` object is a projects manager:: |
| 32 |
|
| 33 |
>>> from keystoneclient.v3 import client |
| 34 |
>>> keystone = client.Client(...) |
| 35 |
>>> keystone.projects.list() # List projects |
| 36 |
|
| 37 |
While it is possible to instantiate a |
| 38 |
:py:class:`keystoneclient.v3.client.Client` object (as done above for |
| 39 |
clarity), the recommended approach is to use the discovery mechanism |
| 40 |
provided by the :py:class:`keystoneclient.client.Client` class. The |
| 41 |
appropriate class will be instantiated depending on the API versions |
| 42 |
available:: |
| 43 |
|
| 44 |
>>> from keystoneclient import client |
| 45 |
>>> keystone = |
| 46 |
... client.Client(auth_url='http://localhost:5000', ...) |
| 47 |
>>> type(keystone) |
| 48 |
<class 'keystoneclient.v3.client.Client'> |
| 49 |
|
| 50 |
One can force the use of a specific version of the API, either by |
| 51 |
using the ``version`` keyword argument:: |
| 52 |
|
| 53 |
>>> from keystoneclient import client |
| 54 |
>>> keystone = client.Client(auth_url='http://localhost:5000', |
| 55 |
version=(2,), ...) |
| 56 |
>>> type(keystone) |
| 57 |
<class 'keystoneclient.v2_0.client.Client'> |
| 58 |
>>> keystone = client.Client(auth_url='http://localhost:5000', |
| 59 |
version=(3,), ...) |
| 60 |
>>> type(keystone) |
| 61 |
<class 'keystoneclient.v3.client.Client'> |
| 62 |
|
| 63 |
Or by specifying directly the specific API version authentication URL |
| 64 |
as the auth_url keyword argument:: |
| 65 |
|
| 66 |
>>> from keystoneclient import client |
| 67 |
>>> keystone = |
| 68 |
... client.Client(auth_url='http://localhost:5000/v2.0', ...) |
| 69 |
>>> type(keystone) |
| 70 |
<class 'keystoneclient.v2_0.client.Client'> |
| 71 |
>>> keystone = |
| 72 |
... client.Client(auth_url='http://localhost:5000/v3', ...) |
| 73 |
>>> type(keystone) |
| 74 |
<class 'keystoneclient.v3.client.Client'> |
| 75 |
|
| 76 |
Upon successful authentication, a :py:class:`keystoneclient.v3.client.Client` |
| 77 |
object is returned (when using the Identity v3 API). Authentication and |
| 78 |
examples of common tasks are provided below. |
| 79 |
|
| 80 |
You can generally expect that when the client needs to propagate an |
| 81 |
exception it will raise an instance of subclass of |
| 82 |
:class:`keystoneclient.exceptions.ClientException`. |
| 83 |
|
| 84 |
Authenticating Using Sessions |
| 85 |
============================= |
| 86 |
|
| 87 |
Instantiate a :py:class:`keystoneclient.v3.client.Client` using a |
| 88 |
:py:class:`~keystoneauth1.session.Session` to provide the authentication |
| 89 |
plugin, SSL/TLS certificates, and other data:: |
| 90 |
|
| 91 |
>>> from keystoneauth1.identity import v3 |
| 92 |
>>> from keystoneauth1 import session |
| 93 |
>>> from keystoneclient.v3 import client |
| 94 |
>>> auth = v3.Password(auth_url='https://my.keystone.com:5000/v3', |
| 95 |
... user_id='myuserid', |
| 96 |
... password='mypassword', |
| 97 |
... project_id='myprojectid') |
| 98 |
>>> sess = session.Session(auth=auth) |
| 99 |
>>> keystone = client.Client(session=sess) |
| 100 |
|
| 101 |
For more information on Sessions refer to: `Using Sessions`_. |
| 102 |
|
| 103 |
.. _`Using Sessions`: using-sessions.html |
| 104 |
|
| 105 |
Getting Metadata Responses |
| 106 |
========================== |
| 107 |
|
| 108 |
Instantiating :py:class:`keystoneclient.v3.client.Client` using |
| 109 |
`include_metadata=True` will cause manager response to return |
| 110 |
:py:class:`keystoneclient.base.Response` instead of just the data. |
| 111 |
The metadata property will be available directly to the |
| 112 |
:py:class:`keystoneclient.base.Response` and the response data will |
| 113 |
be available as property `data` to it. |
| 114 |
|
| 115 |
>>> from keystoneauth1.identity import v3 |
| 116 |
>>> from keystoneauth1 import session |
| 117 |
>>> from keystoneclient.v3 import client |
| 118 |
>>> auth = v3.Password(auth_url='https://my.keystone.com:5000/v3', |
| 119 |
... user_id='myuserid', |
| 120 |
... password='mypassword', |
| 121 |
... project_id='myprojectid') |
| 122 |
>>> sess = session.Session(auth=auth) |
| 123 |
>>> keystone = client.Client(session=sess, include_metadata=True) |
| 124 |
>>> resp = keystone.projects.list() |
| 125 |
>>> resp.request_ids[0] |
| 126 |
req-1234-5678-... |
| 127 |
>>> resp.data |
| 128 |
[<Project ...>, <Project ...>, ...] |
| 129 |
|
| 130 |
Non-Session Authentication (deprecated) |
| 131 |
======================================= |
| 132 |
|
| 133 |
The *deprecated* way to authenticate is to pass the username, the user's domain |
| 134 |
name (which will default to 'Default' if it is not specified), and a |
| 135 |
password:: |
| 136 |
|
| 137 |
>>> from keystoneclient import client |
| 138 |
>>> auth_url = 'http://localhost:5000' |
| 139 |
>>> username = 'adminUser' |
| 140 |
>>> user_domain_name = 'Default' |
| 141 |
>>> password = 'secreetword' |
| 142 |
>>> keystone = client.Client(auth_url=auth_url, version=(3,), |
| 143 |
... username=username, password=password, |
| 144 |
... user_domain_name=user_domain_name) |
| 145 |
|
| 146 |
A :py:class:`~keystoneauth1.session.Session` should be passed to the Client |
| 147 |
instead. Using a Session you're not limited to authentication using a username |
| 148 |
and password but can take advantage of other more secure authentication |
| 149 |
methods. |
| 150 |
|
| 151 |
You may optionally specify a domain or project (along with its project |
| 152 |
domain name), to obtain a scoped token:: |
| 153 |
|
| 154 |
>>> from keystoneclient import client |
| 155 |
>>> auth_url = 'http://localhost:5000' |
| 156 |
>>> username = 'adminUser' |
| 157 |
>>> user_domain_name = 'Default' |
| 158 |
>>> project_name = 'demo' |
| 159 |
>>> project_domain_name = 'Default' |
| 160 |
>>> password = 'secreetword' |
| 161 |
>>> keystone = client.Client(auth_url=auth_url, version=(3,), |
| 162 |
... username=username, password=password, |
| 163 |
... user_domain_name=user_domain_name, |
| 164 |
... project_name=project_name, |
| 165 |
... project_domain_name=project_domain_name) |