Add fixed_ips in post create
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 8 Nov 2013 12:02:23 +0000 (14:02 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 8 Nov 2013 12:02:23 +0000 (14:02 +0200)
Refs: #4563

kamaki/cli/commands/network.py
kamaki/clients/cyclades/__init__.py
kamaki/clients/cyclades/test.py

index 922a690..7bb36cf 100644 (file)
@@ -441,17 +441,32 @@ class port_create(_init_network, _optional_json):
     """Create a new port"""
 
     arguments = dict(
+        name=ValueArgument('A human readable name', '--name'),
         security_group_id=RepeatableArgument(
             'Add a security group id (can be repeated)',
-            ('-g', '--security-group'))
+            ('-g', '--security-group')),
+        subnet_id=ValueArgument(
+            'Subnet id for fixed ips (used with --ip-address)',
+            '--subnet-id'),
+        ip_address=ValueArgument(
+            'IP address for subnet id (used with --subnet-id', '--ip-address')
     )
 
     @errors.generic.all
     @errors.cyclades.connection
     @errors.cyclades.network_id
     def _run(self, network_id, device_id):
+        if not (bool(self['subnet_id']) ^ bool(self['ip_address'])):
+            raise CLIInvalidArgument('Invalid use of arguments', details=[
+                '--subned-id and --ip-address should be used together'])
+        fixed_ips = dict(
+            subnet_id=self['subnet_id'], ip_address=self['ip_address']) if (
+                self['subnet_id']) else None
         r = self.client.create_port(
-            network_id, device_id, security_groups=self['security_group_id'])
+            network_id, device_id,
+            name=self['name'],
+            security_groups=self['security_group_id'],
+            fixed_ips=fixed_ips)
         self._print(r, self.print_dict)
 
     def main(self, network_id, device_id):
index 0959895..bafc736 100644 (file)
@@ -527,11 +527,18 @@ class CycladesNetworkClient(NetworkClient):
         return r.json['network']
 
     def create_port(
-            self, network_id, device_id, security_groups=None, name=None):
+            self, network_id, device_id,
+            security_groups=None, name=None, fixed_ips=None):
         port = dict(network_id=network_id, device_id=device_id)
         if security_groups:
             port['security_groups'] = security_groups
         if name:
             port['name'] = name
+        if fixed_ips:
+            diff = set(['subnet_id', 'ip_address']).difference(fixed_ips)
+            if diff:
+                raise ValueError(
+                    'Invalid format for "fixed_ips", %s missing' % diff)
+            port['fixed_ips'] = fixed_ips
         r = self.ports_post(json_data=dict(port=port), success=201)
         return r.json['port']
index a5a11bd..6026869 100644 (file)
@@ -282,17 +282,36 @@ class CycladesNetworkClient(TestCase):
         return_value=FR)
     def test_create_port(self, ports_post):
         network_id, device_id, FR.json = 'netid', 'devid', dict(port='ret v')
-        for name, sec_grp in product(('port name', None), ([1, 2, 3], None)):
+        for name, sec_grp, fixed_ips in product(
+                ('port name', None),
+                ([1, 2, 3], None),
+                (
+                    dict(subnet_id='sid', ip_address='ipa'),
+                    dict(subnet_id='sid'), dict(ip_address='ipa'),
+                    None)):
+
+            if fixed_ips:
+                diff = set(['subnet_id', 'ip_address']).difference(fixed_ips)
+                if diff:
+                    self.assertRaises(
+                        ValueError, self.client.create_port,
+                        network_id, device_id,
+                        name=name,
+                        security_groups=sec_grp,
+                        fixed_ips=fixed_ips)
+                    continue
             self.assertEqual(
                 self.client.create_port(
                     network_id, device_id,
-                    name=name, security_groups=sec_grp),
+                    name=name, security_groups=sec_grp, fixed_ips=fixed_ips),
                 'ret v')
             req = dict(network_id=network_id, device_id=device_id)
             if sec_grp:
                 req['security_groups'] = sec_grp
             if name:
                 req['name'] = name
+            if fixed_ips:
+                req['fixed_ips'] = fixed_ips
             expargs = dict(json_data=dict(port=req), success=201)
             self.assertEqual(ports_post.mock_calls[-1], call(**expargs))