1 Creating applications with kamaki API
2 =====================================
4 Kamaki features a clients API for building third-party client applications that
5 communicate with OpenStack and / or Synnefo cloud services. The package is
6 called kamaki.clients and servers as a lib.
8 A showcase of an application built on kamaki.clients is kamaki.cli, the command
9 line interface of kamaki.
11 Since synnefo services are build as OpenStack extensions, an inheritance
12 approach has been chosen for implementing clients for both. In specific,
13 the *compute*, *storage* and *image* modules are clients of the OS compute, OS
14 object-store, respectively. On the contrary, all the other modules are Synnefo
15 extensions (*cyclades* extents *compute*, *pithos* and *pithos_rest_api*
16 extent *storage*) or novel synnefo services (e.g. *astakos* for IM, *image*
19 Setup a client instance
20 -----------------------
22 External applications may instantiate one or more kamaki clients.
24 .. code-block:: python
27 Example 1.1: Instantiate Cyclades and Pithos client
30 from kamaki.clients.cyclades import CycladesClient
31 from kamaki.clients.pithos import PithosClient
33 my_cyclades_client = CycladesClient(base_url, token)
34 my_pithos_client = PithosClient(base_url, token, account, container)
36 .. note:: *cyclades* and *pithos* clients inherit all methods of *compute*
37 and *storage* clients respectively. Separate compute or storage objects
38 should be used only when implementing applications for strict OS Compute or
41 Using endpoints to get the base_url
42 -----------------------------------
44 In OpenStack, each service (e.g. `compute`, `object-store`, etc.) has a number
45 of `endpoints`. These `endpoints` are actually URIs that are needed as prefixes
46 for the API calls the kamaki client generates. In this context, we need just
47 one of these these `endpoints`, the `publicURL`, which is also referred to as
48 `base_url` in kamaki client libraries.
50 In general, this is the suggested way of getting the base_url::
52 1. From the deployment UI get the AUTHENTICATION_URL and TOKEN
54 2. Use them to instantiate an AstakosClient
56 3. Use AstakosClient instance to get the endpoints of the service of interest
58 4. The 'publicURL' endpoint is the base_url we are looking for
61 The AstakosClient is a client for the Synnefo/Astakos server. Synnefo/Astakos
62 is an advanced identity server based on OpenStack identity specifications.
63 Therefore, it can be used to get the `base_url` values needed for initializing
64 kamaki clients. Kamaki simplifies this process with the astakos client library.
66 Let's review the process with examples.
68 First, an astakos client must be initialized (Example 1.2). An
69 AUTHENTICATION_URL and a TOKEN can be acquired from the synnefo deployment UI.
71 .. code-block:: python
74 Example 1.2: Initialize an astakos client
76 from kamaki.clients.astakos import AstakosClient
77 my_astakos_client = AstakosClient(AUTHENTICATION_URL, TOKEN)
80 Next, the astakos client can be used to retrieve the base_url values for the
81 servers of interest. In this case (Example 1.3) they are *cyclades*
82 and *pithos*. A number of endpoints is assigned to each service, but kamaki
83 clients only need the one labeled as ``publicURL``.
85 .. code-block:: python
88 Example 1.3: Retrieve cyclades and pithos base_url values
90 cyclades_endpoints = my_astakos_client.get_service_endpoints('compute')
91 cyclades_base_url = cyclades_endpoints['publicURL']
93 pithos_endpoints = my_astakos_client.get_service_endpoints('object-store')
94 pithos_base_url = pithos_endpoints['publicURL']
96 The ``get_service_endpoints`` method gets the service name as an argument. Here
97 are the service names for the most popular kamaki clients::
99 storage, pithos --> object-store
100 compute, cyclades --> compute
107 Client methods can now be called. Developers are advised to
108 consult :ref:`the-client-api-ref` for details on the available methods and how
111 In the following example, the *cyclades* and *pithos* clients of example 1.1
112 are used to extract some information, that is then printed to the standard
116 .. code-block:: python
117 :emphasize-lines: 1,2
119 Example 1.4: Print server name and OS for server with server_id
120 Print objects in container mycont
123 srv = my_cyclades_client.get_server_info(server_id)
124 print("Server Name: %s (with OS %s" % (srv['name'], srv['os']))
126 obj_list = my_pithos_client.list_objects(mycont)
128 print(' %s of %s bytes' % (obj['name'], obj['bytes']))
130 .. code-block:: console
133 Run of examples 1.1 + 1.4
136 $ python test_script.py
137 Server Name: A Debian Server (with OS Debian Base)
139 test.txt of 1232 bytes
146 The kamaki.clients standard error is ClientError. A ClientError is raised for
147 any kind of kamaki.clients errors (errors reported by servers, type errors in
150 A ClientError contains::
152 message The error message.
153 status An optional error code, e.g. after a server error.
154 details Optional list of messages with error details.
156 The following example concatenates examples 1.1 to 1.4 plus error handling
158 .. code-block:: python
160 Example 1.5: Error handling
162 from kamaki.clients.astakos import AstakosClient
163 from kamaki.clients.cyclades import CycladesClient
164 from kamaki.clients.pithos import PithosClient
167 my_astakos_client = AstakosClient(AUTHENTICATION_URL, TOKEN)
169 print('Failed to authenticate user token')
173 cyclades_endpoints = my_astakos_client.get_service_endpoints('compute')
174 cyclades_base_url = cyclades_endpoints['publicURL']
176 print('Failed to get endpoints for cyclades')
179 my_cyclades_client = CycladesClient(cyclades_base_url, token)
181 print('Failed to initialize Cyclades client')
184 pithos_endpoints = my_astakos_client.get_service_endpoints('object-store')
185 pithos_base_url = pithos_endpoints['publicURL']
187 print('Failed to get endpoints for pithos')
190 my_pithos_client = PithosClient(pithos_base_url, token, account, container)
192 print('Failed to initialize Pithos+ client')
195 srv = my_cyclades_client.get_server_info(server_id)
196 print("Server Name: %s (with OS %s" % (srv['name'], srv['os']))
198 obj_list = my_pithos_client.list_objects(mycont)
200 print(' %s of %s bytes' % (obj['name'], obj['bytes']))
201 except ClientError as e:
202 print('Error: %s' % e)
204 print('- error code: %s' % e.status)
206 for detail in e.details:
207 print('- %s' % detail)
216 .. code-block:: python
220 from kamaki.clients.astakos import AstakosClient
221 from kamaki.clients.cyclades import CycladesClient
223 AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0'
224 TOKEN = 'replace this with your token'
226 user = AstakosClient(AUTHENTICATION_URL, TOKEN)
228 cyclades_endpoints = user.get_endpoints('compute')
229 CYCLADES_URL = cyclades_endpoints['publicURL']
230 cyclades = CycladesClient(CYCLADES_URL, TOKEN)
232 # (name, flavor-id, image-id)
234 ('My Debian Server', 1, 'my-debian-base-image-id'),
235 ('My Windows Server', 3, 'my-windows-8-image-id'),
236 ('My Ubuntu Server', 3, 'my-ubuntu-12-image-id'),
239 for name, flavor_id, image_id in servers:
240 cyclades.create_server(name, flavor_id, image_id)
243 Batch-create 4 servers of the same kind
244 '''''''''''''''''''''''''''''''''''''''
246 .. code-block:: python
250 from kamaki.clients.astakos import AstakosClient
251 from kamaki.clients.cyclades import CycladesClient
253 AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0'
254 TOKEN = 'replace this with your token'
256 user = AstakosClient(AUTHENTICATION_URL, TOKEN)
258 cyclades_endpoints = user.get_endpoints('compute')
259 CYCLADES_URL = cyclades_endpoints['publicURL']
260 cyclades = CycladesClient(CYCLADES_URL, TOKEN)
263 name, flavor_id, image_id = 'Server %s' % (i + 1), 3, 'some-image-id'
264 cyclades.create_server(name, flavor_id, image_id)