Revision 447365fe

b/kamaki/cli/commands/network.py
362 362
    def main(self, subnet_id):
363 363
        super(self.__class__, self)._run()
364 364
        self._run(subnet_id=subnet_id)
365

  
366

  
367
@command(port_cmds)
368
class port_info(_init_network, _optional_json):
369
    """Get details about a port"""
370

  
371
    @errors.generic.all
372
    @errors.cyclades.connection
373
    def _run(self, port_id):
374
        net = self.client.get_port_details(port_id)
375
        self._print(net, self.print_dict)
376

  
377
    def main(self, port_id):
378
        super(self.__class__, self)._run()
379
        self._run(port_id=port_id)
380

  
381

  
382
@command(port_cmds)
383
class port_info(_init_network, _optional_json):
384
    """Get details about a port"""
385

  
386
    @errors.generic.all
387
    @errors.cyclades.connection
388
    def _run(self, port_id):
389
        net = self.client.get_port_details(port_id)
390
        self._print(net, self.print_dict)
391

  
392
    def main(self, port_id):
393
        super(self.__class__, self)._run()
394
        self._run(port_id=port_id)
395

  
396

  
397
@command(port_cmds)
398
class port_delete(_init_network, _optional_output_cmd):
399
    """Delete a port"""
400

  
401
    @errors.generic.all
402
    @errors.cyclades.connection
403
    def _run(self, port_id):
404
        r = self.client.delete_port(port_id)
405
        self._optional_output(r)
406

  
407
    def main(self, port_id):
408
        super(self.__class__, self)._run()
409
        self._run(port_id=port_id)
410

  
411

  
412
@command(port_cmds)
413
class port_set(_init_network, _optional_json):
414
    """Set an attribute of a port, leave the rest untouched (update)
415
    Only "--name" is supported for now
416
    """
417

  
418
    arguments = dict(name=ValueArgument('New name of the port', '--name'))
419

  
420
    @errors.generic.all
421
    @errors.cyclades.connection
422
    def _run(self, port_id):
423
        if self['name'] in (None, ):
424
            raise CLISyntaxError(
425
                'Missing port attributes to update',
426
                details=[
427
                    'At least one if the following is expected:',
428
                    '  --name=<new name>'])
429
        r = self.client.get_port_details(port_id)
430
        r = self.client.update_port(
431
            port_id, r['network_id'], name=self['name'])
432
        self._print(r, self.print_dict)
433

  
434
    def main(self, port_id):
435
        super(self.__class__, self)._run()
436
        self._run(port_id=port_id)
437

  
438

  
439
#@command(port_cmds)
440
#class port_create(_init_network, _optional_json):
441
#
b/kamaki/clients/network/__init__.py
290 290
    def delete_port(self, port_id):
291 291
        r = self.ports_delete(port_id, success=204)
292 292
        return r.headers
293

  
294
    def update_port(
295
            self, port_id, network_id,
296
            name=None, status=None, admin_state_up=None, mac_address=None,
297
            fixed_ips=None, security_groups=None):
298
        """
299
        :param network_id: (str)
300

  
301
        :param name: (str)
302
        :param status: (str)
303
        :param admin_state_up: (bool) Router administrative status (UP / DOWN)
304
        :param mac_address: (str)
305
        :param fixed_ips: (str)
306
        :param security_groups: (list)
307
        """
308
        port = dict(network_id=network_id)
309
        if name:
310
            port['name'] = name
311
        if status:
312
            port['status'] = status
313
        if admin_state_up not in (None, ):
314
            port['admin_state_up'] = bool(admin_state_up)
315
        if mac_address:
316
            port['mac_address'] = mac_address
317
        if fixed_ips:
318
            port['fixed_ips'] = fixed_ips
319
        if security_groups:
320
            port['security_groups'] = security_groups
321
        r = self.ports_put(port_id, json_data=dict(port=port), success=201)
322
        return r.json['port']
b/kamaki/clients/network/rest_api.py
72 72
            return self.get(path4url('ports', port_id), **kwargs)
73 73
        return self.get(path4url('ports'), **kwargs)
74 74

  
75
    def ports_post(
76
            self,
77
            json_data=None,
78
            name=None, mac_address=None, fixed_ips=None, security_groups=None,
79
            **kwargs):
80
        self.set_param('name', name, iff=name)
81
        self.set_param('mac_address', mac_address, iff=mac_address)
82
        self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
83
        self.set_param('security_groups', security_groups, iff=security_groups)
75
    def ports_post(self, json_data=None, **kwargs):
84 76
        return self.post(path4url('ports'), json=json_data, **kwargs)
85 77

  
86
    def ports_put(
87
            self, port_id,
88
            json_data=None,
89
            name=None, mac_address=None, fixed_ips=None, security_groups=None,
90
            **kwargs):
91
        self.set_param('name', name, iff=name)
92
        self.set_param('mac_address', mac_address, iff=mac_address)
93
        self.set_param('fixed_ips', fixed_ips, iff=fixed_ips)
94
        self.set_param('security_groups', security_groups, iff=security_groups)
78
    def ports_put(self, port_id, json_data=None, **kwargs):
95 79
        return self.put(path4url('ports', port_id), json=json_data, **kwargs)
96 80

  
97 81
    def ports_delete(self, port_id, **kwargs):
b/kamaki/clients/network/test.py
146 146
                'ret val')
147 147
            self._assert(get, '/ports/%s' % port_id, **kwargs)
148 148

  
149
    @patch('kamaki.clients.Client.set_param')
150 149
    @patch('kamaki.clients.Client.post', return_value='ret val')
151
    def test_ports_post(self, post, set_param):
152
        for params, kwargs in product(
153
                [p for p in product(
154
                    (
155
                        ('name', 'port name', 'port name'),
156
                        ('name', None, None)),
157
                    (
158
                        ('mac_address', 'max address', 'max address'),
159
                        ('mac_address', None, None)),
160
                    (
161
                        ('fixed_ips', 'fixed ip', 'fixed ip'),
162
                        ('fixed_ips', None, None)),
163
                    (
164
                        ('security_groups', 'sec groups', 'sec groups'),
165
                        ('security_groups', None, None))
166
                )],
167
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
168

  
169
            callargs = dict()
170
            for p in params:
171
                callargs[p[0]] = p[2]
172
            callargs.update(kwargs)
173

  
174
            self.assertEqual(self.client.ports_post(**callargs), 'ret val')
175
            self._assert(
176
                post, '/ports', set_param, params=params, json=None, **kwargs)
150
    def test_ports_post(self, post):
151
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
152
            self.assertEqual(self.client.ports_post(**kwargs), 'ret val')
153
            self._assert(post, '/ports', json=None, **kwargs)
177 154

  
178 155
            json_data = dict(id='some id', other_param='other val')
179
            callargs['json_data'] = json_data
180
            self.assertEqual(self.client.ports_post(**callargs), 'ret val')
181
            self._assert(
182
                post, '/ports', set_param, params,
183
                json=json_data, **kwargs)
156
            self.assertEqual(
157
                self.client.ports_post(json_data=json_data, **kwargs),
158
                'ret val')
159
            self._assert(post, '/ports', json=json_data, **kwargs)
184 160

  
185
    @patch('kamaki.clients.Client.set_param')
186 161
    @patch('kamaki.clients.Client.put', return_value='ret val')
187
    def test_ports_put(self, put, set_param):
162
    def test_ports_put(self, put):
188 163
        port_id = 'portid'
189
        for params, kwargs in product(
190
                [p for p in product(
191
                    (
192
                        ('name', 'port name', 'port name'),
193
                        ('name', None, None)),
194
                    (
195
                        ('mac_address', 'max address', 'max address'),
196
                        ('mac_address', None, None)),
197
                    (
198
                        ('fixed_ips', 'fixed ip', 'fixed ip'),
199
                        ('fixed_ips', None, None)),
200
                    (
201
                        ('security_groups', 'sec groups', 'sec groups'),
202
                        ('security_groups', None, None))
203
                )],
204
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
205

  
206
            callargs = dict()
207
            for p in params:
208
                callargs[p[0]] = p[2]
209
            callargs.update(kwargs)
210

  
164
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
211 165
            self.assertEqual(
212
                self.client.ports_put(port_id, **callargs), 'ret val')
213
            self._assert(
214
                put, '/ports/%s' % port_id, set_param,
215
                params=params, json=None, **kwargs)
166
                self.client.ports_put(port_id, **kwargs), 'ret val')
167
            self._assert(put, '/ports/%s' % port_id, json=None, **kwargs)
216 168

  
217 169
            json_data = dict(id='some id', other_param='other val')
218
            callargs['json_data'] = json_data
219 170
            self.assertEqual(
220
                self.client.ports_put(port_id, **callargs), 'ret val')
221
            self._assert(
222
                put, '/ports/%s' % port_id, set_param, params,
223
                json=json_data, **kwargs)
171
                self.client.ports_put(port_id, json_data=json_data, **kwargs),
172
                'ret val')
173
            self._assert(put, '/ports/%s' % port_id, json=json_data, **kwargs)
224 174

  
225 175

  
226 176
class FakeObject(object):
......
517 467
        self.assertEqual(self.client.delete_port(portid), 'ret headers')
518 468
        ports_delete.assert_called_once_with(portid, success=204)
519 469

  
470
    @patch(
471
        'kamaki.clients.network.NetworkClient.ports_put',
472
        return_value=FakeObject())
473
    def test_update_port(self, ports_put):
474
        for (
475
                name, status, admin_state_up, mac_address, fixed_ips,
476
                security_groups) in product(
477
                    ('name', None), ('st', None), (True, None), ('mc', None),
478
                    ('fps', None), ('sg', None)):
479
            FakeObject.json = dict(port='rv')
480
            port_id, network_id = 'pid', 'nid'
481
            kwargs = dict(
482
                network_id=network_id, name=name, status=status,
483
                admin_state_up=admin_state_up, mac_address=mac_address,
484
                fixed_ips=fixed_ips, security_groups=security_groups)
485
            self.assertEqual(
486
                self.client.update_port(port_id, **kwargs), 'rv')
487
            req = dict()
488
            for k, v in kwargs.items():
489
                if v:
490
                    req[k] = v
491
            expargs = dict(json_data=dict(port=req), success=201)
492
            self.assertEqual(
493
                ports_put.mock_calls[-1], call(port_id, **expargs))
494

  
520 495

  
521 496
if __name__ == '__main__':
522 497
    from sys import argv

Also available in: Unified diff