2014-10-10 19:30:37 -05:00
|
|
|
=======================
|
2015-05-27 16:44:26 +00:00
|
|
|
Using the V2 client API
|
2014-10-10 19:30:37 -05:00
|
|
|
=======================
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
Introduction
|
|
|
|
============
|
2012-10-24 07:12:30 -05:00
|
|
|
|
2013-12-24 15:57:20 +00:00
|
|
|
The main concepts in the Identity v2 API are:
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
* tenants
|
|
|
|
* users
|
|
|
|
* roles
|
|
|
|
* services
|
|
|
|
* endpoints
|
|
|
|
|
2014-10-10 19:30:37 -05:00
|
|
|
The V2 client API lets you query and make changes through
|
2013-12-24 15:57:20 +00:00
|
|
|
managers. For example, to manipulate tenants, you interact with a
|
2012-10-24 07:12:30 -05:00
|
|
|
``keystoneclient.v2_0.tenants.TenantManager`` object.
|
2012-05-04 09:33:29 -04:00
|
|
|
|
2014-06-26 20:48:53 -04:00
|
|
|
You obtain access to managers via attributes of the
|
2012-09-28 15:57:25 +00:00
|
|
|
``keystoneclient.v2_0.client.Client`` object. For example, the ``tenants``
|
|
|
|
attribute of the ``Client`` class is a tenant manager::
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
>>> from keystoneclient.v2_0 import client
|
|
|
|
>>> keystone = client.Client(...)
|
|
|
|
>>> keystone.tenants.list() # List tenants
|
|
|
|
|
|
|
|
You create a valid ``keystoneclient.v2_0.client.Client`` object by passing
|
2016-02-28 10:44:44 -06:00
|
|
|
a :class:`~keystoneauth1.session.Session` to the constructor. Authentication
|
|
|
|
and examples of common tasks are provided below.
|
2012-05-04 09:33:29 -04:00
|
|
|
|
2014-01-23 16:30:19 +08:00
|
|
|
You can generally expect that when the client needs to propagate an exception
|
2013-04-03 09:44:20 -07:00
|
|
|
it will raise an instance of subclass of
|
|
|
|
``keystoneclient.exceptions.ClientException``
|
|
|
|
|
2012-05-04 09:33:29 -04:00
|
|
|
Authenticating
|
|
|
|
==============
|
|
|
|
|
2015-05-27 16:44:26 +00:00
|
|
|
There are two ways to authenticate against keystone:
|
2012-05-04 09:33:29 -04:00
|
|
|
* against the admin endpoint with the admin token
|
|
|
|
* against the public endpoint with a username and password
|
|
|
|
|
|
|
|
If you are an administrator, you can authenticate by connecting to the admin
|
|
|
|
endpoint and using the admin token (sometimes referred to as the service
|
2012-09-28 15:57:25 +00:00
|
|
|
token). The token is specified as the ``admin_token`` configuration option in
|
|
|
|
your keystone.conf config file, which is typically in /etc/keystone::
|
2012-05-04 09:33:29 -04:00
|
|
|
|
2016-02-28 10:44:44 -06:00
|
|
|
>>> from keystoneauth1.identity import v2
|
|
|
|
>>> from keystoneauth1 import session
|
2012-05-04 09:33:29 -04:00
|
|
|
>>> from keystoneclient.v2_0 import client
|
|
|
|
>>> token = '012345SECRET99TOKEN012345'
|
|
|
|
>>> endpoint = 'http://192.168.206.130:35357/v2.0'
|
2016-02-28 10:44:44 -06:00
|
|
|
>>> auth = v2.Token(auth_url=endpoint, token=token)
|
|
|
|
>>> sess = session.Session(auth=auth)
|
|
|
|
>>> keystone = client.Client(session=sess)
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
If you have a username and password, authentication is done against the
|
|
|
|
public endpoint. You must also specify a tenant that is associated with the
|
|
|
|
user::
|
|
|
|
|
2016-02-28 10:44:44 -06:00
|
|
|
>>> from keystoneauth1.identity import v2
|
|
|
|
>>> from keystoneauth1 import session
|
2012-05-04 09:33:29 -04:00
|
|
|
>>> from keystoneclient.v2_0 import client
|
|
|
|
>>> username='adminUser'
|
2015-05-27 16:44:26 +00:00
|
|
|
>>> password='secretword'
|
2012-05-04 09:33:29 -04:00
|
|
|
>>> tenant_name='openstackDemo'
|
|
|
|
>>> auth_url='http://192.168.206.130:5000/v2.0'
|
2016-02-28 10:44:44 -06:00
|
|
|
>>> auth = v2.Password(username=username, password=password,
|
|
|
|
... tenant_name=tenant_name, auth_url=auth_url)
|
|
|
|
>>> sess = session.Session(auth=auth)
|
|
|
|
>>> keystone = client.Client(session=sess)
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
Creating tenants
|
|
|
|
================
|
|
|
|
|
2015-05-27 16:44:26 +00:00
|
|
|
This example will create a tenant named *openstackDemo*::
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
>>> from keystoneclient.v2_0 import client
|
|
|
|
>>> keystone = client.Client(...)
|
|
|
|
>>> keystone.tenants.create(tenant_name="openstackDemo",
|
|
|
|
... description="Default Tenant", enabled=True)
|
2021-01-04 17:16:58 +08:00
|
|
|
<Tenant {'id': '9b7962da6eb04745b477ae920ad55939', 'enabled': True, 'description': 'Default Tenant', 'name': 'openstackDemo'}>
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
Creating users
|
|
|
|
==============
|
|
|
|
|
|
|
|
This example will create a user named *adminUser* with a password *secretword*
|
2015-05-27 16:44:26 +00:00
|
|
|
in the openstackDemo tenant. We first need to retrieve the tenant::
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
>>> from keystoneclient.v2_0 import client
|
|
|
|
>>> keystone = client.Client(...)
|
|
|
|
>>> tenants = keystone.tenants.list()
|
|
|
|
>>> my_tenant = [x for x in tenants if x.name=='openstackDemo'][0]
|
2012-09-28 15:57:25 +00:00
|
|
|
>>> my_user = keystone.users.create(name="adminUser",
|
|
|
|
... password="secretword",
|
|
|
|
... tenant_id=my_tenant.id)
|
2012-05-04 09:33:29 -04:00
|
|
|
|
|
|
|
Creating roles and adding users
|
|
|
|
===============================
|
|
|
|
|
|
|
|
This example will create an admin role and add the *my_user* user to that
|
|
|
|
role, but only for the *my_tenant* tenant:
|
|
|
|
|
|
|
|
>>> from keystoneclient.v2_0 import client
|
|
|
|
>>> keystone = client.Client(...)
|
|
|
|
>>> role = keystone.roles.create('admin')
|
|
|
|
>>> my_tenant = ...
|
|
|
|
>>> my_user = ...
|
|
|
|
>>> keystone.roles.add_user_role(my_user, role, my_tenant)
|
|
|
|
|
|
|
|
Creating services and endpoints
|
|
|
|
===============================
|
|
|
|
|
|
|
|
This example will create the service and corresponding endpoint for the
|
|
|
|
Compute service::
|
|
|
|
|
|
|
|
>>> from keystoneclient.v2_0 import client
|
|
|
|
>>> keystone = client.Client(...)
|
|
|
|
>>> service = keystone.services.create(name="nova", service_type="compute",
|
|
|
|
... description="Nova Compute Service")
|
2012-09-28 15:57:25 +00:00
|
|
|
>>> keystone.endpoints.create(
|
|
|
|
... region="RegionOne", service_id=service.id,
|
|
|
|
... publicurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
|
|
|
|
... adminurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
|
|
|
|
... internalurl="http://192.168.206.130:8774/v2/%(tenant_id)s")
|