Revision b7d79306
b/kamaki/cli/commands/network.py | ||
---|---|---|
441 | 441 |
"""Create a new port""" |
442 | 442 |
|
443 | 443 |
arguments = dict( |
444 |
name=ValueArgument('A human readable name', '--name'), |
|
444 | 445 |
security_group_id=RepeatableArgument( |
445 | 446 |
'Add a security group id (can be repeated)', |
446 |
('-g', '--security-group')) |
|
447 |
('-g', '--security-group')), |
|
448 |
subnet_id=ValueArgument( |
|
449 |
'Subnet id for fixed ips (used with --ip-address)', |
|
450 |
'--subnet-id'), |
|
451 |
ip_address=ValueArgument( |
|
452 |
'IP address for subnet id (used with --subnet-id', '--ip-address') |
|
447 | 453 |
) |
448 | 454 |
|
449 | 455 |
@errors.generic.all |
450 | 456 |
@errors.cyclades.connection |
451 | 457 |
@errors.cyclades.network_id |
452 | 458 |
def _run(self, network_id, device_id): |
459 |
if not (bool(self['subnet_id']) ^ bool(self['ip_address'])): |
|
460 |
raise CLIInvalidArgument('Invalid use of arguments', details=[ |
|
461 |
'--subned-id and --ip-address should be used together']) |
|
462 |
fixed_ips = dict( |
|
463 |
subnet_id=self['subnet_id'], ip_address=self['ip_address']) if ( |
|
464 |
self['subnet_id']) else None |
|
453 | 465 |
r = self.client.create_port( |
454 |
network_id, device_id, security_groups=self['security_group_id']) |
|
466 |
network_id, device_id, |
|
467 |
name=self['name'], |
|
468 |
security_groups=self['security_group_id'], |
|
469 |
fixed_ips=fixed_ips) |
|
455 | 470 |
self._print(r, self.print_dict) |
456 | 471 |
|
457 | 472 |
def main(self, network_id, device_id): |
b/kamaki/clients/cyclades/__init__.py | ||
---|---|---|
527 | 527 |
return r.json['network'] |
528 | 528 |
|
529 | 529 |
def create_port( |
530 |
self, network_id, device_id, security_groups=None, name=None): |
|
530 |
self, network_id, device_id, |
|
531 |
security_groups=None, name=None, fixed_ips=None): |
|
531 | 532 |
port = dict(network_id=network_id, device_id=device_id) |
532 | 533 |
if security_groups: |
533 | 534 |
port['security_groups'] = security_groups |
534 | 535 |
if name: |
535 | 536 |
port['name'] = name |
537 |
if fixed_ips: |
|
538 |
diff = set(['subnet_id', 'ip_address']).difference(fixed_ips) |
|
539 |
if diff: |
|
540 |
raise ValueError( |
|
541 |
'Invalid format for "fixed_ips", %s missing' % diff) |
|
542 |
port['fixed_ips'] = fixed_ips |
|
536 | 543 |
r = self.ports_post(json_data=dict(port=port), success=201) |
537 | 544 |
return r.json['port'] |
b/kamaki/clients/cyclades/test.py | ||
---|---|---|
282 | 282 |
return_value=FR) |
283 | 283 |
def test_create_port(self, ports_post): |
284 | 284 |
network_id, device_id, FR.json = 'netid', 'devid', dict(port='ret v') |
285 |
for name, sec_grp in product(('port name', None), ([1, 2, 3], None)): |
|
285 |
for name, sec_grp, fixed_ips in product( |
|
286 |
('port name', None), |
|
287 |
([1, 2, 3], None), |
|
288 |
( |
|
289 |
dict(subnet_id='sid', ip_address='ipa'), |
|
290 |
dict(subnet_id='sid'), dict(ip_address='ipa'), |
|
291 |
None)): |
|
292 |
|
|
293 |
if fixed_ips: |
|
294 |
diff = set(['subnet_id', 'ip_address']).difference(fixed_ips) |
|
295 |
if diff: |
|
296 |
self.assertRaises( |
|
297 |
ValueError, self.client.create_port, |
|
298 |
network_id, device_id, |
|
299 |
name=name, |
|
300 |
security_groups=sec_grp, |
|
301 |
fixed_ips=fixed_ips) |
|
302 |
continue |
|
286 | 303 |
self.assertEqual( |
287 | 304 |
self.client.create_port( |
288 | 305 |
network_id, device_id, |
289 |
name=name, security_groups=sec_grp), |
|
306 |
name=name, security_groups=sec_grp, fixed_ips=fixed_ips),
|
|
290 | 307 |
'ret v') |
291 | 308 |
req = dict(network_id=network_id, device_id=device_id) |
292 | 309 |
if sec_grp: |
293 | 310 |
req['security_groups'] = sec_grp |
294 | 311 |
if name: |
295 | 312 |
req['name'] = name |
313 |
if fixed_ips: |
|
314 |
req['fixed_ips'] = fixed_ips |
|
296 | 315 |
expargs = dict(json_data=dict(port=req), success=201) |
297 | 316 |
self.assertEqual(ports_post.mock_calls[-1], call(**expargs)) |
298 | 317 |
|
Also available in: Unified diff