root / kamaki / cli / commands / network.py @ bdff03d5
History | View | Annotate | Download (27 kB)
1 |
# Copyright 2011-2013 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 io import StringIO |
35 |
from pydoc import pager |
36 |
|
37 |
from kamaki.cli import command |
38 |
from kamaki.cli.command_tree import CommandTree |
39 |
from kamaki.cli.errors import ( |
40 |
CLIBaseUrlError, CLIInvalidArgument, raiseCLIError) |
41 |
from kamaki.clients.cyclades import CycladesNetworkClient, ClientError |
42 |
from kamaki.cli.argument import ( |
43 |
FlagArgument, ValueArgument, RepeatableArgument, IntArgument) |
44 |
from kamaki.cli.commands import _command_init, errors, addLogSettings |
45 |
from kamaki.cli.commands import ( |
46 |
_optional_output_cmd, _optional_json, _name_filter, _id_filter) |
47 |
from kamaki.cli.commands.cyclades import _service_wait |
48 |
|
49 |
|
50 |
network_cmds = CommandTree('network', 'Networking API network commands') |
51 |
port_cmds = CommandTree('port', 'Networking API network commands') |
52 |
subnet_cmds = CommandTree('subnet', 'Networking API network commands') |
53 |
ip_cmds = CommandTree('ip', 'Networking API floatingip commands') |
54 |
_commands = [network_cmds, port_cmds, subnet_cmds, ip_cmds] |
55 |
|
56 |
|
57 |
about_authentication = '\nUser Authentication:\
|
58 |
\n to check authentication: [kamaki] ]user authenticate\
|
59 |
\n to set authentication token: \
|
60 |
[kamaki] config set cloud.<CLOUD>.token <TOKEN>'
|
61 |
|
62 |
|
63 |
class _port_wait(_service_wait): |
64 |
|
65 |
def _wait(self, port_id, current_status, timeout=60): |
66 |
super(_port_wait, self)._wait( |
67 |
'Port', port_id, self.client.wait_port, current_status, |
68 |
timeout=timeout) |
69 |
|
70 |
|
71 |
class _init_network(_command_init): |
72 |
@errors.generic.all
|
73 |
@addLogSettings
|
74 |
def _run(self, service='network'): |
75 |
if getattr(self, 'cloud', None): |
76 |
base_url = self._custom_url(service) or self._custom_url( |
77 |
'network')
|
78 |
if base_url:
|
79 |
token = self._custom_token(service) or self._custom_token( |
80 |
'network') or self.config.get_cloud('token') |
81 |
self.client = CycladesNetworkClient(
|
82 |
base_url=base_url, token=token) |
83 |
return
|
84 |
else:
|
85 |
self.cloud = 'default' |
86 |
if getattr(self, 'auth_base', False): |
87 |
network_endpoints = self.auth_base.get_service_endpoints(
|
88 |
self._custom_type('network') or 'network', |
89 |
self._custom_version('network') or '') |
90 |
base_url = network_endpoints['publicURL']
|
91 |
token = self.auth_base.token
|
92 |
self.client = CycladesNetworkClient(base_url=base_url, token=token)
|
93 |
else:
|
94 |
raise CLIBaseUrlError(service='network') |
95 |
|
96 |
def _filter_by_user_id(self, nets): |
97 |
return [net for net in nets if net['user_id'] == self['user_id']] if ( |
98 |
self['user_id']) else nets |
99 |
|
100 |
def main(self): |
101 |
self._run()
|
102 |
|
103 |
|
104 |
@command(network_cmds)
|
105 |
class network_list(_init_network, _optional_json, _name_filter, _id_filter): |
106 |
"""List networks
|
107 |
Use filtering arguments (e.g., --name-like) to manage long server lists
|
108 |
"""
|
109 |
|
110 |
arguments = dict(
|
111 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
112 |
more=FlagArgument( |
113 |
'output results in pages (-n to set items per page, default 10)',
|
114 |
'--more'),
|
115 |
user_id=ValueArgument( |
116 |
'show only networks belonging to user with this id', '--user-id') |
117 |
) |
118 |
|
119 |
@errors.generic.all
|
120 |
@errors.cyclades.connection
|
121 |
def _run(self): |
122 |
detail = bool(self['detail'] or self['user_id']) |
123 |
nets = self.client.list_networks(detail=detail)
|
124 |
nets = self._filter_by_user_id(nets)
|
125 |
nets = self._filter_by_name(nets)
|
126 |
nets = self._filter_by_id(nets)
|
127 |
if detail and not self['detail']: |
128 |
nets = [dict(
|
129 |
id=n['id'], name=n['name'], links=n['links']) for n in nets] |
130 |
kwargs = dict()
|
131 |
if self['more']: |
132 |
kwargs['out'] = StringIO()
|
133 |
kwargs['title'] = ()
|
134 |
self._print(nets, **kwargs)
|
135 |
if self['more']: |
136 |
pager(kwargs['out'].getvalue())
|
137 |
|
138 |
def main(self): |
139 |
super(self.__class__, self)._run() |
140 |
self._run()
|
141 |
|
142 |
|
143 |
@command(network_cmds)
|
144 |
class network_info(_init_network, _optional_json): |
145 |
"""Get details about a network"""
|
146 |
|
147 |
@errors.generic.all
|
148 |
@errors.cyclades.connection
|
149 |
@errors.cyclades.network_id
|
150 |
def _run(self, network_id): |
151 |
net = self.client.get_network_details(network_id)
|
152 |
self._print(net, self.print_dict) |
153 |
|
154 |
def main(self, network_id): |
155 |
super(self.__class__, self)._run() |
156 |
self._run(network_id=network_id)
|
157 |
|
158 |
|
159 |
class NetworkTypeArgument(ValueArgument): |
160 |
|
161 |
types = ('MAC_FILTERED', 'CUSTOM', 'IP_LESS_ROUTED', 'PHYSICAL_VLAN') |
162 |
|
163 |
@property
|
164 |
def value(self): |
165 |
return getattr(self, '_value', self.types[0]) |
166 |
|
167 |
@value.setter
|
168 |
def value(self, new_value): |
169 |
if new_value and new_value.upper() in self.types: |
170 |
self._value = new_value.upper()
|
171 |
elif new_value:
|
172 |
raise CLIInvalidArgument(
|
173 |
'Invalid network type %s' % new_value, details=[
|
174 |
'Valid types: %s' % ', '.join(self.types), ]) |
175 |
|
176 |
|
177 |
@command(network_cmds)
|
178 |
class network_create(_init_network, _optional_json): |
179 |
"""Create a new network (default type: MAC_FILTERED)"""
|
180 |
|
181 |
arguments = dict(
|
182 |
name=ValueArgument('Network name', '--name'), |
183 |
shared=FlagArgument( |
184 |
'Make network shared (special privileges required)', '--shared'), |
185 |
network_type=NetworkTypeArgument( |
186 |
'Valid network types: %s' % (', '.join(NetworkTypeArgument.types)), |
187 |
'--type')
|
188 |
) |
189 |
|
190 |
@errors.generic.all
|
191 |
@errors.cyclades.connection
|
192 |
@errors.cyclades.network_type
|
193 |
def _run(self, network_type): |
194 |
net = self.client.create_network(
|
195 |
network_type, name=self['name'], shared=self['shared']) |
196 |
self._print(net, self.print_dict) |
197 |
|
198 |
def main(self): |
199 |
super(self.__class__, self)._run() |
200 |
self._run(network_type=self['network_type']) |
201 |
|
202 |
|
203 |
@command(network_cmds)
|
204 |
class network_delete(_init_network, _optional_output_cmd): |
205 |
"""Delete a network"""
|
206 |
|
207 |
@errors.generic.all
|
208 |
@errors.cyclades.connection
|
209 |
@errors.cyclades.network_id
|
210 |
def _run(self, network_id): |
211 |
r = self.client.delete_network(network_id)
|
212 |
self._optional_output(r)
|
213 |
|
214 |
def main(self, network_id): |
215 |
super(self.__class__, self)._run() |
216 |
self._run(network_id=network_id)
|
217 |
|
218 |
|
219 |
@command(network_cmds)
|
220 |
class network_modify(_init_network, _optional_json): |
221 |
"""Modify network attributes"""
|
222 |
|
223 |
arguments = dict(new_name=ValueArgument('Rename the network', '--name')) |
224 |
required = ['new_name', ]
|
225 |
|
226 |
@errors.generic.all
|
227 |
@errors.cyclades.connection
|
228 |
@errors.cyclades.network_id
|
229 |
def _run(self, network_id): |
230 |
r = self.client.update_network(network_id, name=self['new_name']) |
231 |
self._print(r, self.print_dict) |
232 |
|
233 |
def main(self, network_id): |
234 |
super(self.__class__, self)._run() |
235 |
self._run(network_id=network_id)
|
236 |
|
237 |
|
238 |
@command(subnet_cmds)
|
239 |
class subnet_list(_init_network, _optional_json, _name_filter, _id_filter): |
240 |
"""List subnets
|
241 |
Use filtering arguments (e.g., --name-like) to manage long server lists
|
242 |
"""
|
243 |
|
244 |
arguments = dict(
|
245 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
246 |
more=FlagArgument( |
247 |
'output results in pages (-n to set items per page, default 10)',
|
248 |
'--more')
|
249 |
) |
250 |
|
251 |
@errors.generic.all
|
252 |
@errors.cyclades.connection
|
253 |
def _run(self): |
254 |
nets = self.client.list_subnets()
|
255 |
nets = self._filter_by_name(nets)
|
256 |
nets = self._filter_by_id(nets)
|
257 |
if not self['detail']: |
258 |
nets = [dict(
|
259 |
id=n['id'], name=n['name'], links=n['links']) for n in nets] |
260 |
kwargs = dict()
|
261 |
if self['more']: |
262 |
kwargs['out'] = StringIO()
|
263 |
kwargs['title'] = ()
|
264 |
self._print(nets, **kwargs)
|
265 |
if self['more']: |
266 |
pager(kwargs['out'].getvalue())
|
267 |
|
268 |
def main(self): |
269 |
super(self.__class__, self)._run() |
270 |
self._run()
|
271 |
|
272 |
|
273 |
@command(subnet_cmds)
|
274 |
class subnet_info(_init_network, _optional_json): |
275 |
"""Get details about a subnet"""
|
276 |
|
277 |
@errors.generic.all
|
278 |
@errors.cyclades.connection
|
279 |
def _run(self, subnet_id): |
280 |
net = self.client.get_subnet_details(subnet_id)
|
281 |
self._print(net, self.print_dict) |
282 |
|
283 |
def main(self, subnet_id): |
284 |
super(self.__class__, self)._run() |
285 |
self._run(subnet_id=subnet_id)
|
286 |
|
287 |
|
288 |
class AllocationPoolArgument(RepeatableArgument): |
289 |
|
290 |
@property
|
291 |
def value(self): |
292 |
return super(AllocationPoolArgument, self).value or [] |
293 |
|
294 |
@value.setter
|
295 |
def value(self, new_pools): |
296 |
if not new_pools: |
297 |
return
|
298 |
new_list = [] |
299 |
for pool in new_pools: |
300 |
start, comma, end = pool.partition(',')
|
301 |
if not (start and comma and end): |
302 |
raise CLIInvalidArgument(
|
303 |
'Invalid allocation pool argument %s' % pool, details=[
|
304 |
'Allocation values must be of the form:',
|
305 |
' <start address>,<end address>'])
|
306 |
new_list.append(dict(start=start, end=end))
|
307 |
self._value = new_list
|
308 |
|
309 |
|
310 |
@command(subnet_cmds)
|
311 |
class subnet_create(_init_network, _optional_json): |
312 |
"""Create a new subnet"""
|
313 |
|
314 |
arguments = dict(
|
315 |
name=ValueArgument('Subnet name', '--name'), |
316 |
allocation_pools=AllocationPoolArgument( |
317 |
'start_address,end_address of allocation pool (can be repeated)'
|
318 |
' e.g., --alloc-pool=123.45.67.1,123.45.67.8',
|
319 |
'--alloc-pool'),
|
320 |
gateway=ValueArgument('Gateway IP', '--gateway'), |
321 |
subnet_id=ValueArgument('The id for the subnet', '--id'), |
322 |
ipv6=FlagArgument('If set, IP version is set to 6, else 4', '--ipv6'), |
323 |
enable_dhcp=FlagArgument('Enable dhcp (default: off)', '--with-dhcp'), |
324 |
network_id=ValueArgument('Set the network ID', '--network-id'), |
325 |
cidr=ValueArgument('Set the CIDR', '--cidr') |
326 |
) |
327 |
required = ('network_id', 'cidr') |
328 |
|
329 |
@errors.generic.all
|
330 |
@errors.cyclades.connection
|
331 |
@errors.cyclades.network_id
|
332 |
def _run(self, network_id, cidr): |
333 |
net = self.client.create_subnet(
|
334 |
network_id, cidr, |
335 |
self['name'], self['allocation_pools'], self['gateway'], |
336 |
self['subnet_id'], self['ipv6'], self['enable_dhcp']) |
337 |
self._print(net, self.print_dict) |
338 |
|
339 |
def main(self): |
340 |
super(self.__class__, self)._run() |
341 |
self._run(network_id=self['network_id'], cidr=self['cidr']) |
342 |
|
343 |
|
344 |
# @command(subnet_cmds)
|
345 |
# class subnet_delete(_init_network, _optional_output_cmd):
|
346 |
# """Delete a subnet"""
|
347 |
|
348 |
# @errors.generic.all
|
349 |
# @errors.cyclades.connection
|
350 |
# def _run(self, subnet_id):
|
351 |
# r = self.client.delete_subnet(subnet_id)
|
352 |
# self._optional_output(r)
|
353 |
|
354 |
# def main(self, subnet_id):
|
355 |
# super(self.__class__, self)._run()
|
356 |
# self._run(subnet_id=subnet_id)
|
357 |
|
358 |
|
359 |
@command(subnet_cmds)
|
360 |
class subnet_modify(_init_network, _optional_json): |
361 |
"""Modify the attributes of a subnet"""
|
362 |
|
363 |
arguments = dict(
|
364 |
new_name=ValueArgument('New name of the subnet', '--name') |
365 |
) |
366 |
required = ['new_name']
|
367 |
|
368 |
@errors.generic.all
|
369 |
@errors.cyclades.connection
|
370 |
def _run(self, subnet_id): |
371 |
r = self.client.update_subnet(subnet_id, name=self['new_name']) |
372 |
self._print(r, self.print_dict) |
373 |
|
374 |
def main(self, subnet_id): |
375 |
super(self.__class__, self)._run() |
376 |
self._run(subnet_id=subnet_id)
|
377 |
|
378 |
|
379 |
@command(port_cmds)
|
380 |
class port_list(_init_network, _optional_json, _name_filter, _id_filter): |
381 |
"""List all ports"""
|
382 |
|
383 |
arguments = dict(
|
384 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
385 |
more=FlagArgument( |
386 |
'output results in pages (-n to set items per page, default 10)',
|
387 |
'--more'),
|
388 |
user_id=ValueArgument( |
389 |
'show only networks belonging to user with this id', '--user-id') |
390 |
) |
391 |
|
392 |
@errors.generic.all
|
393 |
@errors.cyclades.connection
|
394 |
def _run(self): |
395 |
detail = bool(self['detail'] or self['user_id']) |
396 |
ports = self.client.list_ports(detail=detail)
|
397 |
ports = self._filter_by_user_id(ports)
|
398 |
ports = self._filter_by_name(ports)
|
399 |
ports = self._filter_by_id(ports)
|
400 |
if detail and not self['detail']: |
401 |
ports = [dict(
|
402 |
id=p['id'], name=p['name'], links=p['links']) for p in ports] |
403 |
kwargs = dict()
|
404 |
if self['more']: |
405 |
kwargs['out'] = StringIO()
|
406 |
kwargs['title'] = ()
|
407 |
self._print(ports, **kwargs)
|
408 |
if self['more']: |
409 |
pager(kwargs['out'].getvalue())
|
410 |
|
411 |
def main(self): |
412 |
super(self.__class__, self)._run() |
413 |
self._run()
|
414 |
|
415 |
|
416 |
@command(port_cmds)
|
417 |
class port_info(_init_network, _optional_json): |
418 |
"""Get details about a port"""
|
419 |
|
420 |
@errors.generic.all
|
421 |
@errors.cyclades.connection
|
422 |
def _run(self, port_id): |
423 |
port = self.client.get_port_details(port_id)
|
424 |
self._print(port, self.print_dict) |
425 |
|
426 |
def main(self, port_id): |
427 |
super(self.__class__, self)._run() |
428 |
self._run(port_id=port_id)
|
429 |
|
430 |
|
431 |
@command(port_cmds)
|
432 |
class port_delete(_init_network, _optional_output_cmd, _port_wait): |
433 |
"""Delete a port (== disconnect server from network)"""
|
434 |
|
435 |
arguments = dict(
|
436 |
wait=FlagArgument('Wait port to be established', ('-w', '--wait')) |
437 |
) |
438 |
|
439 |
@errors.generic.all
|
440 |
@errors.cyclades.connection
|
441 |
def _run(self, port_id): |
442 |
if self['wait']: |
443 |
status = self.client.get_port_details(port_id)['status'] |
444 |
r = self.client.delete_port(port_id)
|
445 |
if self['wait']: |
446 |
try:
|
447 |
self._wait(port_id, status)
|
448 |
except ClientError as ce: |
449 |
if ce.status not in (404, ): |
450 |
raise
|
451 |
self.error('Port %s is deleted' % port_id) |
452 |
self._optional_output(r)
|
453 |
|
454 |
def main(self, port_id): |
455 |
super(self.__class__, self)._run() |
456 |
self._run(port_id=port_id)
|
457 |
|
458 |
|
459 |
@command(port_cmds)
|
460 |
class port_modify(_init_network, _optional_json): |
461 |
"""Modify the attributes of a port"""
|
462 |
|
463 |
arguments = dict(new_name=ValueArgument('New name of the port', '--name')) |
464 |
required = ['new_name', ]
|
465 |
|
466 |
@errors.generic.all
|
467 |
@errors.cyclades.connection
|
468 |
def _run(self, port_id): |
469 |
r = self.client.get_port_details(port_id)
|
470 |
r = self.client.update_port(
|
471 |
port_id, r['network_id'], name=self['new_name']) |
472 |
self._print(r, self.print_dict) |
473 |
|
474 |
def main(self, port_id): |
475 |
super(self.__class__, self)._run() |
476 |
self._run(port_id=port_id)
|
477 |
|
478 |
|
479 |
class PortStatusArgument(ValueArgument): |
480 |
|
481 |
valid = ('BUILD', 'ACTIVE', 'DOWN', 'ERROR') |
482 |
|
483 |
@property
|
484 |
def value(self): |
485 |
return getattr(self, '_value', None) |
486 |
|
487 |
@value.setter
|
488 |
def value(self, new_status): |
489 |
if new_status:
|
490 |
new_status = new_status.upper() |
491 |
if new_status in self.valid: |
492 |
raise CLIInvalidArgument(
|
493 |
'Invalid argument %s' % new_status, details=[
|
494 |
'Status valid values: %s'] % ', '.join(self.valid)) |
495 |
self._value = new_status
|
496 |
|
497 |
|
498 |
class _port_create(_init_network, _optional_json, _port_wait): |
499 |
|
500 |
def connect(self, network_id, device_id): |
501 |
fixed_ips = [dict(ip_address=self['ip_address'])] if ( |
502 |
self['ip_address']) else None |
503 |
if fixed_ips and self['subnet_id']: |
504 |
fixed_ips[0]['subnet_id'] = self['subnet_id'] |
505 |
r = self.client.create_port(
|
506 |
network_id, device_id, |
507 |
name=self['name'], |
508 |
security_groups=self['security_group_id'], |
509 |
fixed_ips=fixed_ips) |
510 |
if self['wait']: |
511 |
self._wait(r['id'], r['status']) |
512 |
r = self.client.get_port_details(r['id']) |
513 |
self._print([r])
|
514 |
|
515 |
|
516 |
@command(port_cmds)
|
517 |
class port_create(_port_create): |
518 |
"""Create a new port (== connect server to network)"""
|
519 |
|
520 |
arguments = dict(
|
521 |
name=ValueArgument('A human readable name', '--name'), |
522 |
security_group_id=RepeatableArgument( |
523 |
'Add a security group id (can be repeated)',
|
524 |
('-g', '--security-group')), |
525 |
subnet_id=ValueArgument( |
526 |
'Subnet id for fixed ips (used with --ip-address)',
|
527 |
'--subnet-id'),
|
528 |
ip_address=ValueArgument( |
529 |
'IP address for subnet id', '--ip-address'), |
530 |
network_id=ValueArgument('Set the network ID', '--network-id'), |
531 |
device_id=ValueArgument( |
532 |
'The device is either a virtual server or a virtual router',
|
533 |
'--device-id'),
|
534 |
wait=FlagArgument('Wait port to be established', ('-w', '--wait')), |
535 |
) |
536 |
required = ('network_id', 'device_id') |
537 |
|
538 |
@errors.generic.all
|
539 |
@errors.cyclades.connection
|
540 |
@errors.cyclades.network_id
|
541 |
@errors.cyclades.server_id
|
542 |
def _run(self, network_id, server_id): |
543 |
self.connect(network_id, server_id)
|
544 |
|
545 |
def main(self): |
546 |
super(self.__class__, self)._run() |
547 |
self._run(network_id=self['network_id'], server_id=self['device_id']) |
548 |
|
549 |
|
550 |
@command(port_cmds)
|
551 |
class port_wait(_init_network, _port_wait): |
552 |
"""Wait for port to finish [ACTIVE, DOWN, BUILD, ERROR]"""
|
553 |
|
554 |
arguments = dict(
|
555 |
current_status=PortStatusArgument( |
556 |
'Wait while in this status', '--status'), |
557 |
timeout=IntArgument( |
558 |
'Wait limit in seconds (default: 60)', '--timeout', default=60) |
559 |
) |
560 |
|
561 |
@errors.generic.all
|
562 |
@errors.cyclades.connection
|
563 |
def _run(self, port_id, current_status): |
564 |
port = self.client.get_port_details(port_id)
|
565 |
if port['status'].lower() == current_status.lower(): |
566 |
self._wait(port_id, current_status, timeout=self['timeout']) |
567 |
else:
|
568 |
self.error(
|
569 |
'Port %s: Cannot wait for status %s, '
|
570 |
'status is already %s' % (
|
571 |
port_id, current_status, port['status']))
|
572 |
|
573 |
def main(self, port_id): |
574 |
super(self.__class__, self)._run() |
575 |
current_status = self['current_status'] or self.arguments[ |
576 |
'current_status'].valid[0] |
577 |
self._run(port_id=port_id, current_status=current_status)
|
578 |
|
579 |
|
580 |
@command(ip_cmds)
|
581 |
class ip_list(_init_network, _optional_json): |
582 |
"""List reserved floating IPs"""
|
583 |
|
584 |
@errors.generic.all
|
585 |
@errors.cyclades.connection
|
586 |
def _run(self): |
587 |
self._print(self.client.list_floatingips()) |
588 |
|
589 |
def main(self): |
590 |
super(self.__class__, self)._run() |
591 |
self._run()
|
592 |
|
593 |
|
594 |
@command(ip_cmds)
|
595 |
class ip_info(_init_network, _optional_json): |
596 |
"""Get details on a floating IP"""
|
597 |
|
598 |
@errors.generic.all
|
599 |
@errors.cyclades.connection
|
600 |
def _run(self, ip_id): |
601 |
self._print(
|
602 |
self.client.get_floatingip_details(ip_id), self.print_dict) |
603 |
|
604 |
def main(self, ip_id): |
605 |
super(self.__class__, self)._run() |
606 |
self._run(ip_id=ip_id)
|
607 |
|
608 |
|
609 |
@command(ip_cmds)
|
610 |
class ip_create(_init_network, _optional_json): |
611 |
"""Reserve an IP on a network"""
|
612 |
|
613 |
arguments = dict(
|
614 |
network_id=ValueArgument( |
615 |
'The network to preserve the IP on', '--network-id'), |
616 |
ip_address=ValueArgument('Allocate an IP address', '--address') |
617 |
) |
618 |
required = ('network_id', )
|
619 |
|
620 |
@errors.generic.all
|
621 |
@errors.cyclades.connection
|
622 |
@errors.cyclades.network_id
|
623 |
def _run(self, network_id): |
624 |
self._print(
|
625 |
self.client.create_floatingip(
|
626 |
network_id, floating_ip_address=self['ip_address']), |
627 |
self.print_dict)
|
628 |
|
629 |
def main(self): |
630 |
super(self.__class__, self)._run() |
631 |
self._run(network_id=self['network_id']) |
632 |
|
633 |
|
634 |
@command(ip_cmds)
|
635 |
class ip_delete(_init_network, _optional_output_cmd): |
636 |
"""Unreserve an IP (also delete the port, if attached)"""
|
637 |
|
638 |
def _run(self, ip_id): |
639 |
self._optional_output(self.client.delete_floatingip(ip_id)) |
640 |
|
641 |
def main(self, ip_id): |
642 |
super(self.__class__, self)._run() |
643 |
self._run(ip_id=ip_id)
|
644 |
|
645 |
|
646 |
@command(ip_cmds)
|
647 |
class ip_attach(_port_create): |
648 |
"""Attach an IP on a virtual server"""
|
649 |
|
650 |
arguments = dict(
|
651 |
name=ValueArgument('A human readable name for the port', '--name'), |
652 |
security_group_id=RepeatableArgument( |
653 |
'Add a security group id (can be repeated)',
|
654 |
('-g', '--security-group')), |
655 |
subnet_id=ValueArgument('Subnet id', '--subnet-id'), |
656 |
wait=FlagArgument('Wait IP to be attached', ('-w', '--wait')), |
657 |
server_id=ValueArgument( |
658 |
'Server to attach to this IP', '--server-id') |
659 |
) |
660 |
required = ('server_id', )
|
661 |
|
662 |
@errors.generic.all
|
663 |
@errors.cyclades.connection
|
664 |
@errors.cyclades.server_id
|
665 |
def _run(self, ip_address, server_id): |
666 |
netid = None
|
667 |
for ip in self.client.list_floatingips(): |
668 |
if ip['floating_ip_address'] == ip_address: |
669 |
netid = ip['floating_network_id']
|
670 |
iparg = ValueArgument(parsed_name='--ip')
|
671 |
iparg.value = ip_address |
672 |
self.arguments['ip_address'] = iparg |
673 |
break
|
674 |
if netid:
|
675 |
self.error('Creating a port to attach IP %s to server %s' % ( |
676 |
ip_address, server_id)) |
677 |
self.connect(netid, server_id)
|
678 |
else:
|
679 |
raiseCLIError( |
680 |
'IP address %s does not match any reserved IPs' % ip_address,
|
681 |
details=[ |
682 |
'To reserve an IP:', ' [kamaki] ip create', |
683 |
'To see all reserved IPs:', ' [kamaki] ip list']) |
684 |
|
685 |
def main(self, ip_address): |
686 |
super(self.__class__, self)._run() |
687 |
self._run(ip_address=ip_address, server_id=self['server_id']) |
688 |
|
689 |
|
690 |
@command(ip_cmds)
|
691 |
class ip_detach(_init_network, _port_wait, _optional_json): |
692 |
"""Detach an IP from a virtual server"""
|
693 |
|
694 |
arguments = dict(
|
695 |
wait=FlagArgument('Wait network to disconnect', ('-w', '--wait')), |
696 |
) |
697 |
|
698 |
@errors.generic.all
|
699 |
@errors.cyclades.connection
|
700 |
def _run(self, ip_address): |
701 |
for ip in self.client.list_floatingips(): |
702 |
if ip['floating_ip_address'] == ip_address: |
703 |
if not ip['port_id']: |
704 |
raiseCLIError('IP %s is not attached' % ip_address)
|
705 |
self.error('Deleting port %s:' % ip['port_id']) |
706 |
self.client.delete_port(ip['port_id']) |
707 |
if self['wait']: |
708 |
port_status = self.client.get_port_details(ip['port_id'])[ |
709 |
'status']
|
710 |
try:
|
711 |
self._wait(ip['port_id'], port_status) |
712 |
except ClientError as ce: |
713 |
if ce.status not in (404, ): |
714 |
raise
|
715 |
self.error('Port %s is deleted' % ip['port_id']) |
716 |
return
|
717 |
raiseCLIError('IP %s not found' % ip_address)
|
718 |
|
719 |
def main(self, ip_address): |
720 |
super(self.__class__, self)._run() |
721 |
self._run(ip_address)
|
722 |
|
723 |
|
724 |
# Warn users for some importand changes
|
725 |
|
726 |
@command(network_cmds)
|
727 |
class network_connect(_port_create): |
728 |
"""Connect a network with a device (server or router)"""
|
729 |
|
730 |
arguments = dict(
|
731 |
name=ValueArgument('A human readable name for the port', '--name'), |
732 |
security_group_id=RepeatableArgument( |
733 |
'Add a security group id (can be repeated)',
|
734 |
('-g', '--security-group')), |
735 |
subnet_id=ValueArgument( |
736 |
'Subnet id for fixed ips (used with --ip-address)',
|
737 |
'--subnet-id'),
|
738 |
ip_address=ValueArgument( |
739 |
'IP address for subnet id (used with --subnet-id', '--ip-address'), |
740 |
wait=FlagArgument('Wait network to connect', ('-w', '--wait')), |
741 |
device_id=RepeatableArgument( |
742 |
'Connect this device to the network (can be repeated)',
|
743 |
'--device-id')
|
744 |
) |
745 |
required = ('device_id', )
|
746 |
|
747 |
@errors.generic.all
|
748 |
@errors.cyclades.connection
|
749 |
@errors.cyclades.network_id
|
750 |
@errors.cyclades.server_id
|
751 |
def _run(self, network_id, server_id): |
752 |
self.error('Creating a port to connect network %s with device %s' % ( |
753 |
network_id, server_id)) |
754 |
self.connect(network_id, server_id)
|
755 |
|
756 |
def main(self, network_id): |
757 |
super(self.__class__, self)._run() |
758 |
for sid in self['device_id']: |
759 |
self._run(network_id=network_id, server_id=sid)
|
760 |
|
761 |
|
762 |
@command(network_cmds)
|
763 |
class network_disconnect(_init_network, _port_wait, _optional_json): |
764 |
"""Disconnect a network from a device"""
|
765 |
|
766 |
def _cyclades_client(self): |
767 |
auth = getattr(self, 'auth_base') |
768 |
endpoints = auth.get_service_endpoints('compute')
|
769 |
URL = endpoints['publicURL']
|
770 |
from kamaki.clients.cyclades import CycladesClient |
771 |
return CycladesClient(URL, self.client.token) |
772 |
|
773 |
arguments = dict(
|
774 |
wait=FlagArgument('Wait network to disconnect', ('-w', '--wait')), |
775 |
device_id=RepeatableArgument( |
776 |
'Disconnect device from the network (can be repeated)',
|
777 |
'--device-id')
|
778 |
) |
779 |
required = ('device_id', )
|
780 |
|
781 |
@errors.generic.all
|
782 |
@errors.cyclades.connection
|
783 |
@errors.cyclades.network_id
|
784 |
@errors.cyclades.server_id
|
785 |
def _run(self, network_id, server_id): |
786 |
vm = self._cyclades_client().get_server_details(server_id)
|
787 |
ports = [port for port in vm['attachments'] if ( |
788 |
port['network_id'] in (network_id, ))] |
789 |
if not ports: |
790 |
raiseCLIError('Network %s is not connected to device %s' % (
|
791 |
network_id, server_id)) |
792 |
for port in ports: |
793 |
if self['wait']: |
794 |
port['status'] = self.client.get_port_details(port['id'])[ |
795 |
'status']
|
796 |
self.client.delete_port(port['id']) |
797 |
self.error('Deleting port %s:' % port['id']) |
798 |
self.print_dict(port)
|
799 |
if self['wait']: |
800 |
try:
|
801 |
self._wait(port['id'], port['status']) |
802 |
except ClientError as ce: |
803 |
if ce.status not in (404, ): |
804 |
raise
|
805 |
self.error('Port %s is deleted' % port['id']) |
806 |
|
807 |
def main(self, network_id): |
808 |
super(self.__class__, self)._run() |
809 |
for sid in self['device_id']: |
810 |
self._run(network_id=network_id, server_id=sid)
|