root / kamaki / clients / image / __init__.py @ 56ca8235
History | View | Annotate | Download (7.8 kB)
1 |
# Copyright 2011-2014 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from kamaki.clients import Client, ClientError |
35 |
from kamaki.clients.utils import path4url |
36 |
|
37 |
|
38 |
def _format_image_headers(headers): |
39 |
reply = dict(properties=dict()) |
40 |
meta_prefix = 'x-image-meta-'
|
41 |
property_prefix = 'x-image-meta-property-'
|
42 |
|
43 |
for key, val in headers.items(): |
44 |
key = key.lower() |
45 |
if key.startswith(property_prefix):
|
46 |
key = key[len(property_prefix):].upper().replace('-', '_') |
47 |
reply['properties'][key] = val
|
48 |
elif key.startswith(meta_prefix):
|
49 |
key = key[len(meta_prefix):]
|
50 |
reply[key] = val |
51 |
return reply
|
52 |
|
53 |
|
54 |
class ImageClient(Client): |
55 |
"""Synnefo Plankton API client"""
|
56 |
|
57 |
def __init__(self, base_url, token): |
58 |
super(ImageClient, self).__init__(base_url, token) |
59 |
self.response_header_prefices = ['X-Image-', ] |
60 |
|
61 |
def list_public(self, detail=False, filters={}, order=''): |
62 |
"""
|
63 |
:param detail: (bool)
|
64 |
|
65 |
:param filters: (dict) request filters
|
66 |
|
67 |
:param order: (str) order listing by field (default is ascending, - for
|
68 |
descending)
|
69 |
|
70 |
:returns: (list) id,name + full image info if detail
|
71 |
"""
|
72 |
path = path4url('images', 'detail') if detail else ( |
73 |
'%s/' % path4url('images')) |
74 |
|
75 |
async_params = {} |
76 |
if isinstance(filters, dict): |
77 |
for key, value in filters.items(): |
78 |
if value:
|
79 |
async_params[key] = value |
80 |
if order and order.startswith('-'): |
81 |
async_params['sort_dir'] = 'desc' |
82 |
order = order[1:]
|
83 |
else:
|
84 |
async_params['sort_dir'] = 'asc' |
85 |
if order:
|
86 |
async_params['sort_key'] = order
|
87 |
|
88 |
r = self.get(path, async_params=async_params, success=200) |
89 |
return r.json
|
90 |
|
91 |
def get_meta(self, image_id): |
92 |
"""
|
93 |
:param image_id: (string)
|
94 |
|
95 |
:returns: (list) image metadata (key:val)
|
96 |
"""
|
97 |
path = path4url('images', image_id)
|
98 |
r = self.head(path, success=200) |
99 |
|
100 |
return _format_image_headers(r.headers)
|
101 |
|
102 |
def register(self, name, location, params={}, properties={}): |
103 |
"""Register an image that is uploaded at location
|
104 |
|
105 |
:param name: (str)
|
106 |
|
107 |
:param location: (str or iterable) if iterable, then
|
108 |
(user_uuid, container, image_path) else if string
|
109 |
pithos://<user_uuid>/<container>/<image object>
|
110 |
|
111 |
:param params: (dict) image metadata (X-Image-Meta) can be id, store,
|
112 |
disc_format, container_format, size, checksum, is_public, owner
|
113 |
|
114 |
:param properties: (dict) image properties (X-Image-Meta-Property)
|
115 |
|
116 |
:returns: (dict) metadata of the created image
|
117 |
"""
|
118 |
path = '%s/' % path4url('images') |
119 |
self.set_header('X-Image-Meta-Name', name) |
120 |
location = location if (
|
121 |
isinstance(location, str) or isinstance(location, unicode)) else ( |
122 |
'pithos://%s' % '/'.join(location)) |
123 |
self.set_header('X-Image-Meta-Location', location) |
124 |
|
125 |
async_headers = {} |
126 |
for key, val in params.items(): |
127 |
if key in ('store', 'disk_format', 'container_format', |
128 |
'size', 'checksum', 'is_public', 'owner') and val: |
129 |
key = 'x-image-meta-' + key.replace('_', '-') |
130 |
async_headers[key] = val |
131 |
|
132 |
for key, val in properties.items(): |
133 |
async_headers['x-image-meta-property-%s' % key] = val
|
134 |
|
135 |
r = self.post(path, success=200, async_headers=async_headers) |
136 |
|
137 |
return _format_image_headers(r.headers)
|
138 |
|
139 |
def unregister(self, image_id): |
140 |
"""Unregister an image
|
141 |
|
142 |
:param image_id: (str)
|
143 |
|
144 |
:returns: (dict) response headers
|
145 |
"""
|
146 |
path = path4url('images', image_id)
|
147 |
r = self.delete(path, success=204) |
148 |
return r.headers
|
149 |
|
150 |
def list_members(self, image_id): |
151 |
"""
|
152 |
:param image_id: (str)
|
153 |
|
154 |
:returns: (list) users who can use current user's images
|
155 |
"""
|
156 |
path = path4url('images', image_id, 'members') |
157 |
r = self.get(path, success=200) |
158 |
return r.json['members'] |
159 |
|
160 |
def list_shared(self, member): |
161 |
"""
|
162 |
:param member: (str) sharers account
|
163 |
|
164 |
:returns: (list) images shared by member
|
165 |
"""
|
166 |
path = path4url('shared-images', member)
|
167 |
#self.set_param('format', 'json')
|
168 |
r = self.get(path, success=200) |
169 |
return r.json['shared_images'] |
170 |
|
171 |
def add_member(self, image_id, member): |
172 |
"""
|
173 |
:param image_id: (str)
|
174 |
|
175 |
:param member: (str) user to allow access to current user's images
|
176 |
"""
|
177 |
path = path4url('images', image_id, 'members', member) |
178 |
self.set_header('Content-Length', 0) |
179 |
r = self.put(path, success=204) |
180 |
return r.headers
|
181 |
|
182 |
def remove_member(self, image_id, member): |
183 |
"""
|
184 |
:param image_id: (str)
|
185 |
|
186 |
:param member: (str) user to deprive from current user's images
|
187 |
"""
|
188 |
path = path4url('images', image_id, 'members', member) |
189 |
r = self.delete(path, success=204) |
190 |
return r.headers
|
191 |
|
192 |
def set_members(self, image_id, members): |
193 |
"""
|
194 |
:param image_id: (str)
|
195 |
|
196 |
:param members: (list) user to deprive from current user's images
|
197 |
"""
|
198 |
path = path4url('images', image_id, 'members') |
199 |
req = {'memberships': [{'member_id': member} for member in members]} |
200 |
r = self.put(path, json=req, success=204) |
201 |
return r.headers
|
202 |
|
203 |
def update_image( |
204 |
self, image_id,
|
205 |
name=None, disk_format=None, container_format=None, |
206 |
status=None, public=None, owner_id=None, **properties): |
207 |
path = path4url('images', image_id)
|
208 |
if name is not None: |
209 |
self.set_header('X-Image-Meta-Name', name) |
210 |
if disk_format is not None: |
211 |
self.set_header('X-Image-Meta-Disk-Format', disk_format) |
212 |
if container_format is not None: |
213 |
self.set_header('X-Image-Meta-Container-Format', container_format) |
214 |
if status is not None: |
215 |
self.set_header('X-Image-Meta-Status', status) |
216 |
if public is not None: |
217 |
self.set_header('X-Image-Meta-Is-Public', bool(public)) |
218 |
if owner_id is not None: |
219 |
self.set_header('X-Image-Meta-Owner', owner_id) |
220 |
for k, v in properties.items(): |
221 |
self.set_header('X-Image-Meta-Property-%s' % k, v) |
222 |
self.set_header('Content-Length', 0) |
223 |
r = self.put(path, success=200) |
224 |
return r.headers
|