Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / image / __init__.py @ 8741c407

History | View | Annotate | Download (6.4 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 test(self):
45
        """
46
        :param image_id: (str)
47

48
        :param member: (str) user to allow access to current user's images
49
        """
50
        path = path4url('images')
51
        data = dict(stuff='stuff')
52
        r = self.put(path, json=data, async_headers={'x-image-meta-name': 'lalakis'})
53
        r.release()
54

    
55
    def list_public(self, detail=False, filters={}, order=''):
56
        """
57
        :param detail: (bool)
58

59
        :param filters: (dict) request filters
60

61
        :param order: (str) order listing by field (default is ascending, - for
62
            descending)
63

64
        :returns: (list) id,name + full image info if detail
65
        """
66
        path = path4url('images', 'detail') if detail else (
67
            path4url('images') + '/')
68

    
69
        async_params = {}
70
        if isinstance(filters, dict):
71
            for key, value in filters.items():
72
                if value:
73
                    async_params[key] = value
74
        if order.startswith('-'):
75
            async_params['sort_dir'] = 'desc'
76
            order = order[1:]
77
        else:
78
            async_params['sort_dir'] = 'asc'
79
        if order:
80
            async_params['sort_key'] = order
81

    
82
        r = self.get(path, async_params=async_params, success=200)
83
        return r.json
84

    
85
    def get_meta(self, image_id):
86
        """
87
        :param image_id: (string)
88

89
        :returns: (list) image metadata (key:val)
90
        """
91
        path = path4url('images', image_id)
92
        r = self.head(path, success=200)
93

    
94
        reply = {}
95
        properties = {}
96
        meta_prefix = 'x-image-meta-'
97
        property_prefix = 'x-image-meta-property-'
98

    
99
        for key, val in r.headers.items():
100
            key = key.lower()
101
            if key.startswith(property_prefix):
102
                key = key[len(property_prefix):]
103
                properties[key] = val
104
            elif key.startswith(meta_prefix):
105
                key = key[len(meta_prefix):]
106
                reply[key] = val
107

    
108
        if properties:
109
            reply['properties'] = properties
110
        return reply
111

    
112
    def register(self, name, location, params={}, properties={}):
113
        """Register image put at location
114

115
        :param name: (str)
116

117
        :param location: (str) pithos://<account>/<container>/<path>
118

119
        :param params: (dict) image metadata (X-Image-Meta) can be id, store,
120
            disc_format, container_format, size, checksum, is_public, owner
121

122
        :param properties: (dict) image properties (X-Image-Meta-Property)
123
        """
124
        path = path4url('images') + '/'
125
        self.set_header('X-Image-Meta-Name', name)
126
        self.set_header('X-Image-Meta-Location', location)
127

    
128
        async_headers = {}
129
        for key, val in params.items():
130
            if key in ('id', 'store', 'disk_format', 'container_format',
131
                       'size', 'checksum', 'is_public', 'owner'):
132
                key = 'x-image-meta-' + key.replace('_', '-')
133
                async_headers[key] = val
134

    
135
        for key, val in properties.items():
136
            async_headers['x-image-meta-property-%s' % key] = val
137

    
138
        r = self.post(path, success=200, async_headers=async_headers)
139
        r.release()
140

    
141
    def list_members(self, image_id):
142
        """
143
        :param image_id: (str)
144

145
        :returns: (list) users who can use current user's images
146
        """
147
        path = path4url('images', image_id, 'members')
148
        r = self.get(path, success=200)
149
        return r.json['members']
150

    
151
    def list_shared(self, member):
152
        """
153
        :param member: (str) sharers account
154

155
        :returns: (list) images shared by member
156
        """
157
        path = path4url('shared-images', member)
158
        #self.set_param('format', 'json')
159
        r = self.get(path, success=200)
160
        return r.json['shared_images']
161

    
162
    def add_member(self, image_id, member):
163
        """
164
        :param image_id: (str)
165

166
        :param member: (str) user to allow access to current user's images
167
        """
168
        path = path4url('images', image_id, 'members', member)
169
        r = self.put(path, success=204)
170
        r.release()
171

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

176
        :param member: (str) user to deprive from current user's images
177
        """
178
        path = path4url('images', image_id, 'members', member)
179
        r = self.delete(path, success=204)
180
        r.release()
181

    
182
    def set_members(self, image_id, members):
183
        """
184
        :param image_id: (str)
185

186
        :param members: (list) user to deprive from current user's images
187
        """
188
        path = path4url('images', image_id, 'members')
189
        req = {'memberships': [{'member_id': member} for member in members]}
190
        r = self.put(path, json=req, success=204)
191
        r.release()