bee1f99d0e33deccfd6bf8f5cad0bbc5cbbb0adb
[kamaki] / kamaki / clients / cyclades.py
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 from .utils import path4url
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 = path4url('servers', server_id, 'action')
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 = path4url('servers', server_id, 'action')
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 = path4url('servers', server_id, 'action')
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            The server is specified by id, the profile argument
65            is one of (ENABLED, DISABLED, PROTECTED).
66         """
67         path = path4url('servers', server_id, 'action')
68         req = {'firewallProfile': {'profile': profile}}
69         self.post(path, json=req, success=202)
70     
71     def list_server_nics(self, server_id):
72         path = path4url('servers', server_id, 'ips')
73         r = self.get(path, success=200)
74         return r.json['addresses']['values']
75     
76     def get_server_stats(self, server_id):
77         path = path4url('servers', server_id, 'stats')
78         r = self.get(path, success=200)
79         return r.json['stats']
80     
81     def list_networks(self, detail=False):
82         path = path4url('networks', 'detail') if detail else path4url('networks')
83         r = self.get(path, success=200)
84         return r.json['networks']['values']
85
86     def create_network(self, name, cidr=False, gateway=False, type=False, dhcp=False):
87         """@params cidr, geteway, type and dhcp should be strings
88         """
89         net = dict(name=name)
90         if cidr:
91             net['cidr']=cidr
92         if gateway:
93             net['gateway']=gateway
94         if type:
95             net['type']=type
96         if dhcp:
97             net['dhcp']=dhcp
98         req = dict(network=net)
99         r = self.post(path4url('networks'), json=req, success=202)
100         return r.json['network']
101
102     def get_network_details(self, network_id):
103         path = path4url('networks', network_id)
104         r = self.get(path, success=200)
105         return r.json['network']
106
107     def update_network_name(self, network_id, new_name):
108         path = path4url('networks', network_id)
109         req = {'network': {'name': new_name}}
110         self.put(path, json=req, success=204)
111
112     def delete_network(self, network_id):
113         path = path4url('networks', network_id)
114         self.delete(path, success=204)
115
116     def connect_server(self, server_id, network_id):
117         path = path4url('networks', network_id, 'action')
118         req = {'add': {'serverRef': server_id}}
119         self.post(path, json=req, success=202)
120
121     def disconnect_server(self, server_id, nic_id):
122         server_nets = self.list_server_nics(server_id)
123         nets = [(net['id'],net['network_id'])  for net in server_nets if nic_id == net['id']]
124         for (nic_id, network_id) in nets:
125             path = path4url('networks', network_id, 'action')
126             req = dict(remove=dict(attachment=unicode(nic_id)))
127             self.post(path, json=req, success=202)