Revision 3dabe5d2 kamaki/clients/cyclades.py
b/kamaki/clients/cyclades.py | ||
---|---|---|
35 | 35 |
from kamaki.clients.utils import path4url |
36 | 36 |
import json |
37 | 37 |
|
38 |
|
|
38 | 39 |
class CycladesClient(ComputeClient): |
39 | 40 |
"""GRNet Cyclades API client""" |
40 | 41 |
|
41 |
def networks_get(self, network_id = '', command='', **kwargs):
|
|
42 |
def networks_get(self, network_id='', command='', **kwargs):
|
|
42 | 43 |
"""GET base_url/networks[/network_id][/command] request |
43 | 44 |
@param network_id or '' |
44 | 45 |
@param command can be 'detail', or '' |
45 | 46 |
""" |
46 |
path=path4url('networks', network_id, command)
|
|
47 |
path = path4url('networks', network_id, command)
|
|
47 | 48 |
success = kwargs.pop('success', (200, 203)) |
48 | 49 |
return self.get(path, success=success, **kwargs) |
49 | 50 |
|
50 |
def networks_delete(self, network_id = '', command='', **kwargs):
|
|
51 |
def networks_delete(self, network_id='', command='', **kwargs):
|
|
51 | 52 |
"""DEL ETE base_url/networks[/network_id][/command] request |
52 | 53 |
@param network_id or '' |
53 | 54 |
@param command can be 'detail', or '' |
54 | 55 |
""" |
55 |
path=path4url('networks', network_id, command)
|
|
56 |
path = path4url('networks', network_id, command)
|
|
56 | 57 |
success = kwargs.pop('success', 204) |
57 | 58 |
return self.delete(path, success=success, **kwargs) |
58 | 59 |
|
59 |
def networks_post(self, network_id = '', command='', json_data=None, **kwargs): |
|
60 |
def networks_post(self, |
|
61 |
network_id='', |
|
62 |
command='', |
|
63 |
json_data=None, |
|
64 |
**kwargs): |
|
60 | 65 |
"""POST base_url/servers[/server_id]/[command] request |
61 | 66 |
@param server_id or '' |
62 | 67 |
@param command: can be 'action' or '' |
63 | 68 |
@param json_data: a json valid dict that will be send as data |
64 | 69 |
""" |
65 |
data= json_data |
|
70 |
data = json_data
|
|
66 | 71 |
if json_data is not None: |
67 | 72 |
data = json.dumps(json_data) |
68 | 73 |
self.set_header('Content-Type', 'application/json') |
... | ... | |
72 | 77 |
success = kwargs.pop('success', 202) |
73 | 78 |
return self.post(path, data=data, success=success, **kwargs) |
74 | 79 |
|
75 |
def networks_put(self, network_id = '', command='', json_data=None, **kwargs): |
|
80 |
def networks_put(self, |
|
81 |
network_id='', |
|
82 |
command='', |
|
83 |
json_data=None, |
|
84 |
**kwargs): |
|
76 | 85 |
"""PUT base_url/servers[/server_id]/[command] request |
77 | 86 |
@param server_id or '' |
78 | 87 |
@param command: can be 'action' or '' |
79 | 88 |
@param json_data: a json valid dict that will be send as data |
80 | 89 |
""" |
81 |
data= json_data |
|
90 |
data = json_data
|
|
82 | 91 |
if json_data is not None: |
83 | 92 |
data = json.dumps(json_data) |
84 | 93 |
self.set_header('Content-Type', 'application/json') |
... | ... | |
99 | 108 |
req = {'shutdown': {}} |
100 | 109 |
r = self.servers_post(server_id, 'action', json_data=req, success=202) |
101 | 110 |
r.release() |
102 |
|
|
111 |
|
|
103 | 112 |
def get_server_console(self, server_id): |
104 | 113 |
"""Get a VNC connection to the console of a server specified by id""" |
105 | 114 |
req = {'console': {'type': 'vnc'}} |
... | ... | |
112 | 121 |
return r['attachments']['values'][0]['firewallProfile'] |
113 | 122 |
except KeyError: |
114 | 123 |
raise ClientError('No Firewall Profile', 520, |
115 |
details='Server %s is missing a firewall profile'%server_id)
|
|
124 |
details='Server %s is missing a firewall profile' % server_id)
|
|
116 | 125 |
|
117 | 126 |
def set_firewall_profile(self, server_id, profile): |
118 | 127 |
"""Set the firewall profile for the public interface of a server |
... | ... | |
122 | 131 |
req = {'firewallProfile': {'profile': profile}} |
123 | 132 |
r = self.servers_post(server_id, 'action', json_data=req, success=202) |
124 | 133 |
r.release() |
125 |
|
|
134 |
|
|
126 | 135 |
def list_server_nics(self, server_id): |
127 | 136 |
r = self.servers_get(server_id, 'ips') |
128 | 137 |
return r.json['addresses']['values'] |
... | ... | |
130 | 139 |
def get_server_stats(self, server_id): |
131 | 140 |
r = self.servers_get(server_id, 'stats') |
132 | 141 |
return r.json['stats'] |
133 |
|
|
142 |
|
|
134 | 143 |
def list_networks(self, detail=False): |
135 | 144 |
detail = 'detail' if detail else '' |
136 | 145 |
r = self.networks_get(command=detail) |
... | ... | |
141 | 150 |
r = self.networks_get(network_id=network_id) |
142 | 151 |
return r.json['network']['attachments']['values'] |
143 | 152 |
|
144 |
def create_network(self, name, cidr=False, gateway=False, type=False, dhcp=False): |
|
153 |
def create_network(self, |
|
154 |
name, |
|
155 |
cidr=False, |
|
156 |
gateway=False, |
|
157 |
type=False, |
|
158 |
dhcp=False): |
|
145 | 159 |
"""@params cidr, geteway, type and dhcp should be strings |
146 | 160 |
""" |
147 | 161 |
net = dict(name=name) |
148 | 162 |
if cidr: |
149 |
net['cidr']=cidr
|
|
163 |
net['cidr'] = cidr
|
|
150 | 164 |
if gateway: |
151 |
net['gateway']=gateway
|
|
165 |
net['gateway'] = gateway
|
|
152 | 166 |
if type: |
153 |
net['type']=type
|
|
167 |
net['type'] = type
|
|
154 | 168 |
if dhcp: |
155 |
net['dhcp']=dhcp
|
|
169 |
net['dhcp'] = dhcp
|
|
156 | 170 |
req = dict(network=net) |
157 | 171 |
r = self.networks_post(json_data=req, success=202) |
158 | 172 |
return r.json['network'] |
... | ... | |
171 | 185 |
r = self.networks_delete(network_id) |
172 | 186 |
except ClientError as err: |
173 | 187 |
if err.status == 421: |
174 |
err.details='Network may be still connected to at least one server' |
|
188 |
err.details =\ |
|
189 |
'Network may be still connected to at least one server' |
|
175 | 190 |
raise err |
176 | 191 |
r.release() |
177 | 192 |
|
... | ... | |
182 | 197 |
|
183 | 198 |
def disconnect_server(self, server_id, nic_id): |
184 | 199 |
server_nets = self.list_server_nics(server_id) |
185 |
nets = [(net['id'],net['network_id']) for net in server_nets if nic_id == net['id']] |
|
200 |
nets = [(net['id'], net['network_id']) for net in server_nets\ |
|
201 |
if nic_id == net['id']] |
|
186 | 202 |
if len(nets) == 0: |
187 | 203 |
return |
188 | 204 |
for (nic_id, network_id) in nets: |
189 |
req={'remove':{'attachment':unicode(nic_id)}}
|
|
205 |
req = {'remove': {'attachment': unicode(nic_id)}}
|
|
190 | 206 |
r = self.networks_post(network_id, 'action', json_data=req) |
191 | 207 |
r.release() |
192 | 208 |
|
Also available in: Unified diff