Statistics
| Branch: | Tag: | Revision:

root / docs / developers / clients-api.rst @ cedde35d

History | View | Annotate | Download (8.7 kB)

1 9e4508df Stavros Sachtouris
Creating applications with kamaki API
2 9e4508df Stavros Sachtouris
=====================================
3 9e4508df Stavros Sachtouris
4 fa382f9e Stavros Sachtouris
Kamaki features a clients API for building third-party client applications that
5 fa382f9e Stavros Sachtouris
communicate with OpenStack and / or Synnefo cloud services. The package is
6 fa382f9e Stavros Sachtouris
called kamaki.clients and servers as a lib.
7 9e4508df Stavros Sachtouris
8 fa382f9e Stavros Sachtouris
A showcase of an application built on kamaki.clients is kamaki.cli, the command
9 fa382f9e Stavros Sachtouris
line interface of kamaki.
10 9e4508df Stavros Sachtouris
11 fa382f9e Stavros Sachtouris
Since synnefo services are build as OpenStack extensions, an inheritance
12 fa382f9e Stavros Sachtouris
approach has been chosen for implementing clients for both. In specific,
13 fa382f9e Stavros Sachtouris
the *compute*, *storage* and *image* modules are clients of the OS compute, OS
14 fa382f9e Stavros Sachtouris
object-store, respectively. On the contrary, all the other modules are Synnefo
15 fa382f9e Stavros Sachtouris
extensions (*cyclades* extents *compute*, *pithos* and *pithos_rest_api*
16 fa382f9e Stavros Sachtouris
extent *storage*) or novel synnefo services (e.g. *astakos* for IM, *image*
17 fa382f9e Stavros Sachtouris
for *plankton*).
18 9e4508df Stavros Sachtouris
19 9e4508df Stavros Sachtouris
Setup a client instance
20 9e4508df Stavros Sachtouris
-----------------------
21 9e4508df Stavros Sachtouris
22 9e4508df Stavros Sachtouris
External applications may instantiate one or more kamaki clients.
23 9e4508df Stavros Sachtouris
24 9e4508df Stavros Sachtouris
.. code-block:: python
25 9e4508df Stavros Sachtouris
    :emphasize-lines: 1
26 9e4508df Stavros Sachtouris
27 7ae842c2 Stavros Sachtouris
    Example 1.1: Instantiate Cyclades and Pithos client
28 9e4508df Stavros Sachtouris
29 9e4508df Stavros Sachtouris
30 9e4508df Stavros Sachtouris
    from kamaki.clients.cyclades import CycladesClient
31 9e4508df Stavros Sachtouris
    from kamaki.clients.pithos import PithosClient
32 9e4508df Stavros Sachtouris
33 9e4508df Stavros Sachtouris
    my_cyclades_client = CycladesClient(base_url, token)
34 9e4508df Stavros Sachtouris
    my_pithos_client = PithosClient(base_url, token, account, container)
35 9e4508df Stavros Sachtouris
36 fa382f9e Stavros Sachtouris
.. note:: *cyclades* and *pithos* clients inherit all methods of *compute*
37 fa382f9e Stavros Sachtouris
    and *storage* clients respectively. Separate compute or storage objects
38 fa382f9e Stavros Sachtouris
    should be used only when implementing applications for strict OS Compute or
39 fa382f9e Stavros Sachtouris
    OS Storage services.
40 9e4508df Stavros Sachtouris
41 cedde35d Stavros Sachtouris
Using endpoints to get the base_url
42 cedde35d Stavros Sachtouris
-----------------------------------
43 7ae842c2 Stavros Sachtouris
44 cedde35d Stavros Sachtouris
In OpenStack, each service (e.g. `compute`, `object-store`, etc.) has a number
45 cedde35d Stavros Sachtouris
of `endpoints`. These `endpoints` are actually URIs that are needed as prefixes
46 cedde35d Stavros Sachtouris
for the API calls the kamaki client generates. In this context, we need just
47 cedde35d Stavros Sachtouris
one of these these `endpoints`, the `publicURL`, which is also referred to as
48 cedde35d Stavros Sachtouris
`base_url` in kamaki client libraries.
49 cedde35d Stavros Sachtouris
50 cedde35d Stavros Sachtouris
In general, this is the suggested way of getting the base_url::
51 cedde35d Stavros Sachtouris
52 cedde35d Stavros Sachtouris
    1. From the deployment UI get the AUTHENTICATION_URL and TOKEN
53 cedde35d Stavros Sachtouris
        (Example 1.2)
54 cedde35d Stavros Sachtouris
    2. Use them to instantiate an AstakosClient
55 cedde35d Stavros Sachtouris
        (Example 1.2)
56 cedde35d Stavros Sachtouris
    3. Use AstakosClient instance to get the endpoints of the service of interest
57 cedde35d Stavros Sachtouris
        (Example 1.3)
58 cedde35d Stavros Sachtouris
    4. The 'publicURL' endpoint is the base_url we are looking for
59 cedde35d Stavros Sachtouris
        (Example 1.3)
60 cedde35d Stavros Sachtouris
61 cedde35d Stavros Sachtouris
The AstakosClient is a client for the Synnefo/Astakos server. Synnefo/Astakos
62 cedde35d Stavros Sachtouris
is an advanced identity server based on OpenStack identity specifications.
63 cedde35d Stavros Sachtouris
Therefore, it can be used to get the `base_url` values needed for initializing
64 cedde35d Stavros Sachtouris
kamaki clients. Kamaki simplifies this process with the astakos client library.
65 cedde35d Stavros Sachtouris
66 cedde35d Stavros Sachtouris
Let's review the process with examples.
67 7ae842c2 Stavros Sachtouris
68 6362f3e1 Stavros Sachtouris
First, an astakos client must be initialized (Example 1.2). An
69 6362f3e1 Stavros Sachtouris
AUTHENTICATION_URL and a TOKEN can be acquired from the synnefo deployment UI.
70 6362f3e1 Stavros Sachtouris
71 6362f3e1 Stavros Sachtouris
.. code-block:: python
72 6362f3e1 Stavros Sachtouris
    :emphasize-lines: 1
73 6362f3e1 Stavros Sachtouris
74 6362f3e1 Stavros Sachtouris
    Example 1.2: Initialize an astakos client
75 6362f3e1 Stavros Sachtouris
76 6362f3e1 Stavros Sachtouris
    from kamaki.clients.astakos import AstakosClient
77 6362f3e1 Stavros Sachtouris
    my_astakos_client = AstakosClient(AUTHENTICATION_URL, TOKEN)
78 6362f3e1 Stavros Sachtouris
        
79 6362f3e1 Stavros Sachtouris
80 6362f3e1 Stavros Sachtouris
Next, the astakos client can be used to retrieve the base_url values for the
81 cedde35d Stavros Sachtouris
servers of interest. In this case (Example 1.3) they are *cyclades*
82 6362f3e1 Stavros Sachtouris
and *pithos*. A number of endpoints is assigned to each service, but kamaki
83 6362f3e1 Stavros Sachtouris
clients only need the one labeled as ``publicURL``.
84 6362f3e1 Stavros Sachtouris
85 6362f3e1 Stavros Sachtouris
.. code-block:: python
86 6362f3e1 Stavros Sachtouris
    :emphasize-lines: 1
87 6362f3e1 Stavros Sachtouris
88 6362f3e1 Stavros Sachtouris
    Example 1.3: Retrieve cyclades and pithos base_url values
89 6362f3e1 Stavros Sachtouris
90 6362f3e1 Stavros Sachtouris
    cyclades_endpoints = my_astakos_client.get_service_endpoints('compute')
91 6362f3e1 Stavros Sachtouris
    cyclades_base_url = cyclades_endpoints['publicURL']
92 6362f3e1 Stavros Sachtouris
93 6362f3e1 Stavros Sachtouris
    pithos_endpoints = my_astakos_client.get_service_endpoints('object-store')
94 6362f3e1 Stavros Sachtouris
    pithos_base_url = pithos_endpoints['publicURL']
95 6362f3e1 Stavros Sachtouris
96 6362f3e1 Stavros Sachtouris
The ``get_service_endpoints`` method gets the service name as an argument. Here
97 6362f3e1 Stavros Sachtouris
are the service names for the most popular kamaki clients::
98 6362f3e1 Stavros Sachtouris
99 6362f3e1 Stavros Sachtouris
    storage, pithos     -->     object-store
100 6362f3e1 Stavros Sachtouris
    compute, cyclades   -->     compute
101 6362f3e1 Stavros Sachtouris
    image               -->     image
102 6362f3e1 Stavros Sachtouris
    astakos             -->     identity
103 7ae842c2 Stavros Sachtouris
104 9e4508df Stavros Sachtouris
Use client methods
105 9e4508df Stavros Sachtouris
------------------
106 9e4508df Stavros Sachtouris
107 fa382f9e Stavros Sachtouris
Client methods can now be called. Developers are advised to
108 fa382f9e Stavros Sachtouris
consult :ref:`the-client-api-ref` for details on the available methods and how
109 fa382f9e Stavros Sachtouris
to use them.
110 9e4508df Stavros Sachtouris
111 fa382f9e Stavros Sachtouris
In the following example, the *cyclades* and *pithos* clients of example 1.1
112 fa382f9e Stavros Sachtouris
are used to extract some information, that is then printed to the standard
113 fa382f9e Stavros Sachtouris
output.
114 9e4508df Stavros Sachtouris
115 9e4508df Stavros Sachtouris
116 9e4508df Stavros Sachtouris
.. code-block:: python
117 9e4508df Stavros Sachtouris
    :emphasize-lines: 1,2
118 9e4508df Stavros Sachtouris
119 6362f3e1 Stavros Sachtouris
    Example 1.4: Print server name and OS for server with server_id
120 9e4508df Stavros Sachtouris
                Print objects in container mycont
121 9e4508df Stavros Sachtouris
122 9e4508df Stavros Sachtouris
123 9e4508df Stavros Sachtouris
    srv = my_cyclades_client.get_server_info(server_id)
124 9e4508df Stavros Sachtouris
    print("Server Name: %s (with OS %s" % (srv['name'], srv['os']))
125 9e4508df Stavros Sachtouris
126 9e4508df Stavros Sachtouris
    obj_list = my_pithos_client.list_objects(mycont)
127 9e4508df Stavros Sachtouris
    for obj in obj_list:
128 9e4508df Stavros Sachtouris
        print('  %s of %s bytes' % (obj['name'], obj['bytes']))
129 9e4508df Stavros Sachtouris
130 9e4508df Stavros Sachtouris
.. code-block:: console
131 9e4508df Stavros Sachtouris
    :emphasize-lines: 1
132 9e4508df Stavros Sachtouris
133 6362f3e1 Stavros Sachtouris
    Run of examples 1.1 + 1.4
134 9e4508df Stavros Sachtouris
135 9e4508df Stavros Sachtouris
136 9e4508df Stavros Sachtouris
    $ python test_script.py
137 9e4508df Stavros Sachtouris
    Server Name: A Debian Server (with OS Debian Base)
138 9e4508df Stavros Sachtouris
      lala.txt of 34 bytes
139 9e4508df Stavros Sachtouris
      test.txt of 1232 bytes
140 9e4508df Stavros Sachtouris
      testDir/ of 0 bytes
141 9e4508df Stavros Sachtouris
    $ 
142 9e4508df Stavros Sachtouris
143 9e4508df Stavros Sachtouris
Error handling
144 9e4508df Stavros Sachtouris
--------------
145 9e4508df Stavros Sachtouris
146 fa382f9e Stavros Sachtouris
The kamaki.clients standard error is ClientError. A ClientError is raised for
147 fa382f9e Stavros Sachtouris
any kind of kamaki.clients errors (errors reported by servers, type errors in
148 fa382f9e Stavros Sachtouris
arguments, etc.).
149 9e4508df Stavros Sachtouris
150 9e4508df Stavros Sachtouris
A ClientError contains::
151 9e4508df Stavros Sachtouris
152 9e4508df Stavros Sachtouris
    message     The error message.
153 9e4508df Stavros Sachtouris
    status      An optional error code, e.g. after a server error.
154 9e4508df Stavros Sachtouris
    details     Optional list of messages with error details.
155 9e4508df Stavros Sachtouris
156 6362f3e1 Stavros Sachtouris
The following example concatenates examples 1.1 to 1.4 plus error handling
157 9e4508df Stavros Sachtouris
158 9e4508df Stavros Sachtouris
.. code-block:: python
159 9e4508df Stavros Sachtouris
160 6362f3e1 Stavros Sachtouris
    Example 1.5: Error handling
161 9e4508df Stavros Sachtouris
162 6362f3e1 Stavros Sachtouris
    from kamaki.clients.astakos import AstakosClient
163 9e4508df Stavros Sachtouris
    from kamaki.clients.cyclades import CycladesClient
164 9e4508df Stavros Sachtouris
    from kamaki.clients.pithos import PithosClient
165 9e4508df Stavros Sachtouris
166 9e4508df Stavros Sachtouris
    try:
167 6362f3e1 Stavros Sachtouris
        my_astakos_client = AstakosClient(AUTHENTICATION_URL, TOKEN)
168 6362f3e1 Stavros Sachtouris
    except ClientError:
169 6362f3e1 Stavros Sachtouris
        print('Failed to authenticate user token')
170 6362f3e1 Stavros Sachtouris
        return 1
171 6362f3e1 Stavros Sachtouris
172 6362f3e1 Stavros Sachtouris
    try:
173 6362f3e1 Stavros Sachtouris
        cyclades_endpoints = my_astakos_client.get_service_endpoints('compute')
174 6362f3e1 Stavros Sachtouris
        cyclades_base_url = cyclades_endpoints['publicURL']
175 6362f3e1 Stavros Sachtouris
    except ClientError:
176 6362f3e1 Stavros Sachtouris
        print('Failed to get endpoints for cyclades')
177 6362f3e1 Stavros Sachtouris
178 6362f3e1 Stavros Sachtouris
    try:
179 6362f3e1 Stavros Sachtouris
        my_cyclades_client = CycladesClient(cyclades_base_url, token)
180 9e4508df Stavros Sachtouris
    except ClientError:
181 9e4508df Stavros Sachtouris
        print('Failed to initialize Cyclades client')
182 9e4508df Stavros Sachtouris
183 9e4508df Stavros Sachtouris
    try:
184 6362f3e1 Stavros Sachtouris
        pithos_endpoints = my_astakos_client.get_service_endpoints('object-store')
185 6362f3e1 Stavros Sachtouris
        pithos_base_url = pithos_endpoints['publicURL']
186 6362f3e1 Stavros Sachtouris
    except ClientError:
187 6362f3e1 Stavros Sachtouris
        print('Failed to get endpoints for pithos')
188 6362f3e1 Stavros Sachtouris
189 6362f3e1 Stavros Sachtouris
    try:
190 6362f3e1 Stavros Sachtouris
        my_pithos_client = PithosClient(pithos_base_url, token, account, container)
191 9e4508df Stavros Sachtouris
    except ClientError:
192 9e4508df Stavros Sachtouris
        print('Failed to initialize Pithos+ client')
193 9e4508df Stavros Sachtouris
194 9e4508df Stavros Sachtouris
    try:
195 9e4508df Stavros Sachtouris
        srv = my_cyclades_client.get_server_info(server_id)
196 9e4508df Stavros Sachtouris
        print("Server Name: %s (with OS %s" % (srv['name'], srv['os']))
197 9e4508df Stavros Sachtouris
198 9e4508df Stavros Sachtouris
        obj_list = my_pithos_client.list_objects(mycont)
199 9e4508df Stavros Sachtouris
        for obj in obj_list:
200 9e4508df Stavros Sachtouris
            print('  %s of %s bytes' % (obj['name'], obj['bytes']))
201 9e4508df Stavros Sachtouris
    except ClientError as e:
202 9e4508df Stavros Sachtouris
        print('Error: %s' % e)
203 9e4508df Stavros Sachtouris
        if e.status:
204 9e4508df Stavros Sachtouris
            print('- error code: %s' % e.status)
205 9e4508df Stavros Sachtouris
        if e.details:
206 9e4508df Stavros Sachtouris
            for detail in e.details:
207 9e4508df Stavros Sachtouris
                print('- %s' % detail)
208 6362f3e1 Stavros Sachtouris
209 6362f3e1 Stavros Sachtouris
210 6362f3e1 Stavros Sachtouris
Scripts
211 6362f3e1 Stavros Sachtouris
-------
212 6362f3e1 Stavros Sachtouris
213 6362f3e1 Stavros Sachtouris
Batch-create servers
214 6362f3e1 Stavros Sachtouris
''''''''''''''''''''
215 6362f3e1 Stavros Sachtouris
216 6362f3e1 Stavros Sachtouris
.. code-block:: python
217 6362f3e1 Stavros Sachtouris
218 6362f3e1 Stavros Sachtouris
    #/usr/bin/python
219 6362f3e1 Stavros Sachtouris
220 6362f3e1 Stavros Sachtouris
    from kamaki.clients.astakos import AstakosClient
221 6362f3e1 Stavros Sachtouris
    from kamaki.clients.cyclades import CycladesClient
222 6362f3e1 Stavros Sachtouris
223 6362f3e1 Stavros Sachtouris
    AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0'
224 6362f3e1 Stavros Sachtouris
    TOKEN = 'replace this with your token'
225 6362f3e1 Stavros Sachtouris
226 6362f3e1 Stavros Sachtouris
    user = AstakosClient(AUTHENTICATION_URL, TOKEN)
227 6362f3e1 Stavros Sachtouris
228 6362f3e1 Stavros Sachtouris
    cyclades_endpoints = user.get_endpoints('compute')
229 6362f3e1 Stavros Sachtouris
    CYCLADES_URL = cyclades_endpoints['publicURL']
230 6362f3e1 Stavros Sachtouris
    cyclades = CycladesClient(CYCLADES_URL, TOKEN)
231 6362f3e1 Stavros Sachtouris
232 6362f3e1 Stavros Sachtouris
    #  (name, flavor-id, image-id)
233 6362f3e1 Stavros Sachtouris
    servers = [
234 6362f3e1 Stavros Sachtouris
        ('My Debian Server', 1, 'my-debian-base-image-id'),
235 6362f3e1 Stavros Sachtouris
        ('My Windows Server', 3, 'my-windows-8-image-id'),
236 6362f3e1 Stavros Sachtouris
        ('My Ubuntu Server', 3, 'my-ubuntu-12-image-id'),
237 6362f3e1 Stavros Sachtouris
    ]
238 6362f3e1 Stavros Sachtouris
239 6362f3e1 Stavros Sachtouris
    for name, flavor_id, image_id in servers:
240 6362f3e1 Stavros Sachtouris
        cyclades.create_server(name, flavor_id, image_id)
241 6362f3e1 Stavros Sachtouris
242 6362f3e1 Stavros Sachtouris
243 6362f3e1 Stavros Sachtouris
Batch-create 4 servers of the same kind
244 6362f3e1 Stavros Sachtouris
'''''''''''''''''''''''''''''''''''''''
245 6362f3e1 Stavros Sachtouris
246 6362f3e1 Stavros Sachtouris
.. code-block:: python
247 6362f3e1 Stavros Sachtouris
248 6362f3e1 Stavros Sachtouris
    #/usr/bin/python
249 6362f3e1 Stavros Sachtouris
250 6362f3e1 Stavros Sachtouris
    from kamaki.clients.astakos import AstakosClient
251 6362f3e1 Stavros Sachtouris
    from kamaki.clients.cyclades import CycladesClient
252 6362f3e1 Stavros Sachtouris
253 6362f3e1 Stavros Sachtouris
    AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0'
254 6362f3e1 Stavros Sachtouris
    TOKEN = 'replace this with your token'
255 6362f3e1 Stavros Sachtouris
256 6362f3e1 Stavros Sachtouris
    user = AstakosClient(AUTHENTICATION_URL, TOKEN)
257 6362f3e1 Stavros Sachtouris
258 6362f3e1 Stavros Sachtouris
    cyclades_endpoints = user.get_endpoints('compute')
259 6362f3e1 Stavros Sachtouris
    CYCLADES_URL = cyclades_endpoints['publicURL']
260 6362f3e1 Stavros Sachtouris
    cyclades = CycladesClient(CYCLADES_URL, TOKEN)
261 6362f3e1 Stavros Sachtouris
262 6362f3e1 Stavros Sachtouris
    for i in range(4):
263 6362f3e1 Stavros Sachtouris
        name, flavor_id, image_id = 'Server %s' % (i + 1), 3, 'some-image-id'
264 6362f3e1 Stavros Sachtouris
        cyclades.create_server(name, flavor_id, image_id)