Debian Codegraph @

doc/source/using-api-v2.rst

raw
1 =======================
2 Using the V2 client API
3 =======================
4
5 Introduction
6 ============
7
8 The main concepts in the Identity v2 API are:
9
10 * tenants
11 * users
12 * roles
13 * services
14 * endpoints
15
16 The V2 client API lets you query and make changes through
17 managers. For example, to manipulate tenants, you interact with a
18 ``keystoneclient.v2_0.tenants.TenantManager`` object.
19
20 You obtain access to managers via attributes of the
21 ``keystoneclient.v2_0.client.Client`` object. For example, the ``tenants``
22 attribute of the ``Client`` class is a tenant manager::
23
24 >>> from keystoneclient.v2_0 import client
25 >>> keystone = client.Client(...)
26 >>> keystone.tenants.list() # List tenants
27
28 You create a valid ``keystoneclient.v2_0.client.Client`` object by passing
29 a :class:`~keystoneauth1.session.Session` to the constructor. Authentication
30 and examples of common tasks are provided below.
31
32 You can generally expect that when the client needs to propagate an exception
33 it will raise an instance of subclass of
34 ``keystoneclient.exceptions.ClientException``
35
36 Authenticating
37 ==============
38
39 There are two ways to authenticate against keystone:
40 * against the admin endpoint with the admin token
41 * against the public endpoint with a username and password
42
43 If you are an administrator, you can authenticate by connecting to the admin
44 endpoint and using the admin token (sometimes referred to as the service
45 token). The token is specified as the ``admin_token`` configuration option in
46 your keystone.conf config file, which is typically in /etc/keystone::
47
48 >>> from keystoneauth1.identity import v2
49 >>> from keystoneauth1 import session
50 >>> from keystoneclient.v2_0 import client
51 >>> token = '012345SECRET99TOKEN012345'
52 >>> endpoint = 'http://192.168.206.130:35357/v2.0'
53 >>> auth = v2.Token(auth_url=endpoint, token=token)
54 >>> sess = session.Session(auth=auth)
55 >>> keystone = client.Client(session=sess)
56
57 If you have a username and password, authentication is done against the
58 public endpoint. You must also specify a tenant that is associated with the
59 user::
60
61 >>> from keystoneauth1.identity import v2
62 >>> from keystoneauth1 import session
63 >>> from keystoneclient.v2_0 import client
64 >>> username='adminUser'
65 >>> password='secretword'
66 >>> tenant_name='openstackDemo'
67 >>> auth_url='http://192.168.206.130:5000/v2.0'
68 >>> auth = v2.Password(username=username, password=password,
69 ... tenant_name=tenant_name, auth_url=auth_url)
70 >>> sess = session.Session(auth=auth)
71 >>> keystone = client.Client(session=sess)
72
73 Creating tenants
74 ================
75
76 This example will create a tenant named *openstackDemo*::
77
78 >>> from keystoneclient.v2_0 import client
79 >>> keystone = client.Client(...)
80 >>> keystone.tenants.create(tenant_name="openstackDemo",
81 ... description="Default Tenant", enabled=True)
82 <Tenant {'id': '9b7962da6eb04745b477ae920ad55939', 'enabled': True, 'description': 'Default Tenant', 'name': 'openstackDemo'}>
83
84 Creating users
85 ==============
86
87 This example will create a user named *adminUser* with a password *secretword*
88 in the openstackDemo tenant. We first need to retrieve the tenant::
89
90 >>> from keystoneclient.v2_0 import client
91 >>> keystone = client.Client(...)
92 >>> tenants = keystone.tenants.list()
93 >>> my_tenant = [x for x in tenants if x.name=='openstackDemo'][0]
94 >>> my_user = keystone.users.create(name="adminUser",
95 ... password="secretword",
96 ... tenant_id=my_tenant.id)
97
98 Creating roles and adding users
99 ===============================
100
101 This example will create an admin role and add the *my_user* user to that
102 role, but only for the *my_tenant* tenant:
103
104 >>> from keystoneclient.v2_0 import client
105 >>> keystone = client.Client(...)
106 >>> role = keystone.roles.create('admin')
107 >>> my_tenant = ...
108 >>> my_user = ...
109 >>> keystone.roles.add_user_role(my_user, role, my_tenant)
110
111 Creating services and endpoints
112 ===============================
113
114 This example will create the service and corresponding endpoint for the
115 Compute service::
116
117 >>> from keystoneclient.v2_0 import client
118 >>> keystone = client.Client(...)
119 >>> service = keystone.services.create(name="nova", service_type="compute",
120 ... description="Nova Compute Service")
121 >>> keystone.endpoints.create(
122 ... region="RegionOne", service_id=service.id,
123 ... publicurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
124 ... adminurl="http://192.168.206.130:8774/v2/%(tenant_id)s",
125 ... internalurl="http://192.168.206.130:8774/v2/%(tenant_id)s")