Revision 1ad949a7

b/kamaki/clients/cyclades/__init__.py
407 407
        return self._wait(
408 408
            server_id, current_status, get_status, delay, max_wait, wait_cb)
409 409

  
410
    def get_floating_ip_pools(self):
411
        """
412
        :returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
413
        """
414
        r = self.floating_ip_pools_get()
415
        return r.json
416

  
417
    def get_floating_ips(self):
418
        """
419
        :returns: (dict) {floating_ips: [fixed_ip: , id: , ip: , pool: ]}
420
        """
421
        r = self.floating_ips_get()
422
        return r.json
423

  
424
    def alloc_floating_ip(self, pool=None, address=None):
425
        """
426
        :param pool: (str) pool of ips to allocate from
427

  
428
        :param address: (str) ip address to request
429

  
430
        :returns: (dict) {
431
            fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...}
432
        """
433
        json_data = dict()
434
        if pool:
435
            json_data['pool'] = pool
436
        if address:
437
            json_data['address'] = address
438
        r = self.floating_ips_post(json_data)
439
        return r.json['floating_ip']
440

  
441
    def get_floating_ip(self, fip_id):
442
        """
443
        :param fip_id: (str) floating ip id
444

  
445
        :returns: (dict)
446
            {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
447

  
448
        :raises AssertionError: if fip_id is emtpy
449
        """
450
        assert fip_id, 'floating ip id is needed for get_floating_ip'
451
        r = self.floating_ips_get(fip_id)
452
        return r.json['floating_ip']
453

  
454
    def delete_floating_ip(self, fip_id=None):
455
        """
456
        :param fip_id: (str) floating ip id (if None, all ips are deleted)
457

  
458
        :returns: (dict) request headers
459

  
460
        :raises AssertionError: if fip_id is emtpy
461
        """
462
        assert fip_id, 'floating ip id is needed for delete_floating_ip'
463
        r = self.floating_ips_delete(fip_id)
464
        return r.headers
465

  
466
    def attach_floating_ip(self, server_id, address):
467
        """Associate the address ip to server with server_id
468

  
469
        :param server_id: (int)
470

  
471
        :param address: (str) the ip address to assign to server (vm)
472

  
473
        :returns: (dict) request headers
474

  
475
        :raises ValueError: if server_id cannot be converted to int
476

  
477
        :raises ValueError: if server_id is not of a int-convertable type
478

  
479
        :raises AssertionError: if address is emtpy
480
        """
481
        server_id = int(server_id)
482
        assert address, 'address is needed for attach_floating_ip'
483
        req = dict(addFloatingIp=dict(address=address))
484
        r = self.servers_action_post(server_id, json_data=req)
485
        return r.headers
486

  
487
    def detach_floating_ip(self, server_id, address):
488
        """Disassociate an address ip from the server with server_id
489

  
490
        :param server_id: (int)
491

  
492
        :param address: (str) the ip address to assign to server (vm)
493

  
494
        :returns: (dict) request headers
495

  
496
        :raises ValueError: if server_id cannot be converted to int
497

  
498
        :raises ValueError: if server_id is not of a int-convertable type
499

  
500
        :raises AssertionError: if address is emtpy
501
        """
502
        server_id = int(server_id)
503
        assert address, 'address is needed for detach_floating_ip'
504
        req = dict(removeFloatingIp=dict(address=address))
505
        r = self.servers_action_post(server_id, json_data=req)
506
        return r.headers
507

  
508 410

  
509 411
class CycladesNetworkClient(NetworkClient):
510 412
    """Cyclades Network API extentions"""
b/kamaki/clients/network/__init__.py
320 320
            port['security_groups'] = security_groups
321 321
        r = self.ports_put(port_id, json_data=dict(port=port), success=201)
322 322
        return r.json['port']
323

  
324
    def list_floatingip(self):
325
        r = self.floatingips_get(success=200)
326
        return r['floatingips']
327

  
328
    def get_floatingip_details(self, floatingip_id):
329
        r = self.floatingips_get(floatingip_id, success=201)
330
        return r['floatingip']
331

  
332
    def create_floatingip(
333
            self, floating_network_id,
334
            floating_ip_address='', port_id='', fixed_ip_address=''):
335
        floatingip = dict(floating_network_id=floating_network_id)
336
        if floating_ip_address != '':
337
            floatingip['floating_ip_address'] = floating_ip_address
338
        if port_id != '':
339
            floatingip['port_id'] = port_id
340
        if fixed_ip_address != '':
341
            floatingip['fixed_ip_address'] = fixed_ip_address
342
        r = self.floatingips_post(
343
            json_data=dict(floatingip=floatingip), success=201)
344
        return r.json['floatingip']
345

  
346
    def update_floatingip(
347
            self, floatingip_id,
348
            floating_ip_address='', port_id='', fixed_ip_address=''):
349
        """To nullify something optional, use None"""
350
        floatingip = dict()
351
        if floating_ip_address != '':
352
            floatingip['floating_ip_address'] = floating_ip_address
353
        if port_id != '':
354
            floatingip['port_id'] = port_id
355
        if fixed_ip_address != '':
356
            floatingip['fixed_ip_address'] = fixed_ip_address
357
        r = self.floatingips_put(
358
            floatingip_id, json_data=dict(floatingip=floatingip), success=201)
359
        return r['floatingip']
360

  
361
    def delete_floatingip(self, floatingip_id):
362
        r = self.floatingips_delete(floatingip_id, success=204)
363
        return r.headers
b/kamaki/clients/network/rest_api.py
80 80

  
81 81
    def ports_delete(self, port_id, **kwargs):
82 82
        return self.delete(path4url('ports', port_id), **kwargs)
83

  
84
    #  floatingips (L3) extentions
85

  
86
    def floatingips_get(self, floatingip_id=None, **kwargs):
87
        return self.get(path4url('floatingips', floatingip_id or ''), **kwargs)
88

  
89
    def floatingips_post(self, json_data, **kwargs):
90
        return self.post(path4url('floatingips'), json=json_data, **kwargs)
91

  
92
    def floatingips_put(self, floatingip_id, json_data, **kwargs):
93
        return self.put(
94
            path4url('floatingips', floatingip_id), json=json_data, **kwargs)
95

  
96
    def floatingips_delete(self, floatingip_id, **kwargs):
97
        return self.delete(path4url('floatingips', floatingip_id), **kwargs)
b/kamaki/clients/network/test.py
172 172
                'ret val')
173 173
            self._assert(put, '/ports/%s' % port_id, json=json_data, **kwargs)
174 174

  
175
    @patch('kamaki.clients.Client.get', return_value='ret val')
176
    def test_floatingips_get(self, get):
177
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
178
            self.assertEqual(self.client.floatingips_get(**kwargs), 'ret val')
179
            self._assert(get, '/floatingips', **kwargs)
180

  
181
            floatingip_id = 'port id'
182
            self.assertEqual(
183
                self.client.floatingips_get(
184
                    floatingip_id=floatingip_id, **kwargs),
185
                'ret val')
186
            self._assert(get, '/floatingips/%s' % floatingip_id, **kwargs)
187

  
188
    @patch('kamaki.clients.Client.post', return_value='ret val')
189
    def test_floatingips_post(self, post):
190
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
191
            json_data = dict(id='some id', other_param='other val')
192
            self.assertEqual(
193
                self.client.floatingips_post(json_data=json_data, **kwargs),
194
                'ret val')
195
            self._assert(post, '/floatingips', json=json_data, **kwargs)
196

  
197
    @patch('kamaki.clients.Client.put', return_value='ret val')
198
    def test_floatingips_put(self, put):
199
        floatingip_id = 'portid'
200
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
201
            json_data = dict(id='some id', other_param='other val')
202
            self.assertEqual(
203
                self.client.floatingips_put(
204
                    floatingip_id, json_data=json_data, **kwargs),
205
                'ret val')
206
            self._assert(
207
                put, '/floatingips/%s' % floatingip_id,
208
                json=json_data, **kwargs)
209

  
175 210

  
176 211
class FakeObject(object):
177 212

  

Also available in: Unified diff