root / kamaki / clients / image.py @ cd75ff39
History | View | Annotate | Download (4.5 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 |
|
34 |
|
35 |
from . import Client, ClientError
|
36 |
|
37 |
|
38 |
class ImageClient(Client): |
39 |
"""OpenStack Image Service API 1.0 and GRNET Plankton client"""
|
40 |
|
41 |
def raise_for_status(self, r): |
42 |
if r.status_code == 404: |
43 |
raise ClientError("Image not found", r.status_code) |
44 |
|
45 |
# Fallback to the default
|
46 |
super(ImageClient, self).raise_for_status(r) |
47 |
|
48 |
def list_public(self, detail=False, filters={}, order=''): |
49 |
path = '/images/detail' if detail else '/images/' |
50 |
params = {} |
51 |
params.update(filters) |
52 |
|
53 |
if order.startswith('-'): |
54 |
params['sort_dir'] = 'desc' |
55 |
order = order[1:]
|
56 |
else:
|
57 |
params['sort_dir'] = 'asc' |
58 |
|
59 |
if order:
|
60 |
params['sort_key'] = order
|
61 |
|
62 |
r = self.get(path, params=params, success=200) |
63 |
return r.json
|
64 |
|
65 |
def get_meta(self, image_id): |
66 |
path = '/images/%s' % (image_id,)
|
67 |
r = self.head(path, success=200) |
68 |
|
69 |
reply = {} |
70 |
properties = {} |
71 |
meta_prefix = 'x-image-meta-'
|
72 |
property_prefix = 'x-image-meta-property-'
|
73 |
|
74 |
for key, val in r.headers.items(): |
75 |
key = key.lower() |
76 |
if key.startswith(property_prefix):
|
77 |
key = key[len(property_prefix):]
|
78 |
properties[key] = val |
79 |
elif key.startswith(meta_prefix):
|
80 |
key = key[len(meta_prefix):]
|
81 |
reply[key] = val |
82 |
|
83 |
if properties:
|
84 |
reply['properties'] = properties
|
85 |
return reply
|
86 |
|
87 |
def register(self, name, location, params={}, properties={}): |
88 |
path = '/images/'
|
89 |
headers = {} |
90 |
headers['x-image-meta-name'] = name
|
91 |
headers['x-image-meta-location'] = location
|
92 |
|
93 |
for key, val in params.items(): |
94 |
if key in ('id', 'store', 'disk_format', 'container_format', |
95 |
'size', 'checksum', 'is_public', 'owner'): |
96 |
key = 'x-image-meta-' + key.replace('_', '-') |
97 |
headers[key] = val |
98 |
|
99 |
for key, val in properties.items(): |
100 |
headers['x-image-meta-property-' + key] = val
|
101 |
|
102 |
self.post(path, headers=headers, success=200) |
103 |
|
104 |
def list_members(self, image_id): |
105 |
path = '/images/%s/members' % (image_id,)
|
106 |
r = self.get(path, success=200) |
107 |
return r.json['members'] |
108 |
|
109 |
def list_shared(self, member): |
110 |
path = '/shared-images/%s' % (member,)
|
111 |
r = self.get(path, success=200) |
112 |
return r.json['shared_images'] |
113 |
|
114 |
def add_member(self, image_id, member): |
115 |
path = '/images/%s/members/%s' % (image_id, member)
|
116 |
r = self.put(path, success=204) |
117 |
|
118 |
def remove_member(self, image_id, member): |
119 |
path = '/images/%s/members/%s' % (image_id, member)
|
120 |
self.delete(path, success=204) |
121 |
|
122 |
def set_members(self, image_id, members): |
123 |
path = '/images/%s/members' % image_id
|
124 |
req = {'memberships': [{'member_id': member} for member in members]} |
125 |
self.put(path, json=req, success=204) |