root / kamaki / clients / cyclades / __init__.py @ ccdd1b82
History | View | Annotate | Download (17.1 kB)
1 | 24851aa5 | Stavros Sachtouris | # Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | a1c50326 | Giorgos Verigakis | #
|
3 | a1c50326 | Giorgos Verigakis | # Redistribution and use in source and binary forms, with or
|
4 | a1c50326 | Giorgos Verigakis | # without modification, are permitted provided that the following
|
5 | a1c50326 | Giorgos Verigakis | # conditions are met:
|
6 | a1c50326 | Giorgos Verigakis | #
|
7 | a1c50326 | Giorgos Verigakis | # 1. Redistributions of source code must retain the above
|
8 | a1c50326 | Giorgos Verigakis | # copyright notice, this list of conditions and the following
|
9 | a1c50326 | Giorgos Verigakis | # disclaimer.
|
10 | a1c50326 | Giorgos Verigakis | #
|
11 | a1c50326 | Giorgos Verigakis | # 2. Redistributions in binary form must reproduce the above
|
12 | a1c50326 | Giorgos Verigakis | # copyright notice, this list of conditions and the following
|
13 | a1c50326 | Giorgos Verigakis | # disclaimer in the documentation and/or other materials
|
14 | a1c50326 | Giorgos Verigakis | # provided with the distribution.
|
15 | a1c50326 | Giorgos Verigakis | #
|
16 | a1c50326 | Giorgos Verigakis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | a1c50326 | Giorgos Verigakis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | a1c50326 | Giorgos Verigakis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | a1c50326 | Giorgos Verigakis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | a1c50326 | Giorgos Verigakis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | a1c50326 | Giorgos Verigakis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | a1c50326 | Giorgos Verigakis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | a1c50326 | Giorgos Verigakis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | a1c50326 | Giorgos Verigakis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | a1c50326 | Giorgos Verigakis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | a1c50326 | Giorgos Verigakis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | a1c50326 | Giorgos Verigakis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | a1c50326 | Giorgos Verigakis | #
|
29 | a1c50326 | Giorgos Verigakis | # The views and conclusions contained in the software and
|
30 | a1c50326 | Giorgos Verigakis | # documentation are those of the authors and should not be
|
31 | a1c50326 | Giorgos Verigakis | # interpreted as representing official policies, either expressed
|
32 | a1c50326 | Giorgos Verigakis | # or implied, of GRNET S.A.
|
33 | a1c50326 | Giorgos Verigakis | |
34 | 02846a75 | Stavros Sachtouris | from time import sleep |
35 | 02846a75 | Stavros Sachtouris | |
36 | 55faa0bc | Stavros Sachtouris | from kamaki.clients.cyclades.rest_api import CycladesRestClient |
37 | e864cd9e | Stavros Sachtouris | from kamaki.clients.network import NetworkClient |
38 | 0e27687b | Stavros Sachtouris | from kamaki.clients.utils import path4url |
39 | 6c068db6 | Stavros Sachtouris | from kamaki.clients import ClientError |
40 | a1c50326 | Giorgos Verigakis | |
41 | 3dabe5d2 | Stavros Sachtouris | |
42 | 55faa0bc | Stavros Sachtouris | class CycladesClient(CycladesRestClient): |
43 | 76e7661e | Stavros Sachtouris | """Synnefo Cyclades Compute API client"""
|
44 | 2f749e6e | Stavros Sachtouris | |
45 | dbcbf446 | Stavros Sachtouris | def create_server( |
46 | dbcbf446 | Stavros Sachtouris | self, name, flavor_id, image_id,
|
47 | dbcbf446 | Stavros Sachtouris | metadata=None, personality=None): |
48 | dbcbf446 | Stavros Sachtouris | """Submit request to create a new server
|
49 | dbcbf446 | Stavros Sachtouris |
|
50 | dbcbf446 | Stavros Sachtouris | :param name: (str)
|
51 | dbcbf446 | Stavros Sachtouris |
|
52 | dbcbf446 | Stavros Sachtouris | :param flavor_id: integer id denoting a preset hardware configuration
|
53 | dbcbf446 | Stavros Sachtouris |
|
54 | 2bd23362 | Stavros Sachtouris | :param image_id: (str) id denoting the OS image to run on virt. server
|
55 | dbcbf446 | Stavros Sachtouris |
|
56 | dbcbf446 | Stavros Sachtouris | :param metadata: (dict) vm metadata updated by os/users image metadata
|
57 | dbcbf446 | Stavros Sachtouris |
|
58 | dbcbf446 | Stavros Sachtouris | :param personality: a list of (file path, file contents) tuples,
|
59 | 2bd23362 | Stavros Sachtouris | describing files to be injected into virtual server upon creation
|
60 | dbcbf446 | Stavros Sachtouris |
|
61 | 2bd23362 | Stavros Sachtouris | :returns: a dict with the new virtual server details
|
62 | dbcbf446 | Stavros Sachtouris |
|
63 | dbcbf446 | Stavros Sachtouris | :raises ClientError: wraps request errors
|
64 | dbcbf446 | Stavros Sachtouris | """
|
65 | dbcbf446 | Stavros Sachtouris | image = self.get_image_details(image_id)
|
66 | dbcbf446 | Stavros Sachtouris | metadata = metadata or dict() |
67 | dbcbf446 | Stavros Sachtouris | for key in ('os', 'users'): |
68 | dbcbf446 | Stavros Sachtouris | try:
|
69 | dbcbf446 | Stavros Sachtouris | metadata[key] = image['metadata'][key]
|
70 | dbcbf446 | Stavros Sachtouris | except KeyError: |
71 | dbcbf446 | Stavros Sachtouris | pass
|
72 | dbcbf446 | Stavros Sachtouris | |
73 | 65a8b1da | Stavros Sachtouris | return super(CycladesClient, self).create_server( |
74 | 65a8b1da | Stavros Sachtouris | name, flavor_id, image_id, |
75 | 65a8b1da | Stavros Sachtouris | metadata=metadata, personality=personality) |
76 | dbcbf446 | Stavros Sachtouris | |
77 | a1c50326 | Giorgos Verigakis | def start_server(self, server_id): |
78 | f5eac743 | Stavros Sachtouris | """Submit a startup request
|
79 | f5eac743 | Stavros Sachtouris |
|
80 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
81 | cd295a1d | Stavros Sachtouris |
|
82 | cd295a1d | Stavros Sachtouris | :returns: (dict) response headers
|
83 | f5eac743 | Stavros Sachtouris | """
|
84 | 6a0b1658 | Giorgos Verigakis | req = {'start': {}}
|
85 | ef2e6c9f | Stavros Sachtouris | r = self.servers_action_post(server_id, json_data=req, success=202) |
86 | cd295a1d | Stavros Sachtouris | return r.headers
|
87 | 6f1ec797 | Stavros Sachtouris | |
88 | a1c50326 | Giorgos Verigakis | def shutdown_server(self, server_id): |
89 | f5eac743 | Stavros Sachtouris | """Submit a shutdown request
|
90 | f5eac743 | Stavros Sachtouris |
|
91 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
92 | cd295a1d | Stavros Sachtouris |
|
93 | cd295a1d | Stavros Sachtouris | :returns: (dict) response headers
|
94 | f5eac743 | Stavros Sachtouris | """
|
95 | 6a0b1658 | Giorgos Verigakis | req = {'shutdown': {}}
|
96 | ef2e6c9f | Stavros Sachtouris | r = self.servers_action_post(server_id, json_data=req, success=202) |
97 | cd295a1d | Stavros Sachtouris | return r.headers
|
98 | 3dabe5d2 | Stavros Sachtouris | |
99 | a1c50326 | Giorgos Verigakis | def get_server_console(self, server_id): |
100 | f5eac743 | Stavros Sachtouris | """
|
101 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
102 | f5eac743 | Stavros Sachtouris |
|
103 | 2bd23362 | Stavros Sachtouris | :returns: (dict) info to set a VNC connection to virtual server
|
104 | f5eac743 | Stavros Sachtouris | """
|
105 | 6a0b1658 | Giorgos Verigakis | req = {'console': {'type': 'vnc'}} |
106 | ef2e6c9f | Stavros Sachtouris | r = self.servers_action_post(server_id, json_data=req, success=200) |
107 | 6a0b1658 | Giorgos Verigakis | return r.json['console'] |
108 | 2d67ecbb | Stavros Sachtouris | |
109 | 2d67ecbb | Stavros Sachtouris | def get_firewall_profile(self, server_id): |
110 | f5eac743 | Stavros Sachtouris | """
|
111 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
112 | f5eac743 | Stavros Sachtouris |
|
113 | f5eac743 | Stavros Sachtouris | :returns: (str) ENABLED | DISABLED | PROTECTED
|
114 | f5eac743 | Stavros Sachtouris |
|
115 | f5eac743 | Stavros Sachtouris | :raises ClientError: 520 No Firewall Profile
|
116 | f5eac743 | Stavros Sachtouris | """
|
117 | 2d67ecbb | Stavros Sachtouris | r = self.get_server_details(server_id)
|
118 | 2d67ecbb | Stavros Sachtouris | try:
|
119 | fab9b17f | Stavros Sachtouris | return r['attachments'][0]['firewallProfile'] |
120 | 2d67ecbb | Stavros Sachtouris | except KeyError: |
121 | 24ff0a35 | Stavros Sachtouris | raise ClientError(
|
122 | 7d768bc6 | Stavros Sachtouris | 'No Firewall Profile',
|
123 | 3dabe5d2 | Stavros Sachtouris | details='Server %s is missing a firewall profile' % server_id)
|
124 | 2d67ecbb | Stavros Sachtouris | |
125 | a1c50326 | Giorgos Verigakis | def set_firewall_profile(self, server_id, profile): |
126 | a1c50326 | Giorgos Verigakis | """Set the firewall profile for the public interface of a server
|
127 | f5eac743 | Stavros Sachtouris |
|
128 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
129 | f5eac743 | Stavros Sachtouris |
|
130 | f5eac743 | Stavros Sachtouris | :param profile: (str) ENABLED | DISABLED | PROTECTED
|
131 | cd295a1d | Stavros Sachtouris |
|
132 | cd295a1d | Stavros Sachtouris | :returns: (dict) response headers
|
133 | a1c50326 | Giorgos Verigakis | """
|
134 | 6a0b1658 | Giorgos Verigakis | req = {'firewallProfile': {'profile': profile}} |
135 | ef2e6c9f | Stavros Sachtouris | r = self.servers_action_post(server_id, json_data=req, success=202) |
136 | cd295a1d | Stavros Sachtouris | return r.headers
|
137 | 3dabe5d2 | Stavros Sachtouris | |
138 | c7083665 | Stavros Sachtouris | def list_server_nics(self, server_id): |
139 | f5eac743 | Stavros Sachtouris | """
|
140 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
141 | f5eac743 | Stavros Sachtouris |
|
142 | f5eac743 | Stavros Sachtouris | :returns: (dict) network interface connections
|
143 | f5eac743 | Stavros Sachtouris | """
|
144 | e51c7d5b | Stavros Sachtouris | r = self.servers_ips_get(server_id)
|
145 | 9a70c511 | Stavros Sachtouris | return r.json['attachments'] |
146 | 1a85b14f | Stavros Sachtouris | |
147 | a1c50326 | Giorgos Verigakis | def get_server_stats(self, server_id): |
148 | f5eac743 | Stavros Sachtouris | """
|
149 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
150 | f5eac743 | Stavros Sachtouris |
|
151 | f5eac743 | Stavros Sachtouris | :returns: (dict) auto-generated graphs of statistics (urls)
|
152 | f5eac743 | Stavros Sachtouris | """
|
153 | e51c7d5b | Stavros Sachtouris | r = self.servers_stats_get(server_id)
|
154 | 6a0b1658 | Giorgos Verigakis | return r.json['stats'] |
155 | 3dabe5d2 | Stavros Sachtouris | |
156 | a1c50326 | Giorgos Verigakis | def list_networks(self, detail=False): |
157 | f5eac743 | Stavros Sachtouris | """
|
158 | f5eac743 | Stavros Sachtouris | :param detail: (bool)
|
159 | f5eac743 | Stavros Sachtouris |
|
160 | f5eac743 | Stavros Sachtouris | :returns: (list) id,name if not detail else full info per network
|
161 | f5eac743 | Stavros Sachtouris | """
|
162 | 6f1ec797 | Stavros Sachtouris | detail = 'detail' if detail else '' |
163 | 6f1ec797 | Stavros Sachtouris | r = self.networks_get(command=detail)
|
164 | fab9b17f | Stavros Sachtouris | return r.json['networks'] |
165 | a1c50326 | Giorgos Verigakis | |
166 | a34888b4 | Stavros Sachtouris | def list_network_nics(self, network_id): |
167 | f5eac743 | Stavros Sachtouris | """
|
168 | f5eac743 | Stavros Sachtouris | :param network_id: integer (str or int)
|
169 | f5eac743 | Stavros Sachtouris |
|
170 | f5eac743 | Stavros Sachtouris | :returns: (list)
|
171 | f5eac743 | Stavros Sachtouris | """
|
172 | a34888b4 | Stavros Sachtouris | r = self.networks_get(network_id=network_id)
|
173 | fab9b17f | Stavros Sachtouris | return r.json['network']['attachments'] |
174 | a34888b4 | Stavros Sachtouris | |
175 | 24ff0a35 | Stavros Sachtouris | def create_network( |
176 | 24ff0a35 | Stavros Sachtouris | self, name,
|
177 | 9d1db11e | Stavros Sachtouris | cidr=None, gateway=None, type=None, dhcp=False): |
178 | f5eac743 | Stavros Sachtouris | """
|
179 | f5eac743 | Stavros Sachtouris | :param name: (str)
|
180 | f5eac743 | Stavros Sachtouris |
|
181 | f5eac743 | Stavros Sachtouris | :param cidr: (str)
|
182 | f5eac743 | Stavros Sachtouris |
|
183 | f5eac743 | Stavros Sachtouris | :param geteway: (str)
|
184 | f5eac743 | Stavros Sachtouris |
|
185 | f6f81cd3 | Stavros Sachtouris | :param type: (str) if None, will use MAC_FILTERED as default
|
186 | f6f81cd3 | Stavros Sachtouris | Valid values: CUSTOM, IP_LESS_ROUTED, MAC_FILTERED, PHYSICAL_VLAN
|
187 | f5eac743 | Stavros Sachtouris |
|
188 | 9d1db11e | Stavros Sachtouris | :param dhcp: (bool)
|
189 | f5eac743 | Stavros Sachtouris |
|
190 | f5eac743 | Stavros Sachtouris | :returns: (dict) network detailed info
|
191 | e52840fa | Stavros Sachtouris | """
|
192 | e52840fa | Stavros Sachtouris | net = dict(name=name)
|
193 | e52840fa | Stavros Sachtouris | if cidr:
|
194 | 3dabe5d2 | Stavros Sachtouris | net['cidr'] = cidr
|
195 | e52840fa | Stavros Sachtouris | if gateway:
|
196 | 3dabe5d2 | Stavros Sachtouris | net['gateway'] = gateway
|
197 | f6f81cd3 | Stavros Sachtouris | net['type'] = type or 'MAC_FILTERED' |
198 | 9d1db11e | Stavros Sachtouris | net['dhcp'] = True if dhcp else False |
199 | e52840fa | Stavros Sachtouris | req = dict(network=net)
|
200 | 6f1ec797 | Stavros Sachtouris | r = self.networks_post(json_data=req, success=202) |
201 | 6a0b1658 | Giorgos Verigakis | return r.json['network'] |
202 | a1c50326 | Giorgos Verigakis | |
203 | a1c50326 | Giorgos Verigakis | def get_network_details(self, network_id): |
204 | f5eac743 | Stavros Sachtouris | """
|
205 | f5eac743 | Stavros Sachtouris | :param network_id: integer (str or int)
|
206 | f5eac743 | Stavros Sachtouris |
|
207 | f5eac743 | Stavros Sachtouris | :returns: (dict)
|
208 | f5eac743 | Stavros Sachtouris | """
|
209 | 6f1ec797 | Stavros Sachtouris | r = self.networks_get(network_id=network_id)
|
210 | 6a0b1658 | Giorgos Verigakis | return r.json['network'] |
211 | a1c50326 | Giorgos Verigakis | |
212 | a1c50326 | Giorgos Verigakis | def update_network_name(self, network_id, new_name): |
213 | f5eac743 | Stavros Sachtouris | """
|
214 | f5eac743 | Stavros Sachtouris | :param network_id: integer (str or int)
|
215 | f5eac743 | Stavros Sachtouris |
|
216 | f5eac743 | Stavros Sachtouris | :param new_name: (str)
|
217 | 1b73b4c1 | Stavros Sachtouris |
|
218 | 1b73b4c1 | Stavros Sachtouris | :returns: (dict) response headers
|
219 | f5eac743 | Stavros Sachtouris | """
|
220 | 6a0b1658 | Giorgos Verigakis | req = {'network': {'name': new_name}} |
221 | 1b73b4c1 | Stavros Sachtouris | r = self.networks_put(network_id=network_id, json_data=req)
|
222 | 1b73b4c1 | Stavros Sachtouris | return r.headers
|
223 | a1c50326 | Giorgos Verigakis | |
224 | a1c50326 | Giorgos Verigakis | def delete_network(self, network_id): |
225 | f5eac743 | Stavros Sachtouris | """
|
226 | f5eac743 | Stavros Sachtouris | :param network_id: integer (str or int)
|
227 | f5eac743 | Stavros Sachtouris |
|
228 | 1b73b4c1 | Stavros Sachtouris | :returns: (dict) response headers
|
229 | 1b73b4c1 | Stavros Sachtouris |
|
230 | f5eac743 | Stavros Sachtouris | :raises ClientError: 421 Network in use
|
231 | f5eac743 | Stavros Sachtouris | """
|
232 | a34888b4 | Stavros Sachtouris | try:
|
233 | 1b73b4c1 | Stavros Sachtouris | r = self.networks_delete(network_id)
|
234 | 1b73b4c1 | Stavros Sachtouris | return r.headers
|
235 | a34888b4 | Stavros Sachtouris | except ClientError as err: |
236 | a34888b4 | Stavros Sachtouris | if err.status == 421: |
237 | ec51b97c | Stavros Sachtouris | err.details = [ |
238 | 24ff0a35 | Stavros Sachtouris | 'Network may be still connected to at least one server']
|
239 | 08aad6db | Stavros Sachtouris | raise
|
240 | a1c50326 | Giorgos Verigakis | |
241 | a1c50326 | Giorgos Verigakis | def connect_server(self, server_id, network_id): |
242 | f5eac743 | Stavros Sachtouris | """ Connect a server to a network
|
243 | f5eac743 | Stavros Sachtouris |
|
244 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
245 | f5eac743 | Stavros Sachtouris |
|
246 | f5eac743 | Stavros Sachtouris | :param network_id: integer (str or int)
|
247 | 1b73b4c1 | Stavros Sachtouris |
|
248 | 1b73b4c1 | Stavros Sachtouris | :returns: (dict) response headers
|
249 | f5eac743 | Stavros Sachtouris | """
|
250 | 6a0b1658 | Giorgos Verigakis | req = {'add': {'serverRef': server_id}} |
251 | 1b73b4c1 | Stavros Sachtouris | r = self.networks_post(network_id, 'action', json_data=req) |
252 | 1b73b4c1 | Stavros Sachtouris | return r.headers
|
253 | a1c50326 | Giorgos Verigakis | |
254 | c7083665 | Stavros Sachtouris | def disconnect_server(self, server_id, nic_id): |
255 | f5eac743 | Stavros Sachtouris | """
|
256 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
257 | f5eac743 | Stavros Sachtouris |
|
258 | f5eac743 | Stavros Sachtouris | :param nic_id: (str)
|
259 | b87ed277 | Stavros Sachtouris |
|
260 | b87ed277 | Stavros Sachtouris | :returns: (int) the number of nics disconnected
|
261 | f5eac743 | Stavros Sachtouris | """
|
262 | 2005b18e | Stavros Sachtouris | vm_nets = self.list_server_nics(server_id)
|
263 | 0e806947 | Stavros Sachtouris | num_of_disconnections = 0
|
264 | 24ff0a35 | Stavros Sachtouris | for (nic_id, network_id) in [( |
265 | 2005b18e | Stavros Sachtouris | net['id'],
|
266 | 2005b18e | Stavros Sachtouris | net['network_id']) for net in vm_nets if nic_id == net['id']]: |
267 | a517ff50 | Stavros Sachtouris | req = {'remove': {'attachment': '%s' % nic_id}} |
268 | c2b5da2f | Stavros Sachtouris | self.networks_post(network_id, 'action', json_data=req) |
269 | 0e806947 | Stavros Sachtouris | num_of_disconnections += 1
|
270 | 0e806947 | Stavros Sachtouris | return num_of_disconnections
|
271 | a34888b4 | Stavros Sachtouris | |
272 | a34888b4 | Stavros Sachtouris | def disconnect_network_nics(self, netid): |
273 | f5eac743 | Stavros Sachtouris | """
|
274 | f5eac743 | Stavros Sachtouris | :param netid: integer (str or int)
|
275 | f5eac743 | Stavros Sachtouris | """
|
276 | f5eac743 | Stavros Sachtouris | for nic in self.list_network_nics(netid): |
277 | a34888b4 | Stavros Sachtouris | req = dict(remove=dict(attachment=nic)) |
278 | c2b5da2f | Stavros Sachtouris | self.networks_post(netid, 'action', json_data=req) |
279 | fd1f1d96 | Stavros Sachtouris | |
280 | 7b2e4bf1 | Stavros Sachtouris | def _wait( |
281 | 7b2e4bf1 | Stavros Sachtouris | self, item_id, current_status, get_status,
|
282 | 7b2e4bf1 | Stavros Sachtouris | delay=1, max_wait=100, wait_cb=None): |
283 | 7b2e4bf1 | Stavros Sachtouris | """Wait for item while its status is current_status
|
284 | f5eac743 | Stavros Sachtouris |
|
285 | f5eac743 | Stavros Sachtouris | :param server_id: integer (str or int)
|
286 | f5eac743 | Stavros Sachtouris |
|
287 | 7b2e4bf1 | Stavros Sachtouris | :param current_status: (str)
|
288 | 7b2e4bf1 | Stavros Sachtouris |
|
289 | 7b2e4bf1 | Stavros Sachtouris | :param get_status: (method(self, item_id)) if called, returns
|
290 | 7b2e4bf1 | Stavros Sachtouris | (status, progress %) If no way to tell progress, return None
|
291 | f5eac743 | Stavros Sachtouris |
|
292 | f5eac743 | Stavros Sachtouris | :param delay: time interval between retries
|
293 | f5eac743 | Stavros Sachtouris |
|
294 | 7b2e4bf1 | Stavros Sachtouris | :param wait_cb: if set a progress bar is used to show progress
|
295 | f5eac743 | Stavros Sachtouris |
|
296 | 7b2e4bf1 | Stavros Sachtouris | :returns: (str) the new mode if successful, (bool) False if timed out
|
297 | fd1f1d96 | Stavros Sachtouris | """
|
298 | 7b2e4bf1 | Stavros Sachtouris | status, progress = get_status(self, item_id)
|
299 | fd1f1d96 | Stavros Sachtouris | |
300 | 7b2e4bf1 | Stavros Sachtouris | if wait_cb:
|
301 | e9c73313 | Stavros Sachtouris | wait_gen = wait_cb(max_wait // delay) |
302 | c59aef27 | Stavros Sachtouris | wait_gen.next() |
303 | fd1f1d96 | Stavros Sachtouris | |
304 | e9c73313 | Stavros Sachtouris | if status != current_status:
|
305 | e9c73313 | Stavros Sachtouris | if wait_cb:
|
306 | e9c73313 | Stavros Sachtouris | try:
|
307 | e9c73313 | Stavros Sachtouris | wait_gen.next() |
308 | e9c73313 | Stavros Sachtouris | except Exception: |
309 | e9c73313 | Stavros Sachtouris | pass
|
310 | e9c73313 | Stavros Sachtouris | return status
|
311 | e9c73313 | Stavros Sachtouris | old_wait = total_wait = 0
|
312 | e9c73313 | Stavros Sachtouris | |
313 | 7b2e4bf1 | Stavros Sachtouris | while status == current_status and total_wait <= max_wait: |
314 | 7b2e4bf1 | Stavros Sachtouris | if wait_cb:
|
315 | 7b2e4bf1 | Stavros Sachtouris | try:
|
316 | 7b2e4bf1 | Stavros Sachtouris | for i in range(total_wait - old_wait): |
317 | fd1f1d96 | Stavros Sachtouris | wait_gen.next() |
318 | 7b2e4bf1 | Stavros Sachtouris | except Exception: |
319 | 7b2e4bf1 | Stavros Sachtouris | break
|
320 | 7b2e4bf1 | Stavros Sachtouris | old_wait = total_wait |
321 | e9c73313 | Stavros Sachtouris | total_wait = progress or total_wait + 1 |
322 | fd1f1d96 | Stavros Sachtouris | sleep(delay) |
323 | 7b2e4bf1 | Stavros Sachtouris | status, progress = get_status(self, item_id)
|
324 | fd1f1d96 | Stavros Sachtouris | |
325 | 7b2e4bf1 | Stavros Sachtouris | if total_wait < max_wait:
|
326 | c59aef27 | Stavros Sachtouris | if wait_cb:
|
327 | c59aef27 | Stavros Sachtouris | try:
|
328 | 7b2e4bf1 | Stavros Sachtouris | for i in range(max_wait): |
329 | c59aef27 | Stavros Sachtouris | wait_gen.next() |
330 | c59aef27 | Stavros Sachtouris | except:
|
331 | c59aef27 | Stavros Sachtouris | pass
|
332 | 7b2e4bf1 | Stavros Sachtouris | return status if status != current_status else False |
333 | 7b2e4bf1 | Stavros Sachtouris | |
334 | 7b2e4bf1 | Stavros Sachtouris | def wait_server( |
335 | 7b2e4bf1 | Stavros Sachtouris | self, server_id,
|
336 | 7b2e4bf1 | Stavros Sachtouris | current_status='BUILD',
|
337 | 7b2e4bf1 | Stavros Sachtouris | delay=1, max_wait=100, wait_cb=None): |
338 | 7b2e4bf1 | Stavros Sachtouris | """Wait for server while its status is current_status
|
339 | 7b2e4bf1 | Stavros Sachtouris |
|
340 | 7b2e4bf1 | Stavros Sachtouris | :param server_id: integer (str or int)
|
341 | 7b2e4bf1 | Stavros Sachtouris |
|
342 | 7b2e4bf1 | Stavros Sachtouris | :param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
|
343 | 7b2e4bf1 | Stavros Sachtouris |
|
344 | 7b2e4bf1 | Stavros Sachtouris | :param delay: time interval between retries
|
345 | 7b2e4bf1 | Stavros Sachtouris |
|
346 | c788a761 | Stavros Sachtouris | :max_wait: (int) timeout in secconds
|
347 | c788a761 | Stavros Sachtouris |
|
348 | 7b2e4bf1 | Stavros Sachtouris | :param wait_cb: if set a progressbar is used to show progress
|
349 | 7b2e4bf1 | Stavros Sachtouris |
|
350 | 7b2e4bf1 | Stavros Sachtouris | :returns: (str) the new mode if succesfull, (bool) False if timed out
|
351 | 7b2e4bf1 | Stavros Sachtouris | """
|
352 | 7b2e4bf1 | Stavros Sachtouris | |
353 | 7b2e4bf1 | Stavros Sachtouris | def get_status(self, server_id): |
354 | 7b2e4bf1 | Stavros Sachtouris | r = self.get_server_details(server_id)
|
355 | 7b2e4bf1 | Stavros Sachtouris | return r['status'], (r.get('progress', None) if ( |
356 | 7b2e4bf1 | Stavros Sachtouris | current_status in ('BUILD', )) else None) |
357 | 7b2e4bf1 | Stavros Sachtouris | |
358 | 7b2e4bf1 | Stavros Sachtouris | return self._wait( |
359 | 7b2e4bf1 | Stavros Sachtouris | server_id, current_status, get_status, delay, max_wait, wait_cb) |
360 | 7b2e4bf1 | Stavros Sachtouris | |
361 | 7b2e4bf1 | Stavros Sachtouris | def wait_network( |
362 | 7b2e4bf1 | Stavros Sachtouris | self, net_id,
|
363 | f6822a26 | Stavros Sachtouris | current_status='PENDING', delay=1, max_wait=100, wait_cb=None): |
364 | 7b2e4bf1 | Stavros Sachtouris | """Wait for network while its status is current_status
|
365 | 7b2e4bf1 | Stavros Sachtouris |
|
366 | 7b2e4bf1 | Stavros Sachtouris | :param net_id: integer (str or int)
|
367 | 7b2e4bf1 | Stavros Sachtouris |
|
368 | 7b2e4bf1 | Stavros Sachtouris | :param current_status: (str) PENDING | ACTIVE | DELETED
|
369 | 7b2e4bf1 | Stavros Sachtouris |
|
370 | 7b2e4bf1 | Stavros Sachtouris | :param delay: time interval between retries
|
371 | 7b2e4bf1 | Stavros Sachtouris |
|
372 | c788a761 | Stavros Sachtouris | :max_wait: (int) timeout in secconds
|
373 | c788a761 | Stavros Sachtouris |
|
374 | 7b2e4bf1 | Stavros Sachtouris | :param wait_cb: if set a progressbar is used to show progress
|
375 | 7b2e4bf1 | Stavros Sachtouris |
|
376 | 7b2e4bf1 | Stavros Sachtouris | :returns: (str) the new mode if succesfull, (bool) False if timed out
|
377 | 7b2e4bf1 | Stavros Sachtouris | """
|
378 | 7b2e4bf1 | Stavros Sachtouris | |
379 | 7b2e4bf1 | Stavros Sachtouris | def get_status(self, net_id): |
380 | 7b2e4bf1 | Stavros Sachtouris | r = self.get_network_details(net_id)
|
381 | 7b2e4bf1 | Stavros Sachtouris | return r['status'], None |
382 | 7b2e4bf1 | Stavros Sachtouris | |
383 | 7b2e4bf1 | Stavros Sachtouris | return self._wait( |
384 | 7b2e4bf1 | Stavros Sachtouris | net_id, current_status, get_status, delay, max_wait, wait_cb) |
385 | 03033b54 | Stavros Sachtouris | |
386 | c788a761 | Stavros Sachtouris | def wait_firewall( |
387 | c788a761 | Stavros Sachtouris | self, server_id,
|
388 | c788a761 | Stavros Sachtouris | current_status='DISABLED', delay=1, max_wait=100, wait_cb=None): |
389 | c788a761 | Stavros Sachtouris | """Wait while the public network firewall status is current_status
|
390 | c788a761 | Stavros Sachtouris |
|
391 | c788a761 | Stavros Sachtouris | :param server_id: integer (str or int)
|
392 | c788a761 | Stavros Sachtouris |
|
393 | c788a761 | Stavros Sachtouris | :param current_status: (str) DISABLED | ENABLED | PROTECTED
|
394 | c788a761 | Stavros Sachtouris |
|
395 | c788a761 | Stavros Sachtouris | :param delay: time interval between retries
|
396 | c788a761 | Stavros Sachtouris |
|
397 | c788a761 | Stavros Sachtouris | :max_wait: (int) timeout in secconds
|
398 | c788a761 | Stavros Sachtouris |
|
399 | c788a761 | Stavros Sachtouris | :param wait_cb: if set a progressbar is used to show progress
|
400 | c788a761 | Stavros Sachtouris |
|
401 | c788a761 | Stavros Sachtouris | :returns: (str) the new mode if succesfull, (bool) False if timed out
|
402 | c788a761 | Stavros Sachtouris | """
|
403 | c788a761 | Stavros Sachtouris | |
404 | c788a761 | Stavros Sachtouris | def get_status(self, server_id): |
405 | c788a761 | Stavros Sachtouris | return self.get_firewall_profile(server_id), None |
406 | c788a761 | Stavros Sachtouris | |
407 | c788a761 | Stavros Sachtouris | return self._wait( |
408 | c788a761 | Stavros Sachtouris | server_id, current_status, get_status, delay, max_wait, wait_cb) |
409 | c788a761 | Stavros Sachtouris | |
410 | 03033b54 | Stavros Sachtouris | def get_floating_ip_pools(self): |
411 | 03033b54 | Stavros Sachtouris | """
|
412 | 03033b54 | Stavros Sachtouris | :returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
|
413 | 03033b54 | Stavros Sachtouris | """
|
414 | 03033b54 | Stavros Sachtouris | r = self.floating_ip_pools_get()
|
415 | 03033b54 | Stavros Sachtouris | return r.json
|
416 | 03033b54 | Stavros Sachtouris | |
417 | 03033b54 | Stavros Sachtouris | def get_floating_ips(self): |
418 | 03033b54 | Stavros Sachtouris | """
|
419 | 97086fcd | Stavros Sachtouris | :returns: (dict) {floating_ips: [fixed_ip: , id: , ip: , pool: ]}
|
420 | 03033b54 | Stavros Sachtouris | """
|
421 | 03033b54 | Stavros Sachtouris | r = self.floating_ips_get()
|
422 | 03033b54 | Stavros Sachtouris | return r.json
|
423 | 03033b54 | Stavros Sachtouris | |
424 | 03033b54 | Stavros Sachtouris | def alloc_floating_ip(self, pool=None, address=None): |
425 | 03033b54 | Stavros Sachtouris | """
|
426 | 03033b54 | Stavros Sachtouris | :param pool: (str) pool of ips to allocate from
|
427 | 03033b54 | Stavros Sachtouris |
|
428 | 03033b54 | Stavros Sachtouris | :param address: (str) ip address to request
|
429 | 03033b54 | Stavros Sachtouris |
|
430 | 03033b54 | Stavros Sachtouris | :returns: (dict) {
|
431 | cedde35d | Stavros Sachtouris | fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...}
|
432 | 03033b54 | Stavros Sachtouris | """
|
433 | 03033b54 | Stavros Sachtouris | json_data = dict()
|
434 | 03033b54 | Stavros Sachtouris | if pool:
|
435 | 03033b54 | Stavros Sachtouris | json_data['pool'] = pool
|
436 | 2e6e03da | Stavros Sachtouris | if address:
|
437 | 2e6e03da | Stavros Sachtouris | json_data['address'] = address
|
438 | 03033b54 | Stavros Sachtouris | r = self.floating_ips_post(json_data)
|
439 | 03033b54 | Stavros Sachtouris | return r.json['floating_ip'] |
440 | 03033b54 | Stavros Sachtouris | |
441 | 03033b54 | Stavros Sachtouris | def get_floating_ip(self, fip_id): |
442 | 03033b54 | Stavros Sachtouris | """
|
443 | 03033b54 | Stavros Sachtouris | :param fip_id: (str) floating ip id
|
444 | 03033b54 | Stavros Sachtouris |
|
445 | 03033b54 | Stavros Sachtouris | :returns: (dict)
|
446 | 03033b54 | Stavros Sachtouris | {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
|
447 | 03033b54 | Stavros Sachtouris |
|
448 | 03033b54 | Stavros Sachtouris | :raises AssertionError: if fip_id is emtpy
|
449 | 03033b54 | Stavros Sachtouris | """
|
450 | 03033b54 | Stavros Sachtouris | assert fip_id, 'floating ip id is needed for get_floating_ip' |
451 | 03033b54 | Stavros Sachtouris | r = self.floating_ips_get(fip_id)
|
452 | 03033b54 | Stavros Sachtouris | return r.json['floating_ip'] |
453 | 03033b54 | Stavros Sachtouris | |
454 | 03033b54 | Stavros Sachtouris | def delete_floating_ip(self, fip_id=None): |
455 | 03033b54 | Stavros Sachtouris | """
|
456 | 03033b54 | Stavros Sachtouris | :param fip_id: (str) floating ip id (if None, all ips are deleted)
|
457 | 03033b54 | Stavros Sachtouris |
|
458 | 03033b54 | Stavros Sachtouris | :returns: (dict) request headers
|
459 | 03033b54 | Stavros Sachtouris |
|
460 | 03033b54 | Stavros Sachtouris | :raises AssertionError: if fip_id is emtpy
|
461 | 03033b54 | Stavros Sachtouris | """
|
462 | 03033b54 | Stavros Sachtouris | assert fip_id, 'floating ip id is needed for delete_floating_ip' |
463 | 03033b54 | Stavros Sachtouris | r = self.floating_ips_delete(fip_id)
|
464 | 03033b54 | Stavros Sachtouris | return r.headers
|
465 | 03033b54 | Stavros Sachtouris | |
466 | 9b47150e | Stavros Sachtouris | def attach_floating_ip(self, server_id, address): |
467 | 03033b54 | Stavros Sachtouris | """Associate the address ip to server with server_id
|
468 | 03033b54 | Stavros Sachtouris |
|
469 | 03033b54 | Stavros Sachtouris | :param server_id: (int)
|
470 | 03033b54 | Stavros Sachtouris |
|
471 | 03033b54 | Stavros Sachtouris | :param address: (str) the ip address to assign to server (vm)
|
472 | 03033b54 | Stavros Sachtouris |
|
473 | 03033b54 | Stavros Sachtouris | :returns: (dict) request headers
|
474 | 03033b54 | Stavros Sachtouris |
|
475 | 03033b54 | Stavros Sachtouris | :raises ValueError: if server_id cannot be converted to int
|
476 | 03033b54 | Stavros Sachtouris |
|
477 | 03033b54 | Stavros Sachtouris | :raises ValueError: if server_id is not of a int-convertable type
|
478 | 03033b54 | Stavros Sachtouris |
|
479 | 03033b54 | Stavros Sachtouris | :raises AssertionError: if address is emtpy
|
480 | 03033b54 | Stavros Sachtouris | """
|
481 | 03033b54 | Stavros Sachtouris | server_id = int(server_id)
|
482 | 9b47150e | Stavros Sachtouris | assert address, 'address is needed for attach_floating_ip' |
483 | ef2e6c9f | Stavros Sachtouris | req = dict(addFloatingIp=dict(address=address)) |
484 | ef2e6c9f | Stavros Sachtouris | r = self.servers_action_post(server_id, json_data=req)
|
485 | 03033b54 | Stavros Sachtouris | return r.headers
|
486 | 03033b54 | Stavros Sachtouris | |
487 | 9b47150e | Stavros Sachtouris | def detach_floating_ip(self, server_id, address): |
488 | 03033b54 | Stavros Sachtouris | """Disassociate an address ip from the server with server_id
|
489 | 03033b54 | Stavros Sachtouris |
|
490 | 03033b54 | Stavros Sachtouris | :param server_id: (int)
|
491 | 03033b54 | Stavros Sachtouris |
|
492 | 03033b54 | Stavros Sachtouris | :param address: (str) the ip address to assign to server (vm)
|
493 | 03033b54 | Stavros Sachtouris |
|
494 | 03033b54 | Stavros Sachtouris | :returns: (dict) request headers
|
495 | 03033b54 | Stavros Sachtouris |
|
496 | 03033b54 | Stavros Sachtouris | :raises ValueError: if server_id cannot be converted to int
|
497 | 03033b54 | Stavros Sachtouris |
|
498 | 03033b54 | Stavros Sachtouris | :raises ValueError: if server_id is not of a int-convertable type
|
499 | 03033b54 | Stavros Sachtouris |
|
500 | 03033b54 | Stavros Sachtouris | :raises AssertionError: if address is emtpy
|
501 | 03033b54 | Stavros Sachtouris | """
|
502 | 03033b54 | Stavros Sachtouris | server_id = int(server_id)
|
503 | 9b47150e | Stavros Sachtouris | assert address, 'address is needed for detach_floating_ip' |
504 | ef2e6c9f | Stavros Sachtouris | req = dict(removeFloatingIp=dict(address=address)) |
505 | ef2e6c9f | Stavros Sachtouris | r = self.servers_action_post(server_id, json_data=req)
|
506 | 03033b54 | Stavros Sachtouris | return r.headers
|
507 | e864cd9e | Stavros Sachtouris | |
508 | e864cd9e | Stavros Sachtouris | |
509 | e864cd9e | Stavros Sachtouris | class CycladesNetworkClient(NetworkClient): |
510 | e864cd9e | Stavros Sachtouris | """Cyclades Network API extentions"""
|
511 | e864cd9e | Stavros Sachtouris | |
512 | e864cd9e | Stavros Sachtouris | network_types = ( |
513 | e864cd9e | Stavros Sachtouris | 'CUSTOM', 'MAC_FILTERED', 'IP_LESS_ROUTED', 'PHYSICAL_VLAN') |
514 | e864cd9e | Stavros Sachtouris | |
515 | 0e27687b | Stavros Sachtouris | def list_networks(self, detail=None): |
516 | 0e27687b | Stavros Sachtouris | path = path4url('networks', 'detail' if detail else '') |
517 | 0e27687b | Stavros Sachtouris | r = self.get(path, success=200) |
518 | 0e27687b | Stavros Sachtouris | return r.json['networks'] |
519 | 0e27687b | Stavros Sachtouris | |
520 | e864cd9e | Stavros Sachtouris | def create_network(self, type, name=None, shared=None): |
521 | e864cd9e | Stavros Sachtouris | req = dict(network=dict(type=type, admin_state_up=True)) |
522 | e864cd9e | Stavros Sachtouris | if name:
|
523 | e864cd9e | Stavros Sachtouris | req['network']['name'] = name |
524 | e864cd9e | Stavros Sachtouris | if shared not in (None, ): |
525 | e864cd9e | Stavros Sachtouris | req['network']['shared'] = bool(shared) |
526 | e864cd9e | Stavros Sachtouris | r = self.networks_post(json_data=req, success=201) |
527 | e864cd9e | Stavros Sachtouris | return r.json['network'] |
528 | ccdd1b82 | Stavros Sachtouris | |
529 | ccdd1b82 | Stavros Sachtouris | def create_port(self, network_id, device_id, security_groups=None): |
530 | ccdd1b82 | Stavros Sachtouris | port = dict(network_id=network_id, device_id=device_id)
|
531 | ccdd1b82 | Stavros Sachtouris | if security_groups:
|
532 | ccdd1b82 | Stavros Sachtouris | port['security_groups'] = security_groups
|
533 | ccdd1b82 | Stavros Sachtouris | r = self.ports_post(json_data=dict(port=port), success=201) |
534 | ccdd1b82 | Stavros Sachtouris | return r.json['port'] |