Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / compute.py @ 890e1a48

History | View | Annotate | Download (6.6 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
        key = d.keys()[0]
43
        val = d[key]
44
        message = '%s: %s' % (key, val.get('message', ''))
45
        details = val.get('details', '')
46
        raise ClientError(message, r.status_code, details)
47
    
48
    def list_servers(self, detail=False):
49
        """List servers, returned detailed output if detailed is True"""
50
        
51
        path = '/servers/detail' if detail else '/servers'
52
        r = self.get(path, success=200)
53
        return r.json['servers']['values']
54
    
55
    def get_server_details(self, server_id):
56
        """Return detailed output on a server specified by its id"""
57
        
58
        path = '/servers/%s' % (server_id,)
59
        r = self.get(path, success=200)
60
        return r.json['server']
61
    
62
    def create_server(self, name, flavor_id, image_id, personality=None):
63
        """Submit request to create a new server
64

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

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

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

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

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

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

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

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