root / kamaki / cli / commands / network.py @ cc5da5a5
History | View | Annotate | Download (26.4 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.get_subnet_details(subnet_id)
|
372 |
r = self.client.update_subnet(
|
373 |
subnet_id, r['network_id'], name=self['new_name']) |
374 |
self._print(r, self.print_dict) |
375 |
|
376 |
def main(self, subnet_id): |
377 |
super(self.__class__, self)._run() |
378 |
self._run(subnet_id=subnet_id)
|
379 |
|
380 |
|
381 |
@command(port_cmds)
|
382 |
class port_list(_init_network, _optional_json, _name_filter, _id_filter): |
383 |
"""List all ports"""
|
384 |
|
385 |
arguments = dict(
|
386 |
detail=FlagArgument('show detailed output', ('-l', '--details')), |
387 |
more=FlagArgument( |
388 |
'output results in pages (-n to set items per page, default 10)',
|
389 |
'--more'),
|
390 |
user_id=ValueArgument( |
391 |
'show only networks belonging to user with this id', '--user-id') |
392 |
) |
393 |
|
394 |
@errors.generic.all
|
395 |
@errors.cyclades.connection
|
396 |
def _run(self): |
397 |
detail = bool(self['detail'] or self['user_id']) |
398 |
ports = self.client.list_ports(detail=detail)
|
399 |
ports = self._filter_by_user_id(ports)
|
400 |
ports = self._filter_by_name(ports)
|
401 |
ports = self._filter_by_id(ports)
|
402 |
if detail and not self['detail']: |
403 |
ports = [dict(
|
404 |
id=p['id'], name=p['name'], links=p['links']) for p in ports] |
405 |
kwargs = dict()
|
406 |
if self['more']: |
407 |
kwargs['out'] = StringIO()
|
408 |
kwargs['title'] = ()
|
409 |
self._print(ports, **kwargs)
|
410 |
if self['more']: |
411 |
pager(kwargs['out'].getvalue())
|
412 |
|
413 |
def main(self): |
414 |
super(self.__class__, self)._run() |
415 |
self._run()
|
416 |
|
417 |
|
418 |
@command(port_cmds)
|
419 |
class port_info(_init_network, _optional_json): |
420 |
"""Get details about a port"""
|
421 |
|
422 |
@errors.generic.all
|
423 |
@errors.cyclades.connection
|
424 |
def _run(self, port_id): |
425 |
port = self.client.get_port_details(port_id)
|
426 |
self._print(port, self.print_dict) |
427 |
|
428 |
def main(self, port_id): |
429 |
super(self.__class__, self)._run() |
430 |
self._run(port_id=port_id)
|
431 |
|
432 |
|
433 |
@command(port_cmds)
|
434 |
class port_delete(_init_network, _optional_output_cmd, _port_wait): |
435 |
"""Delete a port (== disconnect server from network)"""
|
436 |
|
437 |
arguments = dict(
|
438 |
wait=FlagArgument('Wait port to be established', ('-w', '--wait')) |
439 |
) |
440 |
|
441 |
@errors.generic.all
|
442 |
@errors.cyclades.connection
|
443 |
def _run(self, port_id): |
444 |
if self['wait']: |
445 |
status = self.client.get_port_details(port_id)['status'] |
446 |
r = self.client.delete_port(port_id)
|
447 |
if self['wait']: |
448 |
try:
|
449 |
self._wait(port_id, status)
|
450 |
except ClientError as ce: |
451 |
if ce.status not in (404, ): |
452 |
raise
|
453 |
self.error('Port %s is deleted' % port_id) |
454 |
self._optional_output(r)
|
455 |
|
456 |
def main(self, port_id): |
457 |
super(self.__class__, self)._run() |
458 |
self._run(port_id=port_id)
|
459 |
|
460 |
|
461 |
@command(port_cmds)
|
462 |
class port_modify(_init_network, _optional_json): |
463 |
"""Modify the attributes of a port"""
|
464 |
|
465 |
arguments = dict(new_name=ValueArgument('New name of the port', '--name')) |
466 |
required = ['new_name', ]
|
467 |
|
468 |
@errors.generic.all
|
469 |
@errors.cyclades.connection
|
470 |
def _run(self, port_id): |
471 |
r = self.client.get_port_details(port_id)
|
472 |
r = self.client.update_port(
|
473 |
port_id, r['network_id'], name=self['new_name']) |
474 |
self._print(r, self.print_dict) |
475 |
|
476 |
def main(self, port_id): |
477 |
super(self.__class__, self)._run() |
478 |
self._run(port_id=port_id)
|
479 |
|
480 |
|
481 |
class _port_create(_init_network, _optional_json, _port_wait): |
482 |
|
483 |
def connect(self, network_id, device_id): |
484 |
fixed_ips = [dict(ip_address=self['ip_address'])] if ( |
485 |
self['ip_address']) else None |
486 |
if fixed_ips and self['subnet_id']: |
487 |
fixed_ips[0]['subnet_id'] = self['subnet_id'] |
488 |
r = self.client.create_port(
|
489 |
network_id, device_id, |
490 |
name=self['name'], |
491 |
security_groups=self['security_group_id'], |
492 |
fixed_ips=fixed_ips) |
493 |
if self['wait']: |
494 |
self._wait(r['id'], r['status']) |
495 |
r = self.client.get_port_details(r['id']) |
496 |
self._print([r])
|
497 |
|
498 |
|
499 |
@command(port_cmds)
|
500 |
class port_create(_port_create): |
501 |
"""Create a new port (== connect server to network)"""
|
502 |
|
503 |
arguments = dict(
|
504 |
name=ValueArgument('A human readable name', '--name'), |
505 |
security_group_id=RepeatableArgument( |
506 |
'Add a security group id (can be repeated)',
|
507 |
('-g', '--security-group')), |
508 |
subnet_id=ValueArgument( |
509 |
'Subnet id for fixed ips (used with --ip-address)',
|
510 |
'--subnet-id'),
|
511 |
ip_address=ValueArgument( |
512 |
'IP address for subnet id', '--ip-address'), |
513 |
network_id=ValueArgument('Set the network ID', '--network-id'), |
514 |
device_id=ValueArgument( |
515 |
'The device is either a virtual server or a virtual router',
|
516 |
'--device-id'),
|
517 |
wait=FlagArgument('Wait port to be established', ('-w', '--wait')), |
518 |
) |
519 |
required = ('network_id', 'device_id') |
520 |
|
521 |
@errors.generic.all
|
522 |
@errors.cyclades.connection
|
523 |
@errors.cyclades.network_id
|
524 |
@errors.cyclades.server_id
|
525 |
def _run(self, network_id, server_id): |
526 |
self.connect(network_id, server_id)
|
527 |
|
528 |
def main(self): |
529 |
super(self.__class__, self)._run() |
530 |
self._run(network_id=self['network_id'], server_id=self['device_id']) |
531 |
|
532 |
|
533 |
@command(port_cmds)
|
534 |
class port_wait(_init_network, _port_wait): |
535 |
"""Wait for port to finish [ACTIVE, DOWN, BUILD, ERROR]"""
|
536 |
|
537 |
arguments = dict(
|
538 |
timeout=IntArgument( |
539 |
'Wait limit in seconds (default: 60)', '--timeout', default=60) |
540 |
) |
541 |
|
542 |
@errors.generic.all
|
543 |
@errors.cyclades.connection
|
544 |
def _run(self, port_id, current_status): |
545 |
port = self.client.get_port_details(port_id)
|
546 |
if port['status'].lower() == current_status.lower(): |
547 |
self._wait(port_id, current_status, timeout=self['timeout']) |
548 |
else:
|
549 |
self.error(
|
550 |
'Port %s: Cannot wait for status %s, '
|
551 |
'status is already %s' % (
|
552 |
port_id, current_status, port['status']))
|
553 |
|
554 |
def main(self, port_id, current_status='BUILD'): |
555 |
super(self.__class__, self)._run() |
556 |
self._run(port_id=port_id, current_status=current_status)
|
557 |
|
558 |
|
559 |
@command(ip_cmds)
|
560 |
class ip_list(_init_network, _optional_json): |
561 |
"""List reserved floating IPs"""
|
562 |
|
563 |
@errors.generic.all
|
564 |
@errors.cyclades.connection
|
565 |
def _run(self): |
566 |
self._print(self.client.list_floatingips()) |
567 |
|
568 |
def main(self): |
569 |
super(self.__class__, self)._run() |
570 |
self._run()
|
571 |
|
572 |
|
573 |
@command(ip_cmds)
|
574 |
class ip_info(_init_network, _optional_json): |
575 |
"""Get details on a floating IP"""
|
576 |
|
577 |
@errors.generic.all
|
578 |
@errors.cyclades.connection
|
579 |
def _run(self, ip_id): |
580 |
self._print(
|
581 |
self.client.get_floatingip_details(ip_id), self.print_dict) |
582 |
|
583 |
def main(self, ip_id): |
584 |
super(self.__class__, self)._run() |
585 |
self._run(ip_id=ip_id)
|
586 |
|
587 |
|
588 |
@command(ip_cmds)
|
589 |
class ip_create(_init_network, _optional_json): |
590 |
"""Reserve an IP on a network"""
|
591 |
|
592 |
arguments = dict(
|
593 |
network_id=ValueArgument( |
594 |
'The network to preserve the IP on', '--network-id'), |
595 |
ip_address=ValueArgument('Allocate an IP address', '--address') |
596 |
) |
597 |
required = ('network_id', )
|
598 |
|
599 |
@errors.generic.all
|
600 |
@errors.cyclades.connection
|
601 |
@errors.cyclades.network_id
|
602 |
def _run(self, network_id): |
603 |
self._print(
|
604 |
self.client.create_floatingip(
|
605 |
network_id, floating_ip_address=self['ip_address']), |
606 |
self.print_dict)
|
607 |
|
608 |
def main(self): |
609 |
super(self.__class__, self)._run() |
610 |
self._run(network_id=self['network_id']) |
611 |
|
612 |
|
613 |
@command(ip_cmds)
|
614 |
class ip_delete(_init_network, _optional_output_cmd): |
615 |
"""Unreserve an IP (also delete the port, if attached)"""
|
616 |
|
617 |
def _run(self, ip_id): |
618 |
self._optional_output(self.client.delete_floatingip(ip_id)) |
619 |
|
620 |
def main(self, ip_id): |
621 |
super(self.__class__, self)._run() |
622 |
self._run(ip_id=ip_id)
|
623 |
|
624 |
|
625 |
@command(ip_cmds)
|
626 |
class ip_attach(_port_create): |
627 |
"""Attach an IP on a virtual server"""
|
628 |
|
629 |
arguments = dict(
|
630 |
name=ValueArgument('A human readable name for the port', '--name'), |
631 |
security_group_id=RepeatableArgument( |
632 |
'Add a security group id (can be repeated)',
|
633 |
('-g', '--security-group')), |
634 |
subnet_id=ValueArgument('Subnet id', '--subnet-id'), |
635 |
wait=FlagArgument('Wait IP to be attached', ('-w', '--wait')), |
636 |
server_id=ValueArgument( |
637 |
'Server to attach to this IP', '--server-id') |
638 |
) |
639 |
required = ('server_id', )
|
640 |
|
641 |
@errors.generic.all
|
642 |
@errors.cyclades.connection
|
643 |
@errors.cyclades.server_id
|
644 |
def _run(self, ip_address, server_id): |
645 |
netid = None
|
646 |
for ip in self.client.list_floatingips(): |
647 |
if ip['floating_ip_address'] == ip_address: |
648 |
netid = ip['floating_network_id']
|
649 |
iparg = ValueArgument(parsed_name='--ip')
|
650 |
iparg.value = ip_address |
651 |
self.arguments['ip_address'] = iparg |
652 |
break
|
653 |
if netid:
|
654 |
self.error('Creating a port to attach IP %s to server %s' % ( |
655 |
ip_address, server_id)) |
656 |
self.connect(netid, server_id)
|
657 |
else:
|
658 |
raiseCLIError( |
659 |
'IP address %s does not match any reserved IPs' % ip_address,
|
660 |
details=[ |
661 |
'To reserve an IP:', ' [kamaki] ip create', |
662 |
'To see all reserved IPs:', ' [kamaki] ip list']) |
663 |
|
664 |
def main(self, ip_address): |
665 |
super(self.__class__, self)._run() |
666 |
self._run(ip_address=ip_address, server_id=self['server_id']) |
667 |
|
668 |
|
669 |
@command(ip_cmds)
|
670 |
class ip_detach(_init_network, _port_wait, _optional_json): |
671 |
"""Detach an IP from a virtual server"""
|
672 |
|
673 |
arguments = dict(
|
674 |
wait=FlagArgument('Wait network to disconnect', ('-w', '--wait')), |
675 |
) |
676 |
|
677 |
@errors.generic.all
|
678 |
@errors.cyclades.connection
|
679 |
def _run(self, ip_address): |
680 |
for ip in self.client.list_floatingips(): |
681 |
if ip['floating_ip_address'] == ip_address: |
682 |
if not ip['port_id']: |
683 |
raiseCLIError('IP %s is not attached' % ip_address)
|
684 |
self.error('Deleting port %s:' % ip['port_id']) |
685 |
self.client.delete_port(ip['port_id']) |
686 |
if self['wait']: |
687 |
port_status = self.client.get_port_details(ip['port_id'])[ |
688 |
'status']
|
689 |
try:
|
690 |
self._wait(ip['port_id'], port_status) |
691 |
except ClientError as ce: |
692 |
if ce.status not in (404, ): |
693 |
raise
|
694 |
self.error('Port %s is deleted' % ip['port_id']) |
695 |
return
|
696 |
raiseCLIError('IP %s not found' % ip_address)
|
697 |
|
698 |
def main(self, ip_address): |
699 |
super(self.__class__, self)._run() |
700 |
self._run(ip_address)
|
701 |
|
702 |
|
703 |
# Warn users for some importand changes
|
704 |
|
705 |
@command(network_cmds)
|
706 |
class network_connect(_port_create): |
707 |
"""Connect a network with a device (server or router)"""
|
708 |
|
709 |
arguments = dict(
|
710 |
name=ValueArgument('A human readable name for the port', '--name'), |
711 |
security_group_id=RepeatableArgument( |
712 |
'Add a security group id (can be repeated)',
|
713 |
('-g', '--security-group')), |
714 |
subnet_id=ValueArgument( |
715 |
'Subnet id for fixed ips (used with --ip-address)',
|
716 |
'--subnet-id'),
|
717 |
ip_address=ValueArgument( |
718 |
'IP address for subnet id (used with --subnet-id', '--ip-address'), |
719 |
wait=FlagArgument('Wait network to connect', ('-w', '--wait')), |
720 |
device_id=RepeatableArgument( |
721 |
'Connect this device to the network (can be repeated)',
|
722 |
'--device-id')
|
723 |
) |
724 |
required = ('device_id', )
|
725 |
|
726 |
@errors.generic.all
|
727 |
@errors.cyclades.connection
|
728 |
@errors.cyclades.network_id
|
729 |
@errors.cyclades.server_id
|
730 |
def _run(self, network_id, server_id): |
731 |
self.error('Creating a port to connect network %s with device %s' % ( |
732 |
network_id, server_id)) |
733 |
self.connect(network_id, server_id)
|
734 |
|
735 |
def main(self, network_id): |
736 |
super(self.__class__, self)._run() |
737 |
for sid in self['device_id']: |
738 |
self._run(network_id=network_id, server_id=sid)
|
739 |
|
740 |
|
741 |
@command(network_cmds)
|
742 |
class network_disconnect(_init_network, _port_wait, _optional_json): |
743 |
"""Disconnnect a network from a device"""
|
744 |
|
745 |
def _cyclades_client(self): |
746 |
auth = getattr(self, 'auth_base') |
747 |
endpoints = auth.get_service_endpoints('compute')
|
748 |
URL = endpoints['publicURL']
|
749 |
from kamaki.clients.cyclades import CycladesClient |
750 |
return CycladesClient(URL, self.client.token) |
751 |
|
752 |
arguments = dict(
|
753 |
wait=FlagArgument('Wait network to disconnect', ('-w', '--wait')), |
754 |
device_id=RepeatableArgument( |
755 |
'Disconnect device from the network (can be repeated)',
|
756 |
'--device-id')
|
757 |
) |
758 |
required = ('device_id', )
|
759 |
|
760 |
@errors.generic.all
|
761 |
@errors.cyclades.connection
|
762 |
@errors.cyclades.network_id
|
763 |
@errors.cyclades.server_id
|
764 |
def _run(self, network_id, server_id): |
765 |
vm = self._cyclades_client().get_server_details(server_id)
|
766 |
ports = [port for port in vm['attachments'] if ( |
767 |
port['network_id'] in (network_id, ))] |
768 |
if not ports: |
769 |
raiseCLIError('Network %s is not connected to device %s' % (
|
770 |
network_id, server_id)) |
771 |
for port in ports: |
772 |
if self['wait']: |
773 |
port['status'] = self.client.get_port_details(port['id'])[ |
774 |
'status']
|
775 |
self.client.delete_port(port['id']) |
776 |
self.error('Deleting port %s:' % port['id']) |
777 |
self.print_dict(port)
|
778 |
if self['wait']: |
779 |
try:
|
780 |
self._wait(port['id'], port['status']) |
781 |
except ClientError as ce: |
782 |
if ce.status not in (404, ): |
783 |
raise
|
784 |
self.error('Port %s is deleted' % port['id']) |
785 |
|
786 |
def main(self, network_id): |
787 |
super(self.__class__, self)._run() |
788 |
for sid in self['device_id']: |
789 |
self._run(network_id=network_id, server_id=sid)
|