Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / compute.py @ ab13eafe

History | View | Annotate | Download (6.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

    
34
from . import Client, ClientError
35

    
36

    
37
class ComputeClient(Client):
38
    """OpenStack Compute API 1.1 client"""
39
    
40
    def raise_for_status(self, r):
41
        d = r.json
42
        if not d:
43
            return super(ComputeClient, self).raise_for_status(r)
44
        key = d.keys()[0]
45
        val = d[key]
46
        message = '%s: %s' % (key, val.get('message', ''))
47
        details = val.get('details', '')
48
        raise ClientError(message, r.status_code, details)
49
    
50
    def list_servers(self, detail=False):
51
        """List servers, returned detailed output if detailed is True"""
52
        
53
        path = '/servers/detail' if detail else '/servers'
54
        r = self.get(path, success=200)
55
        return r.json['servers']['values']
56
    
57
    def get_server_details(self, server_id):
58
        """Return detailed output on a server specified by its id"""
59
        
60
        path = '/servers/%s' % (server_id,)
61
        r = self.get(path, success=200)
62
        return r.json['server']
63
    
64
    def create_server(self, name, flavor_id, image_id, personality=None):
65
        """Submit request to create a new server
66

67
        The flavor_id specifies the hardware configuration to use,
68
        the image_id specifies the OS Image to be deployed inside the new
69
        server.
70

71
        The personality argument is a list of (file path, file contents)
72
        tuples, describing files to be injected into the server upon creation.
73

74
        The call returns a dictionary describing the newly created server.
75
        """
76
        req = {'server': {'name': name,
77
                          'flavorRef': flavor_id,
78
                          'imageRef': image_id}}
79
        if personality:
80
            req['server']['personality'] = personality
81
        
82
        r = self.post('/servers', json=req, success=202)
83
        return r.json['server']
84
    
85
    def update_server_name(self, server_id, new_name):
86
        """Update the name of the server as reported by the API.
87

88
        This call does not modify the hostname actually used by the server
89
        internally.
90
        """
91
        path = '/servers/%s' % (server_id,)
92
        req = {'server': {'name': new_name}}
93
        self.put(path, json=req, success=204)
94
    
95
    def delete_server(self, server_id):
96
        """Submit a deletion request for a server specified by id"""
97
        
98
        path = '/servers/%s' % (server_id,)
99
        self.delete(path, success=204)
100
    
101
    def reboot_server(self, server_id, hard=False):
102
        """Submit a reboot request for a server specified by id"""
103
        
104
        path = '/servers/%s/action' % (server_id,)
105
        type = 'HARD' if hard else 'SOFT'
106
        req = {'reboot': {'type': type}}
107
        self.post(path, json=req, success=202)
108
    
109
    def get_server_metadata(self, server_id, key=None):
110
        path = '/servers/%s/meta' % (server_id,)
111
        if key:
112
            path += '/%s' % key
113
        r = self.get(path, success=200)
114
        return r.json['meta'] if key else r.json['metadata']['values']
115
    
116
    def create_server_metadata(self, server_id, key, val):
117
        path = '/servers/%d/meta/%s' % (server_id, key)
118
        req = {'meta': {key: val}}
119
        r = self.put(path, json=req, success=201)
120
        return r.json['meta']
121
    
122
    def update_server_metadata(self, server_id, **metadata):
123
        path = '/servers/%d/meta' % (server_id,)
124
        req = {'metadata': metadata}
125
        r = self.post(path, json=req, success=201)
126
        return r.json['metadata']
127
    
128
    def delete_server_metadata(self, server_id, key):
129
        path = '/servers/%d/meta/%s' % (server_id, key)
130
        self.delete(path, success=204)
131
    
132
    
133
    def list_flavors(self, detail=False):
134
        path = '/flavors/detail' if detail else '/flavors'
135
        r = self.get(path, success=200)
136
        return r.json['flavors']['values']
137

    
138
    def get_flavor_details(self, flavor_id):
139
        path = '/flavors/%d' % flavor_id
140
        r = self.get(path, success=200)
141
        return r.json['flavor']
142
    
143
    
144
    def list_images(self, detail=False):
145
        path = '/images/detail' if detail else '/images'
146
        r = self.get(path, success=200)
147
        return r.json['images']['values']
148
    
149
    def get_image_details(self, image_id):
150
        path = '/images/%s' % (image_id,)
151
        r = self.get(path, success=200)
152
        return r.json['image']
153
    
154
    def delete_image(self, image_id):
155
        path = '/images/%s' % (image_id,)
156
        self.delete(path, success=204)
157

    
158
    def get_image_metadata(self, image_id, key=None):
159
        path = '/images/%s/meta' % (image_id,)
160
        if key:
161
            path += '/%s' % key
162
        r = self.get(path, success=200)
163
        return r.json['meta'] if key else r.json['metadata']['values']
164
    
165
    def create_image_metadata(self, image_id, key, val):
166
        path = '/images/%s/meta/%s' % (image_id, key)
167
        req = {'meta': {key: val}}
168
        r = self.put(path, json=req, success=201)
169
        return r.json['meta']
170

    
171
    def update_image_metadata(self, image_id, **metadata):
172
        path = '/images/%s/meta' % (image_id,)
173
        req = {'metadata': metadata}
174
        r = self.post(path, json=req, success=201)
175
        return r.json['metadata']
176

    
177
    def delete_image_metadata(self, image_id, key):
178
        path = '/images/%s/meta/%s' % (image_id, key)
179
        self.delete(path, success=204)