root / kamaki / clients / cyclades.py @ 6c62a96d
History | View | Annotate | Download (4.5 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 .compute import ComputeClient |
35 |
|
36 |
|
37 |
class CycladesClient(ComputeClient): |
38 |
"""GRNet Cyclades API client"""
|
39 |
|
40 |
def start_server(self, server_id): |
41 |
"""Submit a startup request for a server specified by id"""
|
42 |
|
43 |
path = '/servers/%s/action' % (server_id,)
|
44 |
req = {'start': {}}
|
45 |
self.post(path, json=req, success=202) |
46 |
|
47 |
def shutdown_server(self, server_id): |
48 |
"""Submit a shutdown request for a server specified by id"""
|
49 |
|
50 |
path = '/servers/%s/action' % (server_id,)
|
51 |
req = {'shutdown': {}}
|
52 |
self.post(path, json=req, success=202) |
53 |
|
54 |
def get_server_console(self, server_id): |
55 |
"""Get a VNC connection to the console of a server specified by id"""
|
56 |
|
57 |
path = '/servers/%s/action' % (server_id,)
|
58 |
req = {'console': {'type': 'vnc'}} |
59 |
r = self.post(path, json=req, success=200) |
60 |
return r.json['console'] |
61 |
|
62 |
def set_firewall_profile(self, server_id, profile): |
63 |
"""Set the firewall profile for the public interface of a server
|
64 |
|
65 |
The server is specified by id, the profile argument
|
66 |
is one of (ENABLED, DISABLED, PROTECTED).
|
67 |
"""
|
68 |
path = '/servers/%s/action' % (server_id,)
|
69 |
req = {'firewallProfile': {'profile': profile}} |
70 |
self.post(path, json=req, success=202) |
71 |
|
72 |
def list_server_addresses(self, server_id, network=None): |
73 |
path = '/servers/%s/ips' % (server_id,)
|
74 |
if network:
|
75 |
path += '/%s' % network
|
76 |
r = self.get(path, success=200) |
77 |
if network:
|
78 |
return [r.json['network']] |
79 |
else:
|
80 |
return r.json['addresses']['values'] |
81 |
|
82 |
def get_server_stats(self, server_id): |
83 |
path = '/servers/%s/stats' % (server_id,)
|
84 |
r = self.get(path, success=200) |
85 |
return r.json['stats'] |
86 |
|
87 |
|
88 |
def list_networks(self, detail=False): |
89 |
path = '/networks/detail' if detail else '/networks' |
90 |
r = self.get(path, success=200) |
91 |
return r.json['networks']['values'] |
92 |
|
93 |
def create_network(self, name): |
94 |
req = {'network': {'name': name}} |
95 |
r = self.post('/networks', json=req, success=202) |
96 |
return r.json['network'] |
97 |
|
98 |
def get_network_details(self, network_id): |
99 |
path = '/networks/%s' % (network_id,)
|
100 |
r = self.get(path, success=200) |
101 |
return r.json['network'] |
102 |
|
103 |
def update_network_name(self, network_id, new_name): |
104 |
path = '/networks/%s' % (network_id,)
|
105 |
req = {'network': {'name': new_name}} |
106 |
self.put(path, json=req, success=204) |
107 |
|
108 |
def delete_network(self, network_id): |
109 |
path = '/networks/%s' % (network_id,)
|
110 |
self.delete(path, success=204) |
111 |
|
112 |
def connect_server(self, server_id, network_id): |
113 |
path = '/networks/%s/action' % (network_id,)
|
114 |
req = {'add': {'serverRef': server_id}} |
115 |
self.post(path, json=req, success=202) |
116 |
|
117 |
def disconnect_server(self, server_id, network_id): |
118 |
path = '/networks/%s/action' % (network_id,)
|
119 |
req = {'remove': {'serverRef': server_id}} |
120 |
self.post(path, json=req, success=202) |