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