root / kamaki / clients / image.py @ b3b32add
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 |
OpenStack Image Service API 1.0 client
|
36 |
"""
|
37 |
|
38 |
from urllib import quote |
39 |
|
40 |
from . import ClientError
|
41 |
from .http import HTTPClient |
42 |
|
43 |
|
44 |
class ImageClient(HTTPClient): |
45 |
@property
|
46 |
def url(self): |
47 |
url = self.config.get('image_url') or self.config.get('url') |
48 |
if not url: |
49 |
raise ClientError('No URL was given') |
50 |
return url
|
51 |
|
52 |
@property
|
53 |
def token(self): |
54 |
token = self.config.get('image_token') or self.config.get('token') |
55 |
if not token: |
56 |
raise ClientError('No token was given') |
57 |
return token
|
58 |
|
59 |
def list_public(self, detail=False, filters={}, order=''): |
60 |
path = '/images/detail' if detail else '/images/' |
61 |
params = {} |
62 |
params.update(filters) |
63 |
|
64 |
if order.startswith('-'): |
65 |
params['sort_dir'] = 'desc' |
66 |
order = order[1:]
|
67 |
else:
|
68 |
params['sort_dir'] = 'asc' |
69 |
|
70 |
if order:
|
71 |
params['sort_key'] = order
|
72 |
|
73 |
if params:
|
74 |
path += '?' + '&'.join('%s=%s' % item for item in params.items()) |
75 |
return self.http_get(path) |
76 |
|
77 |
def get_meta(self, image_id): |
78 |
path = '/images/%s' % image_id
|
79 |
resp, buf = self.raw_http_cmd('HEAD', path) |
80 |
reply = {} |
81 |
prefix = 'x-image-meta-'
|
82 |
for key, val in resp.getheaders(): |
83 |
key = key.lower() |
84 |
if not key.startswith(prefix): |
85 |
continue
|
86 |
key = key[len(prefix):]
|
87 |
reply[key] = val |
88 |
return reply
|
89 |
|
90 |
def register(self, name, location, params={}, properties={}): |
91 |
path = '/images/'
|
92 |
headers = {} |
93 |
headers['x-image-meta-name'] = quote(name)
|
94 |
headers['x-image-meta-location'] = location
|
95 |
for key, val in params.items(): |
96 |
if key in ('id', 'store', 'disk_format', 'container_format', |
97 |
'size', 'checksum', 'is_public', 'owner'): |
98 |
key = 'x-image-meta-' + key.replace('_', '-') |
99 |
headers[key] = val |
100 |
for key, val in properties.items(): |
101 |
headers['x-image-meta-property-' + quote(key)] = quote(val)
|
102 |
return self.http_post(path, headers=headers, success=200) |
103 |
|
104 |
def list_members(self, image_id): |
105 |
path = '/images/%s/members' % image_id
|
106 |
reply = self.http_get(path)
|
107 |
return reply['members'] |
108 |
|
109 |
def list_shared(self, member): |
110 |
path = '/shared-images/%s' % member
|
111 |
reply = self.http_get(path)
|
112 |
return reply['shared_images'] |
113 |
|
114 |
def add_member(self, image_id, member): |
115 |
path = '/images/%s/members/%s' % (image_id, member)
|
116 |
self.http_put(path)
|
117 |
|
118 |
def remove_member(self, image_id, member): |
119 |
path = '/images/%s/members/%s' % (image_id, member)
|
120 |
self.http_delete(path)
|
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 |
body = json.dumps(req) |
126 |
self.http_put(path, body)
|