Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / compute / __init__.py @ 89a1c636

History | View | Annotate | Download (14.5 kB)

1 24851aa5 Stavros Sachtouris
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 a1c50326 Giorgos Verigakis
#
3 a1c50326 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 a1c50326 Giorgos Verigakis
# without modification, are permitted provided that the following
5 a1c50326 Giorgos Verigakis
# conditions are met:
6 a1c50326 Giorgos Verigakis
#
7 a1c50326 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 a1c50326 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 a1c50326 Giorgos Verigakis
#      disclaimer.
10 a1c50326 Giorgos Verigakis
#
11 a1c50326 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 a1c50326 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 a1c50326 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 a1c50326 Giorgos Verigakis
#      provided with the distribution.
15 a1c50326 Giorgos Verigakis
#
16 a1c50326 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a1c50326 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a1c50326 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a1c50326 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a1c50326 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a1c50326 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a1c50326 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a1c50326 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a1c50326 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a1c50326 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a1c50326 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a1c50326 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 a1c50326 Giorgos Verigakis
#
29 a1c50326 Giorgos Verigakis
# The views and conclusions contained in the software and
30 a1c50326 Giorgos Verigakis
# documentation are those of the authors and should not be
31 a1c50326 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 a1c50326 Giorgos Verigakis
# or implied, of GRNET S.A.
33 a1c50326 Giorgos Verigakis
34 606fe15f Stavros Sachtouris
from kamaki.clients import ClientError
35 55faa0bc Stavros Sachtouris
from kamaki.clients.compute.rest_api import ComputeRestClient
36 a1c50326 Giorgos Verigakis
37 3dabe5d2 Stavros Sachtouris
38 55faa0bc Stavros Sachtouris
class ComputeClient(ComputeRestClient):
39 d2cea1e2 Giorgos Verigakis
    """OpenStack Compute API 1.1 client"""
40 2f749e6e Stavros Sachtouris
41 dbcbf446 Stavros Sachtouris
    def list_servers(
42 dbcbf446 Stavros Sachtouris
            self,
43 dbcbf446 Stavros Sachtouris
            detail=False,
44 dbcbf446 Stavros Sachtouris
            changes_since=None,
45 dbcbf446 Stavros Sachtouris
            image=None,
46 dbcbf446 Stavros Sachtouris
            flavor=None,
47 dbcbf446 Stavros Sachtouris
            name=None,
48 dbcbf446 Stavros Sachtouris
            marker=None,
49 dbcbf446 Stavros Sachtouris
            limit=None,
50 dbcbf446 Stavros Sachtouris
            status=None,
51 dbcbf446 Stavros Sachtouris
            host=None,
52 dbcbf446 Stavros Sachtouris
            response_headers=dict(previous=None, next=None)):
53 a2ef112e Stavros Sachtouris
        """
54 a2ef112e Stavros Sachtouris
        :param detail: if true, append full server details to each item
55 a2ef112e Stavros Sachtouris

56 dbcbf446 Stavros Sachtouris
        :param response_headers: (dict) use it to get previous/next responses
57 dbcbf446 Stavros Sachtouris
            Keep the existing dict format to actually get the server responses
58 dbcbf446 Stavros Sachtouris
            Use it with very long lists or with marker
59 dbcbf446 Stavros Sachtouris

60 a2ef112e Stavros Sachtouris
        :returns: list of server ids and names
61 a2ef112e Stavros Sachtouris
        """
62 dbcbf446 Stavros Sachtouris
        r = self.servers_get(
63 dbcbf446 Stavros Sachtouris
            detail=bool(detail),
64 dbcbf446 Stavros Sachtouris
            changes_since=changes_since,
65 dbcbf446 Stavros Sachtouris
            image=image,
66 dbcbf446 Stavros Sachtouris
            flavor=flavor,
67 dbcbf446 Stavros Sachtouris
            name=name,
68 dbcbf446 Stavros Sachtouris
            marker=marker,
69 dbcbf446 Stavros Sachtouris
            limit=limit,
70 dbcbf446 Stavros Sachtouris
            status=status,
71 dbcbf446 Stavros Sachtouris
            host=host)
72 0b179c69 Stavros Sachtouris
        for k, v in response_headers.items():
73 0b179c69 Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
74 fab9b17f Stavros Sachtouris
        return r.json['servers']
75 3dabe5d2 Stavros Sachtouris
76 dbcbf446 Stavros Sachtouris
    def get_server_details(
77 dbcbf446 Stavros Sachtouris
            self, server_id,
78 dbcbf446 Stavros Sachtouris
            changes_since=None,
79 dbcbf446 Stavros Sachtouris
            image=None,
80 dbcbf446 Stavros Sachtouris
            flavor=None,
81 dbcbf446 Stavros Sachtouris
            name=None,
82 dbcbf446 Stavros Sachtouris
            marker=None,
83 dbcbf446 Stavros Sachtouris
            limit=None,
84 dbcbf446 Stavros Sachtouris
            status=None,
85 dbcbf446 Stavros Sachtouris
            host=None,
86 dbcbf446 Stavros Sachtouris
            response_headers=dict(previous=None, next=None),
87 dbcbf446 Stavros Sachtouris
            **kwargs):
88 a2ef112e Stavros Sachtouris
        """Return detailed info for a server
89 a2ef112e Stavros Sachtouris

90 a2ef112e Stavros Sachtouris
        :param server_id: integer (int or str)
91 a2ef112e Stavros Sachtouris

92 a2ef112e Stavros Sachtouris
        :returns: dict with server details
93 a2ef112e Stavros Sachtouris
        """
94 dbcbf446 Stavros Sachtouris
        r = self.servers_get(
95 dbcbf446 Stavros Sachtouris
            server_id,
96 dbcbf446 Stavros Sachtouris
            changes_since=changes_since,
97 dbcbf446 Stavros Sachtouris
            image=image,
98 dbcbf446 Stavros Sachtouris
            flavor=flavor,
99 dbcbf446 Stavros Sachtouris
            name=name,
100 dbcbf446 Stavros Sachtouris
            marker=marker,
101 dbcbf446 Stavros Sachtouris
            limit=limit,
102 dbcbf446 Stavros Sachtouris
            status=status,
103 dbcbf446 Stavros Sachtouris
            host=host,
104 dbcbf446 Stavros Sachtouris
            **kwargs)
105 0b179c69 Stavros Sachtouris
        for k, v in response_headers.items():
106 0b179c69 Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
107 6a0b1658 Giorgos Verigakis
        return r.json['server']
108 3dabe5d2 Stavros Sachtouris
109 dbcbf446 Stavros Sachtouris
    def create_server(
110 dbcbf446 Stavros Sachtouris
            self, name, flavor_id, image_id,
111 dbcbf446 Stavros Sachtouris
            security_group=None,
112 dbcbf446 Stavros Sachtouris
            user_data=None,
113 dbcbf446 Stavros Sachtouris
            availability_zone=None,
114 dbcbf446 Stavros Sachtouris
            metadata=None,
115 dbcbf446 Stavros Sachtouris
            personality=None,
116 264a13f7 Stavros Sachtouris
            networks=None,
117 89a1c636 Giorgos Korfiatis
            project=None,
118 dbcbf446 Stavros Sachtouris
            response_headers=dict(location=None)):
119 a1c50326 Giorgos Verigakis
        """Submit request to create a new server
120 a1c50326 Giorgos Verigakis

121 a2ef112e Stavros Sachtouris
        :param name: (str)
122 a2ef112e Stavros Sachtouris

123 a2ef112e Stavros Sachtouris
        :param flavor_id: integer id denoting a preset hardware configuration
124 a2ef112e Stavros Sachtouris

125 2bd23362 Stavros Sachtouris
        :param image_id: (str) id of the image of the virtual server
126 a1c50326 Giorgos Verigakis

127 dbcbf446 Stavros Sachtouris
        :param metadata: (dict) vm metadata
128 dbcbf446 Stavros Sachtouris

129 a2ef112e Stavros Sachtouris
        :param personality: a list of (file path, file contents) tuples,
130 2bd23362 Stavros Sachtouris
            describing files to be injected into virtual server upon creation
131 a1c50326 Giorgos Verigakis

132 eb647cfe Stavros Sachtouris
        :param networks: (list of dicts) Networks to connect to, list this:
133 db6438fd Stavros Sachtouris
            [
134 f9953e1a Stavros Sachtouris
            {"uuid": <network_uuid>},
135 f9953e1a Stavros Sachtouris
            {"uuid": <network_uuid>, "fixed_ip": address},
136 1c366ac9 Stavros Sachtouris
            {"port": <port_id>}, ...]
137 58f4caba Stavros Sachtouris
            ATTENTION: Empty list is different to None.
138 eb647cfe Stavros Sachtouris

139 2bd23362 Stavros Sachtouris
        :returns: a dict with the new virtual server details
140 a2ef112e Stavros Sachtouris

141 a2ef112e Stavros Sachtouris
        :raises ClientError: wraps request errors
142 a1c50326 Giorgos Verigakis
        """
143 dbcbf446 Stavros Sachtouris
        req = {'server': {
144 dbcbf446 Stavros Sachtouris
            'name': name, 'flavorRef': flavor_id, 'imageRef': image_id}}
145 dbcbf446 Stavros Sachtouris
146 ae3c77f9 Stavros Sachtouris
        if metadata:
147 ae3c77f9 Stavros Sachtouris
            req['server']['metadata'] = metadata
148 ae3c77f9 Stavros Sachtouris
149 a1c50326 Giorgos Verigakis
        if personality:
150 c4922a05 Giorgos Verigakis
            req['server']['personality'] = personality
151 3dabe5d2 Stavros Sachtouris
152 eb647cfe Stavros Sachtouris
        if networks is not None:
153 58f4caba Stavros Sachtouris
            req['server']['networks'] = networks
154 264a13f7 Stavros Sachtouris
155 89a1c636 Giorgos Korfiatis
        if project:
156 89a1c636 Giorgos Korfiatis
            req['server']['project'] = project
157 89a1c636 Giorgos Korfiatis
158 dbcbf446 Stavros Sachtouris
        r = self.servers_post(
159 dbcbf446 Stavros Sachtouris
            json_data=req,
160 dbcbf446 Stavros Sachtouris
            security_group=security_group,
161 dbcbf446 Stavros Sachtouris
            user_data=user_data,
162 dbcbf446 Stavros Sachtouris
            availability_zone=availability_zone)
163 0b179c69 Stavros Sachtouris
        for k, v in response_headers.items():
164 0b179c69 Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
165 6a0b1658 Giorgos Verigakis
        return r.json['server']
166 3dabe5d2 Stavros Sachtouris
167 a1c50326 Giorgos Verigakis
    def update_server_name(self, server_id, new_name):
168 a2ef112e Stavros Sachtouris
        """Update the name of the server as reported by the API (does not
169 2bd23362 Stavros Sachtouris
            modify the hostname used inside the virtual server)
170 a2ef112e Stavros Sachtouris

171 a2ef112e Stavros Sachtouris
        :param server_id: integer (str or int)
172 a1c50326 Giorgos Verigakis

173 8194b51b Stavros Sachtouris
        :param new_name: (str)
174 cd295a1d Stavros Sachtouris

175 cd295a1d Stavros Sachtouris
        :returns: (dict) response headers
176 a1c50326 Giorgos Verigakis
        """
177 6a0b1658 Giorgos Verigakis
        req = {'server': {'name': new_name}}
178 cd295a1d Stavros Sachtouris
        r = self.servers_put(server_id, json_data=req)
179 cd295a1d Stavros Sachtouris
        return r.headers
180 3dabe5d2 Stavros Sachtouris
181 a1c50326 Giorgos Verigakis
    def delete_server(self, server_id):
182 a2ef112e Stavros Sachtouris
        """Submit a deletion request for a server specified by id
183 a2ef112e Stavros Sachtouris

184 a2ef112e Stavros Sachtouris
        :param server_id: integer (str or int)
185 cd295a1d Stavros Sachtouris

186 cd295a1d Stavros Sachtouris
        :returns: (dict) response headers
187 a2ef112e Stavros Sachtouris
        """
188 cd295a1d Stavros Sachtouris
        r = self.servers_delete(server_id)
189 cd295a1d Stavros Sachtouris
        return r.headers
190 6ce9fc72 Stavros Sachtouris
191 ef2e6c9f Stavros Sachtouris
    def change_admin_password(self, server_id, new_password):
192 ef2e6c9f Stavros Sachtouris
        """
193 ef2e6c9f Stavros Sachtouris
        :param server_id: (int)
194 ef2e6c9f Stavros Sachtouris

195 ef2e6c9f Stavros Sachtouris
        :param new_password: (str)
196 ef2e6c9f Stavros Sachtouris
        """
197 ef2e6c9f Stavros Sachtouris
        req = {"changePassword": {"adminPass": new_password}}
198 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(server_id, json_data=req)
199 ef2e6c9f Stavros Sachtouris
        return r.headers
200 ef2e6c9f Stavros Sachtouris
201 ef2e6c9f Stavros Sachtouris
    def rebuild_server(self, server_id, response_headers=dict(location=None)):
202 ef2e6c9f Stavros Sachtouris
        """OS"""
203 ef2e6c9f Stavros Sachtouris
        server = self.get_server_details(server_id)
204 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(
205 ef2e6c9f Stavros Sachtouris
            server_id, json_data=dict(rebuild=server['server']))
206 ef2e6c9f Stavros Sachtouris
        for k, v in response_headers.items():
207 ef2e6c9f Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
208 ef2e6c9f Stavros Sachtouris
        return r.json['server']
209 ef2e6c9f Stavros Sachtouris
210 a1c50326 Giorgos Verigakis
    def reboot_server(self, server_id, hard=False):
211 a2ef112e Stavros Sachtouris
        """
212 a2ef112e Stavros Sachtouris
        :param server_id: integer (str or int)
213 a2ef112e Stavros Sachtouris

214 a2ef112e Stavros Sachtouris
        :param hard: perform a hard reboot if true, soft reboot otherwise
215 a2ef112e Stavros Sachtouris
        """
216 ef2e6c9f Stavros Sachtouris
        req = {'reboot': {'type': 'HARD' if hard else 'SOFT'}}
217 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(server_id, json_data=req)
218 cd295a1d Stavros Sachtouris
        return r.headers
219 3dabe5d2 Stavros Sachtouris
220 fc7c3c4c Stavros Sachtouris
    def resize_server(self, server_id, flavor_id):
221 fc7c3c4c Stavros Sachtouris
        """
222 fc7c3c4c Stavros Sachtouris
        :param server_id: (str)
223 fc7c3c4c Stavros Sachtouris

224 fc7c3c4c Stavros Sachtouris
        :param flavor_id: (int)
225 fc7c3c4c Stavros Sachtouris

226 fc7c3c4c Stavros Sachtouris
        :returns: (dict) request headers
227 fc7c3c4c Stavros Sachtouris
        """
228 fc7c3c4c Stavros Sachtouris
        req = {'resize': {'flavorRef': flavor_id}}
229 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(server_id, json_data=req)
230 ef2e6c9f Stavros Sachtouris
        return r.headers
231 ef2e6c9f Stavros Sachtouris
232 ef2e6c9f Stavros Sachtouris
    def confirm_resize_server(self, server_id):
233 ef2e6c9f Stavros Sachtouris
        """OS"""
234 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(
235 ef2e6c9f Stavros Sachtouris
            server_id, json_data=dict(confirmResize=None))
236 ef2e6c9f Stavros Sachtouris
        return r.headers
237 ef2e6c9f Stavros Sachtouris
238 ef2e6c9f Stavros Sachtouris
    def revert_resize_server(self, server_id):
239 ef2e6c9f Stavros Sachtouris
        """OS"""
240 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(
241 ef2e6c9f Stavros Sachtouris
            server_id, json_data=dict(revertResize=None))
242 ef2e6c9f Stavros Sachtouris
        return r.headers
243 ef2e6c9f Stavros Sachtouris
244 ef2e6c9f Stavros Sachtouris
    def create_server_image(self, server_id, image_name, **metadata):
245 ef2e6c9f Stavros Sachtouris
        """OpenStack method for taking snapshots"""
246 ef2e6c9f Stavros Sachtouris
        req = dict(createImage=dict(name=image_name, metadata=metadata))
247 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(server_id, json_data=req)
248 ef2e6c9f Stavros Sachtouris
        return r.headers['location']
249 ef2e6c9f Stavros Sachtouris
250 ef2e6c9f Stavros Sachtouris
    def start_server(self, server_id):
251 ef2e6c9f Stavros Sachtouris
        """OS Extentions"""
252 ef2e6c9f Stavros Sachtouris
        req = {'os-start': None}
253 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(server_id, json_data=req, success=202)
254 ef2e6c9f Stavros Sachtouris
        return r.headers
255 ef2e6c9f Stavros Sachtouris
256 ef2e6c9f Stavros Sachtouris
    def shutdown_server(self, server_id):
257 ef2e6c9f Stavros Sachtouris
        """OS Extentions"""
258 ef2e6c9f Stavros Sachtouris
        req = {'os-stop': None}
259 ef2e6c9f Stavros Sachtouris
        r = self.servers_action_post(server_id, json_data=req, success=202)
260 fc7c3c4c Stavros Sachtouris
        return r.headers
261 fc7c3c4c Stavros Sachtouris
262 0b179c69 Stavros Sachtouris
    def get_server_metadata(self, server_id, key='', response_headers=dict(
263 0b179c69 Stavros Sachtouris
            previous=None, next=None)):
264 0b179c69 Stavros Sachtouris
        r = self.servers_metadata_get(server_id, key)
265 0b179c69 Stavros Sachtouris
        for k, v in response_headers.items():
266 0b179c69 Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
267 7749f75a Stavros Sachtouris
        return r.json['meta' if key else 'metadata']
268 3dabe5d2 Stavros Sachtouris
269 a1c50326 Giorgos Verigakis
    def create_server_metadata(self, server_id, key, val):
270 df0045d8 Stavros Sachtouris
        req = {'meta': {key: val}}
271 6d6d5141 Stavros Sachtouris
        r = self.servers_metadata_put(
272 6d6d5141 Stavros Sachtouris
            server_id, key, json_data=req, success=201)
273 df0045d8 Stavros Sachtouris
        return r.json['meta']
274 3dabe5d2 Stavros Sachtouris
275 0b179c69 Stavros Sachtouris
    def update_server_metadata(
276 0b179c69 Stavros Sachtouris
            self, server_id,
277 0b179c69 Stavros Sachtouris
            response_headers=dict(previous=None, next=None), **metadata):
278 6a0b1658 Giorgos Verigakis
        req = {'metadata': metadata}
279 6d6d5141 Stavros Sachtouris
        r = self.servers_metadata_post(server_id, json_data=req, success=201)
280 0b179c69 Stavros Sachtouris
        for k, v in response_headers.items():
281 0b179c69 Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
282 6a0b1658 Giorgos Verigakis
        return r.json['metadata']
283 3dabe5d2 Stavros Sachtouris
284 a1c50326 Giorgos Verigakis
    def delete_server_metadata(self, server_id, key):
285 6d6d5141 Stavros Sachtouris
        r = self.servers_metadata_delete(server_id, key)
286 cd295a1d Stavros Sachtouris
        return r.headers
287 6666f3b3 Stavros Sachtouris
288 c77131f4 Stavros Sachtouris
    def get_server_nics(self, server_id, changes_since=None):
289 c77131f4 Stavros Sachtouris
        r = self.servers_ips_get(server_id, changes_since=changes_since)
290 c77131f4 Stavros Sachtouris
        return r.json
291 c77131f4 Stavros Sachtouris
292 c77131f4 Stavros Sachtouris
    def get_server_network_nics(
293 c77131f4 Stavros Sachtouris
            self, server_id, network_id, changes_since=None):
294 c77131f4 Stavros Sachtouris
        r = self.servers_ips_get(
295 c77131f4 Stavros Sachtouris
            server_id, network_id=network_id, changes_since=changes_since)
296 c77131f4 Stavros Sachtouris
        return r.json['network']
297 c77131f4 Stavros Sachtouris
298 e51c7d5b Stavros Sachtouris
    def list_flavors(self, detail=False, response_headers=dict(
299 e51c7d5b Stavros Sachtouris
            previous=None, next=None)):
300 e51c7d5b Stavros Sachtouris
        r = self.flavors_get(detail=bool(detail))
301 e51c7d5b Stavros Sachtouris
        for k, v in response_headers.items():
302 e51c7d5b Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
303 fab9b17f Stavros Sachtouris
        return r.json['flavors']
304 a1c50326 Giorgos Verigakis
305 a1c50326 Giorgos Verigakis
    def get_flavor_details(self, flavor_id):
306 6666f3b3 Stavros Sachtouris
        r = self.flavors_get(flavor_id)
307 6a0b1658 Giorgos Verigakis
        return r.json['flavor']
308 6666f3b3 Stavros Sachtouris
309 e51c7d5b Stavros Sachtouris
    def list_images(self, detail=False, response_headers=dict(
310 e51c7d5b Stavros Sachtouris
            next=None, previous=None)):
311 e51c7d5b Stavros Sachtouris
        r = self.images_get(detail=bool(detail))
312 e51c7d5b Stavros Sachtouris
        for k, v in response_headers.items():
313 e51c7d5b Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
314 fab9b17f Stavros Sachtouris
        return r.json['images']
315 3dabe5d2 Stavros Sachtouris
316 71286858 Stavros Sachtouris
    def get_image_details(self, image_id, **kwargs):
317 a2ef112e Stavros Sachtouris
        """
318 a2ef112e Stavros Sachtouris
        :returns: dict
319 a2ef112e Stavros Sachtouris

320 a2ef112e Stavros Sachtouris
        :raises ClientError: 404 if image not available
321 a2ef112e Stavros Sachtouris
        """
322 71286858 Stavros Sachtouris
        r = self.images_get(image_id, **kwargs)
323 71286858 Stavros Sachtouris
        try:
324 71286858 Stavros Sachtouris
            return r.json['image']
325 71286858 Stavros Sachtouris
        except KeyError:
326 de73876b Stavros Sachtouris
            raise ClientError('Image not available', 404, details=[
327 de73876b Stavros Sachtouris
                'Image %d not found or not accessible'])
328 3dabe5d2 Stavros Sachtouris
329 a1c50326 Giorgos Verigakis
    def delete_image(self, image_id):
330 a2ef112e Stavros Sachtouris
        """
331 a2ef112e Stavros Sachtouris
        :param image_id: (str)
332 a2ef112e Stavros Sachtouris
        """
333 f5f35422 Stavros Sachtouris
        r = self.images_delete(image_id)
334 f5f35422 Stavros Sachtouris
        return r.headers
335 6666f3b3 Stavros Sachtouris
336 e51c7d5b Stavros Sachtouris
    def get_image_metadata(self, image_id, key='', response_headers=dict(
337 e51c7d5b Stavros Sachtouris
            previous=None, next=None)):
338 a2ef112e Stavros Sachtouris
        """
339 a2ef112e Stavros Sachtouris
        :param image_id: (str)
340 a2ef112e Stavros Sachtouris

341 a2ef112e Stavros Sachtouris
        :param key: (str) the metadatum key
342 a2ef112e Stavros Sachtouris

343 a2ef112e Stavros Sachtouris
        :returns (dict) metadata if key not set, specific metadatum otherwise
344 a2ef112e Stavros Sachtouris
        """
345 e51c7d5b Stavros Sachtouris
        r = self.images_metadata_get(image_id, key)
346 e51c7d5b Stavros Sachtouris
        for k, v in response_headers.items():
347 e51c7d5b Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
348 7749f75a Stavros Sachtouris
        return r.json['meta' if key else 'metadata']
349 3dabe5d2 Stavros Sachtouris
350 ca5528f1 Stavros Sachtouris
    # def create_image_metadata(self, image_id, key, val):
351 ca5528f1 Stavros Sachtouris
    #     """
352 ca5528f1 Stavros Sachtouris
    #     :param image_id: integer (str or int)
353 a2ef112e Stavros Sachtouris
354 ca5528f1 Stavros Sachtouris
    #     :param key: (str) metadatum key
355 a2ef112e Stavros Sachtouris
356 ca5528f1 Stavros Sachtouris
    #     :param val: (str) metadatum value
357 a2ef112e Stavros Sachtouris
358 ca5528f1 Stavros Sachtouris
    #     :returns: (dict) updated metadata
359 ca5528f1 Stavros Sachtouris
    #     """
360 ca5528f1 Stavros Sachtouris
    #     req = {'meta': {key: val}}
361 ca5528f1 Stavros Sachtouris
    #     r = self.images_metadata_put(image_id, key, json_data=req)
362 ca5528f1 Stavros Sachtouris
    #     return r.json['meta']
363 a1c50326 Giorgos Verigakis
364 e51c7d5b Stavros Sachtouris
    def update_image_metadata(
365 e51c7d5b Stavros Sachtouris
            self, image_id,
366 e51c7d5b Stavros Sachtouris
            response_headers=dict(previous=None, next=None), **metadata):
367 a2ef112e Stavros Sachtouris
        """
368 a2ef112e Stavros Sachtouris
        :param image_id: (str)
369 a2ef112e Stavros Sachtouris

370 a2ef112e Stavros Sachtouris
        :param metadata: dict
371 a2ef112e Stavros Sachtouris

372 a2ef112e Stavros Sachtouris
        :returns: updated metadata
373 a2ef112e Stavros Sachtouris
        """
374 6a0b1658 Giorgos Verigakis
        req = {'metadata': metadata}
375 e51c7d5b Stavros Sachtouris
        r = self.images_metadata_post(image_id, json_data=req)
376 e51c7d5b Stavros Sachtouris
        for k, v in response_headers.items():
377 e51c7d5b Stavros Sachtouris
            response_headers[k] = r.headers.get(k, v)
378 6a0b1658 Giorgos Verigakis
        return r.json['metadata']
379 a1c50326 Giorgos Verigakis
380 a1c50326 Giorgos Verigakis
    def delete_image_metadata(self, image_id, key):
381 a2ef112e Stavros Sachtouris
        """
382 a2ef112e Stavros Sachtouris
        :param image_id: (str)
383 a2ef112e Stavros Sachtouris

384 a2ef112e Stavros Sachtouris
        :param key: (str) metadatum key
385 cd295a1d Stavros Sachtouris

386 cd295a1d Stavros Sachtouris
        :returns: (dict) response headers
387 a2ef112e Stavros Sachtouris
        """
388 6d6d5141 Stavros Sachtouris
        r = self.images_metadata_delete(image_id, key)
389 f5f35422 Stavros Sachtouris
        return r.headers
390 03033b54 Stavros Sachtouris
391 c77131f4 Stavros Sachtouris
    #  Extensions
392 c77131f4 Stavros Sachtouris
393 03033b54 Stavros Sachtouris
    def get_floating_ip_pools(self, tenant_id):
394 03033b54 Stavros Sachtouris
        """
395 03033b54 Stavros Sachtouris
        :param tenant_id: (str)
396 03033b54 Stavros Sachtouris

397 03033b54 Stavros Sachtouris
        :returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
398 03033b54 Stavros Sachtouris
        """
399 03033b54 Stavros Sachtouris
        r = self.floating_ip_pools_get(tenant_id)
400 03033b54 Stavros Sachtouris
        return r.json
401 03033b54 Stavros Sachtouris
402 03033b54 Stavros Sachtouris
    def get_floating_ips(self, tenant_id):
403 03033b54 Stavros Sachtouris
        """
404 03033b54 Stavros Sachtouris
        :param tenant_id: (str)
405 03033b54 Stavros Sachtouris

406 03033b54 Stavros Sachtouris
        :returns: (dict) {floating_ips:[
407 03033b54 Stavros Sachtouris
            {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
408 03033b54 Stavros Sachtouris
            ... ]}
409 03033b54 Stavros Sachtouris
        """
410 03033b54 Stavros Sachtouris
        r = self.floating_ips_get(tenant_id)
411 03033b54 Stavros Sachtouris
        return r.json
412 03033b54 Stavros Sachtouris
413 03033b54 Stavros Sachtouris
    def alloc_floating_ip(self, tenant_id, pool=None):
414 03033b54 Stavros Sachtouris
        """
415 03033b54 Stavros Sachtouris
        :param tenant_id: (str)
416 03033b54 Stavros Sachtouris

417 03033b54 Stavros Sachtouris
        :param pool: (str) pool of ips to allocate from
418 03033b54 Stavros Sachtouris

419 d252a7a8 Stavros Sachtouris
        :returns: (dict) {fixed_ip: . id: . instance_id: . ip: . pool: .}
420 03033b54 Stavros Sachtouris
        """
421 03033b54 Stavros Sachtouris
        json_data = dict(pool=pool) if pool else dict()
422 03033b54 Stavros Sachtouris
        r = self.floating_ips_post(tenant_id, json_data)
423 03033b54 Stavros Sachtouris
        return r.json['floating_ip']
424 03033b54 Stavros Sachtouris
425 03033b54 Stavros Sachtouris
    def get_floating_ip(self, tenant_id, fip_id=None):
426 03033b54 Stavros Sachtouris
        """
427 03033b54 Stavros Sachtouris
        :param tenant_id: (str)
428 03033b54 Stavros Sachtouris

429 03033b54 Stavros Sachtouris
        :param fip_id: (str) floating ip id (if None, all ips are returned)
430 03033b54 Stavros Sachtouris

431 03033b54 Stavros Sachtouris
        :returns: (list) [
432 03033b54 Stavros Sachtouris
            {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
433 03033b54 Stavros Sachtouris
            ... ]
434 03033b54 Stavros Sachtouris
        """
435 03033b54 Stavros Sachtouris
        r = self.floating_ips_get(tenant_id, fip_id)
436 03033b54 Stavros Sachtouris
        return r.json['floating_ips']
437 03033b54 Stavros Sachtouris
438 03033b54 Stavros Sachtouris
    def delete_floating_ip(self, tenant_id, fip_id=None):
439 03033b54 Stavros Sachtouris
        """
440 03033b54 Stavros Sachtouris
        :param tenant_id: (str)
441 03033b54 Stavros Sachtouris

442 03033b54 Stavros Sachtouris
        :param fip_id: (str) floating ip id (if None, all ips are deleted)
443 03033b54 Stavros Sachtouris

444 03033b54 Stavros Sachtouris
        :returns: (dict) request headers
445 03033b54 Stavros Sachtouris
        """
446 03033b54 Stavros Sachtouris
        r = self.floating_ips_delete(tenant_id, fip_id)
447 03033b54 Stavros Sachtouris
        return r.headers