Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / image.py @ 58a7ac27

History | View | Annotate | Download (6.9 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
from kamaki.clients import Client, ClientError
35
from kamaki.clients.utils import path4url
36

    
37

    
38
class ImageClient(Client):
39
    """OpenStack Image Service API 1.0 and GRNET Plankton client"""
40

    
41
    def __init__(self, base_url, token):
42
        super(ImageClient, self).__init__(base_url, token)
43

    
44
    def list_public(self, detail=False, filters={}, order=''):
45
        """
46
        :param detail: (bool)
47

48
        :param filters: (dict) request filters
49

50
        :param order: (str) order listing by field (default is ascending, - for
51
            descending)
52

53
        :returns: (list) id,name + full image info if detail
54
        """
55
        path = path4url('images', 'detail') if detail else (
56
            path4url('images') + '/')
57

    
58
        async_params = {}
59
        if isinstance(filters, dict):
60
            for key, value in filters.items():
61
                if value:
62
                    async_params[key] = value
63
        if order.startswith('-'):
64
            self.set_param('sort_dir', 'desc')
65
            order = order[1:]
66
        else:
67
            self.set_param('sort_dir', 'asc')
68
        self.set_param('sort_key', order, iff=order)
69

    
70
        r = self.get(path, async_params=async_params, success=200)
71
        return r.json
72

    
73
    def get_meta(self, image_id):
74
        """
75
        :param image_id: (string)
76

77
        :returns: (list) image metadata (key:val)
78
        """
79
        path = path4url('images', image_id)
80
        r = self.head(path, success=200)
81

    
82
        reply = {}
83
        properties = {}
84
        meta_prefix = 'x-image-meta-'
85
        property_prefix = 'x-image-meta-property-'
86

    
87
        for key, val in r.headers.items():
88
            key = key.lower()
89
            if key.startswith(property_prefix):
90
                key = key[len(property_prefix):]
91
                properties[key] = val
92
            elif key.startswith(meta_prefix):
93
                key = key[len(meta_prefix):]
94
                reply[key] = val
95

    
96
        if properties:
97
            reply['properties'] = properties
98
        return reply
99

    
100
    def register(self, name, location, params={}, properties={}):
101
        """Register image put at location
102

103
        :param name: (str)
104

105
        :param location: (str) pithos://<account>/<container>/<path>
106

107
        :param params: (dict) image metadata (X-Image-Meta) can be id, store,
108
            disc_format, container_format, size, checksum, is_public, owner
109

110
        :param properties: (dict) image properties (X-Image-Meta-Property)
111
        """
112
        path = path4url('images') + '/'
113
        self.set_header('X-Image-Meta-Name', name)
114
        self.set_header('X-Image-Meta-Location', location)
115

    
116
        for key, val in params.items():
117
            if key in ('id', 'store', 'disk_format', 'container_format',
118
                       'size', 'checksum', 'is_public', 'owner'):
119
                key = 'x-image-meta-' + key.replace('_', '-')
120
                self.set_header(key, val)
121

    
122
        for key, val in properties.items():
123
            self.set_header('X-Image-Meta-Property-%s' % key, val)
124

    
125
        r = self.post(path, success=200)
126
        r.release()
127

    
128
    def reregister(self, location, name=None, params={}, properties={}):
129
        """Update existing image (key: location)
130

131
        :param location: (str) pithos://<account>/<container>/<path>
132

133
        :param name: (str)
134

135
        :param params: (dict) image metadata (X-Image-Meta) can be id, store,
136
            disc_format, container_format, size, checksum, is_public, owner
137

138
        :param properties: (dict) image properties (X-Image-Meta-Property)
139
        """
140
        path = path4url('images', 'detail')
141
        r = self.get(path, success=200)
142
        imgs = [img for img in r.json if img['location'] == location]
143
        for img in imgs:
144
            img_name = name if name else img['name']
145
            img_properties = img['properties']
146
            for k, v in properties.items():
147
                img_properties[k] = v
148
            self.register(img_name, location, params, img_properties)
149
        r.release()
150

    
151
    def list_members(self, image_id):
152
        """
153
        :param image_id: (str)
154

155
        :returns: (list) users who can use current user's images
156
        """
157
        path = path4url('images', image_id, 'members')
158
        r = self.get(path, success=200)
159
        return r.json['members']
160

    
161
    def list_shared(self, member):
162
        """
163
        :param member: (str) sharers account
164

165
        :returns: (list) images shared by member
166
        """
167
        path = path4url('shared-images', member)
168
        #self.set_param('format', 'json')
169
        r = self.get(path, success=200)
170
        return r.json['shared_images']
171

    
172
    def add_member(self, image_id, member):
173
        """
174
        :param image_id: (str)
175

176
        :param member: (str) user to allow access to current user's images
177
        """
178
        path = path4url('images', image_id, 'members', member)
179
        r = self.put(path, success=204)
180
        r.release()
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
        r.release()
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
        r.release()