Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / image.py @ c270fe96

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

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

    
39
    def __init__(self, base_url, token):
40
        super(ImageClient, self).__init__(base_url, token)
41

    
42
    def raise_for_status(self, r):
43
        if r.status_code == 404:
44
            raise ClientError("Image not found", r.status_code)
45

    
46
        # Fallback to the default
47
        super(ImageClient, self).raise_for_status(r)
48

    
49
    def list_public(self, detail=False, filters={}, order=''):
50
        path = path4url('images','detail') if detail else path4url('images/')
51

    
52
        if isinstance(filters, dict):
53
            self.http_client.params.update(filters)
54
        if order.startswith('-'):
55
            self.set_param('sort_dir', 'desc')
56
            order = order[1:]
57
        else:
58
            self.set_param('sort_dir', 'asc')
59
        self.set_param('sort_key', order, iff=order)
60

    
61
        r = self.get(path, success=200)
62
        return r.json
63

    
64
    def get_meta(self, image_id):
65
        path=path4url('images', image_id)
66
        r = self.head(path, success=200)
67

    
68
        reply = {}
69
        properties = {}
70
        meta_prefix = 'x-image-meta-'
71
        property_prefix = 'x-image-meta-property-'
72

    
73
        for key, val in r.headers.items():
74
            key = key.lower()
75
            if key.startswith(property_prefix):
76
                key = key[len(property_prefix):]
77
                properties[key] = val
78
            elif key.startswith(meta_prefix):
79
                key = key[len(meta_prefix):]
80
                reply[key] = val
81

    
82
        if properties:
83
            reply['properties'] = properties
84
        return reply
85

    
86
    def register(self, name, location, params={}, properties={}):
87
        path = path4url('images/')
88
        self.set_header('X-Image-Meta-Name', name)
89
        self.set_header('X-Image-Meta-Location', location)
90

    
91
        for key, val in params.items():
92
            if key in ('id', 'store', 'disk_format', 'container_format',
93
                       'size', 'checksum', 'is_public', 'owner'):
94
                key = 'x-image-meta-' + key.replace('_', '-')
95
                self.set_header(key, val)
96

    
97
        for key, val in properties.items():
98
            self.set_header('X-Image-Meta-Property-'+key, val)
99

    
100
        self.post(path, success=200)
101

    
102
    def list_members(self, image_id):
103
        path = path4url('images',image_id,'members')
104
        r = self.get(path, success=200)
105
        return r.json['members']
106

    
107
    def list_shared(self, member):
108
        path = path4url('shared-images', member)
109
        #self.set_param('format', 'json')
110
        r = self.get(path, success=200)
111
        return r.json['shared_images']
112

    
113
    def add_member(self, image_id, member):
114
        path = path4url('images', image_id, 'members', member)
115
        r = self.put(path, success=204)
116
        r.release()
117

    
118
    def remove_member(self, image_id, member):
119
        path = path4url('images', image_id, 'members', member)
120
        r = self.delete(path, success=204)
121
        r.release()
122

    
123
    def set_members(self, image_id, members):
124
        path = path4url('images', image_id, 'members')
125
        req = {'memberships': [{'member_id': member} for member in members]}
126
        r = self.put(path, json=req, success=204)
127
        r.release()