40 |
40 |
"""GRNet Cyclades API client"""
|
41 |
41 |
|
42 |
42 |
def start_server(self, server_id):
|
43 |
|
"""Submit a startup request for a server specified by id"""
|
|
43 |
"""Submit a startup request
|
|
44 |
|
|
45 |
:param server_id: integer (str or int)
|
|
46 |
"""
|
44 |
47 |
req = {'start': {}}
|
45 |
48 |
r = self.servers_post(server_id, 'action', json_data=req, success=202)
|
46 |
49 |
r.release()
|
47 |
50 |
|
48 |
51 |
def shutdown_server(self, server_id):
|
49 |
|
"""Submit a shutdown request for a server specified by id"""
|
|
52 |
"""Submit a shutdown request
|
|
53 |
|
|
54 |
:param server_id: integer (str or int)
|
|
55 |
"""
|
50 |
56 |
req = {'shutdown': {}}
|
51 |
57 |
r = self.servers_post(server_id, 'action', json_data=req, success=202)
|
52 |
58 |
r.release()
|
53 |
59 |
|
54 |
60 |
def get_server_console(self, server_id):
|
55 |
|
"""Get a VNC connection to the console of a server specified by id"""
|
|
61 |
"""
|
|
62 |
:param server_id: integer (str or int)
|
|
63 |
|
|
64 |
:returns: (dict) info to set a VNC connection to VM
|
|
65 |
"""
|
56 |
66 |
req = {'console': {'type': 'vnc'}}
|
57 |
67 |
r = self.servers_post(server_id, 'action', json_data=req, success=200)
|
58 |
68 |
return r.json['console']
|
59 |
69 |
|
60 |
70 |
def get_firewall_profile(self, server_id):
|
|
71 |
"""
|
|
72 |
:param server_id: integer (str or int)
|
|
73 |
|
|
74 |
:returns: (str) ENABLED | DISABLED | PROTECTED
|
|
75 |
|
|
76 |
:raises ClientError: 520 No Firewall Profile
|
|
77 |
"""
|
61 |
78 |
r = self.get_server_details(server_id)
|
62 |
79 |
try:
|
63 |
80 |
return r['attachments']['values'][0]['firewallProfile']
|
... | ... | |
67 |
84 |
|
68 |
85 |
def set_firewall_profile(self, server_id, profile):
|
69 |
86 |
"""Set the firewall profile for the public interface of a server
|
70 |
|
The server is specified by id, the profile argument
|
71 |
|
is one of (ENABLED, DISABLED, PROTECTED).
|
|
87 |
|
|
88 |
:param server_id: integer (str or int)
|
|
89 |
|
|
90 |
:param profile: (str) ENABLED | DISABLED | PROTECTED
|
72 |
91 |
"""
|
73 |
92 |
req = {'firewallProfile': {'profile': profile}}
|
74 |
93 |
r = self.servers_post(server_id, 'action', json_data=req, success=202)
|
75 |
94 |
r.release()
|
76 |
95 |
|
77 |
96 |
def list_server_nics(self, server_id):
|
|
97 |
"""
|
|
98 |
:param server_id: integer (str or int)
|
|
99 |
|
|
100 |
:returns: (dict) network interface connections
|
|
101 |
"""
|
78 |
102 |
r = self.servers_get(server_id, 'ips')
|
79 |
103 |
return r.json['addresses']['values']
|
80 |
104 |
|
81 |
105 |
def get_server_stats(self, server_id):
|
|
106 |
"""
|
|
107 |
:param server_id: integer (str or int)
|
|
108 |
|
|
109 |
:returns: (dict) auto-generated graphs of statistics (urls)
|
|
110 |
"""
|
82 |
111 |
r = self.servers_get(server_id, 'stats')
|
83 |
112 |
return r.json['stats']
|
84 |
113 |
|
85 |
114 |
def list_networks(self, detail=False):
|
|
115 |
"""
|
|
116 |
:param detail: (bool)
|
|
117 |
|
|
118 |
:returns: (list) id,name if not detail else full info per network
|
|
119 |
"""
|
86 |
120 |
detail = 'detail' if detail else ''
|
87 |
121 |
r = self.networks_get(command=detail)
|
88 |
122 |
return r.json['networks']['values']
|
89 |
123 |
|
90 |
|
#NEW
|
91 |
124 |
def list_network_nics(self, network_id):
|
|
125 |
"""
|
|
126 |
:param network_id: integer (str or int)
|
|
127 |
|
|
128 |
:returns: (list)
|
|
129 |
"""
|
92 |
130 |
r = self.networks_get(network_id=network_id)
|
93 |
131 |
return r.json['network']['attachments']['values']
|
94 |
132 |
|
95 |
133 |
def create_network(self,
|
96 |
|
name,
|
97 |
|
cidr=False,
|
98 |
|
gateway=False,
|
99 |
|
type=False,
|
100 |
|
dhcp=False):
|
101 |
|
"""@params cidr, geteway, type and dhcp should be strings
|
|
134 |
name, cidr=None, gateway=None, type=None, dhcp=None):
|
|
135 |
"""
|
|
136 |
:param name: (str)
|
|
137 |
|
|
138 |
:param cidr: (str)
|
|
139 |
|
|
140 |
:param geteway: (str)
|
|
141 |
|
|
142 |
:param type: (str)
|
|
143 |
|
|
144 |
:param dhcp: (str)
|
|
145 |
|
|
146 |
:returns: (dict) network detailed info
|
102 |
147 |
"""
|
103 |
148 |
net = dict(name=name)
|
104 |
149 |
if cidr:
|
... | ... | |
114 |
159 |
return r.json['network']
|
115 |
160 |
|
116 |
161 |
def get_network_details(self, network_id):
|
|
162 |
"""
|
|
163 |
:param network_id: integer (str or int)
|
|
164 |
|
|
165 |
:returns: (dict)
|
|
166 |
"""
|
117 |
167 |
r = self.networks_get(network_id=network_id)
|
118 |
168 |
return r.json['network']
|
119 |
169 |
|
120 |
170 |
def update_network_name(self, network_id, new_name):
|
|
171 |
"""
|
|
172 |
:param network_id: integer (str or int)
|
|
173 |
|
|
174 |
:param new_name: (str)
|
|
175 |
"""
|
121 |
176 |
req = {'network': {'name': new_name}}
|
122 |
177 |
r = self.networks_put(network_id=network_id, json_data=req)
|
123 |
178 |
r.release()
|
124 |
179 |
|
125 |
180 |
def delete_network(self, network_id):
|
|
181 |
"""
|
|
182 |
:param network_id: integer (str or int)
|
|
183 |
|
|
184 |
:raises ClientError: 421 Network in use
|
|
185 |
"""
|
126 |
186 |
try:
|
127 |
187 |
r = self.networks_delete(network_id)
|
128 |
188 |
except ClientError as err:
|
... | ... | |
133 |
193 |
r.release()
|
134 |
194 |
|
135 |
195 |
def connect_server(self, server_id, network_id):
|
|
196 |
""" Connect a server to a network
|
|
197 |
|
|
198 |
:param server_id: integer (str or int)
|
|
199 |
|
|
200 |
:param network_id: integer (str or int)
|
|
201 |
"""
|
136 |
202 |
req = {'add': {'serverRef': server_id}}
|
137 |
203 |
r = self.networks_post(network_id, 'action', json_data=req)
|
138 |
204 |
r.release()
|
139 |
205 |
|
140 |
206 |
def disconnect_server(self, server_id, nic_id):
|
|
207 |
"""
|
|
208 |
:param server_id: integer (str or int)
|
|
209 |
|
|
210 |
:param nic_id: (str)
|
|
211 |
"""
|
141 |
212 |
server_nets = self.list_server_nics(server_id)
|
142 |
213 |
nets = [(net['id'], net['network_id']) for net in server_nets\
|
143 |
214 |
if nic_id == net['id']]
|
144 |
|
if len(nets) == 0:
|
145 |
|
return
|
146 |
215 |
for (nic_id, network_id) in nets:
|
147 |
216 |
req = {'remove': {'attachment': unicode(nic_id)}}
|
148 |
217 |
r = self.networks_post(network_id, 'action', json_data=req)
|
149 |
218 |
r.release()
|
150 |
219 |
|
151 |
|
#NEW
|
152 |
220 |
def disconnect_network_nics(self, netid):
|
153 |
|
r = self.list_network_nics(netid)
|
154 |
|
for nic in r:
|
|
221 |
"""
|
|
222 |
:param netid: integer (str or int)
|
|
223 |
"""
|
|
224 |
for nic in self.list_network_nics(netid):
|
155 |
225 |
req = dict(remove=dict(attachment=nic))
|
156 |
226 |
r = self.networks_post(netid, 'action', json_data=req)
|
|
227 |
r.release()
|
157 |
228 |
|
158 |
229 |
def wait_server(self, server_id,
|
159 |
230 |
current_status='BUILD',
|
160 |
231 |
delay=0.5,
|
161 |
232 |
max_wait=128,
|
162 |
233 |
wait_cb=None):
|
163 |
|
"""Wait for server to reach a status different from current_status
|
164 |
|
@return the new mode if succesfull, False if timed out
|
165 |
|
@server_id
|
166 |
|
@current_status
|
167 |
|
@delay time interval between retries
|
168 |
|
@wait_cb if set a progressbar is used to show progress
|
|
234 |
"""Wait for server while its status is current_status
|
|
235 |
|
|
236 |
:param server_id: integer (str or int)
|
|
237 |
|
|
238 |
:param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
|
|
239 |
|
|
240 |
:param delay: time interval between retries
|
|
241 |
|
|
242 |
:param wait_cb: if set a progressbar is used to show progress
|
|
243 |
|
|
244 |
:returns: (str) the new mode if succesfull, (bool) False if timed out
|
169 |
245 |
"""
|
170 |
246 |
r = self.get_server_details(server_id)
|
171 |
247 |
if r['status'] != current_status:
|