root / kamaki / clients / image.py @ f5eac743
History | View | Annotate | Download (5.3 kB)
1 |
# Copyright 2011 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 |
from kamaki.clients import Client |
34 |
from kamaki.clients.utils import path4url |
35 |
|
36 |
|
37 |
class ImageClient(Client): |
38 |
"""OpenStack Image Service API 1.0 and GRNET Plankton client"""
|
39 |
|
40 |
def __init__(self, base_url, token): |
41 |
super(ImageClient, self).__init__(base_url, token) |
42 |
|
43 |
def list_public(self, detail=False, filters={}, order=''): |
44 |
"""
|
45 |
:param detail: (bool)
|
46 |
|
47 |
:param filters: (dict) request filters
|
48 |
|
49 |
:param order: (str) sort_dir|desc
|
50 |
|
51 |
:returns: (list) id,name + full image info if detail
|
52 |
"""
|
53 |
path = path4url('images', 'detail') if detail else path4url('images/') |
54 |
|
55 |
if isinstance(filters, dict): |
56 |
self.http_client.params.update(filters)
|
57 |
if order.startswith('-'): |
58 |
self.set_param('sort_dir', 'desc') |
59 |
order = order[1:]
|
60 |
else:
|
61 |
self.set_param('sort_dir', 'asc') |
62 |
self.set_param('sort_key', order, iff=order) |
63 |
|
64 |
r = self.get(path, success=200) |
65 |
return r.json
|
66 |
|
67 |
def get_meta(self, image_id): |
68 |
path = path4url('images', image_id)
|
69 |
r = self.head(path, success=200) |
70 |
|
71 |
reply = {} |
72 |
properties = {} |
73 |
meta_prefix = 'x-image-meta-'
|
74 |
property_prefix = 'x-image-meta-property-'
|
75 |
|
76 |
for key, val in r.headers.items(): |
77 |
key = key.lower() |
78 |
if key.startswith(property_prefix):
|
79 |
key = key[len(property_prefix):]
|
80 |
properties[key] = val |
81 |
elif key.startswith(meta_prefix):
|
82 |
key = key[len(meta_prefix):]
|
83 |
reply[key] = val |
84 |
|
85 |
if properties:
|
86 |
reply['properties'] = properties
|
87 |
return reply
|
88 |
|
89 |
def register(self, name, location, params={}, properties={}): |
90 |
path = path4url('images/')
|
91 |
self.set_header('X-Image-Meta-Name', name) |
92 |
self.set_header('X-Image-Meta-Location', location) |
93 |
|
94 |
for key, val in params.items(): |
95 |
if key in ('id', 'store', 'disk_format', 'container_format', |
96 |
'size', 'checksum', 'is_public', 'owner'): |
97 |
key = 'x-image-meta-' + key.replace('_', '-') |
98 |
self.set_header(key, val)
|
99 |
|
100 |
for key, val in properties.items(): |
101 |
self.set_header('X-Image-Meta-Property-%s' % key, val) |
102 |
|
103 |
r = self.post(path, success=200) |
104 |
r.release() |
105 |
|
106 |
def reregister(self, location, name=None, params={}, properties={}): |
107 |
path = path4url('images', 'detail') |
108 |
r = self.get(path, success=200) |
109 |
imgs = [img for img in r.json if img['location'] == location] |
110 |
for img in imgs: |
111 |
img_name = name if name else img['name'] |
112 |
img_properties = img['properties']
|
113 |
for k, v in properties.items(): |
114 |
img_properties[k] = v |
115 |
self.register(img_name, location, params, img_properties)
|
116 |
r.release() |
117 |
|
118 |
def list_members(self, image_id): |
119 |
path = path4url('images', image_id, 'members') |
120 |
r = self.get(path, success=200) |
121 |
return r.json['members'] |
122 |
|
123 |
def list_shared(self, member): |
124 |
path = path4url('shared-images', member)
|
125 |
#self.set_param('format', 'json')
|
126 |
r = self.get(path, success=200) |
127 |
return r.json['shared_images'] |
128 |
|
129 |
def add_member(self, image_id, member): |
130 |
path = path4url('images', image_id, 'members', member) |
131 |
r = self.put(path, success=204) |
132 |
r.release() |
133 |
|
134 |
def remove_member(self, image_id, member): |
135 |
path = path4url('images', image_id, 'members', member) |
136 |
r = self.delete(path, success=204) |
137 |
r.release() |
138 |
|
139 |
def set_members(self, image_id, members): |
140 |
path = path4url('images', image_id, 'members') |
141 |
req = {'memberships': [{'member_id': member} for member in members]} |
142 |
r = self.put(path, json=req, success=204) |
143 |
r.release() |