1 # Copyright 2011 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
34 from .compute import ComputeClient, ClientError
35 from .utils import path4url
38 class CycladesClient(ComputeClient):
39 """GRNet Cyclades API client"""
41 def servers_get(self, server_id='', command='', **kwargs):
42 """GET base_url/servers[/server_id][/command] request
43 @param server_id or ''
44 @param command: can be 'ips', 'stats', or ''
46 path = path4url('servers', server_id, command)
47 success = kwargs.pop('success', 200)
48 return self.get(path, success=success, **kwargs)
50 def servers_post(self, server_id='', command='', json_data=None, **kwargs):
51 """POST base_url/servers[/server_id]/[command] request
52 @param server_id or ''
53 @param command: can be 'action' or ''
54 @param json_data: a json valid dict that will be send as data
57 if json_data is not None:
58 data = json.dumps(json_data)
59 self.set_header('Content-Type', 'application/json')
60 self.set_header('Content-Length', len(data))
62 path = path4url('servers', server_id, command)
63 success = kwargs.pop('success', 202)
64 return self.post(path, data=data, success=success, **kwargs)
67 def networks_get(self, network_id = '', command='', **kwargs):
68 """GET base_url/networks[/network_id][/command] request
69 @param network_id or ''
70 @param command can be 'detail', or ''
72 path=path4url('networks', network_id, command)
73 success = kwargs.pop('success', (200, 203))
74 return self.get(path, success=success, **kwargs)
76 def networks_delete(self, network_id = '', command='', **kwargs):
77 """DEL ETE base_url/networks[/network_id][/command] request
78 @param network_id or ''
79 @param command can be 'detail', or ''
81 path=path4url('networks', network_id, command)
82 success = kwargs.pop('success', 204)
83 return self.delete(path, success=success, **kwargs)
85 def networks_post(self, network_id = '', command='', json_data=None, **kwargs):
86 """POST base_url/servers[/server_id]/[command] request
87 @param server_id or ''
88 @param command: can be 'action' or ''
89 @param json_data: a json valid dict that will be send as data
92 if json_data is not None:
93 data = json.dumps(json_data)
94 self.set_header('Content-Type', 'application/json')
95 self.set_header('Content-Length', len(data))
97 path = path4url('networks', network_id, command)
98 success = kwargs.pop('success', 202)
99 return self.post(path, data=data, success=success, **kwargs)
101 def networks_put(self, network_id = '', command='', json_data=None, **kwargs):
102 """PUT base_url/servers[/server_id]/[command] request
103 @param server_id or ''
104 @param command: can be 'action' or ''
105 @param json_data: a json valid dict that will be send as data
108 if json_data is not None:
109 data = json.dumps(json_data)
110 self.set_header('Content-Type', 'application/json')
111 self.set_header('Content-Length', len(data))
113 path = path4url('networks', network_id, command)
114 success = kwargs.pop('success', 204)
115 return self.put(path, data=data, success=success, **kwargs)
117 def start_server(self, server_id):
118 """Submit a startup request for a server specified by id"""
120 self.servers_post(server_id, 'action', json_data=req, success=202)
122 def shutdown_server(self, server_id):
123 """Submit a shutdown request for a server specified by id"""
124 req = {'shutdown': {}}
125 self.servers_post(server_id, 'action', json_data=req, success=202)
127 def get_server_console(self, server_id):
128 """Get a VNC connection to the console of a server specified by id"""
129 req = {'console': {'type': 'vnc'}}
130 r = self.servers_post(server_id, 'action', json_data=req, success=200)
131 return r.json['console']
133 def set_firewall_profile(self, server_id, profile):
134 """Set the firewall profile for the public interface of a server
135 The server is specified by id, the profile argument
136 is one of (ENABLED, DISABLED, PROTECTED).
138 req = {'firewallProfile': {'profile': profile}}
139 self.servers_post(server_id, 'action', json_data=req, success=202)
141 def list_server_nics(self, server_id):
142 r = self.servers_get(server_id, 'ips')
143 return r.json['addresses']['values']
145 def get_server_stats(self, server_id):
146 r = self.servers_get(server_id, 'stats')
147 return r.json['stats']
149 def list_networks(self, detail=False):
150 detail = 'detail' if detail else ''
151 r = self.networks_get(command=detail)
152 return r.json['networks']['values']
154 def create_network(self, name, cidr=False, gateway=False, type=False, dhcp=False):
155 """@params cidr, geteway, type and dhcp should be strings
157 print('cidr[%s], type[%s]'%(cidr, type))
158 net = dict(name=name)
162 net['gateway']=gateway
167 req = dict(network=net)
168 r = self.networks_post(json_data=req, success=202)
169 return r.json['network']
171 def get_network_details(self, network_id):
172 r = self.networks_get(network_id=network_id)
173 return r.json['network']
175 def update_network_name(self, network_id, new_name):
176 req = {'network': {'name': new_name}}
177 self.networks_put(network_id=network_id, json_data=req)
179 def delete_network(self, network_id):
180 self.networks_delete(network_id)
182 def connect_server(self, server_id, network_id):
183 req = {'add': {'serverRef': server_id}}
184 self.networks_post(network_id, 'action', json_data=req)
186 def disconnect_server(self, server_id, nic_id):
187 server_nets = self.list_server_nics(server_id)
188 nets = [(net['id'],net['network_id']) for net in server_nets if nic_id == net['id']]
191 for (nic_id, network_id) in nets:
192 req={'remove':{'attachment':unicode(nic_id)}}
193 self.networks_post(network_id, 'action', json_data=req)