root / docs / developers / clients-api.rst @ 736072e2
History | View | Annotate | Download (11.9 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 | 16d7b9ff | Stavros Sachtouris | called *kamaki.clients* and serves as a lib. |
7 | 9e4508df | Stavros Sachtouris | |
8 | 16d7b9ff | Stavros Sachtouris | A showcase of an application built on *kamaki.clients* is *kamaki.cli*, the |
9 | 16d7b9ff | Stavros Sachtouris | command line interface of kamaki. |
10 | 9e4508df | Stavros Sachtouris | |
11 | abfaa6d9 | Vangelis Koukis | 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 | 16d7b9ff | Stavros Sachtouris | the *compute*, *storage* and *image* modules are client implementations for the |
14 | 1c366ac9 | Stavros Sachtouris | OpenStack compute, OpenStack object-store and Image APIs respectively. The rest |
15 | 1c366ac9 | Stavros Sachtouris | of the modules implement the Synnefo extensions (i.e., *cyclades* and |
16 | 16d7b9ff | Stavros Sachtouris | *cyclades_rest_api* extents *compute*, *pithos* and *pithos_rest_api* extent |
17 | 1c366ac9 | Stavros Sachtouris | *storage*). |
18 | 9e4508df | Stavros Sachtouris | |
19 | 9e4508df | Stavros Sachtouris | Setup a client instance |
20 | 9e4508df | Stavros Sachtouris | ----------------------- |
21 | 9e4508df | Stavros Sachtouris | |
22 | 16d7b9ff | Stavros Sachtouris | There is a client for every API, therefore an external applications should |
23 | 16d7b9ff | Stavros Sachtouris | instantiate they kamaki clients they need. For example, to manage virtual |
24 | 16d7b9ff | Stavros Sachtouris | servers and stored objects / files, an application would probably need to |
25 | 16d7b9ff | Stavros Sachtouris | instantiate the CycladesClient and PithosClient respectively. |
26 | 9e4508df | Stavros Sachtouris | |
27 | 9e4508df | Stavros Sachtouris | .. code-block:: python |
28 | 9e4508df | Stavros Sachtouris | :emphasize-lines: 1 |
29 | 9e4508df | Stavros Sachtouris | |
30 | 16d7b9ff | Stavros Sachtouris | Example 1.1: Instantiate Cyclades and Pithos clients |
31 | 9e4508df | Stavros Sachtouris | |
32 | 9e4508df | Stavros Sachtouris | |
33 | 9e4508df | Stavros Sachtouris | from kamaki.clients.cyclades import CycladesClient |
34 | 9e4508df | Stavros Sachtouris | from kamaki.clients.pithos import PithosClient |
35 | 9e4508df | Stavros Sachtouris | |
36 | 9e4508df | Stavros Sachtouris | my_cyclades_client = CycladesClient(base_url, token) |
37 | 9e4508df | Stavros Sachtouris | my_pithos_client = PithosClient(base_url, token, account, container) |
38 | 9e4508df | Stavros Sachtouris | |
39 | 16d7b9ff | Stavros Sachtouris | .. note:: *cyclades* and *pithos* clients inherit ComputeClient from *compute* |
40 | 16d7b9ff | Stavros Sachtouris | and StorageClient from *storage*, respectively. Separate ComputeClient or |
41 | 16d7b9ff | Stavros Sachtouris | StorageClient objects should be used only when implementing applications for |
42 | 16d7b9ff | Stavros Sachtouris | strict OpenStack Compute or Storage services. |
43 | 9e4508df | Stavros Sachtouris | |
44 | cedde35d | Stavros Sachtouris | Using endpoints to get the base_url |
45 | cedde35d | Stavros Sachtouris | ----------------------------------- |
46 | 7ae842c2 | Stavros Sachtouris | |
47 | 16d7b9ff | Stavros Sachtouris | In OpenStack, each service (e.g., `compute`, `object-store`, etc.) has a number |
48 | 1c366ac9 | Stavros Sachtouris | of `endpoints`. These `endpoints` are URIs that are used by kamaki as |
49 | 16d7b9ff | Stavros Sachtouris | prefixes to form the corresponding API calls. Client applications need just |
50 | 16d7b9ff | Stavros Sachtouris | one of these these `endpoints`, namely the `publicURL`, which is also referred |
51 | 16d7b9ff | Stavros Sachtouris | to as `base_url` in kamaki client libraries. |
52 | cedde35d | Stavros Sachtouris | |
53 | 16d7b9ff | Stavros Sachtouris | Here are instructions for getting the base_url for a service:: |
54 | cedde35d | Stavros Sachtouris | |
55 | cedde35d | Stavros Sachtouris | 1. From the deployment UI get the AUTHENTICATION_URL and TOKEN |
56 | cedde35d | Stavros Sachtouris | (Example 1.2) |
57 | cedde35d | Stavros Sachtouris | 2. Use them to instantiate an AstakosClient |
58 | cedde35d | Stavros Sachtouris | (Example 1.2) |
59 | 1c366ac9 | Stavros Sachtouris | 3. Use AstakosClient instance to get endpoints for the service of interest |
60 | cedde35d | Stavros Sachtouris | (Example 1.3) |
61 | cedde35d | Stavros Sachtouris | 4. The 'publicURL' endpoint is the base_url we are looking for |
62 | cedde35d | Stavros Sachtouris | (Example 1.3) |
63 | cedde35d | Stavros Sachtouris | |
64 | cedde35d | Stavros Sachtouris | The AstakosClient is a client for the Synnefo/Astakos server. Synnefo/Astakos |
65 | 16d7b9ff | Stavros Sachtouris | is an identity server that implements the OpenStack identity API. Therefore, it |
66 | 16d7b9ff | Stavros Sachtouris | can be used to get the `base_url` values needed for initializing kamaki clients. |
67 | 16d7b9ff | Stavros Sachtouris | Kamaki simplifies this process with the astakos client library. |
68 | cedde35d | Stavros Sachtouris | |
69 | cedde35d | Stavros Sachtouris | Let's review the process with examples. |
70 | 7ae842c2 | Stavros Sachtouris | |
71 | 6362f3e1 | Stavros Sachtouris | First, an astakos client must be initialized (Example 1.2). An |
72 | abfaa6d9 | Vangelis Koukis | AUTHENTICATION_URL and a TOKEN can be acquired from the Synnefo deployment UI. |
73 | 6362f3e1 | Stavros Sachtouris | |
74 | 6362f3e1 | Stavros Sachtouris | .. code-block:: python |
75 | 6362f3e1 | Stavros Sachtouris | :emphasize-lines: 1 |
76 | 6362f3e1 | Stavros Sachtouris | |
77 | 6362f3e1 | Stavros Sachtouris | Example 1.2: Initialize an astakos client |
78 | 6362f3e1 | Stavros Sachtouris | |
79 | 6362f3e1 | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient |
80 | 6362f3e1 | Stavros Sachtouris | my_astakos_client = AstakosClient(AUTHENTICATION_URL, TOKEN) |
81 | 6362f3e1 | Stavros Sachtouris | |
82 | 6362f3e1 | Stavros Sachtouris | |
83 | 6362f3e1 | Stavros Sachtouris | Next, the astakos client can be used to retrieve the base_url values for the |
84 | cedde35d | Stavros Sachtouris | servers of interest. In this case (Example 1.3) they are *cyclades* |
85 | 6362f3e1 | Stavros Sachtouris | and *pithos*. A number of endpoints is assigned to each service, but kamaki |
86 | 6362f3e1 | Stavros Sachtouris | clients only need the one labeled as ``publicURL``. |
87 | 6362f3e1 | Stavros Sachtouris | |
88 | 6362f3e1 | Stavros Sachtouris | .. code-block:: python |
89 | 6362f3e1 | Stavros Sachtouris | :emphasize-lines: 1 |
90 | 6362f3e1 | Stavros Sachtouris | |
91 | 6362f3e1 | Stavros Sachtouris | Example 1.3: Retrieve cyclades and pithos base_url values |
92 | 6362f3e1 | Stavros Sachtouris | |
93 | 6362f3e1 | Stavros Sachtouris | cyclades_endpoints = my_astakos_client.get_service_endpoints('compute') |
94 | 6362f3e1 | Stavros Sachtouris | cyclades_base_url = cyclades_endpoints['publicURL'] |
95 | 6362f3e1 | Stavros Sachtouris | |
96 | 6362f3e1 | Stavros Sachtouris | pithos_endpoints = my_astakos_client.get_service_endpoints('object-store') |
97 | 6362f3e1 | Stavros Sachtouris | pithos_base_url = pithos_endpoints['publicURL'] |
98 | 6362f3e1 | Stavros Sachtouris | |
99 | 16d7b9ff | Stavros Sachtouris | The ``get_service_endpoints`` method is called with the service name as an |
100 | 16d7b9ff | Stavros Sachtouris | argument. Here are the service names for the kamaki clients:: |
101 | 6362f3e1 | Stavros Sachtouris | |
102 | 1c366ac9 | Stavros Sachtouris | storage.StorageClient, pithos.PithosClient --> object-store |
103 | 1c366ac9 | Stavros Sachtouris | compute.ComputeClient, cyclades.CycladesClient --> compute |
104 | 1c366ac9 | Stavros Sachtouris | network.NetworkClient, cyclades.CycladesNetworkClient --> network |
105 | 1c366ac9 | Stavros Sachtouris | image.ImageClient --> image |
106 | 1c366ac9 | Stavros Sachtouris | astakos.AstakosClient --> identity, account |
107 | 7ae842c2 | Stavros Sachtouris | |
108 | 9e4508df | Stavros Sachtouris | Use client methods |
109 | 9e4508df | Stavros Sachtouris | ------------------ |
110 | 9e4508df | Stavros Sachtouris | |
111 | 16d7b9ff | Stavros Sachtouris | At this point we assume that we can initialize a client, so the initialization |
112 | 16d7b9ff | Stavros Sachtouris | step will be omitted in most of the examples that follow. |
113 | 16d7b9ff | Stavros Sachtouris | |
114 | 16d7b9ff | Stavros Sachtouris | The next step is to take a look at the member methods of each particular client. |
115 | 16d7b9ff | Stavros Sachtouris | A detailed catalog of the member methods for all client classes can be found at |
116 | 16d7b9ff | Stavros Sachtouris | :ref:`the-client-api-ref` |
117 | 9e4508df | Stavros Sachtouris | |
118 | fa382f9e | Stavros Sachtouris | In the following example, the *cyclades* and *pithos* clients of example 1.1 |
119 | 16d7b9ff | Stavros Sachtouris | are used to extract some information through the remote service APIs. The information is then printed to the standard output. |
120 | 9e4508df | Stavros Sachtouris | |
121 | 9e4508df | Stavros Sachtouris | |
122 | 9e4508df | Stavros Sachtouris | .. code-block:: python |
123 | 9e4508df | Stavros Sachtouris | :emphasize-lines: 1,2 |
124 | 9e4508df | Stavros Sachtouris | |
125 | 6362f3e1 | Stavros Sachtouris | Example 1.4: Print server name and OS for server with server_id |
126 | 9e4508df | Stavros Sachtouris | Print objects in container mycont |
127 | 9e4508df | Stavros Sachtouris | |
128 | 9e4508df | Stavros Sachtouris | srv = my_cyclades_client.get_server_info(server_id) |
129 | 9e4508df | Stavros Sachtouris | print("Server Name: %s (with OS %s" % (srv['name'], srv['os'])) |
130 | 9e4508df | Stavros Sachtouris | |
131 | 9e4508df | Stavros Sachtouris | obj_list = my_pithos_client.list_objects(mycont) |
132 | 9e4508df | Stavros Sachtouris | for obj in obj_list: |
133 | 9e4508df | Stavros Sachtouris | print(' %s of %s bytes' % (obj['name'], obj['bytes'])) |
134 | 9e4508df | Stavros Sachtouris | |
135 | 9e4508df | Stavros Sachtouris | .. code-block:: console |
136 | 9e4508df | Stavros Sachtouris | :emphasize-lines: 1 |
137 | 9e4508df | Stavros Sachtouris | |
138 | 16d7b9ff | Stavros Sachtouris | * A run of examples 1.1 + 1.4 * |
139 | 9e4508df | Stavros Sachtouris | |
140 | 9e4508df | Stavros Sachtouris | |
141 | 9e4508df | Stavros Sachtouris | $ python test_script.py |
142 | 9e4508df | Stavros Sachtouris | Server Name: A Debian Server (with OS Debian Base) |
143 | 9e4508df | Stavros Sachtouris | lala.txt of 34 bytes |
144 | 9e4508df | Stavros Sachtouris | test.txt of 1232 bytes |
145 | 9e4508df | Stavros Sachtouris | testDir/ of 0 bytes |
146 | 9e4508df | Stavros Sachtouris | $ |
147 | 9e4508df | Stavros Sachtouris | |
148 | 9e4508df | Stavros Sachtouris | Error handling |
149 | 9e4508df | Stavros Sachtouris | -------------- |
150 | 9e4508df | Stavros Sachtouris | |
151 | 16d7b9ff | Stavros Sachtouris | The *kamaki.clients* error class is ClientError. A ClientError is raised for |
152 | 16d7b9ff | Stavros Sachtouris | any kind of *kamaki.clients* errors (errors reported by servers, type errors in |
153 | fa382f9e | Stavros Sachtouris | arguments, etc.). |
154 | 9e4508df | Stavros Sachtouris | |
155 | 9e4508df | Stavros Sachtouris | A ClientError contains:: |
156 | 9e4508df | Stavros Sachtouris | |
157 | 9e4508df | Stavros Sachtouris | message The error message. |
158 | 16d7b9ff | Stavros Sachtouris | status An optional error code, e.g., after a server error. |
159 | 9e4508df | Stavros Sachtouris | details Optional list of messages with error details. |
160 | 9e4508df | Stavros Sachtouris | |
161 | 6362f3e1 | Stavros Sachtouris | The following example concatenates examples 1.1 to 1.4 plus error handling |
162 | 9e4508df | Stavros Sachtouris | |
163 | 9e4508df | Stavros Sachtouris | .. code-block:: python |
164 | 9e4508df | Stavros Sachtouris | |
165 | 6362f3e1 | Stavros Sachtouris | Example 1.5: Error handling |
166 | 9e4508df | Stavros Sachtouris | |
167 | 16d7b9ff | Stavros Sachtouris | from kamaki.clients import ClientError |
168 | 16d7b9ff | Stavros Sachtouris | |
169 | 6362f3e1 | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient |
170 | 9e4508df | Stavros Sachtouris | from kamaki.clients.cyclades import CycladesClient |
171 | 9e4508df | Stavros Sachtouris | from kamaki.clients.pithos import PithosClient |
172 | 9e4508df | Stavros Sachtouris | |
173 | 9e4508df | Stavros Sachtouris | try: |
174 | 6362f3e1 | Stavros Sachtouris | my_astakos_client = AstakosClient(AUTHENTICATION_URL, TOKEN) |
175 | 16d7b9ff | Stavros Sachtouris | my_astakos_client.authenticate() |
176 | 6362f3e1 | Stavros Sachtouris | except ClientError: |
177 | 6362f3e1 | Stavros Sachtouris | print('Failed to authenticate user token') |
178 | 6362f3e1 | Stavros Sachtouris | return 1 |
179 | 6362f3e1 | Stavros Sachtouris | |
180 | 6362f3e1 | Stavros Sachtouris | try: |
181 | 6362f3e1 | Stavros Sachtouris | cyclades_endpoints = my_astakos_client.get_service_endpoints('compute') |
182 | 6362f3e1 | Stavros Sachtouris | cyclades_base_url = cyclades_endpoints['publicURL'] |
183 | 6362f3e1 | Stavros Sachtouris | except ClientError: |
184 | 6362f3e1 | Stavros Sachtouris | print('Failed to get endpoints for cyclades') |
185 | 6362f3e1 | Stavros Sachtouris | |
186 | 6362f3e1 | Stavros Sachtouris | try: |
187 | 6362f3e1 | Stavros Sachtouris | my_cyclades_client = CycladesClient(cyclades_base_url, token) |
188 | 9e4508df | Stavros Sachtouris | except ClientError: |
189 | 9e4508df | Stavros Sachtouris | print('Failed to initialize Cyclades client') |
190 | 9e4508df | Stavros Sachtouris | |
191 | 9e4508df | Stavros Sachtouris | try: |
192 | 6362f3e1 | Stavros Sachtouris | pithos_endpoints = my_astakos_client.get_service_endpoints('object-store') |
193 | 6362f3e1 | Stavros Sachtouris | pithos_base_url = pithos_endpoints['publicURL'] |
194 | 6362f3e1 | Stavros Sachtouris | except ClientError: |
195 | 6362f3e1 | Stavros Sachtouris | print('Failed to get endpoints for pithos') |
196 | 6362f3e1 | Stavros Sachtouris | |
197 | 6362f3e1 | Stavros Sachtouris | try: |
198 | 6362f3e1 | Stavros Sachtouris | my_pithos_client = PithosClient(pithos_base_url, token, account, container) |
199 | 9e4508df | Stavros Sachtouris | except ClientError: |
200 | 9e4508df | Stavros Sachtouris | print('Failed to initialize Pithos+ client') |
201 | 9e4508df | Stavros Sachtouris | |
202 | 9e4508df | Stavros Sachtouris | try: |
203 | 9e4508df | Stavros Sachtouris | srv = my_cyclades_client.get_server_info(server_id) |
204 | 9e4508df | Stavros Sachtouris | print("Server Name: %s (with OS %s" % (srv['name'], srv['os'])) |
205 | 9e4508df | Stavros Sachtouris | |
206 | 9e4508df | Stavros Sachtouris | obj_list = my_pithos_client.list_objects(mycont) |
207 | 9e4508df | Stavros Sachtouris | for obj in obj_list: |
208 | 9e4508df | Stavros Sachtouris | print(' %s of %s bytes' % (obj['name'], obj['bytes'])) |
209 | 9e4508df | Stavros Sachtouris | except ClientError as e: |
210 | 9e4508df | Stavros Sachtouris | print('Error: %s' % e) |
211 | 9e4508df | Stavros Sachtouris | if e.status: |
212 | 9e4508df | Stavros Sachtouris | print('- error code: %s' % e.status) |
213 | 9e4508df | Stavros Sachtouris | if e.details: |
214 | 9e4508df | Stavros Sachtouris | for detail in e.details: |
215 | 9e4508df | Stavros Sachtouris | print('- %s' % detail) |
216 | 6362f3e1 | Stavros Sachtouris | |
217 | 6362f3e1 | Stavros Sachtouris | |
218 | 6362f3e1 | Stavros Sachtouris | Scripts |
219 | 6362f3e1 | Stavros Sachtouris | ------- |
220 | 6362f3e1 | Stavros Sachtouris | |
221 | 6362f3e1 | Stavros Sachtouris | Batch-create servers |
222 | 6362f3e1 | Stavros Sachtouris | '''''''''''''''''''' |
223 | 6362f3e1 | Stavros Sachtouris | |
224 | 6362f3e1 | Stavros Sachtouris | .. code-block:: python |
225 | 6362f3e1 | Stavros Sachtouris | |
226 | a582b70e | Stavros Sachtouris | #! /usr/bin/python |
227 | 6362f3e1 | Stavros Sachtouris | |
228 | 6362f3e1 | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient |
229 | 6362f3e1 | Stavros Sachtouris | from kamaki.clients.cyclades import CycladesClient |
230 | 6362f3e1 | Stavros Sachtouris | |
231 | 6362f3e1 | Stavros Sachtouris | AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0' |
232 | 6362f3e1 | Stavros Sachtouris | TOKEN = 'replace this with your token' |
233 | 6362f3e1 | Stavros Sachtouris | |
234 | 6362f3e1 | Stavros Sachtouris | user = AstakosClient(AUTHENTICATION_URL, TOKEN) |
235 | 6362f3e1 | Stavros Sachtouris | |
236 | 38db356b | Stavros Sachtouris | cyclades_endpoints = user.get_service_endpoints('compute') |
237 | 6362f3e1 | Stavros Sachtouris | CYCLADES_URL = cyclades_endpoints['publicURL'] |
238 | 6362f3e1 | Stavros Sachtouris | cyclades = CycladesClient(CYCLADES_URL, TOKEN) |
239 | 6362f3e1 | Stavros Sachtouris | |
240 | 6362f3e1 | Stavros Sachtouris | # (name, flavor-id, image-id) |
241 | 6362f3e1 | Stavros Sachtouris | servers = [ |
242 | 6362f3e1 | Stavros Sachtouris | ('My Debian Server', 1, 'my-debian-base-image-id'), |
243 | 6362f3e1 | Stavros Sachtouris | ('My Windows Server', 3, 'my-windows-8-image-id'), |
244 | 6362f3e1 | Stavros Sachtouris | ('My Ubuntu Server', 3, 'my-ubuntu-12-image-id'), |
245 | 6362f3e1 | Stavros Sachtouris | ] |
246 | 6362f3e1 | Stavros Sachtouris | |
247 | 6362f3e1 | Stavros Sachtouris | for name, flavor_id, image_id in servers: |
248 | 6362f3e1 | Stavros Sachtouris | cyclades.create_server(name, flavor_id, image_id) |
249 | 6362f3e1 | Stavros Sachtouris | |
250 | 6362f3e1 | Stavros Sachtouris | |
251 | 6362f3e1 | Stavros Sachtouris | Batch-create 4 servers of the same kind |
252 | 6362f3e1 | Stavros Sachtouris | ''''''''''''''''''''''''''''''''''''''' |
253 | 6362f3e1 | Stavros Sachtouris | |
254 | 6362f3e1 | Stavros Sachtouris | .. code-block:: python |
255 | 6362f3e1 | Stavros Sachtouris | |
256 | a582b70e | Stavros Sachtouris | #! /usr/bin/python |
257 | 6362f3e1 | Stavros Sachtouris | |
258 | 6362f3e1 | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient |
259 | 6362f3e1 | Stavros Sachtouris | from kamaki.clients.cyclades import CycladesClient |
260 | 6362f3e1 | Stavros Sachtouris | |
261 | 6362f3e1 | Stavros Sachtouris | AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0' |
262 | 6362f3e1 | Stavros Sachtouris | TOKEN = 'replace this with your token' |
263 | 6362f3e1 | Stavros Sachtouris | |
264 | 6362f3e1 | Stavros Sachtouris | user = AstakosClient(AUTHENTICATION_URL, TOKEN) |
265 | 6362f3e1 | Stavros Sachtouris | |
266 | 38db356b | Stavros Sachtouris | cyclades_endpoints = user.get_service_endpoints('compute') |
267 | 6362f3e1 | Stavros Sachtouris | CYCLADES_URL = cyclades_endpoints['publicURL'] |
268 | 6362f3e1 | Stavros Sachtouris | cyclades = CycladesClient(CYCLADES_URL, TOKEN) |
269 | 6362f3e1 | Stavros Sachtouris | |
270 | 6362f3e1 | Stavros Sachtouris | for i in range(4): |
271 | 6362f3e1 | Stavros Sachtouris | name, flavor_id, image_id = 'Server %s' % (i + 1), 3, 'some-image-id' |
272 | 6362f3e1 | Stavros Sachtouris | cyclades.create_server(name, flavor_id, image_id) |
273 | a582b70e | Stavros Sachtouris | |
274 | a582b70e | Stavros Sachtouris | Register a banch of pre-uploaded images |
275 | a582b70e | Stavros Sachtouris | ''''''''''''''''''''''''''''''''''''''' |
276 | a582b70e | Stavros Sachtouris | |
277 | a582b70e | Stavros Sachtouris | .. code-block:: python |
278 | a582b70e | Stavros Sachtouris | |
279 | a582b70e | Stavros Sachtouris | #! /usr/bin/python |
280 | a582b70e | Stavros Sachtouris | |
281 | a582b70e | Stavros Sachtouris | from kamaki.clients import ClientError |
282 | a582b70e | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient |
283 | a582b70e | Stavros Sachtouris | from kamaki.clients.pithos import PithosClient |
284 | a582b70e | Stavros Sachtouris | from kamaki.clients.image import ImageClient |
285 | a582b70e | Stavros Sachtouris | |
286 | a582b70e | Stavros Sachtouris | AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0' |
287 | a582b70e | Stavros Sachtouris | TOKEN = 'replace this with your token' |
288 | a582b70e | Stavros Sachtouris | IMAGE_CONTAINER = 'images' |
289 | a582b70e | Stavros Sachtouris | |
290 | a582b70e | Stavros Sachtouris | astakos = AstakosClient(AUTHENTICATION_URL, TOKEN) |
291 | 736072e2 | Stavros Sachtouris | USER_UUID = astakos.user_info['uuid'] |
292 | a582b70e | Stavros Sachtouris | |
293 | 38db356b | Stavros Sachtouris | PITHOS_URL = astakos.get_service_endpoints('object-store')['publicURL'] |
294 | a582b70e | Stavros Sachtouris | pithos = PithosClient(PITHOS_URL, TOKEN, USER_UUID, IMAGE_CONTAINER) |
295 | a582b70e | Stavros Sachtouris | |
296 | 38db356b | Stavros Sachtouris | IMAGE_URL = astakos.get_service_endpoints('image')['publicURL'] |
297 | a582b70e | Stavros Sachtouris | plankton = ImageClient(IMAGE_URL, TOKEN) |
298 | a582b70e | Stavros Sachtouris | |
299 | a582b70e | Stavros Sachtouris | for img in pithos.list_objects(): |
300 | a582b70e | Stavros Sachtouris | IMAGE_PATH = img['name'] |
301 | a582b70e | Stavros Sachtouris | try: |
302 | a582b70e | Stavros Sachtouris | r = plankton.register( |
303 | a582b70e | Stavros Sachtouris | name='Image %s' % img, |
304 | a582b70e | Stavros Sachtouris | location=(USER_UUID, IMAGE_CONTAINER, IMAGE_PATH)) |
305 | a582b70e | Stavros Sachtouris | print 'Image %s registered with id %s' % (r['name'], r['id']) |
306 | a582b70e | Stavros Sachtouris | except ClientError: |
307 | a582b70e | Stavros Sachtouris | print 'Failed to register image %s' % IMAGE_PATH |
308 | 16d7b9ff | Stavros Sachtouris | |
309 | 16d7b9ff | Stavros Sachtouris | Two servers and a private network |
310 | 16d7b9ff | Stavros Sachtouris | ''''''''''''''''''''''''''''''''' |
311 | 16d7b9ff | Stavros Sachtouris | |
312 | 16d7b9ff | Stavros Sachtouris | .. code-block:: python |
313 | 16d7b9ff | Stavros Sachtouris | |
314 | 16d7b9ff | Stavros Sachtouris | #! /user/bin/python |
315 | 16d7b9ff | Stavros Sachtouris | |
316 | 16d7b9ff | Stavros Sachtouris | from kamaki.clients.astakos import AstakosClient |
317 | 16d7b9ff | Stavros Sachtouris | from kamaki.clients.cyclades import CycladesClient |
318 | 16d7b9ff | Stavros Sachtouris | |
319 | 16d7b9ff | Stavros Sachtouris | AUTHENTICATION_URL = 'https://accounts.example.com/identity/v2.0' |
320 | 16d7b9ff | Stavros Sachtouris | TOKEN = 'replace this with your token' |
321 | 16d7b9ff | Stavros Sachtouris | |
322 | 16d7b9ff | Stavros Sachtouris | astakos = AstakosClient(AUTHENTICATION_URL, TOKEN) |
323 | 16d7b9ff | Stavros Sachtouris | |
324 | 16d7b9ff | Stavros Sachtouris | cyclades_endpoints = user.get_service_endpoints('compute') |
325 | 16d7b9ff | Stavros Sachtouris | CYCLADES_URL = cyclades_endpoints['publicURL'] |
326 | 16d7b9ff | Stavros Sachtouris | |
327 | 16d7b9ff | Stavros Sachtouris | FLAVOR_ID = 'put your flavor id here' |
328 | 16d7b9ff | Stavros Sachtouris | IMAGE_ID = 'put your image id here' |
329 | 16d7b9ff | Stavros Sachtouris | |
330 | 16d7b9ff | Stavros Sachtouris | cyclades = CycladesClient(CYCLADES_URL, TOKEN) |
331 | 16d7b9ff | Stavros Sachtouris | |
332 | 16d7b9ff | Stavros Sachtouris | srv1 = cyclades.create_server('server 1', FLAVOR_ID, IMAGE_ID) |
333 | 16d7b9ff | Stavros Sachtouris | srv2 = cyclades.create_server('server 2', FLAVOR_ID, IMAGE_ID) |
334 | 16d7b9ff | Stavros Sachtouris | |
335 | 16d7b9ff | Stavros Sachtouris | srv_state1 = cyclades.wait_server(srv1['id']) |
336 | 16d7b9ff | Stavros Sachtouris | assert srv_state1 in ('ACTIVE'), 'Server 1 built failure' |
337 | 16d7b9ff | Stavros Sachtouris | |
338 | 16d7b9ff | Stavros Sachtouris | srv_state2 = cyclades.wait_server(srv2['id']) |
339 | 16d7b9ff | Stavros Sachtouris | assert srv_state2 in ('ACTIVE'), 'Server 2 built failure' |
340 | 16d7b9ff | Stavros Sachtouris | |
341 | 16d7b9ff | Stavros Sachtouris | net = cyclades.create_network('My private network') |
342 | 16d7b9ff | Stavros Sachtouris | net_state = cyclades.wait_network(net['id']) |
343 | 16d7b9ff | Stavros Sachtouris | assert net_state in ('ACTIVE', ), 'Network built failure' |
344 | 16d7b9ff | Stavros Sachtouris | |
345 | 16d7b9ff | Stavros Sachtouris | cyclades.connect_server(srv1['id'], net['id']) |
346 | 16d7b9ff | Stavros Sachtouris | cyclades.connect_server(srv2['id'], net['id']) |